diff --git a/src/Mod/Import/Gui/AppImportGuiPy.cpp b/src/Mod/Import/Gui/AppImportGuiPy.cpp index c47dd6b45..0c0a30360 100644 --- a/src/Mod/Import/Gui/AppImportGuiPy.cpp +++ b/src/Mod/Import/Gui/AppImportGuiPy.cpp @@ -675,11 +675,258 @@ static PyObject * exporter(PyObject *self, PyObject *args) Py_Return; } +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class OCAFBrowser +{ +public: + OCAFBrowser(Handle_TDocStd_Document h) + : pDoc(h) + { + myGroupIcon = QApplication::style()->standardIcon(QStyle::SP_DirIcon); + myTree = new QTreeWidget(); + myTree->setHeaderLabel(QString::fromAscii("OCAF Browser")); + + TDataStd::IDList(myList); + myList.Append(TDataStd_TreeNode::GetDefaultTreeID()); + myList.Append(TDataStd_Integer::GetID()); + myList.Append(TDocStd_Owner::GetID()); + myList.Append(TNaming_NamedShape::GetID()); + myList.Append(TNaming_UsedShapes::GetID()); + myList.Append(XCAFDoc_Color::GetID()); + myList.Append(XCAFDoc_ColorTool::GetID()); + myList.Append(XCAFDoc_LayerTool::GetID()); + myList.Append(XCAFDoc_ShapeTool::GetID()); + myList.Append(XCAFDoc_ShapeMapTool::GetID()); + myList.Append(XCAFDoc_Location::GetID()); + } + + void load(); + +private: + void load(const TDF_Label& label, QTreeWidgetItem* item, const QString&); + std::string toString(const TCollection_ExtendedString& extstr) const + { + char* str = new char[extstr.LengthOfCString()+1]; + extstr.ToUTF8CString(str); + std::string text(str); + delete [] str; + return text; + } + +private: + QIcon myGroupIcon; + QTreeWidget* myTree; + TDF_IDList myList; + Handle_TDocStd_Document pDoc; +}; + +void OCAFBrowser::load() +{ + myTree->clear(); + + QTreeWidgetItem* root = new QTreeWidgetItem(); + root->setText(0, QLatin1String("0")); + root->setIcon(0, myGroupIcon); + myTree->addTopLevelItem(root); + + load(pDoc->GetData()->Root(), root, QString::fromAscii("0")); + myTree->show(); +} + +void OCAFBrowser::load(const TDF_Label& label, QTreeWidgetItem* item, const QString& s) +{ + Handle(TDataStd_Name) name; + if (label.FindAttribute(TDataStd_Name::GetID(),name)) { + QString text = QString::fromAscii("%1 %2").arg(s).arg(QString::fromUtf8(toString(name->Get()).c_str())); + item->setText(0, text); + } + + for (TDF_ListIteratorOfIDList it(myList); it.More(); it.Next()) { + Handle(TDF_Attribute) attr; + if (label.FindAttribute(it.Value(), attr)) { + QTreeWidgetItem* child = new QTreeWidgetItem(); + item->addChild(child); + if (it.Value() == TDataStd_Name::GetID()) { + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name(); + str << " = " << toString(Handle_TDataStd_Name::DownCast(attr)->Get()).c_str(); + child->setText(0, text); + } + else if (it.Value() == TDF_TagSource::GetID()) { + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name(); + str << " = " << Handle_TDF_TagSource::DownCast(attr)->Get(); + child->setText(0, text); + } + else if (it.Value() == TDataStd_Integer::GetID()) { + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name(); + str << " = " << Handle_TDataStd_Integer::DownCast(attr)->Get(); + child->setText(0, text); + } + else if (it.Value() == TNaming_NamedShape::GetID()) { + TopoDS_Shape shape = Handle_TNaming_NamedShape::DownCast(attr)->Get(); + QString text; + QTextStream str(&text); + str << attr->DynamicType()->Name() << " = "; + if (!shape.IsNull()) { + switch (shape.ShapeType()) { + case TopAbs_COMPOUND: + str << "COMPOUND PRIMITIVE"; + break; + case TopAbs_COMPSOLID: + str << "COMPSOLID PRIMITIVE"; + break; + case TopAbs_SOLID: + str << "SOLID PRIMITIVE"; + break; + case TopAbs_SHELL: + str << "SHELL PRIMITIVE"; + break; + case TopAbs_FACE: + str << "FACE PRIMITIVE"; + break; + case TopAbs_WIRE: + str << "WIRE PRIMITIVE"; + break; + case TopAbs_EDGE: + str << "EDGE PRIMITIVE"; + break; + case TopAbs_VERTEX: + str << "VERTEX PRIMITIVE"; + break; + case TopAbs_SHAPE: + str << "SHAPE PRIMITIVE"; + break; + } + } + child->setText(0, text); + } + else { + child->setText(0, QLatin1String(attr->DynamicType()->Name())); + } + } + } + + //TDF_ChildIDIterator nodeIterator(label, XCAFDoc::ShapeRefGUID()); + //for (; nodeIterator.More(); nodeIterator.Next()) { + // Handle(TDataStd_TreeNode) node = Handle(TDataStd_TreeNode)::DownCast(nodeIterator.Value()); + // //if (node->HasFather()) + // // ; + // QTreeWidgetItem* child = new QTreeWidgetItem(); + // child->setText(0, QString::fromAscii("TDataStd_TreeNode")); + // item->addChild(child); + //} + + int i=1; + for (TDF_ChildIterator it(label); it.More(); it.Next(),i++) { + QString text = QString::fromAscii("%1:%2").arg(s).arg(i); + QTreeWidgetItem* child = new QTreeWidgetItem(); + child->setText(0, text); + child->setIcon(0, myGroupIcon); + item->addChild(child); + load(it.Value(), child, text); + } +} + +static PyObject * ocaf(PyObject *self, PyObject *args) +{ + const char* Name; + if (!PyArg_ParseTuple(args, "s",&Name)) + return 0; + + PY_TRY { + //Base::Console().Log("Insert in Part with %s",Name); + Base::FileInfo file(Name); + + 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")) { + STEPCAFControl_Reader aReader; + aReader.SetColorMode(true); + aReader.SetNameMode(true); + aReader.SetLayerMode(true); + if (aReader.ReadFile((Standard_CString)Name) != IFSelect_RetDone) { + PyErr_SetString(PyExc_Exception, "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(); + } + else if (file.hasExtension("igs") || file.hasExtension("iges")) { + 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)Name) != IFSelect_RetDone) { + PyErr_SetString(PyExc_Exception, "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(); + } + else { + PyErr_SetString(PyExc_Exception, "no supported file format"); + return 0; + } + + OCAFBrowser browse(hDoc); + browse.load(); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PyExc_Exception, e->GetMessageString()); + return 0; + } + PY_CATCH + + Py_Return; +} + /* registration table */ struct PyMethodDef ImportGui_Import_methods[] = { {"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."}, + {"ocaf" ,ocaf ,METH_VARARGS, + "ocaf(string) -- Browse the ocaf structure."}, {NULL, NULL} /* end of table marker */ }; diff --git a/src/Mod/Import/Gui/Makefile.am b/src/Mod/Import/Gui/Makefile.am index 348856fe2..255e13e93 100644 --- a/src/Mod/Import/Gui/Makefile.am +++ b/src/Mod/Import/Gui/Makefile.am @@ -18,7 +18,7 @@ libImportGui_la_LDFLAGS = \ -L../../../Gui \ -L../../Part/App \ -L../../Part/Gui \ - -L$(OCC_LIB) $(QT4_CORE_LIBS) $(all_libraries) \ + -L$(OCC_LIB) $(QT_LIBS) $(all_libraries) \ -version-info @LIB_CURRENT@:@LIB_REVISION@:@LIB_AGE@ libImportGui_la_CPPFLAGS = -DAppPartExport= -DAppPartGuiExport=