Add writeDXFObject to Import

- add a function to write DocumentObject(s) to Dxf file.
- all caller to specifiy parameter source to ImpExpDxf
  functions.
This commit is contained in:
wandererfan
2018-05-01 09:04:07 -04:00
parent 717a4a50a2
commit fe6c07b24f
2 changed files with 157 additions and 27 deletions

View File

@@ -68,6 +68,8 @@
#include <Mod/Part/App/encodeFilename.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/TopoShapePy.h>
#include <Mod/Part/App/PartFeature.h>
#include <Mod/Part/App/PartFeaturePy.h>
#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<Part::TopoShapePy*>((*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<Part::TopoShapePy*>(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<App::DocumentObjectPy*>(item)->getDocumentObjectPtr();
Part::Feature* part = static_cast<Part::Feature*>(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<App::DocumentObjectPy*>(docObj)->getDocumentObjectPtr();
Part::Feature* part = static_cast<Part::Feature*>(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();
}