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, 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 __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 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
from OCC import XCAFApp, STEPCAFControl, TDocStd, TCollection,\ XCAFDoc, BRepPrimAPI, Quantity, STEPControl,\ TopLoc, gp, TDF, TPrsStd 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
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()
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