def export_STEPFile_single(shape, filename, tol=1.0E-6): """ Exports a .stp file containing the input shapes Parameters ---------- shape : TopoDS_Shape filename : string The output filename """ step = STEPCAFControl_Writer() step.SetNameMode(True) step.SetPropsMode(True) h_doc = Handle_TDocStd_Document() x_app = XCAFApp_Application.GetApplication().GetObject() x_app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) shape_tool = h_shape_tool.GetObject() Interface_Static_SetCVal("write.step.schema", "AP214") # transfer shapes print(filename) shape_tool.AddShape(shape) step.Transfer(h_doc, STEPControl_AsIs) status = step.Write(filename) assert (status == IFSelect_RetDone)
def offset_cube(event=None): # smoothed # S1 = BRepPrimAPI_MakeBox(150,200,110).Shape() # offsetA = BRepOffsetAPI_MakeOffsetShape(S1,60,0.01) # display.EraseAll() # display.Context # display.DisplayColoredShape(S1, 'BLUE') # offA = display.DisplayColoredShape(offsetA.Shape(), 'GREEN') # display.Context.SetTransparency( offA, 0.3 ) # sharp S2 = BRepPrimAPI_MakeBox(gp_Pnt(300,0,0),220,140,180).Shape() offsetB = BRepOffsetAPI_MakeOffsetShape(S2,-20,0.01,BRepOffset_Skin,False,False,GeomAbs_Arc) offB = display.DisplayColoredShape(S2, 'BLUE') display.Context.SetTransparency( offB, 0.3 ) display.DisplayColoredShape(offsetB.Shape(), 'GREEN') from OCC.TCollection import TCollection_ExtendedString topo = Topo(S2) faces = topo.faces() # faceA, faceB = topo.faces_from_edge(topo.edges().next()) faceA = faces.next() faces.next();faces.next(); faces.next() faceB = faces.next() dim = AIS_LengthDimension(faceA, faceB, 120, TCollection_ExtendedString('jelle')) dim.SetValue(30) display.Context.Display(dim.GetHandle()) display.FitAll()
def test_write_step_file(self): ''' Exports a colored box into a STEP file ''' ### initialisation h_doc = Handle_TDocStd_Document() assert (h_doc.IsNull()) # Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) l_Colors = XCAFDoc_DocumentTool_ColorTool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = l_Colors.GetObject() ### 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.GetHandle(), False) writer.Transfer(h_doc, STEPControl_AsIs) status = writer.Write("./test_io/test_ocaf_generated.stp") assert status assert os.path.isfile("./test_io/test_ocaf_generated.stp")
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 read_step_file_shapes(filename): _shapes = [] # create an handle to a document h_doc = Handle_TDocStd_Document() # Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) step_reader = STEPCAFControl_Reader() step_reader.SetNameMode(True) status = step_reader.ReadFile(filename) if status == IFSelect_RetDone: step_reader.Transfer(doc.GetHandle()) labels = TDF_LabelSequence() 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()): label = labels.Value(i + 1) a_shape = h_shape_tool.GetObject().GetShape(label) _shapes.append(a_shape) return _shapes
def test_create_app(self): ''' Creates an OCAF app and an empty document ''' # create an handle to a document h_doc = Handle_TDocStd_Document() assert (h_doc.IsNull()) # Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)
def export_STEPFile_Airconics(AirconicsShapes, filename): """ Writes a Step file with names defined in the AirconicsShapes. This function is not fully tested and should not yet be used. Notes ----- Work in progress """ print("This function is a work in progress. For now, use export_STEPFile") # create an handle to a document h_doc = Handle_TDocStd_Document() # Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()).GetObject() # l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main()) # l_layers = XCAFDoc_DocumentTool_LayerTool(doc.Main()) # l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main()) step_writer = STEPCAFControl_Writer() step_writer.SetColorMode(True) step_writer.SetLayerMode(True) step_writer.SetNameMode(True) # step_writer.SetMaterialMode(True) for ACshape in AirconicsShapes: for comp in ACshape.Components: print("Writing {} to {}".format(comp, filename)) lbl = shape_tool.AddShape(ACshape.Components[comp]) name = TCollection_ExtendedString(comp) # tdn = TDataStd_Name() # tdn.Set(lbl, name) step_writer.Transfer(lbl, STEPControl_AsIs) status = step_writer.Write(filename) assert (status == IFSelect_RetDone) return status
def __init__(self, name="name", tol=1.0E-10): self.name = name self.step = STEPCAFControl_Writer() self.step.SetNameMode(True) self.h_doc = Handle_TDocStd_Document() self.x_app = XCAFApp_Application.GetApplication().GetObject() self.x_app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), self.h_doc) self.doc = self.h_doc.GetObject() self.h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main()) self.shape_tool = self.h_shape_tool.GetObject() Interface_Static_SetCVal("write.step.schema", "AP214")
def set_label_name(label, name): """ Sets the name of a :class:`.OCC.TDF.TDF_Label` (**label**) :param label: :class:`.OCC.TDF.TDF_Label` :param name: new name for a :class:`.OCC.TDF.TDF_Label` """ entry = TCollection_AsciiString() TDF_Tool.Entry(label, entry) N = Handle_TDataStd_Name() label.FindAttribute(TDataStd_Name_GetID(), N) n = N.GetObject() n.Set(TCollection_ExtendedString(name.encode("latin-1")))
def test_read_step_file(self): ''' Reads the previous step file ''' # create an handle to a document h_doc = Handle_TDocStd_Document() # Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main()) step_reader = STEPCAFControl_Reader() step_reader.SetColorMode(True) step_reader.SetLayerMode(True) step_reader.SetNameMode(True) step_reader.SetMatMode(True) status = step_reader.ReadFile("./test_io/test_ocaf.stp") if status == IFSelect_RetDone: step_reader.Transfer(doc.GetHandle()) labels = TDF_LabelSequence() color_labels = TDF_LabelSequence() shape_tool = h_shape_tool.GetObject() h_shape_tool.GetObject().GetFreeShapes(labels) assert (labels.Length() == 1) sub_shapes_labels = TDF_LabelSequence() assert (not shape_tool.IsAssembly(labels.Value(1))) shape_tool.GetSubShapes(labels.Value(1), sub_shapes_labels) assert (sub_shapes_labels.Length() == 0) l_colors.GetObject().GetColors(color_labels) assert (color_labels.Length() == 1) label_shp = labels.Value(1) a_shape = h_shape_tool.GetObject().GetShape(label_shp) assert (not a_shape.IsNull())
def to_string(_string): from OCC.TCollection import TCollection_ExtendedString return TCollection_ExtendedString(_string)
XCAFDoc_DocumentTool_MaterialTool) from OCC.STEPCAFControl import STEPCAFControl_Reader from OCC.IFSelect import IFSelect_RetDone from OCC.TDF import TDF_LabelSequence from OCC.Display.SimpleGui import init_display filename = './models/as1_pe_203.stp' _shapes = [] # create an handle to a document h_doc = Handle_TDocStd_Document() # Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main()) l_layers = XCAFDoc_DocumentTool_LayerTool(doc.Main()) l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main()) step_reader = STEPCAFControl_Reader() step_reader.SetColorMode(True) step_reader.SetLayerMode(True) step_reader.SetNameMode(True) step_reader.SetMatMode(True) status = step_reader.ReadFile(filename)
def to_string(_string): return TCollection_ExtendedString(_string)
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 loadShape(self, shape_list): """ Method for loading one or more shapes and displaying to Output Viewer. This method uses libraries of iges caf control for fetching sub-shape names within .igs files. This method is used when adding a case in the main routine. @param shape_list [list] First index contains the path of shape, second index contains a list of display exceptions, e.g: [[igs_2d_shape_path, ["HUB", "SHROUD"], [igs_3d_shape_path, ["STREAM"]] @return First return contains list of ais_shapes handles and second return contains a list of sub-shape names in strings """ loaded_ais_shape = [] loaded_h_ais_shape = [] loaded_subshape_names = [] default_displaying_h_ais_shape = [] for shape_case in shape_list: loaded_shape_filename = os.path.basename(shape_case[0]) exception_list = shape_case[1] exception_list = list( filter(None, exception_list) ) # Mistake-prevention of user filling of exception list # creates a handle for TdocStd documents h_doc = Handle_TDocStd_Document() # create the application app = _XCAFApp.XCAFApp_Application_GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString(""), h_doc) # get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool().ShapeTool(doc.Main()) # creates a reader responsible for reading an IGS file reader = IGESCAFControl_Reader() reader.ReadFile(shape_case[0]) # Translates currently loaded IGES file into the document reader.Transfer(doc.GetHandle()) # labels for the shapes. Every IGS file contains a name for each individual shape labels = TDF_LabelSequence() shape_tool = h_shape_tool.GetObject() shape_tool.GetShapes(labels) # gets the number of individual shapes contained in the igs file nb = reader.NbShapes() # for each individual shape gets the label nad creates a AIS_Shape for data contained in reader.Shape() for i in range(1, nb + 1): label = labels.Value(i) h_name = Handle_TDataStd_Name() label.FindAttribute(TDataStd_Name_GetID(), h_name) str_dump = h_name.GetObject().DumpToString() name_subshape = str_dump.split('|')[-2] name = "%s - %s" % (loaded_shape_filename, name_subshape) loaded_subshape_names.append(name) shape = AIS_ColoredShape(reader.Shape(i)) loaded_ais_shape.append(shape) loaded_h_ais_shape.append(shape.GetHandle()) if not any(iterator in name_subshape for iterator in exception_list): default_displaying_h_ais_shape.append(shape.GetHandle()) self.op_viewer.master_shape_list.append(shape.GetHandle()) # number of cases is a variable used to make the loaded shape color different from the previous one number_of_cases = self.op_viewer.model.rowCount( self.op_viewer.ui_case_treeview.rootIndex()) # sets the default attributes for ais shapes handles for h_ais_shape in loaded_h_ais_shape: self.op_viewer.display.Context.SetDeviationCoefficient( h_ais_shape, self.op_viewer.DC / self.op_viewer.default_shape_factor) self.op_viewer.display.Context.SetHLRDeviationCoefficient( h_ais_shape, self.op_viewer.DC_HLR / self.op_viewer.default_shape_factor) self.op_viewer.display.Context.SetColor( h_ais_shape, shape_colordictionary[shape_colorlist[ (self.op_viewer.default_shape_color + number_of_cases) % len(shape_colorlist)]]) self.op_viewer.display.Context.SetTransparency( h_ais_shape, self.op_viewer.default_shape_transparency) # displays the handles of the ais_shapes in the viewer3d context. for h_ais_shape in default_displaying_h_ais_shape: self.op_viewer.display.Context.Display(h_ais_shape) return loaded_h_ais_shape, loaded_subshape_names