class ExportCAFMethod (object): def __init__(self, name="name", tol=1.0E-10): self.name = name self.step = STEPCAFControl_Writer() self.step.SetNameMode(True) self.doc = TDocStd_Document(TCollection_ExtendedString("")) self.x_app = XCAFApp_Application.GetApplication() self.x_app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), self.doc) self.shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main()) Interface_Static_SetCVal("write.step.schema", "AP214") Interface_Static_SetCVal('write.step.unit', 'mm') def Add(self, shape, name="name"): """ STEPControl_AsIs translates an Open CASCADE shape to its highest possible STEP representation. STEPControl_ManifoldSolidBrep translates an Open CASCADE shape to a STEP manifold_solid_brep or brep_with_voids entity. STEPControl_FacetedBrep translates an Open CASCADE shape into a STEP faceted_brep entity. STEPControl_ShellBasedSurfaceModel translates an Open CASCADE shape into a STEP shell_based_surface_model entity. STEPControl_GeometricCurveSet translates an Open CASCADE shape into a STEP geometric_curve_set entity. """ label = self.shape_tool.AddShape(shape) Interface_Static_SetCVal('write.step.product.name', name) self.step.Transfer(self.doc, STEPControl_AsIs) def Write(self, filename=None): if not filename: filename = self.name path, ext = os.path.splitext(filename) if not ext: ext = ".stp" status = self.step.Write(path + ext) assert(status == IFSelect_RetDone)
def test_write_step_file(self): ''' Exports a colored box into a STEP file ''' ### initialisation doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc")) self.assertTrue(doc is not None) # Get root assembly shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) colors = XCAFDoc_DocumentTool_ColorTool(doc.Main()) ### create the shape to export test_shape = BRepPrimAPI_MakeBox(100., 100., 100.).Shape() ### add shape shp_label = shape_tool.AddShape(test_shape) ### set a color for this shape r = 1 g = b = 0.5 red_color = Quantity_Color(r, g, b, 0) colors.SetColor(shp_label, red_color, XCAFDoc_ColorGen) # write file WS = XSControl_WorkSession() writer = STEPCAFControl_Writer(WS, False) writer.Transfer(doc, STEPControl_AsIs) status = writer.Write("./test_io/test_ocaf_generated.stp") self.assertTrue(status) self.assertTrue(os.path.isfile("./test_io/test_ocaf_generated.stp"))
def externalGeometryAssembly(self): # Create a TDoc holding the assembly of structure parts with external geometry. When assembled, write the STEP file # Initialize the writer shapes = [] step_writer = STEPCAFControl_Writer() step_writer.SetNameMode(True) step_writer.SetPropsMode(True) # create the handle to a document doc = TDocStd_Document(TCollection_ExtendedString("ocx-doc")) # Get root assembly shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) shape_tool.SetAutoNaming(False) l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main()) l_layers = XCAFDoc_DocumentTool_LayerTool(doc.Main()) l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main()) aBuilder = BRep_Builder() # Loop over all Panels panelchildren = [] for panel in self.model.panels: OCXCommon.LogMessage(panel, self.logging) guid = self.model.getGUID(panel) children = self.model.getPanelChildren(guid) panelchildren = panelchildren + children # Build the Panel compound compound = TopoDS_Compound() aBuilder.MakeCompound(compound) label = shape_tool.AddShape(compound) pname = panel.get('name') tname = TDataStd_Name() tname.Set(TCollection_ExtendedString(pname)) label.AddAttribute(tname) for child in children: object = self.model.getObject(child) name = object.get('name') extg = ExternalGeometry(self.model, object, self.dict, self.logging) # Init the creator extg.readExtGeometry() # Read the Brep if extg.IsDone(): aBuilder.Add(compound, extg.Shape()) tname = TDataStd_Name() label = shape_tool.AddShape(extg.Shape()) tname.Set(TCollection_ExtendedString(name)) label.AddAttribute(tname) # Root brackets compound = TopoDS_Compound() aBuilder.MakeCompound(compound) label = shape_tool.AddShape(compound) tname = TDataStd_Name() tname.Set(TCollection_ExtendedString('Brackets')) label.AddAttribute(tname) for br in self.model.brackets: guid = self.model.getGUID(br) name = br.get('name') if guid not in panelchildren: extg = ExternalGeometry(self.model, br, self.dict, self.logging) # Init the creator extg.readExtGeometry() # Read the Brep if extg.IsDone(): aBuilder.Add(compound, extg.Shape()) tname = TDataStd_Name() label = shape_tool.AddShape(extg.Shape()) tname.Set(TCollection_ExtendedString(name)) label.AddAttribute(tname) # Root plates compound = TopoDS_Compound() aBuilder.MakeCompound(compound) label = shape_tool.AddShape(compound) tname = TDataStd_Name() tname.Set(TCollection_ExtendedString('Plates')) label.AddAttribute(tname) for pl in self.model.plates: guid = self.model.getGUID(pl) name = pl.get('name') if guid not in panelchildren: extg = ExternalGeometry(self.model, pl, self.dict, self.logging) # Init the creator extg.readExtGeometry() # Read the Brep if extg.IsDone(): aBuilder.Add(compound, extg.Shape()) tname = TDataStd_Name() label = shape_tool.AddShape(extg.Shape()) tname.Set(TCollection_ExtendedString(name)) label.AddAttribute(tname) # Root pillars compound = TopoDS_Compound() aBuilder.MakeCompound(compound) label = shape_tool.AddShape(compound) tname = TDataStd_Name() tname.Set(TCollection_ExtendedString('Pillars')) label.AddAttribute(tname) for pil in self.model.pillars: guid = self.model.getGUID(pil) name = pil.get('name') if guid not in panelchildren: extg = ExternalGeometry(self.model, pil, self.dict, self.logging) # Init the creator extg.readExtGeometry() # Read the Brep if extg.IsDone(): aBuilder.Add(compound, extg.Shape()) tname = TDataStd_Name() label = shape_tool.AddShape(extg.Shape()) tname.Set(TCollection_ExtendedString(name)) label.AddAttribute(tname) step_writer.Perform( doc, TCollection_AsciiString(self.model.ocxfile.stem + '.stp')) return