remove import dependency on assembly workbench

This commit is contained in:
Stefan Tröger 2015-06-03 06:59:52 +02:00
parent cb9be3ac9f
commit c0e850cd0d
3 changed files with 273 additions and 79 deletions

View File

@ -52,7 +52,7 @@
#include <CXX/Objects.hxx>
#include "ImportOCAF.h"
#include "ImportOCAFAssembly.h"
//#include "ImportOCAFAssembly.h"
#include <Base/PyObjectBase.h>
#include <Base/Console.h>
#include <App/Application.h>
@ -76,9 +76,9 @@ public:
add_varargs_method("insert",&Module::importer,
"insert(string,string) -- Insert the file into the given document."
);
add_varargs_method("openAssembly",&Module::importAssembly,
"openAssembly(string) -- Open the assembly file and create a new document."
);
// add_varargs_method("openAssembly",&Module::importAssembly,
// "openAssembly(string) -- Open the assembly file and create a new document."
// );
add_varargs_method("export",&Module::exporter,
"export(list,string) -- Export a list of objects into a single file."
);
@ -304,10 +304,8 @@ private:
return Py::None();
}
{"importAssembly" ,importAssembly ,METH_VARARGS,
"importAssembly(FileName,Target) -- Import a Assembly file and creates a Assembly structure."},
};
/*
static PyObject * importAssembly(PyObject *self, PyObject *args)
{
char* Name;
@ -414,7 +412,7 @@ static PyObject * importAssembly(PyObject *self, PyObject *args)
PY_CATCH
Py_Return;
}
}*/

View File

@ -52,7 +52,7 @@
#include <CXX/Objects.hxx>
#include "ImportOCAF.h"
#include "ImportOCAFAssembly.h"
//#include "ImportOCAFAssembly.h"
#include <Base/PyObjectBase.h>
#include <Base/Console.h>
#include <App/Application.h>
@ -64,10 +64,10 @@
#include <Mod/Part/App/ImportStep.h>
#include <Mod/Part/App/encodeFilename.h>
<<<<<<< e2142800e3730f3726818e80efa9d0c125b57558
namespace Import {
class Module : public Py::ExtensionModule<Module>
{
<<<<<<< 1b2c7a1da1451e3e0f5b8968455de0323c77c7f7
public:
Module() : Py::ExtensionModule<Module>("Import")
{
@ -77,32 +77,20 @@ public:
add_varargs_method("insert",&Module::importer,
"insert(string,string) -- Insert the file into the given document."
);
add_varargs_method("openAssembly",&Module::importAssembly,
"openAssembly(string) -- Open the assembly file and create a new document."
);
add_varargs_method("export",&Module::exporter,
"export(list,string) -- Export a list of objects into a single file."
);
initialize("This module is the Import module."); // register with Python
}
virtual ~Module() {}
=======
char* Name;
PyObject* TargetObjectPy=0;
if (!PyArg_ParseTuple(args, "et|O!","utf-8",&Name,&(App::DocumentObjectPy::Type),&TargetObjectPy))
return 0;
std::string Utf8Name = std::string(Name);
PyMem_Free(Name);
std::string name8bit = Part::encodeFilename(Utf8Name);
PY_TRY {
//Base::Console().Log("Insert in Part with %s",Name);
Base::FileInfo file(name8bit);
App::DocumentObject* target = nullptr;
if(TargetObjectPy)
target = static_cast<App::DocumentObjectPy*>(TargetObjectPy)->getDocumentObjectPtr();
>>>>>>> [Assembly] fixing the commands and some import
}
virtual ~Module() {}
private:
Py::Object importer(const Py::Tuple& args)
@ -113,22 +101,9 @@ private:
if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&DocName))
throw Py::Exception();
<<<<<<< 1b2c7a1da1451e3e0f5b8968455de0323c77c7f7
std::string Utf8Name = std::string(Name);
PyMem_Free(Name);
std::string name8bit = Part::encodeFilename(Utf8Name);
=======
if (file.hasExtension("stp") || file.hasExtension("step")) {
try {
STEPCAFControl_Reader aReader;
aReader.SetColorMode(true);
aReader.SetNameMode(true);
aReader.SetLayerMode(true);
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
PyErr_SetString(PyExc_Exception, "cannot read STEP file");
return 0;
}
>>>>>>> [Assembly] fixing the commands and some import
try {
//Base::Console().Log("Insert in Part with %s",Name);
@ -138,7 +113,6 @@ private:
if (DocName) {
pcDoc = App::GetApplication().getDocument(DocName);
}
<<<<<<< 1b2c7a1da1451e3e0f5b8968455de0323c77c7f7
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument("Unnamed");
}
@ -163,20 +137,6 @@ private:
pi->Show();
aReader.Transfer(hDoc);
pi->EndScope();
=======
}
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
try {
IGESControl_Controller::Init();
Interface_Static::SetIVal("read.surfacecurve.mode",3);
IGESCAFControl_Reader aReader;
aReader.SetColorMode(true);
aReader.SetNameMode(true);
aReader.SetLayerMode(true);
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
PyErr_SetString(PyExc_Exception, "cannot read IGES file");
return 0;
>>>>>>> [Assembly] fixing the commands and some import
}
catch (OSD_Exception) {
Handle_Standard_Failure e = Standard_Failure::Caught();
@ -241,19 +201,7 @@ private:
throw Py::RuntimeError(e.what());
}
<<<<<<< 1b2c7a1da1451e3e0f5b8968455de0323c77c7f7
return Py::None();
=======
Import::ImportOCAFAssembly ocaf(hDoc, pcDoc, file.fileNamePure(),target);
ocaf.loadAssembly();
pcDoc->recompute();
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
>>>>>>> [Assembly] fixing the commands and some import
}
Py::Object exporter(const Py::Tuple& args)
{
@ -361,16 +309,29 @@ private:
"importAssembly(FileName,Target) -- Import a Assembly file and creates a Assembly structure."},
};
PyObject * importAssembly(PyObject *self, PyObject *args)
=======
/* module functions */
/*
>>>>>>> remove import dependency on assembly workbench
static PyObject * importAssembly(PyObject *self, PyObject *args)
{
char* Name;
PyObject* TargetObject=0;
if (!PyArg_ParseTuple(args, "s|O",&Name,&TargetObject))
PyObject* TargetObjectPy=0;
if (!PyArg_ParseTuple(args, "et|O!","utf-8",&Name,&(App::DocumentObjectPy::Type),&TargetObjectPy))
return 0;
std::string Utf8Name = std::string(Name);
PyMem_Free(Name);
std::string name8bit = Part::encodeFilename(Utf8Name);
PY_TRY {
//Base::Console().Log("Insert in Part with %s",Name);
Base::FileInfo file(Name);
Base::FileInfo file(name8bit);
App::DocumentObject* target = nullptr;
if(TargetObjectPy)
target = static_cast<App::DocumentObjectPy*>(TargetObjectPy)->getDocumentObjectPtr();
App::Document *pcDoc = 0;
@ -390,7 +351,7 @@ PyObject * importAssembly(PyObject *self, PyObject *args)
aReader.SetColorMode(true);
aReader.SetNameMode(true);
aReader.SetLayerMode(true);
if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
PyErr_SetString(PyExc_Exception, "cannot read STEP file");
return 0;
}
@ -419,7 +380,7 @@ PyObject * importAssembly(PyObject *self, PyObject *args)
aReader.SetColorMode(true);
aReader.SetNameMode(true);
aReader.SetLayerMode(true);
if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) {
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
PyErr_SetString(PyExc_Exception, "cannot read IGES file");
return 0;
}
@ -445,8 +406,8 @@ PyObject * importAssembly(PyObject *self, PyObject *args)
return 0;
}
Import::ImportOCAFAssembly ocaf(hDoc, pcDoc, file.fileNamePure());
ocaf.loadShapes();
Import::ImportOCAFAssembly ocaf(hDoc, pcDoc, file.fileNamePure(),target);
ocaf.loadAssembly();
pcDoc->recompute();
}
@ -457,12 +418,247 @@ PyObject * importAssembly(PyObject *self, PyObject *args)
}
PY_CATCH
Py_Return;
}*/
static PyObject * importer(PyObject *self, PyObject *args)
{
char* Name;
char* DocName=0;
if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&DocName))
return NULL;
std::string Utf8Name = std::string(Name);
PyMem_Free(Name);
std::string name8bit = Part::encodeFilename(Utf8Name);
PY_TRY {
//Base::Console().Log("Insert in Part with %s",Name);
Base::FileInfo file(Utf8Name.c_str());
App::Document *pcDoc = 0;
if (DocName) {
pcDoc = App::GetApplication().getDocument(DocName);
}
if (!pcDoc) {
pcDoc = App::GetApplication().newDocument("Unnamed");
}
Handle(XCAFApp_Application) hApp = XCAFApp_Application::GetApplication();
Handle(TDocStd_Document) hDoc;
hApp->NewDocument(TCollection_ExtendedString("MDTV-CAF"), hDoc);
if (file.hasExtension("stp") || file.hasExtension("step")) {
try {
STEPCAFControl_Reader aReader;
aReader.SetColorMode(true);
aReader.SetNameMode(true);
aReader.SetLayerMode(true);
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
PyErr_SetString(Base::BaseExceptionFreeCADError, "cannot read STEP file");
return 0;
}
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
aReader.Reader().WS()->MapReader()->SetProgress(pi);
pi->NewScope(100, "Reading STEP file...");
pi->Show();
aReader.Transfer(hDoc);
pi->EndScope();
}
catch (OSD_Exception) {
Handle_Standard_Failure e = Standard_Failure::Caught();
Base::Console().Error("%s\n", e->GetMessageString());
Base::Console().Message("Try to load STEP file without colors...\n");
Part::ImportStepParts(pcDoc,Utf8Name.c_str());
pcDoc->recompute();
}
}
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/Part")->GetGroup("IGES");
try {
IGESControl_Controller::Init();
IGESCAFControl_Reader aReader;
// http://www.opencascade.org/org/forum/thread_20603/?forum=3
aReader.SetReadVisible(hGrp->GetBool("SkipBlankEntities", true)
? Standard_True : Standard_False);
aReader.SetColorMode(true);
aReader.SetNameMode(true);
aReader.SetLayerMode(true);
if (aReader.ReadFile((Standard_CString)(name8bit.c_str())) != IFSelect_RetDone) {
PyErr_SetString(Base::BaseExceptionFreeCADError, "cannot read IGES file");
return 0;
}
Handle_Message_ProgressIndicator pi = new Part::ProgressIndicator(100);
aReader.WS()->MapReader()->SetProgress(pi);
pi->NewScope(100, "Reading IGES file...");
pi->Show();
aReader.Transfer(hDoc);
pi->EndScope();
}
catch (OSD_Exception) {
Handle_Standard_Failure e = Standard_Failure::Caught();
Base::Console().Error("%s\n", e->GetMessageString());
Base::Console().Message("Try to load IGES file without colors...\n");
Part::ImportIgesParts(pcDoc,Utf8Name.c_str());
pcDoc->recompute();
}
}
else {
PyErr_SetString(Base::BaseExceptionFreeCADError, "no supported file format");
return 0;
}
#if 1
Import::ImportOCAF ocaf(hDoc, pcDoc, file.fileNamePure());
ocaf.loadShapes();
#else
Import::ImportXCAF xcaf(hDoc, pcDoc, file.fileNamePure());
xcaf.loadShapes();
#endif
pcDoc->recompute();
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(Base::BaseExceptionFreeCADError, e->GetMessageString());
return 0;
}
PY_CATCH
Py_Return;
}
static PyObject * open(PyObject *self, PyObject *args)
{
return importer(self, args);
}
static PyObject * exporter(PyObject *self, PyObject *args)
{
PyObject* object;
char* Name;
if (!PyArg_ParseTuple(args, "Oet",&object,"utf-8",&Name))
return NULL;
std::string Utf8Name = std::string(Name);
PyMem_Free(Name);
std::string name8bit = Part::encodeFilename(Utf8Name);
PY_TRY {
Handle(XCAFApp_Application) hApp = XCAFApp_Application::GetApplication();
Handle(TDocStd_Document) hDoc;
hApp->NewDocument(TCollection_ExtendedString("MDTV-CAF"), hDoc);
Import::ExportOCAF ocaf(hDoc);
Py::Sequence list(object);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
PyObject* item = (*it).ptr();
if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) {
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(item)->getDocumentObjectPtr();
if (obj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) {
Part::Feature* part = static_cast<Part::Feature*>(obj);
std::vector<App::Color> colors;
ocaf.saveShape(part, colors);
}
else {
Base::Console().Message("'%s' is not a shape, export will be ignored.\n", obj->Label.getValue());
}
}
else if (PyTuple_Check(item) && PyTuple_Size(item) == 2) {
Py::Tuple tuple(*it);
Py::Object item0 = tuple.getItem(0);
Py::Object item1 = tuple.getItem(1);
if (PyObject_TypeCheck(item0.ptr(), &(App::DocumentObjectPy::Type))) {
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(item0.ptr())->getDocumentObjectPtr();
if (obj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) {
Part::Feature* part = static_cast<Part::Feature*>(obj);
App::PropertyColorList colors;
colors.setPyObject(item1.ptr());
ocaf.saveShape(part, colors.getValues());
}
else {
Base::Console().Message("'%s' is not a shape, export will be ignored.\n", obj->Label.getValue());
}
}
}
}
Base::FileInfo file(Utf8Name.c_str());
if (file.hasExtension("stp") || file.hasExtension("step")) {
//Interface_Static::SetCVal("write.step.schema", "AP214IS");
STEPCAFControl_Writer writer;
writer.Transfer(hDoc, STEPControl_AsIs);
// edit STEP header
#if OCC_VERSION_HEX >= 0x060500
APIHeaderSection_MakeHeader makeHeader(writer.ChangeWriter().Model());
#else
APIHeaderSection_MakeHeader makeHeader(writer.Writer().Model());
#endif
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/Part")->GetGroup("STEP");
makeHeader.SetName(new TCollection_HAsciiString((const Standard_CString)Utf8Name.c_str()));
makeHeader.SetAuthorValue (1, new TCollection_HAsciiString(hGrp->GetASCII("Author", "Author").c_str()));
makeHeader.SetOrganizationValue (1, new TCollection_HAsciiString(hGrp->GetASCII("Company").c_str()));
makeHeader.SetOriginatingSystem(new TCollection_HAsciiString(App::GetApplication().getExecutableName()));
makeHeader.SetDescriptionValue(1, new TCollection_HAsciiString("FreeCAD Model"));
IFSelect_ReturnStatus ret = writer.Write(name8bit.c_str());
if (ret == IFSelect_RetError || ret == IFSelect_RetFail || ret == IFSelect_RetStop) {
PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str());
return 0;
}
}
else if (file.hasExtension("igs") || file.hasExtension("iges")) {
IGESControl_Controller::Init();
IGESCAFControl_Writer writer;
IGESData_GlobalSection header = writer.Model()->GlobalSection();
header.SetAuthorName(new TCollection_HAsciiString(Interface_Static::CVal("write.iges.header.author")));
header.SetCompanyName(new TCollection_HAsciiString(Interface_Static::CVal("write.iges.header.company")));
header.SetSendName(new TCollection_HAsciiString(Interface_Static::CVal("write.iges.header.product")));
writer.Model()->SetGlobalSection(header);
writer.Transfer(hDoc);
Standard_Boolean ret = writer.Write(name8bit.c_str());
if (!ret) {
PyErr_Format(PyExc_IOError, "Cannot open file '%s'", Utf8Name.c_str());
return 0;
}
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(Base::BaseExceptionFreeCADError, e->GetMessageString());
return 0;
}
PY_CATCH
Py_Return;
}
<<<<<<< e2142800e3730f3726818e80efa9d0c125b57558
PyObject* initModule()
{
return (new Module)->module().ptr();
}
} // namespace Import
=======
/* registration table */
struct PyMethodDef Import_Import_methods[] = {
{"open" ,open ,METH_VARARGS,
"open(string) -- Open the file and create a new document."},
{"insert" ,importer ,METH_VARARGS,
"insert(string,string) -- Insert the file into the given document."},
{"export" ,exporter ,METH_VARARGS,
"export(list,string) -- Export a list of objects into a single file."},
// {"importAssembly" ,importAssembly ,METH_VARARGS,
// "importAssembly(FileName,Target) -- Import a Assembly file and creates a Assembly structure."},
{NULL, NULL} /* end of table marker */
};
>>>>>>> remove import dependency on assembly workbench

View File

@ -20,7 +20,7 @@ link_directories(${OCC_LIBRARY_DIR})
set(Import_LIBS
Part
Assembly
#Assembly
${OCC_OCAF_LIBRARIES}
${OCC_OCAF_DEBUG_LIBRARIES}
)
@ -30,8 +30,8 @@ SET(Import_SRCS
AppImportPy.cpp
ImportOCAF.cpp
ImportOCAF.h
ImportOCAFAssembly.cpp
ImportOCAFAssembly.h
#ImportOCAFAssembly.cpp
#ImportOCAFAssembly.h
StepShapePy.xml
StepShape.h
StepShape.cpp