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 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'): 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 __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 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 set_color(self, r=1, g=1, b=1, color=None): r"""Set color Parameters ---------- r : float g : float b : float color : Quantity.Quantity_Color """ if color is not None: self.current_color = color else: clr = Quantity.Quantity_Color(r, g, b, 0) self.current_color = clr
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()
def display(self, shape, color=None, material='default', transparency=0.0, line_type='solid', line_width=1, logging=True): """ Displays a ccad shape. color is used for all shape types. It is a tuple of (R, G, B) from 0.0 to 1.0. material sets the solid material (unused for non-solids). Material can be: brass bronze copper gold pewter plaster plastic silver steel stone shiny_plastic satin metallized neon_gnc chrome aluminum obsidian neon_phc jade default transparency sets the solid transparency; 0 is opaque; 1 is transparent line_type can be solid, dash, or dot for edges and wires line_width sets the edge or wire width in pixels logging allows you to keep a list of all shapes displayed """ if hasattr(shape, 'shape'): s = shape.shape else: s = shape self.selected_shape = s display_shape = {'shape': s, 'color': color, 'material': material, 'transparency': transparency, 'line_type': line_type, 'line_width': line_width} if logging: self.display_shapes.append(display_shape) aisshape = _AIS.AIS_Shape(s) handle_aisshape = aisshape.GetHandle() # Set Color if not color: color = self.foreground #print('color', color) #drawer = AIS_Drawer() #handle_drawer = drawer.GetHandle() handle_drawer = aisshape.Attributes() drawer = handle_drawer.GetObject() qcolor = _Quantity.Quantity_Color(color[0], color[1], color[2], _Quantity.Quantity_TOC_RGB) # Set Point Type aspect_point = _Prs3d.Prs3d_PointAspect(_Aspect.Aspect_TOM_PLUS, qcolor, 1.0) handle_aspect_point = aspect_point.GetHandle() drawer.SetPointAspect(handle_aspect_point) # Set Line Type local_line_type = {'solid': _Aspect.Aspect_TOL_SOLID, 'dash': _Aspect.Aspect_TOL_DASH, 'dot': _Aspect.Aspect_TOL_DOT}[line_type] aspect_line = _Prs3d.Prs3d_LineAspect(qcolor, local_line_type, line_width) handle_aspect_line = aspect_line.GetHandle() #drawer = self.glarea.occ_context.DefaultDrawer().GetObject() drawer.SetSeenLineAspect(handle_aspect_line) drawer.SetWireAspect(handle_aspect_line) # Set Shading Type aspect_shading = _Prs3d.Prs3d_ShadingAspect() handle_aspect_shading = aspect_shading.GetHandle() #print('shading color', color) aspect_shading.SetColor(qcolor, _Aspect.Aspect_TOFM_BOTH_SIDE) local_materials = {'brass': _Graphic3d.Graphic3d_NOM_BRASS, 'bronze': _Graphic3d.Graphic3d_NOM_BRONZE, 'copper': _Graphic3d.Graphic3d_NOM_COPPER, 'gold': _Graphic3d.Graphic3d_NOM_GOLD, 'pewter': _Graphic3d.Graphic3d_NOM_PEWTER, 'plaster': _Graphic3d.Graphic3d_NOM_PLASTER, 'plastic': _Graphic3d.Graphic3d_NOM_PLASTIC, 'silver': _Graphic3d.Graphic3d_NOM_SILVER, 'steel': _Graphic3d.Graphic3d_NOM_STEEL, 'stone': _Graphic3d.Graphic3d_NOM_STONE, 'shiny_plastic': \ _Graphic3d.Graphic3d_NOM_SHINY_PLASTIC, 'satin': _Graphic3d.Graphic3d_NOM_SATIN, 'metallized': _Graphic3d.Graphic3d_NOM_METALIZED, 'neon_gnc': _Graphic3d.Graphic3d_NOM_NEON_GNC, 'chrome': _Graphic3d.Graphic3d_NOM_CHROME, 'aluminum': _Graphic3d.Graphic3d_NOM_ALUMINIUM, 'obsidian': _Graphic3d.Graphic3d_NOM_OBSIDIAN, 'neon_phc': _Graphic3d.Graphic3d_NOM_NEON_PHC, 'jade': _Graphic3d.Graphic3d_NOM_JADE, 'default': _Graphic3d.Graphic3d_NOM_DEFAULT} local_material = local_materials[material] aspect_shading.SetMaterial(local_material) aspect_shading.SetTransparency(transparency) drawer.SetShadingAspect(handle_aspect_shading) self.glarea.occ_context.Display(handle_aisshape, True)
def _color_changed(self, new_color): c = Quantity.Quantity_Color(ColorDict[new_color]) self.ais_shape.SetColor(c) self.render = True
def set_color(self, r=1,g=1,b=1, color=None): if not color is None: self.current_color = color else: clr = Quantity.Quantity_Color(r,g,b,0) self.current_color = clr
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) context = display.Context # # Set up AIS Presentation stuff (I don't understand this, but it kinda works) # aisView = TPrsStd.TPrsStd_AISViewer().New(top_label, context.GetHandle()) aisPres = TPrsStd.TPrsStd_AISPresentation().Set(top_label, XCAFPrs.XCAFPrs_Driver().GetID()) aisPres.GetObject().Display(True) #context.UpdateCurrentViewer()
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