def read_step_file_with_attribute2(filename): """ Read a step file and returns a shape (geometrical information). This uses the XDE of OCCD and will also be able to extract the attribute information such as colour or name or layer. """ h_doc = Handle_TDocStd_Document() # print "Empty Doc?", h_doc.IsNull() ### Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) ### Get tools, here only enable the colour attribute doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) h_colour_tool = XCAFDoc_DocumentTool_ColorTool(doc.Main()) # l_Layers = XCAFDoc_DocumentTool_LayerTool(doc.Main()) # l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main()) shape_tool = h_shape_tool.GetObject() colour_tool = h_colour_tool.GetObject() ### Read files STEPReader = STEPCAFControl_Reader() STEPReader.SetColorMode(True) # STEPReader.SetLayerMode(True) # STEPReader.SetNameMode(True) # STEPReader.SetMatMode(True) status = STEPReader.ReadFile(filename) STEPReader.Reader().PrintCheckLoad(True, 0) if status == IFSelect_RetDone: STEPReader.Transfer(doc.GetHandle()) ### Get root assembly shapeLabels = TDF_LabelSequence() shape_tool.GetFreeShapes(shapeLabels) # print ('Number of shapes at root :%i'%shapeLabels.Length()) ### Here we only have one solid in the model so we directly use the first solid ### If there are more, please use the for loop to get all solids shape = shape_tool.GetShape(shapeLabels.Value(1)) topo = Topo(shape) # for i in range(shapeLabels.Length()): # #print(Labels.Value(i+1).Tag()) # shape=shape_tool.GetShape(shapeLabels.Value(i+1)) # shapes.append(shape) return [topo, colour_tool]
def read_step(self, fn): """ Read and translate a STEP file. :param str fn: The filename. :return: The shapes label. :rtype: afem.exchange.xde.Label. :raise RuntimeError: If the file cannot be read. """ reader = STEPCAFControl_Reader() reader.SetNameMode(True) reader.SetColorMode(True) status = reader.Perform(fn, self._doc) if not status: raise RuntimeError("Error reading STEP file.") self._shape = Shape.wrap(reader.Reader().OneShape()) label = XCAFDoc_DocumentTool.ShapesLabel_(self._doc.Main()) return XdeLabel(label)