int Part::ImportStepParts(App::Document *pcDoc, const char* Name) { STEPControl_Reader aReader; TopoDS_Shape aShape; Base::FileInfo fi(Name); if (!fi.exists()) { std::stringstream str; str << "File '" << Name << "' does not exist!"; throw Base::Exception(str.str().c_str()); } std::string encodednamestr = encodeFilename(std::string(Name)); const char * encodedname = encodednamestr.c_str(); if (aReader.ReadFile((Standard_CString)encodedname) != IFSelect_RetDone) { throw Base::Exception("Cannot open STEP file"); } Handle_Message_ProgressIndicator pi = new ProgressIndicator(100); aReader.WS()->MapReader()->SetProgress(pi); pi->NewScope(100, "Reading STEP file..."); pi->Show(); // Root transfers Standard_Integer nbr = aReader.NbRootsForTransfer(); //aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity); for (Standard_Integer n = 1; n<= nbr; n++) { Base::Console().Log("STEP: Transferring Root %d\n",n); aReader.TransferRoot(n); } pi->EndScope(); // Collecting resulting entities Standard_Integer nbs = aReader.NbShapes(); if (nbs == 0) { throw Base::Exception("No shapes found in file "); } else { //Handle(StepData_StepModel) Model = aReader.StepModel(); //Handle_XSControl_WorkSession ws = aReader.WS(); //Handle_XSControl_TransferReader tr = ws->TransferReader(); std::map<int, Quantity_Color> hash_col; //ReadColors(aReader.WS(), hash_col); //ReadNames(aReader.WS()); for (Standard_Integer i=1; i<=nbs; i++) { Base::Console().Log("STEP: Transferring Shape %d\n",i); aShape = aReader.Shape(i); // load each solid as an own object TopExp_Explorer ex; for (ex.Init(aShape, TopAbs_SOLID); ex.More(); ex.Next()) { // get the shape const TopoDS_Solid& aSolid = TopoDS::Solid(ex.Current()); std::string name = fi.fileNamePure(); //Handle_Standard_Transient ent = tr->EntityFromShapeResult(aSolid, 3); //if (!ent.IsNull()) { // name += ws->Model()->StringLabel(ent)->ToCString(); //} Part::Feature *pcFeature; pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name.c_str())); pcFeature->Shape.setValue(aSolid); // This is a trick to access the GUI via Python and set the color property // of the associated view provider. If no GUI is up an exception is thrown // and cleared immediately std::map<int, Quantity_Color>::iterator it = hash_col.find(aSolid.HashCode(INT_MAX)); if (it != hash_col.end()) { try { Py::Object obj(pcFeature->getPyObject(), true); Py::Object vp(obj.getAttr("ViewObject")); Py::Tuple col(3); col.setItem(0, Py::Float(it->second.Red())); col.setItem(1, Py::Float(it->second.Green())); col.setItem(2, Py::Float(it->second.Blue())); vp.setAttr("ShapeColor", col); //Base::Console().Message("Set color to shape\n"); } catch (Py::Exception& e) { e.clear(); } } } // load all non-solids now for (ex.Init(aShape, TopAbs_SHELL, TopAbs_SOLID); ex.More(); ex.Next()) { // get the shape const TopoDS_Shell& aShell = TopoDS::Shell(ex.Current()); std::string name = fi.fileNamePure(); //Handle_Standard_Transient ent = tr->EntityFromShapeResult(aShell, 3); //if (!ent.IsNull()) { // name += ws->Model()->StringLabel(ent)->ToCString(); //} Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject("Part::Feature", name.c_str())); pcFeature->Shape.setValue(aShell); } // put all other free-flying shapes into a single compound Standard_Boolean emptyComp = Standard_True; BRep_Builder builder; TopoDS_Compound comp; builder.MakeCompound(comp); for (ex.Init(aShape, TopAbs_FACE, TopAbs_SHELL); ex.More(); ex.Next()) { if (!ex.Current().IsNull()) { builder.Add(comp, ex.Current()); emptyComp = Standard_False; } } for (ex.Init(aShape, TopAbs_WIRE, TopAbs_FACE); ex.More(); ex.Next()) { if (!ex.Current().IsNull()) { builder.Add(comp, ex.Current()); emptyComp = Standard_False; } } for (ex.Init(aShape, TopAbs_EDGE, TopAbs_WIRE); ex.More(); ex.Next()) { if (!ex.Current().IsNull()) { builder.Add(comp, ex.Current()); emptyComp = Standard_False; } } for (ex.Init(aShape, TopAbs_VERTEX, TopAbs_EDGE); ex.More(); ex.Next()) { if (!ex.Current().IsNull()) { builder.Add(comp, ex.Current()); emptyComp = Standard_False; } } if (!emptyComp) { std::string name = fi.fileNamePure(); Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject ("Part::Feature", name.c_str())); pcFeature->Shape.setValue(comp); } } } return 0; }
static PyObject * importer(PyObject *self, PyObject *args) { char* Name; char* DocName=0; if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&DocName)) 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(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((const char*)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((const char*)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; } ImportOCAFExt ocaf(hDoc, pcDoc, file.fileNamePure()); ocaf.loadShapes(); pcDoc->recompute(); } catch (Standard_Failure) {
static PyObject * importer(PyObject *self, PyObject *args) { char* Name; char* DocName=0; if (!PyArg_ParseTuple(args, "s|s",&Name,&DocName)) return 0; PY_TRY { //Base::Console().Log("Insert in Part with %s",Name); Base::FileInfo file(Name); 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); QString fn = QString::fromUtf8(Name); if (aReader.ReadFile((const char*)fn.toLocal8Bit()) != 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(); } 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,Name); pcDoc->recompute(); } } 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); QString fn = QString::fromUtf8(Name); if (aReader.ReadFile((const char*)fn.toLocal8Bit()) != 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(); } 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,Name); pcDoc->recompute(); } } else { PyErr_SetString(PyExc_Exception, "no supported file format"); return 0; } ImportOCAFExt ocaf(hDoc, pcDoc, file.fileNamePure()); ocaf.loadShapes(); pcDoc->recompute(); } catch (Standard_Failure) {
int Part::ImportIgesParts(App::Document *pcDoc, const char* FileName) { try { Base::FileInfo fi(FileName); // read iges file // http://www.opencascade.org/org/forum/thread_20801/ IGESControl_Controller::Init(); Interface_Static::SetIVal("read.surfacecurve.mode",3); // load data exchange message files Message_MsgFile::LoadFromEnv("CSF_XSMessage","IGES"); // load shape healing message files Message_MsgFile::LoadFromEnv("CSF_SHMessageStd","SHAPEStd"); IGESControl_Reader aReader; if (aReader.ReadFile((const Standard_CString)FileName) != IFSelect_RetDone) throw Base::Exception("Error in reading IGES"); // check file conformity and output stats aReader.PrintCheckLoad(Standard_True,IFSelect_GeneralInfo); #if 1 std::string aName = fi.fileNamePure(); Handle_Message_ProgressIndicator pi = new ProgressIndicator(100); pi->NewScope(100, "Reading IGES file..."); pi->Show(); aReader.WS()->MapReader()->SetProgress(pi); // make model aReader.ClearShapes(); Standard_Integer nbRootsForTransfer = aReader.NbRootsForTransfer(); aReader.TransferRoots(); pi->EndScope(); // put all other free-flying shapes into a single compound Standard_Boolean emptyComp = Standard_True; BRep_Builder builder; TopoDS_Compound comp; builder.MakeCompound(comp); Standard_Integer nbShapes = aReader.NbShapes(); for (Standard_Integer i=1; i<=nbShapes; i++) { TopoDS_Shape aShape = aReader.Shape(i); if (!aShape.IsNull()) { if (aShape.ShapeType() == TopAbs_SOLID || aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_SHELL) { App::DocumentObject* obj = pcDoc->addObject("Part::Feature", aName.c_str()); static_cast<Part::Feature*>(obj)->Shape.setValue(aShape); } else { builder.Add(comp, aShape); emptyComp = Standard_False; } } } if (!emptyComp) { std::string name = fi.fileNamePure(); Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject ("Part::Feature", name.c_str())); pcFeature->Shape.setValue(comp); } #else // put all other free-flying shapes into a single compound Standard_Boolean emptyComp = Standard_True; BRep_Builder builder; TopoDS_Compound comp; builder.MakeCompound(comp); // get all entities Handle_TColStd_HSequenceOfTransient aRootList=aReader.GiveList("xst-transferrable-roots"); Base::SequencerLauncher seq("Reading IGES file...", aRootList->Length()); Standard_Integer j; for (j=1; j<=aRootList->Length(); j++) { seq.next(); Handle(IGESData_IGESEntity) igesEntity = Handle(IGESData_IGESEntity)::DownCast(aRootList->Value(j)); if (igesEntity.IsNull()) continue; // clear any old shape aReader.ClearShapes(); #ifdef _DEBUG std::string type = igesEntity->DynamicType()->Name(); (void)type; #endif // is it a group, singular sub-figure or solid? if (igesEntity->IsKind(STANDARD_TYPE(IGESBasic_Group)) || igesEntity->IsKind(STANDARD_TYPE(IGESBasic_SingularSubfigure)) || igesEntity->IsKind(STANDARD_TYPE(IGESSolid_ManifoldSolid))) { try { if (aReader.TransferEntity(igesEntity)) { if (aReader.NbShapes()>0) { // get the shape std::string aName = fi.fileNamePure(); if (igesEntity->HasShortLabel()) { Handle(TCollection_HAsciiString) aLabel=igesEntity->ShortLabel(); aName = aLabel->ToCString(); } TopoDS_Shape aShape=aReader.OneShape(); App::DocumentObject* obj = pcDoc->addObject("Part::Feature", aName.c_str()); obj->Label.setValue(aName); static_cast<Part::Feature*>(obj)->Shape.setValue(aShape); int iColor; if (igesEntity->RankColor()>-1) { iColor = igesEntity->RankColor(); } } } } catch (Standard_Failure) { } } // normal shapes else { try { if (aReader.TransferEntity(igesEntity)) { if (aReader.NbShapes()>0) { TopoDS_Shape aShape=aReader.OneShape(); builder.Add(comp, aShape); emptyComp = Standard_False; } } } catch (Standard_Failure) { } } } if (!emptyComp) { std::string name = fi.fileNamePure(); Part::Feature *pcFeature = static_cast<Part::Feature*>(pcDoc->addObject ("Part::Feature", name.c_str())); pcFeature->Shape.setValue(comp); } #endif } catch (Standard_Failure) { Handle(Standard_Failure) aFail = Standard_Failure::Caught(); throw Base::Exception(aFail->GetMessageString()); } return 0; }