def addComponent(self, shape, name, color): """Add new shape to top assembly of self.doc & return uid""" labels = TDF_LabelSequence() shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main()) color_tool = XCAFDoc_DocumentTool_ColorTool(self.doc.Main()) shape_tool.GetShapes(labels) try: rootLabel = labels.Value(1) # First label at root except RuntimeError as e: print(e) return newLabel = shape_tool.AddComponent(rootLabel, shape, True) # Get referrred label and apply color to it refLabel = TDF_Label() # label of referred shape isRef = shape_tool.GetReferredShape(newLabel, refLabel) if isRef: color_tool.SetColor(refLabel, color, XCAFDoc_ColorGen) self.setLabelName(newLabel, name) logger.info('Part %s added to root label', name) shape_tool.UpdateAssemblies() self.doc = self.doc_linter() # This gets color to work self.parse_doc() entry = newLabel.EntryDumpToString() uid = entry + '.0' # this should work OK since it is new return uid
def saveStep(self): """Export self.doc to STEP file.""" prompt = 'Choose filename for step file.' fnametuple = QFileDialog.getSaveFileName( None, prompt, './', "STEP files (*.stp *.STP *.step)") fname, _ = fnametuple if not fname: print("Save step cancelled.") return # Reconstruct XCAFDoc related code from stepXD.StepImporter labels = TDF_LabelSequence() shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main()) shape_tool.GetShapes(labels) logger.info('Number of labels at root : %i', labels.Length()) try: rootlabel = labels.Value(1) # First label at root except RuntimeError: return name = rootlabel.GetLabelName() logger.info('Name of root label: %s', name) isAssy = shape_tool.IsAssembly(rootlabel) logger.info("First label at root holds an assembly? %s", isAssy) # Modify self.doc by adding active part to rootlabel. #Standard_Boolean expand = Standard_False; //default #TDF_Label aLabel = myAssembly->AddComponent (aShape [,expand]); newLabel = shape_tool.AddComponent(rootlabel, self.activePart, True) #set a name to newlabel (as a reminder using OCAF), use: #TCollection_ExtendedString aName ...; #// contains the desired name for this Label (ASCII) #TDataStd_Name::Set (aLabel, aName); newName = TCollection_ExtendedString( self._nameDict[self.activePartUID]) TDataStd_Name.Set(newLabel, newName) logger.info('Name of new part: %s', newName) #myAssembly->UpdateAssemblies(); shape_tool.UpdateAssemblies() # initialize the STEP exporter step_writer = STEPCAFControl_Writer() # transfer shapes and write file step_writer.Transfer(self.doc) status = step_writer.Write(fname) assert status == IFSelect_RetDone