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, file_path): self.h_doc = TDocStd.Handle_TDocStd_Document() self.app = XCAFApp.GetApplication().GetObject() self.app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"), self.h_doc) self.STEPReader = STEPCAFControl_Reader() if not self.STEPReader.ReadFile(file_path.encode("utf-8")): raise Exception("OpenCascade could not read STEP file") self.STEPReader.Transfer(self.h_doc) self.doc = self.h_doc.GetObject() self.h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool_ShapeTool( self.doc.Main()) self.h_colors_tool = XCAFDoc.XCAFDoc_DocumentTool_ColorTool( self.doc.Main()) self.shape_tool = self.h_shape_tool.GetObject() self.color_tool = self.h_colors_tool.GetObject() self.shapes = TDF_LabelSequence() self.shape_tool.GetShapes(self.shapes) roots = TDF_LabelSequence() self.shape_tool.GetFreeShapes(roots) ws = self.STEPReader.Reader().WS().GetObject() model = ws.Model().GetObject() model.Clear() for i in range(1, 8): ws.ClearData(i) ws.ClearFile()
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 _XCAFApp_default(self): return XCAFApp.GetApplication().GetObject()
from viewer import viewXDE 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())
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 __init__(self, file_path, id=None): self.file = file_path.encode("utf-8") self.id = id self.shapes_simples = [] self.main_product = None basename = os.path.basename(self.file) self.fileName = os.path.splitext(basename)[0] self.STEPReader = STEPCAFControl_Reader() if self.STEPReader.ReadFile(self.file) != 1: raise OCCReadingStepError self.h_doc = TDocStd.Handle_TDocStd_Document() self.app = XCAFApp.GetApplication().GetObject() self.app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"), self.h_doc) self.STEPReader.Transfer(self.h_doc) self.doc = self.h_doc.GetObject() self.h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool_ShapeTool( self.doc.Main()) self.h_colors_tool = XCAFDoc.XCAFDoc_DocumentTool_ColorTool( self.doc.Main()) self.shape_tool = self.h_shape_tool.GetObject() self.color_tool = self.h_colors_tool.GetObject() self.shapes = TDF_LabelSequence() self.shape_tool.GetShapes(self.shapes) for i in range(self.shapes.Length()): shape = self.shapes.Value(i + 1) if self.shape_tool.IsSimpleShape(shape): compShape = self.shape_tool.GetShape(shape) t = Topo(compShape) if t.number_of_vertices() > 0: label = get_label_name(shape) color = find_color(shape, self.color_tool, self.shape_tool) self.shapes_simples.append( SimpleShape(label, compShape, color)) roots = TDF_LabelSequence() self.shape_tool.GetFreeShapes(roots) self.thumbnail_valid = False if roots.Length() == 1: shape = self.shape_tool.GetShape(roots.Value(1)) t = Topo(shape) if t.number_of_vertices() > 0: bbox = Bnd_Box() gap = 0 bbox.SetGap(gap) BRepMesh_Mesh(shape, get_mesh_precision(shape, 1)) faces_iterator = Topo(shape).faces() for F in faces_iterator: face_location = TopLoc_Location() BRep_Tool_Triangulation(F, face_location) BRepBndLib_Add(shape, bbox) x_min, y_min, z_min, x_max, y_max, z_max = bbox.Get() diagonal = max(x_max - x_min, y_max - y_min, z_max - z_min) if diagonal > 0: self.scale = 200. / diagonal self.trans = ((x_max - x_min) / 2. - x_max, (y_max - y_min) / 2. - y_max, (z_max - z_min) / 2. - z_max) self.thumbnail_valid = True ws = self.STEPReader.Reader().WS().GetObject() model = ws.Model().GetObject() model.Clear()