def InitDoc(self): 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() self.shape_tool = shape_tool top_label = shape_tool.NewShape( ) #this is the "root" label for the assembly self.top_label = top_label #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(-100, -100, -100)) loc = TopLoc.TopLoc_Location(tr) box_comp2 = shape_tool.AddComponent(top_label, box_label, loc) tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(10, 10, 10)) loc = TopLoc.TopLoc_Location(tr) cyl_comp = shape_tool.AddComponent(top_label, cyl_label, loc) #Add some colors red = Quantity.Quantity_Color(Quantity.Quantity_NOC_RED) green = Quantity.Quantity_Color(Quantity.Quantity_NOC_GREEN) blue = Quantity.Quantity_Color(Quantity.Quantity_NOC_BLUE1) colors.SetColor(cyl_comp, red, XCAFDoc.XCAFDoc_ColorGen) colors.SetColor(box_label, blue, XCAFDoc.XCAFDoc_ColorGen) colors.SetColor(box_comp2, green, XCAFDoc.XCAFDoc_ColorGen) self.box_label = box_label self.cyl_label = cyl_label self.box_comp1 = box_comp1 self.box_comp2 = box_comp2 self.cyl_comp = cyl_comp
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 preview(self, input, direction): if self.step == 0: # object to rotate self.prev_dat['object'] = input return [] elif self.step == 1: # axis to rotate about dirvec = vec(0, 0, 0) dirvec[direction] = 1 axis = gp.gp_Ax1(input, dirvec.to_gp_Dir()) # make a copy (copy.copy does not work) point = gp_.gp_Pnt_(input) point[direction] += 2 input[direction] -= .2 axis_plot = BRepBuilderAPI.BRepBuilderAPI_MakeEdge( GC.GC_MakeSegment(input, point).Value()).Edge() self.prev_dat['point'] = input self.prev_dat['dirvec'] = dirvec self.prev_dat['axis'] = axis self.prev_dat['axis-plot'] = axis_plot return [axis_plot] elif self.step == 2: # start point self.prev_dat['point_start'] = input return [self.prev_dat['axis-plot']] elif self.step == 3: p0 = self.prev_dat['point'] vec0 = self.prev_dat['point_start'] - p0 vec1 = input - p0 dirvec = self.prev_dat['dirvec'] n0 = dirvec.cross(vec0) n1 = dirvec.cross(vec1) try: cos = n0.dot(n1) / (n0.length() * n1.length()) except ZeroDivisionError: raise InvalidInputException angle = math.acos(min(max(cos, -1), 1)) # cos might be (-)1.000001 oriented_dirvec = n0.cross(n1) if oriented_dirvec == vec(0, 0, 0): raise InvalidInputException axis = gp.gp_Ax1(p0, oriented_dirvec.to_gp_Dir()) tr = gp.gp_Trsf() tr.SetRotation(axis, angle) t = self.prev_dat['object'].Moved(TopLoc.TopLoc_Location(tr)) t = copy_geom.copy(t) self._final = [t] self.remove = [self.prev_dat['object']] return [t, self.prev_dat['axis-plot']]
def FillTestDoc(doc): h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().shapetool(doc.Main()) h_Colors = XCAFDoc.XCAFDoc_DocumentTool().colortool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = h_Colors.GetObject() top_label = shape_tool.NewShape( ) #this is the "root" label for the assembly print "top entry", Label(TDF_Label=top_label).entry 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) # 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) 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)
def position_shape(shape, centre, direction, x_axis): ax = gp.gp_Ax3() ax.SetLocation(gp.gp_Pnt(*centre)) ax.SetDirection(gp.gp_Dir(*direction)) ax.SetXDirection(gp.gp_Dir(*x_axis)) tr = gp.gp_Trsf() tr.SetTransformation(ax, gp.gp_Ax3()) loc = TopLoc.TopLoc_Location(tr) shape.Location(loc) #trans = BRepBuilderAPI.BRepBuilderAPI_Transform(shape, tr) #return toshape(trans) return shape
def preview(self, input, direction): if self.step == 0: self.previous_data = [input] return [] elif self.step == 1: self.previous_data = [self.previous_data[0], input] return [] elif self.step == 2: object, point0 = self.previous_data vector = input - point0 tr = gp.gp_Trsf() tr.SetTranslation(vector.to_gp_Vec()) loc = TopLoc.TopLoc_Location(tr) t = object.Moved(loc) self.remove = [self.previous_data[0]] self._final = [copy_geom.copy(t)] return self._final
def SetBoxPosition(self, val): d = (val - 50.) * 10 tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(100, 100, d)) loc = TopLoc.TopLoc_Location(tr) ### One way to set the location is to remove the label and re-add it. However, ### this will lose any color or material attributes on the component (although ### attributes on the underlying Shape are retained). E.g. #~ self.shape_tool.RemoveComponent(self.box_comp2) #~ self.box_comp2 = self.shape_tool.AddComponent(self.top_label, self.box_label, loc) ###I can't see how to make the ShapeTool do this, so we need to edit the TDF_Attribute ###directly. h_a = XCAFDoc.Handle_XCAFDoc_Location() self.box_comp1.FindAttribute(XCAFDoc.XCAFDoc_Location().ID(), h_a) attr = h_a.GetObject() attr.Set(loc) self.shape_tool.UpdateAssembly(self.top_label)
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 _move(shape, x, y, z): tr = gp.gp_Trsf() tr.SetTranslation(gp.gp_Vec(x, y, z)) loc = TopLoc.TopLoc_Location(tr) shape.Move(loc)
# #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... #
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 pixmapFromFaceTriangulation(face): """ create a filled pixmap from a defined face. ideally, this is really really fast, based on a triangulation of the face. a face with 800x600 pixels took 56ms to raster with this algo. @ 0.012" that is a 6x8 " part! this creates a pixel map that has all the interior pixels filled this might fix: TopoDS_Face tFace = BRepBuilderAPI_MakeFace( tFrame ); BRepBuilderAPI_MakeFace MF( tFace ); MF.Add( tHole ); if ( MF.IsDone()) { Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape; sfs->Init ( MF.Shape() ); sfs->Perform(); Handle(AIS_Shape) aShape = new AIS_Shape(sfs->Shape()); myAISContext->Display(aShape); """ PIXEL = 0.005 DEFLECTION = PIXEL / 20.0 triangulateAtDeflection(face, DEFLECTION) #get bounding box (xMin, yMin, zMin, xMax, yMax, zMax) = boundingBox([face]) #adjust boundaries a bit BUFFER = PIXEL * 5 #make pixmap pixmap = pixmaptest.pixmap((xMin - BUFFER, yMin - BUFFER), (xMax + BUFFER, yMax + BUFFER), PIXEL) transformPoints = True loc = TopLoc.TopLoc_Location() hT = btool.Triangulation(face, loc).GetObject() trsf = loc.Transformation() if loc.IsIdentity(): transformPoints = False nodes = hT.Nodes() triangles = hT.Triangles() #mark pixels to fill for i in range(triangles.Lower(), triangles.Upper()): tr = triangles.Value(i) (n1, n2, n3) = tr.Get() p1 = nodes.Value(n1) p2 = nodes.Value(n2) p3 = nodes.Value(n3) if transformPoints: p1.Transform(trsf) p2.Transform(trsf) p3.Transform(trsf) #fill triangle. easy peasy! pixmap.fillTriangle(tP(p1), tP(p2), tP(p3), 7) #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p1,p2)); #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p2,p3)); #TestDisplay.display.showShape(Wrappers.edgeFromTwoPoints(p3,p1)); #time.sleep(1); #TestDisplay.display.showShape(Wrappers.make_vertex(p1)); #TestDisplay.display.showShape(Wrappers.make_vertex(p2)); #TestDisplay.display.showShape(Wrappers.make_vertex(p3)); #tile hex pattern onto the filling #pixmap.tileOnto(fillpattern); #mark boundaries #approximate each wire with a set of segments """ bb = TopExp.TopExp_Explorer(); bb.Init(face,TopAbs.TopAbs_WIRE); edgeNum = 1; #while False: while bb.More(): #print "plotting wire" w = Wrappers.Wire(Wrappers.cast(bb.Current())); #divide the edge into discrete segments lastPnt = None; for e in w.edges2(): #for each edge, set vertices, and compute points on the edge ew = Wrappers.Edge(e); lastPnt = None; for pnt in ew.discretePoints(DEFLECTION): pixmap.set(tP(pnt),7); edgeNum += 1; #plot the line #if lastPnt != None: pixmap.drawLine(tP(lastPnt),tP(pnt),edgeNum ); lastPnt = pnt; bb.Next(); """ return pixmap