Remove DraftUtils.readDXF

This commit is contained in:
Benjamin Alterauge
2022-06-26 18:24:41 +02:00
committed by wwmayer
parent a980b08719
commit 3ef8a641c6
6 changed files with 7 additions and 2441 deletions

View File

@@ -26,19 +26,10 @@
# include <Python.h>
#endif
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>
#include <Base/Console.h>
#include <Base/VectorPy.h>
#include <Base/FileInfo.h>
#include <Base/Interpreter.h>
#include <App/Document.h>
#include <App/DocumentObjectPy.h>
#include <App/Application.h>
#include "DraftDxf.h"
namespace DraftUtils {
class Module : public Py::ExtensionModule<Module>
{
@@ -46,7 +37,11 @@ public:
Module() : Py::ExtensionModule<Module>("DraftUtils")
{
add_varargs_method("readDXF",&Module::readDXF,
"readDXF(filename,[document,ignore_errors]): Imports a DXF file into the given document. ignore_errors is True by default."
"readDXF(filename,[document,ignore_errors]): "
"Imports a DXF file into the given document. "
"ignore_errors is True by default. "
"NOTE: DraftUtils.readDXF is removed. "
"Use Import.readDxf instead."
);
initialize("The DraftUtils module contains utility functions for the Draft module."); // register with Python
}
@@ -56,38 +51,8 @@ public:
private:
Py::Object readDXF(const Py::Tuple& args)
{
Base::Console().Warning("DraftUtils.readDXF is deprecated. Use Import.readDxf instead.\n");
char* Name;
const char* DocName=nullptr;
bool IgnoreErrors=true;
if (!PyArg_ParseTuple(args.ptr(), "et|sb","utf-8",&Name,&DocName,&IgnoreErrors))
throw Py::Exception();
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
Base::FileInfo file(EncodedName.c_str());
if (!file.exists())
throw Py::RuntimeError("File doesn't exist");
App::Document *pcDoc;
if (DocName)
pcDoc = App::GetApplication().getDocument(DocName);
else
pcDoc = App::GetApplication().getActiveDocument();
if (!pcDoc)
pcDoc = App::GetApplication().newDocument(DocName);
try {
// read the DXF file
DraftDxfRead dxf_file(EncodedName,pcDoc);
dxf_file.DoRead(IgnoreErrors);
pcDoc->recompute();
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
Base::Console().Warning("DraftUtils.readDXF is removed. "
"Use Import.readDxf instead.\n");
return Py::None();
}
};

View File

@@ -29,10 +29,6 @@ SET(DraftUtils_SRCS
AppDraftUtilsPy.cpp
PreCompiled.cpp
PreCompiled.h
dxf.cpp
dxf.h
DraftDxf.cpp
DraftDxf.h
)
add_library(DraftUtils SHARED ${DraftUtils_SRCS})

View File

@@ -1,289 +0,0 @@
/***************************************************************************
* Copyright (c) 2015 Yorik van Havre <yorik@uncreated.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#endif
#include "DraftDxf.h"
#include <gp_Circ.hxx>
#include <gp_Ax1.hxx>
#include <gp_Ax2.hxx>
#include <gp_Elips.hxx>
#include <BRep_Builder.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Shape.hxx>
#include <Base/Parameter.h>
#include <Base/Matrix.h>
#include <Base/Vector3D.h>
#include <Base/Interpreter.h>
#include <App/Application.h>
#include <App/Document.h>
#include <App/Annotation.h>
#include <Mod/Part/App/PartFeature.h>
using namespace DraftUtils;
DraftDxfRead::DraftDxfRead(std::string filepath, App::Document *pcDoc) : CDxfRead(filepath.c_str())
{
document = pcDoc;
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Draft");
optionGroupLayers = hGrp->GetBool("groupLayers",false);
optionImportAnnotations = hGrp->GetBool("dxftext",false);
optionScaling = hGrp->GetFloat("dxfScaling",1.0);
}
gp_Pnt DraftDxfRead::makePoint(const double* p)
{
double sp1(p[0]);
double sp2(p[1]);
double sp3(p[2]);
if (optionScaling != 1.0) {
sp1 = sp1 * optionScaling;
sp2 = sp2 * optionScaling;
sp3 = sp3 * optionScaling;
}
return gp_Pnt(sp1,sp2,sp3);
}
void DraftDxfRead::OnReadLine(const double* s, const double* e, bool /*hidden*/)
{
gp_Pnt p0 = makePoint(s);
gp_Pnt p1 = makePoint(e);
if (p0.IsEqual(p1,0.00000001))
return;
BRepBuilderAPI_MakeEdge makeEdge(p0, p1);
TopoDS_Edge edge = makeEdge.Edge();
AddObject(new Part::TopoShape(edge));
}
void DraftDxfRead::OnReadPoint(const double* s)
{
BRepBuilderAPI_MakeVertex makeVertex(makePoint(s));
TopoDS_Vertex vertex = makeVertex.Vertex();
AddObject(new Part::TopoShape(vertex));
}
void DraftDxfRead::OnReadArc(const double* s, const double* e, const double* c, bool dir, bool /*hidden*/)
{
gp_Pnt p0 = makePoint(s);
gp_Pnt p1 = makePoint(e);
gp_Dir up(0, 0, 1);
if (!dir)
up = -up;
gp_Pnt pc = makePoint(c);
gp_Circ circle(gp_Ax2(pc, up), p0.Distance(pc));
BRepBuilderAPI_MakeEdge makeEdge(circle, p0, p1);
TopoDS_Edge edge = makeEdge.Edge();
AddObject(new Part::TopoShape(edge));
}
void DraftDxfRead::OnReadCircle(const double* s, const double* c, bool dir, bool /*hidden*/)
{
gp_Pnt p0 = makePoint(s);
gp_Dir up(0, 0, 1);
if (!dir)
up = -up;
gp_Pnt pc = makePoint(c);
gp_Circ circle(gp_Ax2(pc, up), p0.Distance(pc));
BRepBuilderAPI_MakeEdge makeEdge(circle);
TopoDS_Edge edge = makeEdge.Edge();
AddObject(new Part::TopoShape(edge));
}
void DraftDxfRead::OnReadSpline(struct SplineData& /*sd*/)
{
// not yet implemented
}
void DraftDxfRead::OnReadEllipse(const double* c, double major_radius, double minor_radius, double rotation, double /*start_angle*/, double /*end_angle*/, bool dir)
{
gp_Dir up(0, 0, 1);
if(!dir)
up = -up;
gp_Pnt pc = makePoint(c);
gp_Elips ellipse(gp_Ax2(pc, up), major_radius * optionScaling, minor_radius * optionScaling);
ellipse.Rotate(gp_Ax1(pc,up),rotation);
BRepBuilderAPI_MakeEdge makeEdge(ellipse);
TopoDS_Edge edge = makeEdge.Edge();
AddObject(new Part::TopoShape(edge));
}
void DraftDxfRead::OnReadText(const double *point, const double /*height*/, const char* text)
{
if (optionImportAnnotations) {
Base::Vector3d pt(point[0] * optionScaling, point[1] * optionScaling, point[2] * optionScaling);
if(LayerName().substr(0, 6) != "BLOCKS") {
App::Annotation *pcFeature = (App::Annotation *)document->addObject("App::Annotation", "Text");
pcFeature->LabelText.setValue(Deformat(text));
pcFeature->Position.setValue(pt);
}
//else std::cout << "skipped text in block: " << LayerName() << std::endl;
}
}
void DraftDxfRead::OnReadInsert(const double* point, const double* scale, const char* name, double rotation)
{
//std::cout << "Inserting block " << name << " rotation " << rotation << " pos " << point[0] << "," << point[1] << "," << point[2] << " scale " << scale[0] << "," << scale[1] << "," << scale[2] << std::endl;
std::string prefix = "BLOCKS ";
prefix += name;
prefix += " ";
auto checkScale = [=](double v) {
return v != 0.0 ? v : 1.0;
};
for(std::map<std::string,std::vector<Part::TopoShape*> > ::const_iterator i = layers.begin(); i != layers.end(); ++i) {
std::string k = i->first;
if(k.substr(0, prefix.size()) == prefix) {
BRep_Builder builder;
TopoDS_Compound comp;
builder.MakeCompound(comp);
std::vector<Part::TopoShape*> v = i->second;
for(std::vector<Part::TopoShape*>::const_iterator j = v.begin(); j != v.end(); ++j) {
const TopoDS_Shape& sh = (*j)->getShape();
if (!sh.IsNull())
builder.Add(comp, sh);
}
if (!comp.IsNull()) {
Part::TopoShape* pcomp = new Part::TopoShape(comp);
Base::Matrix4D mat;
mat.scale(checkScale(scale[0]),checkScale(scale[1]),checkScale(scale[2]));
mat.rotZ(rotation);
mat.move(point[0]*optionScaling,point[1]*optionScaling,point[2]*optionScaling);
pcomp->transformShape(mat,true);
AddObject(pcomp);
}
}
}
}
void DraftDxfRead::OnReadDimension(const double* s, const double* e, const double* point, double /*rotation*/)
{
if (optionImportAnnotations) {
Base::Interpreter().runString("import Draft");
Base::Interpreter().runStringArg("p1=FreeCAD.Vector(%f,%f,%f)",s[0]*optionScaling,s[1]*optionScaling,s[2]*optionScaling);
Base::Interpreter().runStringArg("p2=FreeCAD.Vector(%f,%f,%f)",e[0]*optionScaling,e[1]*optionScaling,e[2]*optionScaling);
Base::Interpreter().runStringArg("p3=FreeCAD.Vector(%f,%f,%f)",point[0]*optionScaling,point[1]*optionScaling,point[2]*optionScaling);
Base::Interpreter().runString("Draft.make_dimension(p1,p2,p3)");
}
}
void DraftDxfRead::AddObject(Part::TopoShape *shape)
{
//std::cout << "layer:" << LayerName() << std::endl;
std::vector <Part::TopoShape*> vec;
if (layers.count(LayerName()))
vec = layers[LayerName()];
vec.push_back(shape);
layers[LayerName()] = vec;
if (!optionGroupLayers) {
if(LayerName().substr(0, 6) != "BLOCKS") {
Part::Feature *pcFeature = (Part::Feature *)document->addObject("Part::Feature", "Shape");
pcFeature->Shape.setValue(shape->getShape());
}
}
}
std::string DraftDxfRead::Deformat(const char* text)
{
// this function removes DXF formatting from texts
std::stringstream ss;
bool escape = false; // turned on when finding an escape character
bool longescape = false; // turned on for certain escape codes that expect additional chars
for(unsigned int i = 0; i<strlen(text); i++) {
if (text[i] == '\\')
escape = true;
else if (escape) {
if (longescape) {
if (text[i] == ';') {
escape = false;
longescape = false;
}
} else {
if ( (text[i] == 'H') || (text[i] == 'h') ||
(text[i] == 'Q') || (text[i] == 'q') ||
(text[i] == 'W') || (text[i] == 'w') ||
(text[i] == 'F') || (text[i] == 'f') ||
(text[i] == 'A') || (text[i] == 'a') ||
(text[i] == 'C') || (text[i] == 'c') ||
(text[i] == 'T') || (text[i] == 't') )
longescape = true;
else {
if ( (text[i] == 'P') || (text[i] == 'p') )
ss << "\n";
escape = false;
}
}
}
else if ( (text[i] != '{') && (text[i] != '}') ) {
ss << text[i];
}
}
return ss.str();
}
void DraftDxfRead::AddGraphics() const
{
if (optionGroupLayers) {
for(std::map<std::string,std::vector<Part::TopoShape*> > ::const_iterator i = layers.begin(); i != layers.end(); ++i) {
BRep_Builder builder;
TopoDS_Compound comp;
builder.MakeCompound(comp);
std::string k = i->first;
if (k == "0") // FreeCAD doesn't like an object name being '0'...
k = "LAYER_0";
std::vector<Part::TopoShape*> v = i->second;
if(k.substr(0, 6) != "BLOCKS") {
for(std::vector<Part::TopoShape*>::const_iterator j = v.begin(); j != v.end(); ++j) {
const TopoDS_Shape& sh = (*j)->getShape();
if (!sh.IsNull())
builder.Add(comp, sh);
}
if (!comp.IsNull()) {
Part::Feature *pcFeature = (Part::Feature *)document->addObject("Part::Feature", k.c_str());
pcFeature->Shape.setValue(comp);
}
}
}
}
}

View File

@@ -1,67 +0,0 @@
/***************************************************************************
* Copyright (c) 2015 Yorik van Havre <yorik@uncreated.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef DRAFTDXF_H
#define DRAFTDXF_H
#include "dxf.h"
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Draft/DraftGlobal.h>
#include <App/Document.h>
#include <gp_Pnt.hxx>
namespace DraftUtils
{
class DraftUtilsExport DraftDxfRead : public CDxfRead
{
public:
DraftDxfRead(std::string filepath, App::Document *pcDoc);
// CDxfRead's virtual functions
void OnReadLine(const double* s, const double* e, bool hidden);
void OnReadPoint(const double* s);
void OnReadText(const double* point, const double height, const char* text);
void OnReadArc(const double* s, const double* e, const double* c, bool dir, bool hidden);
void OnReadCircle(const double* s, const double* c, bool dir, bool hidden);
void OnReadEllipse(const double* c, double major_radius, double minor_radius, double rotation, double start_angle, double end_angle, bool dir);
void OnReadSpline(struct SplineData& sd);
void OnReadInsert(const double* point, const double* scale, const char* name, double rotation);
void OnReadDimension(const double* s, const double* e, const double* point, double rotation);
void AddGraphics() const;
// FreeCAD-specific functions
void AddObject(Part::TopoShape *shape); //Called by OnRead functions to add Part objects
std::string Deformat(const char* text); // Removes DXF formatting from texts
private:
gp_Pnt makePoint(const double* p);
protected:
App::Document *document;
bool optionGroupLayers;
bool optionImportAnnotations;
double optionScaling;
std::map <std::string, std::vector <Part::TopoShape*> > layers;
};
}
#endif // DRAFTDXF_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,161 +0,0 @@
// dxf.h
// Copyright (c) 2009, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
#pragma once
#include <algorithm>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iosfwd>
#include <cstdio>
#include <cstring>
#include <cmath>
//Following is required to be defined on Ubuntu with OCC 6.3.1
#ifndef HAVE_IOSTREAM
#define HAVE_IOSTREAM
#endif
typedef int Aci_t; // AutoCAD color index
typedef enum
{
eUnspecified = 0, // Unspecified (No units)
eInches,
eFeet,
eMiles,
eMillimeters,
eCentimeters,
eMeters,
eKilometers,
eMicroinches,
eMils,
eYards,
eAngstroms,
eNanometers,
eMicrons,
eDecimeters,
eDekameters,
eHectometers,
eGigameters,
eAstronomicalUnits,
eLightYears,
eParsecs
} eDxfUnits_t;
struct SplineData
{
double norm[3];
int degree;
int knots;
int control_points;
int fit_points;
int flag;
std::list<double> starttanx;
std::list<double> starttany;
std::list<double> starttanz;
std::list<double> endtanx;
std::list<double> endtany;
std::list<double> endtanz;
std::list<double> knot;
std::list<double> weight;
std::list<double> controlx;
std::list<double> controly;
std::list<double> controlz;
std::list<double> fitx;
std::list<double> fity;
std::list<double> fitz;
};
class CDxfWrite{
private:
std::ofstream* m_ofs;
bool m_fail;
public:
CDxfWrite(const char* filepath);
~CDxfWrite();
bool Failed(){return m_fail;}
void WriteLine(const double* s, const double* e, const char* layer_name );
void WritePoint(const double*, const char*);
void WriteArc(const double* s, const double* e, const double* c, bool dir, const char* layer_name );
void WriteEllipse(const double* c, double major_radius, double minor_radius, double rotation, double start_angle, double end_angle, bool dir, const char* layer_name );
void WriteCircle(const double* c, double radius, const char* layer_name );
};
// derive a class from this and implement it's virtual functions
class CDxfRead{
private:
std::ifstream* m_ifs;
bool m_fail;
char m_str[1024];
char m_unused_line[1024];
eDxfUnits_t m_eUnits;
bool m_measurement_inch;
char m_layer_name[1024];
char m_section_name[1024];
char m_block_name[1024];
bool m_ignore_errors;
typedef std::map< std::string,Aci_t > LayerAciMap_t;
LayerAciMap_t m_layer_aci; // layer names -> layer color aci map
bool ReadUnits();
bool ReadLayer();
bool ReadLine();
bool ReadText();
bool ReadArc();
bool ReadCircle();
bool ReadEllipse();
bool ReadPoint();
bool ReadSpline();
bool ReadLwPolyLine();
bool ReadPolyLine();
bool ReadVertex(double *pVertex, bool *bulge_found, double *bulge);
void OnReadArc(double start_angle, double end_angle, double radius, const double* c, double z_extrusion_dir, bool hidden);
void OnReadCircle(const double* c, double radius, bool hidden);
void OnReadEllipse(const double* c, const double* m, double ratio, double start_angle, double end_angle);
bool ReadInsert();
bool ReadDimension();
bool ReadBlockInfo();
void get_line();
void put_line(const char *value);
void DerefACI();
protected:
Aci_t m_aci; // manifest color name or 256 for layer color
public:
CDxfRead(const char* filepath); // this opens the file
virtual ~CDxfRead(); // this closes the file
bool Failed(){return m_fail;}
void DoRead(const bool ignore_errors = false); // this reads the file and calls the following functions
double mm( double value ) const;
bool IgnoreErrors() const { return(m_ignore_errors); }
virtual void OnReadLine(const double* /*s*/, const double* /*e*/, bool /*hidden*/){}
virtual void OnReadPoint(const double* /*s*/){}
virtual void OnReadText(const double* /*point*/, const double /*height*/, const char* /*text*/){}
virtual void OnReadArc(const double* /*s*/, const double* /*e*/, const double* /*c*/, bool /*dir*/, bool /*hidden*/){}
virtual void OnReadCircle(const double* /*s*/, const double* /*c*/, bool /*dir*/, bool /*hidden*/){}
virtual void OnReadEllipse(const double* /*c*/, double /*major_radius*/, double /*minor_radius*/, double /*rotation*/, double /*start_angle*/, double /*end_angle*/, bool /*dir*/){}
virtual void OnReadSpline(struct SplineData& /*sd*/){}
virtual void OnReadInsert(const double* /*point*/, const double* /*scale*/, const char* /*name*/, double /*rotation*/){}
virtual void OnReadDimension(const double* /*s*/, const double* /*e*/, const double* /*point*/, double /*rotation*/){}
virtual void AddGraphics() const { }
std::string LayerName() const;
};