diff --git a/src/Mod/Import/App/AppImportPy.cpp b/src/Mod/Import/App/AppImportPy.cpp index b0a11bb3c3..8ae59ee3ad 100644 --- a/src/Mod/Import/App/AppImportPy.cpp +++ b/src/Mod/Import/App/AppImportPy.cpp @@ -68,6 +68,8 @@ #include #include #include +#include +#include #include "ImpExpDxf.h" @@ -93,7 +95,10 @@ public: "readDXF(filename,[document,ignore_errors]): Imports a DXF file into the given document. ignore_errors is True by default." ); add_varargs_method("writeDXFShape",&Module::writeDXFShape, - "writeDXFShape(shape,filename): Exports a Shape to a DXF file." + "writeDXFShape([shape],filename): Exports Shape(s) to a DXF file." + ); + add_varargs_method("writeDXFObject",&Module::writeDXFObject, + "writeDXFObject([objects],filename): Exports DocumentObject(s) to a DXF file." ); initialize("This module is the Import module."); // register with Python } @@ -328,8 +333,11 @@ private: { char* Name; const char* DocName=0; + const char* optionSource = nullptr; + char* defaultOptions = "User parameter:BaseApp/Preferences/Mod/Draft"; + char* useOptionSource = nullptr; bool IgnoreErrors=true; - if (!PyArg_ParseTuple(args.ptr(), "et|sb","utf-8",&Name,&DocName,&IgnoreErrors)) + if (!PyArg_ParseTuple(args.ptr(), "et|sbs","utf-8",&Name,&DocName,&IgnoreErrors,&optionSource)) throw Py::Exception(); std::string EncodedName = std::string(Name); @@ -339,6 +347,7 @@ private: if (!file.exists()) throw Py::RuntimeError("File doesn't exist"); + App::Document *pcDoc; if (DocName) pcDoc = App::GetApplication().getDocument(DocName); @@ -347,11 +356,17 @@ private: if (!pcDoc) pcDoc = App::GetApplication().newDocument(DocName); + if (optionSource) { + strcpy(useOptionSource,optionSource); + } else { + useOptionSource = defaultOptions; + } + try { // read the DXF file ImpExpDxfRead dxf_file(EncodedName,pcDoc); - //dxf_file.setOptionSource("myoptionpath"); - //dxf_file.setOptions(); + dxf_file.setOptionSource(useOptionSource); + dxf_file.setOptions(); dxf_file.DoRead(IgnoreErrors); pcDoc->recompute(); } @@ -364,29 +379,148 @@ private: Py::Object writeDXFShape(const Py::Tuple& args) { PyObject *shapeObj; - char* name; - if (!PyArg_ParseTuple(args.ptr(), "Oet", &shapeObj, "utf-8",&name)) { - throw Py::TypeError("expected (Page,path"); - } - - std::string filePath = std::string(name); - std::string layerName = "none"; - PyMem_Free(name); - try { - ImpExpDxfWrite writer(filePath); - //writer.setOptionSource("myoptionpath"); - //writer.setOptions(); - writer.setLayerName(layerName); - if (PyObject_TypeCheck(shapeObj, &(Part::TopoShapePy::Type))) { + char* fname; + std::string filePath; + std::string layerName; + const char* optionSource = nullptr; + char* defaultOptions = "User parameter:BaseApp/Preferences/Mod/Draft"; + char* useOptionSource = nullptr; + + + if (PyArg_ParseTuple(args.ptr(), "O!et|s", &(PyList_Type) ,&shapeObj, "utf-8",&fname, &optionSource)) { + filePath = std::string(fname); + layerName = "none"; + PyMem_Free(fname); + + if (optionSource) { + strcpy(useOptionSource,optionSource); + } else { + useOptionSource = defaultOptions; + } + try { + ImpExpDxfWrite writer(filePath); + writer.setOptionSource(useOptionSource); + writer.setOptions(); + writer.setLayerName(layerName); + Py::Sequence list(shapeObj); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + if (PyObject_TypeCheck((*it).ptr(), &(Part::TopoShapePy::Type))) { + const TopoDS_Shape& shape = static_cast((*it).ptr())->getTopoShapePtr()->getShape(); + writer.exportShape(shape); + } + } + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + } else if (PyArg_ParseTuple(args.ptr(), "O!et|s", + &(Part::TopoShapePy::Type) , + &shapeObj, + "utf-8", + &fname, + &optionSource)) { + filePath = std::string(fname); + layerName = "none"; + PyMem_Free(fname); + + if (optionSource) { + strcpy(useOptionSource,optionSource); + } else { + useOptionSource = defaultOptions; + } + try { + ImpExpDxfWrite writer(filePath); + writer.setOptionSource(useOptionSource); + writer.setOptions(); + writer.setLayerName(layerName); Part::TopoShape* obj = static_cast(shapeObj)->getTopoShapePtr(); TopoDS_Shape shape = obj->getShape(); writer.exportShape(shape); } - } - catch (const Base::Exception& e) { - throw Py::RuntimeError(e.what()); - } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + } else { + throw Py::TypeError("expected ([Shape],path"); + } + return Py::None(); + } + Py::Object writeDXFObject(const Py::Tuple& args) + { + PyObject *docObj; + char* fname; + std::string filePath; + std::string layerName; + const char* optionSource = nullptr; + char* defaultOptions = "User parameter:BaseApp/Preferences/Mod/Draft"; + char* useOptionSource = nullptr; + + + if (PyArg_ParseTuple(args.ptr(), "O!et|s", &(PyList_Type) ,&docObj, "utf-8",&fname, &optionSource)) { + filePath = std::string(fname); + layerName = "none"; + PyMem_Free(fname); + + if (optionSource) { + strcpy(useOptionSource,optionSource); + } else { + useOptionSource = defaultOptions; + } + try { + ImpExpDxfWrite writer(filePath); + writer.setOptionSource(useOptionSource); + writer.setOptions(); + writer.setLayerName(layerName); + Py::Sequence list(docObj); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + if (PyObject_TypeCheck((*it).ptr(), &(Part::PartFeaturePy::Type))) { + PyObject* item = (*it).ptr(); + App::DocumentObject* obj = static_cast(item)->getDocumentObjectPtr(); + Part::Feature* part = static_cast(obj); + layerName = part->getNameInDocument(); + writer.setLayerName(layerName); + const TopoDS_Shape& shape = part->Shape.getValue(); + writer.exportShape(shape); + } + } + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + } else if (PyArg_ParseTuple(args.ptr(), "O!et|s", + &(Part::PartFeaturePy::Type) , + &docObj, + "utf-8", + &fname, + &optionSource)) { + filePath = std::string(fname); + layerName = "none"; + PyMem_Free(fname); + + if (optionSource) { + strcpy(useOptionSource,optionSource); + } else { + useOptionSource = defaultOptions; + } + try { + ImpExpDxfWrite writer(filePath); + writer.setOptionSource(useOptionSource); + writer.setOptions(); + writer.setLayerName(layerName); + App::DocumentObject* obj = static_cast(docObj)->getDocumentObjectPtr(); + Part::Feature* part = static_cast(obj); + layerName = part->getNameInDocument(); + writer.setLayerName(layerName); + const TopoDS_Shape& shape = part->Shape.getValue(); + writer.exportShape(shape); + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + } else { + throw Py::TypeError("expected ([DocObject],path"); + } return Py::None(); } diff --git a/src/Mod/Import/App/ImpExpDxf.cpp b/src/Mod/Import/App/ImpExpDxf.cpp index fbf0f0c9ab..eddffa9741 100644 --- a/src/Mod/Import/App/ImpExpDxf.cpp +++ b/src/Mod/Import/App/ImpExpDxf.cpp @@ -75,10 +75,6 @@ ImpExpDxfRead::ImpExpDxfRead(std::string filepath, App::Document *pcDoc) : CDxfR document = pcDoc; setOptionSource("User parameter:BaseApp/Preferences/Mod/Draft"); setOptions(); -// 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); } void ImpExpDxfRead::setOptions(void) @@ -340,7 +336,7 @@ ImpExpDxfWrite::ImpExpDxfWrite(std::string filepath) : void ImpExpDxfWrite::setOptions(void) { - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Draft"); + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(getOptionSource().c_str()); optionMaxLength = hGrp->GetFloat("maxsegmentlength",5.0); optionPolyLine = hGrp->GetBool("DiscretizeEllipses",true); }