Draft: Starting a C++ DXF importer - issue #2205

This commit is contained in:
Yorik van Havre
2015-08-10 19:55:59 -03:00
parent 2d63a8a620
commit 279d156d30
10 changed files with 2246 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net) 2015 *
* *
* 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_
# include <Python.h>
#endif
#include <Base/Console.h>
#include <Base/Interpreter.h>
extern struct PyMethodDef DraftUtils_methods[];
PyDoc_STRVAR(module_DraftUtils_doc, "The DraftUtils module contains utility functions for the Draft module.");
/* Python entry */
extern "C" {
void DraftUtilsExport initDraftUtils()
{
// load dependent module
try {
Base::Interpreter().loadModule("Part");
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
}
PyObject* DraftUtilsModule = Py_InitModule3("DraftUtils", DraftUtils_methods, module_DraftUtils_doc); /* mod name, table ptr */
Base::Console().Log("Loading DraftUtils module... done\n");
}
} // extern "C"

View File

@@ -0,0 +1,81 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net) 2015 *
* *
* 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_
# include <Python.h>
#endif
#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"
using namespace DraftUtils;
static PyObject * readDXF (PyObject *self, PyObject *args)
{
char* Name;
const char* DocName=0;
bool IgnoreErrors=true;
if (!PyArg_ParseTuple(args, "et|sb","utf-8",&Name,&DocName,&IgnoreErrors))
return NULL;
std::string EncodedName = std::string(Name);
PyMem_Free(Name);
Base::FileInfo file(EncodedName.c_str());
if (!file.exists())
Py_Error(Base::BaseExceptionFreeCADError, "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);
PY_TRY {
// read the DXF file
DraftDxfRead dxf_file(EncodedName,pcDoc);
dxf_file.DoRead(IgnoreErrors);
pcDoc->recompute();
} PY_CATCH;
Py_Return;
}
/* registration table */
struct PyMethodDef DraftUtils_methods[] = {
{"readDXF" ,readDXF ,METH_VARARGS,
"readDXF(filename,[document,ignore_errors]): Imports a DXF file into the given document. ignore_errors is True by default."},
{NULL, NULL} /* end of table marker */
};

View File

@@ -0,0 +1,42 @@
if(MSVC)
add_definitions(-DHAVE_ACOSH -DHAVE_ASINH -DHAVE_ATANH)
else(MSVC)
add_definitions(-DHAVE_LIMITS_H -DHAVE_CONFIG_H)
endif(MSVC)
include_directories(
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/src
${CMAKE_BINARY_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}
${Boost_INCLUDE_DIRS}
${OCC_INCLUDE_DIR}
${PYTHON_INCLUDE_DIRS}
)
link_directories(${OCC_LIBRARY_DIR})
set(DraftUtils_LIBS
Part
FreeCADApp
)
SET(DraftUtils_SRCS
AppDraftUtils.cpp
AppDraftUtilsPy.cpp
PreCompiled.cpp
PreCompiled.h
dxf.cpp
dxf.h
DraftDxf.cpp
DraftDxf.h
)
add_library(DraftUtils SHARED ${DraftUtils_SRCS})
target_link_libraries(DraftUtils ${DraftUtils_LIBS})
SET_BIN_DIR(DraftUtils DraftUtils /Mod/Draft)
SET_PYTHON_PREFIX_SUFFIX(DraftUtils)
INSTALL(TARGETS DraftUtils DESTINATION ${CMAKE_INSTALL_LIBDIR})

View File

@@ -0,0 +1,175 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net) 2015 *
* *
* 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_Pnt.hxx>
#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 <App/Application.h>
#include <App/Document.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);
}
void DraftDxfRead::OnReadLine(const double* s, const double* e, bool hidden)
{
gp_Pnt p0(s[0], s[1], s[2]);
gp_Pnt p1(e[0], e[1], e[2]);
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(gp_Pnt(s[0], s[1], s[2]));
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(s[0], s[1], s[2]);
gp_Pnt p1(e[0], e[1], e[2]);
gp_Dir up(0, 0, 1);
if (!dir)
up = -up;
gp_Pnt pc(c[0], c[1], c[2]);
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(s[0], s[1], s[2]);
gp_Dir up(0, 0, 1);
if (!dir)
up = -up;
gp_Pnt pc(c[0], c[1], c[2]);
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(c[0], c[1], c[2]);
gp_Elips ellipse(gp_Ax2(pc, up), major_radius, minor_radius);
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 std::string text)
{
// not yet implemented
}
void DraftDxfRead::AddObject(Part::TopoShape *shape)
{
if (optionGroupLayers) {
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;
} else {
Part::Feature *pcFeature = (Part::Feature *)document->addObject("Part::Feature", "Shape");
pcFeature->Shape.setValue(*shape);
}
}
void DraftDxfRead::AddGraphics() const
{
std::cout << "end of file" << std::endl;
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;
std::vector<Part::TopoShape*> v = i->second;
std::cout << "joining:" << k << " size " << v.size() << std::endl;
for(std::vector<Part::TopoShape*>::const_iterator j = v.begin(); j != v.end(); ++j) {
const TopoDS_Shape& sh = (*j)->_Shape;
if (!sh.IsNull())
builder.Add(comp, sh);
}
if (!comp.IsNull()) {
std::cout << "valid shape" << std::endl;
Part::Feature *pcFeature = (Part::Feature *)document->addObject("Part::Feature", k.c_str());
pcFeature->Shape.setValue(comp);
}
else std::cout << "invalid shape" << std::endl;
}
}
}

View File

@@ -0,0 +1,57 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net) 2015 *
* *
* 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 <App/Document.h>
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 std::string 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 AddGraphics() const;
// FreeCAD-specific functions
void AddObject(Part::TopoShape *shape);
protected:
App::Document *document;
bool optionGroupLayers;
std::map <std::string, std::vector <Part::TopoShape*> > layers;
};
}
#endif // DRAFTDXF_H

View File

@@ -0,0 +1,24 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net) 2015 *
* *
* 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"

View File

@@ -0,0 +1,58 @@
/***************************************************************************
* Copyright (c) Yorik van Havre (yorik@uncreated.net) 2015 *
* *
* 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 DRAFTUTILS_PRECOMPILED_H
#define DRAFTUTILS_PRECOMPILED_H
#include <FCConfig.h>
// Exporting of App classes
#ifdef FC_OS_WIN32
# define DraftUtilsExport __declspec(dllexport)
# define PartExport __declspec(dllexport)
# define BaseExport __declspec(dllimport)
#else // for Linux
# define DraftUtilsExport
# define PartExport
# define BaseExport
#endif
#ifdef _PreComp_
// standard
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <assert.h>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <bitset>
#include <cctype>
#include <Python.h>
#endif // _PreComp_
#endif

1597
src/Mod/Draft/App/dxf.cpp Normal file

File diff suppressed because it is too large Load Diff

157
src/Mod/Draft/App/dxf.h Normal file
View File

@@ -0,0 +1,157 @@
// 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 <fstream>
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
//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);
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
~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 AddGraphics() const { }
std::string LayerName() const;
};

View File

@@ -1,3 +1,5 @@
add_subdirectory(App)
PYSIDE_WRAP_RC(Draft_QRC_SRCS Resources/Draft.qrc)
SET(Draft_SRCS