def __init__(self, filename, layer_name='layer-00'): self.filename = filename self.h_doc = h_doc = TDocStd.Handle_TDocStd_Document() print "Empty Doc?", h_doc.IsNull() # Create the application app = XCAFApp.GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"),h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()) l_Colors = XCAFDoc.XCAFDoc_DocumentTool().ColorTool(doc.Main()) l_Layers = XCAFDoc.XCAFDoc_DocumentTool().LayerTool(doc.Main()) labels = TDF_LabelSequence() ColorLabels = TDF_LabelSequence() #TopoDS_Shape aShape; self.shape_tool = h_shape_tool.GetObject() self.top_label = self.shape_tool.NewShape() self.colors = l_Colors.GetObject() self.layers = l_Layers.GetObject() self.current_color = Quantity.Quantity_Color(Quantity.Quantity_NOC_RED) self.current_layer = self.layers.AddLayer(TCollection_ExtendedString(layer_name)) self.layer_names = {}
def export_shapes2(shapeList, filename, colorList=[]): h_doc = TDocStd.Handle_TDocStd_Document() app = XCAFApp.XCAFApp_Application_GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"),h_doc) doc = h_doc.GetObject() h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()) h_color_tool = XCAFDoc.XCAFDoc_DocumentTool().ColorTool(doc.Main()) shape_tool = h_shape_tool.GetObject() color_tool = h_color_tool.GetObject() top_label = shape_tool.NewShape() colorList.extend(["brown"]*(len(shapeList) - len(colorList))) cmap = get_color_map() tr = gp.gp_Trsf() loc = TopLoc.TopLoc_Location(tr) print(colorList) print(shapeList) colorMap = dict((c, Quantity.Quantity_Color(cmap.get(c, 133))) for c in colorList) for shape, color in zip(shapeList, colorList): if not shape: continue print("color:", color, "shape", shape) label = shape_tool.AddShape(shape, False, True) ref_label = shape_tool.AddComponent(top_label, label, loc) c = colorMap[color] color_tool.SetColor(ref_label, c, XCAFDoc.XCAFDoc_ColorGen) mode = STEPControl.STEPControl_AsIs writer = STEPCAFControl.STEPCAFControl_Writer() writer.Transfer(h_doc, mode) writer.Write(str(filename))
def __init__(self, filename, layer_name='layer-00'): logger.info("StepOcafExporter instantiated with filename : %s" % filename) check_exporter_filename(filename, step_extensions) check_overwrite(filename) self.filename = filename self.h_doc = h_doc = TDocStd.Handle_TDocStd_Document() logger.info("Empty Doc?", h_doc.IsNull()) # Create the application app = XCAFApp._XCAFApp.XCAFApp_Application_GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() self.shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool( doc.Main()).GetObject() self.colors = XCAFDoc.XCAFDoc_DocumentTool().ColorTool( doc.Main()).GetObject() self.layers = XCAFDoc.XCAFDoc_DocumentTool().LayerTool( doc.Main()).GetObject() labels = TDF.TDF_LabelSequence() color_labels = TDF.TDF_LabelSequence() # TopoDS_Shape aShape; self.top_label = self.shape_tool.NewShape() self.current_color = Quantity.Quantity_Color(Quantity.Quantity_NOC_RED) self.current_layer = self.layers.AddLayer( TCollection.TCollection_ExtendedString(layer_name)) self.layer_names = {}
def InitDoc(self): h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()) l_Colors = XCAFDoc.XCAFDoc_DocumentTool().ColorTool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = l_Colors.GetObject() self.shape_tool = shape_tool top_label = shape_tool.NewShape( ) #this is the "root" label for the assembly self.top_label = top_label #Add some shapes box = BRepPrimAPI.BRepPrimAPI_MakeBox(10, 20, 30).Shape() box_label = shape_tool.AddShape(box, False) cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(25, 50).Shape() cyl_label = shape_tool.AddShape(cyl, False) #Add components as references to our shape tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(100, 100, 100)) loc = TopLoc.TopLoc_Location(tr) box_comp1 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(-100, -100, -100)) loc = TopLoc.TopLoc_Location(tr) box_comp2 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(10, 10, 10)) loc = TopLoc.TopLoc_Location(tr) cyl_comp = shape_tool.AddComponent(top_label, cyl_label, loc) #Add some colors red = Quantity.Quantity_Color(Quantity.Quantity_NOC_RED) green = Quantity.Quantity_Color(Quantity.Quantity_NOC_GREEN) blue = Quantity.Quantity_Color(Quantity.Quantity_NOC_BLUE1) colors.SetColor(cyl_comp, red, XCAFDoc.XCAFDoc_ColorGen) colors.SetColor(box_label, blue, XCAFDoc.XCAFDoc_ColorGen) colors.SetColor(box_comp2, green, XCAFDoc.XCAFDoc_ColorGen) self.box_label = box_label self.cyl_label = cyl_label self.box_comp1 = box_comp1 self.box_comp2 = box_comp2 self.cyl_comp = cyl_comp
def FillTestDoc(doc): h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().shapetool(doc.Main()) h_Colors = XCAFDoc.XCAFDoc_DocumentTool().colortool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = h_Colors.GetObject() top_label = shape_tool.NewShape( ) #this is the "root" label for the assembly print "top entry", Label(TDF_Label=top_label).entry box = BRepPrimAPI.BRepPrimAPI_MakeBox(10, 20, 30).Shape() box_label = shape_tool.AddShape(box, False) cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(25, 50).Shape() cyl_label = shape_tool.AddShape(cyl, False) # tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(100, 100, 100)) loc = TopLoc.TopLoc_Location(tr) box_comp1 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(200, 200, 200)) loc = TopLoc.TopLoc_Location(tr) box_comp2 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(150, 200, 100)) loc = TopLoc.TopLoc_Location(tr) cyl_comp = shape_tool.AddComponent(top_label, cyl_label, loc) red = Quantity.Quantity_Color(Quantity.Quantity_NOC_RED) green = Quantity.Quantity_Color(Quantity.Quantity_NOC_GREEN) colors.SetColor(cyl_comp, red, XCAFDoc.XCAFDoc_ColorGen) colors.SetColor(box_comp2, green, XCAFDoc.XCAFDoc_ColorGen)
def assembly(event=None): # Create the TDocStd document h_doc = TDocStd.Handle_TDocStd_Document() print("Empty Doc?", h_doc.IsNull()) # Create the application app = XCAFApp.GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()) l_colors = XCAFDoc.XCAFDoc_DocumentTool().ColorTool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = l_colors.GetObject() top_label = shape_tool.NewShape( ) #this is the "root" label for the assembly print("Is Assembly", shape_tool.IsAssembly(top_label)) # not yet, 'cos it's empty # Add some shapes box = BRepPrimAPI.BRepPrimAPI_MakeBox(10, 20, 30).Shape() box_label = shape_tool.AddShape(box, False) cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(25, 50).Shape() cyl_label = shape_tool.AddShape(cyl, False) # Add components as references to our shape tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(100, 100, 100)) loc = TopLoc.TopLoc_Location(tr) box_comp1 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(200, 200, 200)) loc = TopLoc.TopLoc_Location(tr) box_comp2 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(150, 200, 100)) loc = TopLoc.TopLoc_Location(tr) cyl_comp = shape_tool.AddComponent(top_label, cyl_label, loc) print("Is Assembly", shape_tool.IsAssembly(top_label)) # it is now... # Add some colors red = Quantity.Quantity_Color(Quantity.Quantity_NOC_RED) green = Quantity.Quantity_Color(Quantity.Quantity_NOC_GREEN) colors.SetColor(cyl_comp, red, XCAFDoc.XCAFDoc_ColorGen) colors.SetColor(box_comp2, green, XCAFDoc.XCAFDoc_ColorGen) # Set up AIS Presentation stuff (I don't understand this, but it kinda works) ais_view = TPrsStd.TPrsStd_AISViewer().New(top_label, display.Context.GetHandle()) ais_pres = TPrsStd.TPrsStd_AISPresentation().Set( top_label, XCAFPrs.XCAFPrs_Driver().GetID()) ais_pres.GetObject().Display(True) display.FitAll()
#app = Handle_XCAFApp_Application().GetObject() app = XCAFApp.GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"),h_doc) print "3" # # Transfer # print h_doc.IsNull() if not reader.Transfer(h_doc): print "Error" print "4 bis" # # Get root assembly # doc = h_doc.GetObject() print "4" h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()) shape_tool = h_shape_tool.GetObject() # get the top level shapes l_LabelShapes = TDF.TDF_LabelSequence() shape_tool.GetShapes(l_LabelShapes) count = l_LabelShapes.Length() root_label = l_LabelShapes.Value(1) shape = shape_tool.GetShape(root_label) viewXDE(doc, root_label, shape)
# Create the TDocStd document # h_doc = TDocStd.Handle_TDocStd_Document() print "Empty Doc?", h_doc.IsNull() # # Create the application # app = XCAFApp.GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"),h_doc) # # Get root assembly # doc = h_doc.GetObject() h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()) l_Colors = XCAFDoc.XCAFDoc_DocumentTool().ColorTool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = l_Colors.GetObject() top_label = shape_tool.NewShape() #this is the "root" label for the assembly print "Is Assembly", shape_tool.IsAssembly(top_label) #not yet, 'cos it's empty # #Add some shapes # box = BRepPrimAPI.BRepPrimAPI_MakeBox(10,20,30).Shape() box_label = shape_tool.AddShape(box, False)
def step_export_layers_and_colors(event=None): r""" demo showing how to export step files with layers & colors. adapted from Bryan's names_shape_demo """ # Create the TDocStd document h_doc = TDocStd.Handle_TDocStd_Document() print("Empty Doc?", h_doc.IsNull()) # Create the application app = XCAFApp.GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()) l_colors = XCAFDoc.XCAFDoc_DocumentTool().ColorTool(doc.Main()) l_layers = XCAFDoc.XCAFDoc_DocumentTool().LayerTool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = l_colors.GetObject() layers = l_layers.GetObject() top_label = shape_tool.NewShape( ) # this is the "root" label for the assembly print("Is Assembly", shape_tool.IsAssembly(top_label)) # not yet, 'cos it's empty) # Add some shapes box = BRepPrimAPI.BRepPrimAPI_MakeBox(10, 20, 30).Shape() box_label = shape_tool.AddShape(box, False) cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(25, 50).Shape() cyl_label = shape_tool.AddShape(cyl, False) # Add components as references to our shape tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(100, 100, 100)) loc = TopLoc.TopLoc_Location(tr) box_comp1 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(200, 200, 200)) loc = TopLoc.TopLoc_Location(tr) box_comp2 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(150, 200, 100)) loc = TopLoc.TopLoc_Location(tr) cyl_comp = shape_tool.AddComponent(top_label, cyl_label, loc) print("Is Assembly", shape_tool.IsAssembly(top_label)) # it is now... # Add some colors red = Quantity.Quantity_Color(Quantity.Quantity_NOC_RED) green = Quantity.Quantity_Color(Quantity.Quantity_NOC_GREEN) colors.SetColor(cyl_comp, red, XCAFDoc.XCAFDoc_ColorGen) colors.SetColor(box_comp2, green, XCAFDoc.XCAFDoc_ColorGen) # Set the box on the 'box' layer, the cylinder on the 'cylinder' layer layers.SetLayer(box_comp1, TCollection_ExtendedString('BOX')) layers.SetLayer(cyl_comp, TCollection_ExtendedString('CYLINDER')) # Set up AIS Presentation stuff (I don't understand this, but it kinda works) aisView = TPrsStd.TPrsStd_AISViewer().New(top_label, display.Context.GetHandle()) aisPres = TPrsStd.TPrsStd_AISPresentation().Set( top_label, XCAFPrs.XCAFPrs_Driver().GetID()) aisPres.GetObject().Display(True) display.FitAll() # write the stuff to STEP, with layers & colors work_session = XSControl_WorkSession() writer = STEPCAFControl_Writer(work_session.GetHandle(), False) writer.Transfer(h_doc, STEPControl_AsIs) pth = '.' print('writing STEP file') status = writer.Write(os.path.join(pth, 'step_layers_colors.step')) print('status:', status)
def read_file(self): r"""Read file""" logger.info("Reading STEP file") h_doc = TDocStd.Handle_TDocStd_Document() # Create the application app = XCAFApp._XCAFApp.XCAFApp_Application_GetApplication().GetObject() app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool( doc.Main()).GetObject() color_tool = XCAFDoc.XCAFDoc_DocumentTool().ColorTool( doc.Main()).GetObject() layer_tool = XCAFDoc.XCAFDoc_DocumentTool().LayerTool( doc.Main()).GetObject() l_materials = XCAFDoc.XCAFDoc_DocumentTool().MaterialTool( doc.Main()).GetObject() step_reader = STEPCAFControl.STEPCAFControl_Reader() step_reader.SetColorMode(True) step_reader.SetLayerMode(True) step_reader.SetNameMode(True) step_reader.SetMatMode(True) status = step_reader.ReadFile(self.filename) if status == IFSelect.IFSelect_RetDone: logger.info("Transfer doc to STEPCAFControl_Reader") step_reader.Transfer(doc.GetHandle()) else: raise ValueError("could not read {}".format(self.filename)) labels = TDF.TDF_LabelSequence() color_labels = TDF.TDF_LabelSequence() # TopoDS_Shape a_shape; h_shape_tool.GetFreeShapes(labels) h_shape_tool.GetShapes(labels) logger.info('Number of shapes at root :%i' % labels.Length()) # for i in range(labels.Length()): # a_shape = h_shape_tool.GetObject().GetShape(labels.Value(i+1)) # logger.debug("%i - type : %s" % (i, a_shape.ShapeType())) # sub_shapes_labels = TDF.TDF_LabelSequence() # print("Is Assembly?", shape_tool.IsAssembly(labels.Value(i + 1))) # # sub_shapes = shape_tool.getsubshapes(labels.Value(i+1), sub_shapes_labels) # # sub_shapes = shape_tool.FindSubShape(labels.Value(i + 1), a_shape, labels.Value(i + 1)) # print('Number of subshapes in the assembly : %i' % sub_shapes_labels.Length()) # # color_tool.GetObject().GetColors(color_labels) # logger.info('Number of colors : %i' % color_labels.Length()) for i in range(labels.Length()): # print i label = labels.Value(i + 1) logger.debug("Label : %s" % label) a_shape = h_shape_tool.GetShape(labels.Value(i + 1)) # string_seq = TColStd.TColStd_HSequenceOfExtendedString() # string_seq is an TColStd.TColStd_HSequenceOfExtendedString string_seq = layer_tool.GetLayers(a_shape) color = Quantity.Quantity_Color() c = color_tool.GetColor(a_shape, XCAFDoc.XCAFDoc_ColorSurf, color) logger.info("The shape type is : %i" % a_shape.ShapeType()) if a_shape.ShapeType() == TopAbs.TopAbs_COMPOUND: logger.info("The shape type is TopAbs.TopAbs_COMPOUND") topo = Topo(a_shape) logger.info("Nb of compounds : %i" % topo.number_of_compounds) logger.info("Nb of solids : %i" % topo.number_of_solids) logger.info("Nb of shells : %i" % topo.number_of_shells) for solid in topo.solids: logger.info("Adding solid to the shapes list") self._shapes.append(solid) elif a_shape.ShapeType() == TopAbs.TopAbs_SOLID: logger.info("The shape type is TopAbs.TopAbs_SOLID") self._shapes.append(a_shape) self._colors.append(color) self._layers.append(string_seq) return True