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 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 _get_name(self): try: a = self.Downcast() name = TCollection.TCollection_AsciiString(a.Get()) return name.ToCString() except: return "no name"
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 _get_name_attr(self): label = self.TDF_Label h_a = TDataStd.Handle_TDataStd_Name() gid = TDataStd.TDataStd_Name().ID() if label.FindAttribute(gid, h_a): a = h_a.GetObject() name = TCollection.TCollection_AsciiString(a.Get()) return name.ToCString() else: return "no name"
def print_children(label): tool = TDF.TDF_Tool() itr = TDF.TDF_ChildIterator(label, True) while itr.More(): val = itr.Value() output = TCollection.TCollection_AsciiString() tool.Entry(val, output) entry = output.ToCString() print "entry", entry itr.Next() print "end iteration"
def _TDF_Label_changed(self, new_label): tool = TDF.TDF_Tool() output = TCollection.TCollection_AsciiString() tool.entry(new_label, output) entry = output.ToCString() self.entry = entry itr = TDF.TDF_AttributeIterator(new_label, False) attrs = [] while itr.More(): val = itr.Value() #print val a = Attribute(TDF_Attribute=val) #print val, "->", a.Downcast() attrs.append(a) itr.Next() self.attributes = attrs
def set_layer(self, layer_name): r"""set the current layer name if the layer has already been set before, that TDF_Label will be used Parameters ---------- layer_name: str name of the layer """ if layer_name in self.layer_names: self.current_layer = self.layer_names[layer_name] else: self.current_layer = self.layers.AddLayer( TCollection.TCollection_ExtendedString(layer_name)) self.layer_names[layer_name] = self.current_layer
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 MakeNewDocHandle(self): h_doc = TDocStd.Handle_TDocStd_Document() schema = TCollection.TCollection_ExtendedString("MDTV-CAF") self.XCAFApp.NewDocument(schema, h_doc) return h_doc
reader = STEPCAFControl.STEPCAFControl_Reader() print "1" reader.ReadFile('XDE_test.step') print "2" # # Create the TDocStd document # h_doc = TDocStd.Handle_TDocStd_Document() print h_doc.IsNull() # # Create the application: really *awfull* way to do that # #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()
##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from OCC import BRepPrimAPI, BRepFilletAPI, TNaming,\ TDocStd, AppStd, TCollection, TDF, BRepAlgoAPI, gp, TopTools,\ TopoDS, Quantity, Graphic3d, AIS from OCC.Utils.Topology import Topo from OCC.Display.SimpleGui import * display, start_display, add_menu, add_function_to_menu = init_display() app = AppStd.AppStd_Application() h_doc = TDocStd.Handle_TDocStd_Document() schema = TCollection.TCollection_ExtendedString("MyFormat") app.NewDocument(schema, h_doc) doc = h_doc.GetObject() root = doc.Main() ts = TDF.TDF_TagSource() box = BRepPrimAPI.BRepPrimAPI_MakeBox(20.0, 20.0, 20.0).Shape() box_label = ts.NewChild(root) ns_builder = TNaming.TNaming_Builder(box_label) ns_builder.Generated(box) topo = Topo(box)
val = event.GetInt() self.canvas.SetBoxSize(val / 3.) self.canvas.Render() def OnSlide2(self, event): val = event.GetInt() self.canvas.SetBoxPosition(val) self.canvas.Render() def OnSlide3(self, event): val = event.GetInt() self.canvas.SetCylDirection(val) self.canvas.Render() wxApp = wx.App() app = XCAFApp.GetApplication().GetObject() h_doc = TDocStd.Handle_TDocStd_Document() schema = TCollection.TCollection_ExtendedString("MDTV-CAF") app.NewDocument(schema, h_doc) doc = h_doc.GetObject() frame = MyFrame(doc) frame.Show() wxApp.MainLoop()
def read_file(self): 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()) l_materials = XCAFDoc.XCAFDoc_DocumentTool_materialtool(doc.Main()) STEPReader = STEPCAFControl_Reader() STEPReader.SetColorMode(True) STEPReader.SetLayerMode(True) STEPReader.SetNameMode(True) STEPReader.SetMatMode(True) status = STEPReader.ReadFile(str(self.filename)) if status == IFSelect_RetDone: STEPReader.Transfer(doc.GetHandle()) Labels = TDF_LabelSequence() ColorLabels = TDF_LabelSequence() #TopoDS_Shape aShape; shape_tool = h_shape_tool.GetObject() h_shape_tool.GetObject().GetFreeShapes(Labels) print 'Number of shapes at root :%i'%Labels.Length() for i in range(Labels.Length()): sub_shapes_labels = TDF_LabelSequence() print "Is Assembly?",shape_tool.isassembly(Labels.Value(i+1)) sub_shapes = shape_tool.getsubshapes(Labels.Value(i+1),sub_shapes_labels) print 'Number of subshapes in the assemly :%i'%sub_shapes_labels.Length() l_Colors.GetObject().GetColors(ColorLabels) print 'Number of colors=%i'%ColorLabels.Length() #if(CL_Len>0): # ColorTool->GetColor(ColorLabels.Value(1),DefaultColor); for i in range(Labels.Length()): #print i print Labels.Value(i+1) aShape = h_shape_tool.GetObject().getshape(Labels.Value(i+1)) m = l_Layers.GetObject().GetLayers(aShape) print dir(h_shape_tool.GetObject()) #print c print "Lenght of popo :%i"%m.GetObject().Length() #v = m.GetObject().Value(0)#Value() #print v,v.Length() #for i in v.Length(): # print chr(v.Value(i+1)) #layer_labels = TDF_LabelSequence() #popo = Handle_TColStd_HSequenceOfExtendedString() #print 'Number of layers :%i'%layer_labels.Length() #print aShape,aShape.ShapeType() if aShape.ShapeType() == TopAbs_COMPOUND: t = Topo(aShape) for t in t.solids(): self._shapes.append(t) return True
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