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 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 _get_sublabels(self): itr = TDF.TDF_ChildIterator(self.TDF_Label, False) result = [] while itr.More(): val = itr.Value() result.append(Label(TDF_Label=val)) itr.Next() return result
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 execute(self): input_shape = self.input.shape topo = Topo(input_shape) self._n_edges = topo.number_of_edges() builder = BRepFilletAPI.BRepFilletAPI_MakeChamfer(input_shape) Map = TDF.TDF_LabelMap() itr = TDF.TDF_ChildIterator(self.parent_label, True) while itr.More(): sub_label = itr.Value() Map.Add(sub_label) itr.Next() selector = self.selector ret = selector.Solve(Map) if not ret: raise Exception("Failed to solve for edge") #print "Failed to solve for edge" nt = TNaming.TNaming_Tool() selected_shape = nt.CurrentShape(selector.NamedShape()) selected_edge = TopoDS.TopoDS().Edge(selected_shape) try: face = Topo(input_shape).faces_from_edge(selected_edge).next() except RuntimeError: raise #not sure how to handle this size = self.size builder.Add(size, size, selected_edge, face) self.update_naming(builder) return builder.Shape()
#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() # get the top level shapes l_LabelShapes = TDF.TDF_LabelSequence() shape_tool.GetShapes(l_LabelShapes) count = l_LabelShapes.Length() root_label = l_LabelShapes.Value(1) shape = shape_tool.GetShape(root_label) viewXDE(doc, root_label, shape)
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) ## ##Name all the subshape we *might* want to refer to later ## for edge in topo.edges(): sub_label = ts.NewChild(box_label) ns_builder = TNaming.TNaming_Builder(sub_label)
##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 AppStd, TDocStd, TCollection, TDF # # Create a default Application object. You only need one of these per process # app = AppStd.AppStd_Application() # # Make a Standard document # h_doc = TDocStd.Handle_TDocStd_Document() #I'm going to invent my own document structure as I go along schema = TCollection.TCollection_ExtendedString("MyFormat") app.NewDocument(schema, h_doc) doc = h_doc.GetObject() root = doc.Main() ts = TDF.TDF_TagSource() # # We'll add all shapes under this node in the label tree # # shape_root = ts.NewChild(root) shape_root = root.NewChild()
def _parent_label_changed(self, old_label, new_label): ts = TDF.TDF_TagSource() self.label = ts.NewChild(new_label)
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) tag_tool = TDF.TDF_Tool() tagList = TColStd.TColStd_ListOfInteger() tag_tool.TagList(cyl_comp, tagList) iterTagList = TColStd.TColStd_ListIteratorOfListOfInteger(tagList) while iterTagList.More(): v = iterTagList.Value() iterTagList.Next() asc = TCollection.TCollection_AsciiString() tag_tool.Entry(cyl_comp, asc) print asc.ToCString() #for a in dir(v): print a #mode = STEPControl.STEPControl_AsIs #writer = STEPCAFControl.STEPCAFControl_Writer()
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