def attach(self, vobj): ArchComponent.ViewProviderComponent.attach(self, vobj) from pivy import coin self.color = coin.SoBaseColor() self.font = coin.SoFont() self.text1 = coin.SoAsciiText() self.text1.string = " " self.text1.justification = coin.SoAsciiText.LEFT self.text2 = coin.SoAsciiText() self.text2.string = " " self.text2.justification = coin.SoAsciiText.LEFT self.coords = coin.SoTransform() self.header = coin.SoTransform() label = coin.SoSeparator() label.addChild(self.coords) label.addChild(self.color) label.addChild(self.font) label.addChild(self.text2) label.addChild(self.header) label.addChild(self.text1) vobj.Annotation.addChild(label) self.onChanged(vobj, "TextColor") self.onChanged(vobj, "FontSize") self.onChanged(vobj, "FirstLine") self.onChanged(vobj, "LineSpacing") self.onChanged(vobj, "FontName")
def __init__(self): GRID_TRANSPARENCY = 0 col = self.getGridColor() pick = coin.SoPickStyle() pick.style.setValue(coin.SoPickStyle.UNPICKABLE) self.trans = coin.SoTransform() self.trans.translation.setValue([0, 0, 0]) mat1 = coin.SoMaterial() mat1.transparency.setValue(0.7*(1-GRID_TRANSPARENCY)) mat1.diffuseColor.setValue(col) self.font = coin.SoFont() self.coords1 = coin.SoCoordinate3() self.lines1 = coin.SoLineSet() texts = coin.SoSeparator() t1 = coin.SoSeparator() self.textpos1 = coin.SoTransform() self.text1 = coin.SoAsciiText() self.text1.string = " " t2 = coin.SoSeparator() self.textpos2 = coin.SoTransform() self.textpos2.rotation.setValue((0.0, 0.0, 0.7071067811865475, 0.7071067811865476)) self.text2 = coin.SoAsciiText() self.text2.string = " " t1.addChild(self.textpos1) t1.addChild(self.text1) t2.addChild(self.textpos2) t2.addChild(self.text2) texts.addChild(self.font) texts.addChild(t1) texts.addChild(t2) mat2 = coin.SoMaterial() mat2.transparency.setValue(0.3*(1-GRID_TRANSPARENCY)) mat2.diffuseColor.setValue(col) self.coords2 = coin.SoCoordinate3() self.lines2 = coin.SoLineSet() mat3 = coin.SoMaterial() mat3.transparency.setValue(GRID_TRANSPARENCY) mat3.diffuseColor.setValue(col) self.coords3 = coin.SoCoordinate3() self.lines3 = coin.SoLineSet() self.pts = [] s = coin.SoSeparator() s.addChild(pick) s.addChild(self.trans) s.addChild(mat1) s.addChild(self.coords1) s.addChild(self.lines1) s.addChild(mat2) s.addChild(self.coords2) s.addChild(self.lines2) s.addChild(mat3) s.addChild(self.coords3) s.addChild(self.lines3) s.addChild(texts) Tracker.__init__(self, children=[s], name="gridTracker") self.reset()
def attach(self,vobj): '''Setup the scene sub-graph of the view provider''' self.mattext = coin.SoMaterial() textdrawstyle = coin.SoDrawStyle() textdrawstyle.style = coin.SoDrawStyle.FILLED self.trans = coin.SoTransform() self.font = coin.SoFont() self.text2d = coin.SoAsciiText() self.text3d = coin.SoText2() self.text2d.string = self.text3d.string = "Label" # need to init with something, otherwise, crash! self.text2d.justification = coin.SoAsciiText.LEFT self.text3d.justification = coin.SoText2.LEFT self.node2d = coin.SoGroup() self.node2d.addChild(self.trans) self.node2d.addChild(self.mattext) self.node2d.addChild(textdrawstyle) self.node2d.addChild(self.font) self.node2d.addChild(self.text2d) self.node3d = coin.SoGroup() self.node3d.addChild(self.trans) self.node3d.addChild(self.mattext) self.node3d.addChild(textdrawstyle) self.node3d.addChild(self.font) self.node3d.addChild(self.text3d) vobj.addDisplayMode(self.node2d,"2D text") vobj.addDisplayMode(self.node3d,"3D text") self.onChanged(vobj,"TextColor") self.onChanged(vobj,"FontSize") self.onChanged(vobj,"FontName") self.onChanged(vobj,"Justification") self.onChanged(vobj,"LineSpacing")
def displaytext(self, pos, color=(1, 1, 1), text=["aaa", "bbb"]): textpos = coin.SoTransform() p = pos textpos.translation.setValue(p.x + 10, p.y + 10, p.z + 10) font = coin.SoFont() font.size = 20 text2d = coin.SoText2() text3d = coin.SoAsciiText() text2d.string.setValues([l.encode("utf8") for l in text if l]) text3d.string.setValues([l.encode("utf8") for l in text if l]) myMaterial = SoMaterial() myMaterial.diffuseColor.set1Value(0, SbColor(*color)) try: sg = self._sg except: sg = SoSeparator() self._sg = sg root = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph() root.addChild(sg) node2d = SoSeparator() node2d.addChild(textpos) node2d.addChild(myMaterial) node2d.addChild(font) node2d.addChild(text2d) #node2d.addChild(text3d) sg.addChild(node2d)
def attach(self, vobj): self.Object = vobj.Object self.clip = None from pivy import coin self.sep = coin.SoGroup() self.mat = coin.SoMaterial() self.sep.addChild(self.mat) self.dst = coin.SoDrawStyle() self.sep.addChild(self.dst) self.lco = coin.SoCoordinate3() self.sep.addChild(self.lco) lin = coin.SoType.fromName("SoBrepEdgeSet").createInstance() lin.coordIndex.setValues([0, 1, -1, 2, 3, -1, 4, 5, -1]) self.sep.addChild(lin) self.tra = coin.SoTransform() self.tra.rotation.setValue(FreeCAD.Rotation(0, 0, 90).Q) self.sep.addChild(self.tra) self.fon = coin.SoFont() self.sep.addChild(self.fon) self.txt = coin.SoAsciiText() self.txt.justification = coin.SoText2.LEFT self.txt.string.setValue("level") self.sep.addChild(self.txt) vobj.addDisplayMode(self.sep, "Default") self.onChanged(vobj, "ShapeColor") self.onChanged(vobj, "FontName") self.onChanged(vobj, "ShowLevel") self.onChanged(vobj, "FontSize") return
def attach(self,vobj): '''Setup the scene sub-graph of the view provider''' self.arrow = coin.SoSeparator() self.arrowpos = coin.SoTransform() self.arrow.addChild(self.arrowpos) self.matline = coin.SoMaterial() self.drawstyle = coin.SoDrawStyle() self.drawstyle.style = coin.SoDrawStyle.LINES self.lcoords = coin.SoCoordinate3() self.line = coin.SoType.fromName("SoBrepEdgeSet").createInstance() self.mattext = coin.SoMaterial() textdrawstyle = coin.SoDrawStyle() textdrawstyle.style = coin.SoDrawStyle.FILLED self.textpos = coin.SoTransform() self.font = coin.SoFont() self.text2d = coin.SoText2() self.text3d = coin.SoAsciiText() self.text2d.string = self.text3d.string = "Label" # need to init with something, otherwise, crash! self.text2d.justification = coin.SoText2.RIGHT self.text3d.justification = coin.SoAsciiText.RIGHT self.fcoords = coin.SoCoordinate3() self.frame = coin.SoType.fromName("SoBrepEdgeSet").createInstance() self.lineswitch = coin.SoSwitch() switchnode = coin.SoSeparator() switchnode.addChild(self.line) switchnode.addChild(self.arrow) self.lineswitch.addChild(switchnode) self.lineswitch.whichChild = 0 self.node2d = coin.SoGroup() self.node2d.addChild(self.matline) self.node2d.addChild(self.arrow) self.node2d.addChild(self.drawstyle) self.node2d.addChild(self.lcoords) self.node2d.addChild(self.lineswitch) self.node2d.addChild(self.mattext) self.node2d.addChild(textdrawstyle) self.node2d.addChild(self.textpos) self.node2d.addChild(self.font) self.node2d.addChild(self.text2d) self.node2d.addChild(self.fcoords) self.node2d.addChild(self.frame) self.node3d = coin.SoGroup() self.node3d.addChild(self.matline) self.node3d.addChild(self.arrow) self.node3d.addChild(self.drawstyle) self.node3d.addChild(self.lcoords) self.node3d.addChild(self.lineswitch) self.node3d.addChild(self.mattext) self.node3d.addChild(textdrawstyle) self.node3d.addChild(self.textpos) self.node3d.addChild(self.font) self.node3d.addChild(self.text3d) self.node3d.addChild(self.fcoords) self.node3d.addChild(self.frame) vobj.addDisplayMode(self.node2d,"2D text") vobj.addDisplayMode(self.node3d,"3D text") self.onChanged(vobj,"LineColor") self.onChanged(vobj,"TextColor") self.onChanged(vobj,"ArrowSize") self.onChanged(vobj,"Line")
def attach(self, vobj): '''Setup the scene sub-graph of the view provider''' from pivy import coin self.Object = vobj.Object self.color = coin.SoBaseColor() if hasattr(vobj, "LineColor"): self.color.rgb.setValue(vobj.LineColor[0], vobj.LineColor[1], vobj.LineColor[2]) self.font = coin.SoFont() self.font3d = coin.SoFont() self.text = coin.SoAsciiText() self.text3d = coin.SoText2() self.text.string = "d" # some versions of coin crash if string is not set self.text3d.string = "d" self.text.justification = self.text3d.justification = coin.SoAsciiText.CENTER self.textpos = coin.SoTransform() label = coin.SoSeparator() label.addChild(self.textpos) label.addChild(self.color) label.addChild(self.font) label.addChild(self.text) label3d = coin.SoSeparator() label3d.addChild(self.textpos) label3d.addChild(self.color) label3d.addChild(self.font3d) label3d.addChild(self.text3d) self.coord1 = coin.SoCoordinate3() self.trans1 = coin.SoTransform() self.coord2 = coin.SoCoordinate3() self.trans2 = coin.SoTransform() self.marks = coin.SoSeparator() self.drawstyle = coin.SoDrawStyle() self.coords = coin.SoCoordinate3() self.arc = coin.SoType.fromName("SoBrepEdgeSet").createInstance() self.node = coin.SoGroup() self.node.addChild(self.color) self.node.addChild(self.drawstyle) self.node.addChild(self.coords) self.node.addChild(self.arc) self.node.addChild(self.marks) self.node.addChild(label) self.node3d = coin.SoGroup() self.node3d.addChild(self.color) self.node3d.addChild(self.drawstyle) self.node3d.addChild(self.coords) self.node3d.addChild(self.arc) self.node3d.addChild(self.marks) self.node3d.addChild(label3d) vobj.addDisplayMode(self.node, "2D") vobj.addDisplayMode(self.node3d, "3D") self.updateData(vobj.Object, None) self.onChanged(vobj, "FontSize") self.onChanged(vobj, "FontName") self.onChanged(vobj, "ArrowType") self.onChanged(vobj, "LineColor") # backwards compatibility self.ScaleMultiplier = 1.00
def attach(self,vobj): ArchComponent.ViewProviderComponent.attach(self,vobj) from pivy import coin self.color = coin.SoBaseColor() self.font = coin.SoFont() self.text1 = coin.SoAsciiText() self.text1.string = " " self.text1.justification = coin.SoAsciiText.LEFT self.text2 = coin.SoAsciiText() self.text2.string = " " self.text2.justification = coin.SoAsciiText.LEFT self.coords = coin.SoTransform() self.header = coin.SoTransform() self.label = coin.SoSwitch() sep = coin.SoSeparator() self.label.whichChild = 0 sep.addChild(self.coords) sep.addChild(self.color) sep.addChild(self.font) sep.addChild(self.text2) sep.addChild(self.header) sep.addChild(self.text1) self.label.addChild(sep) vobj.Annotation.addChild(self.label) self.onChanged(vobj,"TextColor") self.onChanged(vobj,"FontSize") self.onChanged(vobj,"FirstLine") self.onChanged(vobj,"LineSpacing") self.onChanged(vobj,"FontName") self.Object = vobj.Object # footprint mode self.fmat = coin.SoMaterial() self.fcoords = coin.SoCoordinate3() self.fset = coin.SoIndexedFaceSet() fhints = coin.SoShapeHints() fhints.vertexOrdering = fhints.COUNTERCLOCKWISE sep = coin.SoSeparator() sep.addChild(self.fmat) sep.addChild(self.fcoords) sep.addChild(fhints) sep.addChild(self.fset) vobj.RootNode.addChild(sep)
def draw_label(text=[], prop: propertyValues=None): ''' Draw widgets label relative to the position with alignment''' if text=='' or prop ==None: return # Nothing to do here try: delta=App.Vector(0,0,0) print (prop.vectors) p1=App.Vector(prop.vectors[0]) #You must cast the value or it will fail p2=App.Vector(prop.vectors[1]) delta.x=p1.x+2 delta.y=p1.y+2 delta.z=p1.z (r,thi,phi)=calculateLineSpherical(prop.vectors) #get spherical representation of the point(p2) _transPositionPOS=coin.SoTransform() #_transPositionX = coin.SoTransform() _transPositionY = coin.SoTransform() _transPositionZ = coin.SoTransform() _transPositionPOS.translation.setValue(delta) _transPositionY.translation.setValue(App.Vector(0,0,0)) _transPositionZ.translation.setValue(App.Vector(0,0,0)) print (delta) _transPositionY.rotation.setValue(coin.SbVec3f(1,0, 0),phi) _transPositionZ.rotation.setValue(coin.SbVec3f(0, 0, 1),thi) font = coin.SoFont() font.size = prop.fontsize # Font size font.Name = prop.labelfont # Font used _text3D = coin.SoAsciiText() # Draw text in the 3D world _text3D.string.setValues([l.encode("utf8") for l in text if l]) #_text3D.justification = coin.SoAsciiText.LEFT coinColor = coin.SoMaterial() # Font color coinColor.diffuseColor.set1Value(0, coin.SbColor(*prop.labelcolor)) _textNode = coin.SoSeparator() # A Separator to separate the text from the drawing _textNode.addChild(_transPositionPOS) if phi!=0: _textNode.addChild(_transPositionY) if thi!=0: _textNode.addChild(_transPositionZ) _textNode.addChild(coinColor) _textNode.addChild(font) _textNode.addChild(_text3D) return _textNode # Return the created SoSeparator that contains the text except Exception as err: App.Console.PrintError("'draw_label' Failed. " "{err}\n".format(err=str(err))) exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno)
def attach(self,vobj): self.Object = vobj.Object self.clip = None from pivy import coin self.sep = coin.SoGroup() self.mat = coin.SoMaterial() self.sep.addChild(self.mat) self.dst = coin.SoDrawStyle() self.sep.addChild(self.dst) self.lco = coin.SoCoordinate3() self.sep.addChild(self.lco) lin = coin.SoType.fromName("SoBrepEdgeSet").createInstance() lin.coordIndex.setValues([0,1,-1,2,3,-1,4,5,-1]) self.sep.addChild(lin) self.bbox = coin.SoSwitch() self.bbox.whichChild = -1 bboxsep = coin.SoSeparator() self.bbox.addChild(bboxsep) drawstyle = coin.SoDrawStyle() drawstyle.style = coin.SoDrawStyle.LINES drawstyle.lineWidth = 3 drawstyle.linePattern = 0x0f0f # 0xaa bboxsep.addChild(drawstyle) self.bbco = coin.SoCoordinate3() bboxsep.addChild(self.bbco) lin = coin.SoIndexedLineSet() lin.coordIndex.setValues([0,1,2,3,0,-1,4,5,6,7,4,-1,0,4,-1,1,5,-1,2,6,-1,3,7,-1]) bboxsep.addChild(lin) self.sep.addChild(self.bbox) self.tra = coin.SoTransform() self.tra.rotation.setValue(FreeCAD.Rotation(0,0,90).Q) self.sep.addChild(self.tra) self.fon = coin.SoFont() self.sep.addChild(self.fon) self.txt = coin.SoAsciiText() self.txt.justification = coin.SoText2.LEFT self.txt.string.setValue("level") self.sep.addChild(self.txt) vobj.addDisplayMode(self.sep,"Default") self.onChanged(vobj,"ShapeColor") self.onChanged(vobj,"FontName") self.onChanged(vobj,"ShowLevel") self.onChanged(vobj,"FontSize") self.onChanged(vobj,"AutogroupBox") self.setProperties(vobj) return
def onChanged(self, vobj, prop): ''' Update Object visuals when a view property changed. ''' labels = vobj.getPropertyByName("Labels") self.point_labels.removeAllChildren() if labels: if prop == "Labels" or prop == "Name" or prop == "NortingEasting"\ or prop == "Elevation" or prop == "Description": origin = geo_origin.get(vobj.Object.Points[0]) show_name = vobj.getPropertyByName("Name") show_ne = vobj.getPropertyByName("NortingEasting") show_z = vobj.getPropertyByName("Elevation") show_des = vobj.getPropertyByName("Description") for vector in vobj.Object.Points: font = coin.SoFont() font.size = 1000 point_label = coin.SoSeparator() location = coin.SoTranslation() text = coin.SoAsciiText() index = vobj.Object.Points.index(vector) labels =[] if show_name: labels.append(vobj.Object.PointNames[index]) if show_ne: labels.extend([str(round(vector.x/1000, 3)), str(round(vector.y/1000,3))]) if show_z: labels.append(str(round(vector.z/1000,3))) if show_des and vobj.Object.Descriptions: labels.append(vobj.Object.Descriptions[index]) location.translation = vector.sub(FreeCAD.Vector(origin.Origin.x, origin.Origin.y, 0)) text.string.setValues(labels) point_label.addChild(font) point_label.addChild(location) point_label.addChild(text) self.point_labels.addChild(point_label) if prop == "PointSize": size = vobj.getPropertyByName("PointSize") self.point_style.pointSize = size if prop == "PointColor": color = vobj.getPropertyByName("PointColor") self.color_mat.diffuseColor = (color[0],color[1],color[2])
def attach(self, vobj): """Set up the scene sub-graph of the view provider.""" # Main attributes of the Coin scenegraph self.mattext = coin.SoMaterial() self.trans = coin.SoTransform() self.font = coin.SoFont() self.text2d = coin.SoText2() # Faces the camera always self.text3d = coin.SoAsciiText() # Can be oriented in 3D space textdrawstyle = coin.SoDrawStyle() textdrawstyle.style = coin.SoDrawStyle.FILLED # The text string needs to be initialized to something, # otherwise it may crash self.text2d.string = self.text3d.string = "Label" self.text2d.justification = coin.SoText2.LEFT self.text3d.justification = coin.SoAsciiText.LEFT self.node2d = coin.SoGroup() self.node2d.addChild(self.trans) self.node2d.addChild(self.mattext) self.node2d.addChild(textdrawstyle) self.node2d.addChild(self.font) self.node2d.addChild(self.text2d) self.node3d = coin.SoGroup() self.node3d.addChild(self.trans) self.node3d.addChild(self.mattext) self.node3d.addChild(textdrawstyle) self.node3d.addChild(self.font) self.node3d.addChild(self.text3d) vobj.addDisplayMode(self.node2d, "2D text") vobj.addDisplayMode(self.node3d, "3D text") self.onChanged(vobj, "TextColor") self.onChanged(vobj, "FontSize") self.onChanged(vobj, "FontName") self.onChanged(vobj, "Justification") self.onChanged(vobj, "LineSpacing") self.onChanged(vobj, "ScaleMultiplier") self.Object = vobj.Object
def _update_grid(self, grid_x, grid_y, drag_release=False): self.grid.removeAllChildren() x_points_lower = [[x, grid_y[0], -0.001] for x in grid_x] x_points_upper = [[x, grid_y[-1], -0.001] for x in grid_x] y_points_lower = [[grid_x[0], y, -0.001] for y in grid_y] y_points_upper = [[grid_x[-1], y, -0.001] for y in grid_y] for l in zip(x_points_lower, x_points_upper): self.grid += [pp.Line(l, color='grey').object] for l in zip(y_points_lower, y_points_upper): self.grid += [pp.Line(l, color='grey').object] for l in y_points_upper: color = coin.SoMaterial() color.diffuseColor = [0, 0, 0] textsep = coin.SoSeparator() text = coin.SoText2() trans = coin.SoTranslation() trans.translation = l text.string = self.text_repr(l[1] * self.value_scale) textsep += [color, trans, text] self.grid += [textsep] interpolation = self.spline.interpolation(50) if drag_release: for i in self.back: color = coin.SoMaterial() color.diffuseColor = [0, 0, 0] textsep = coin.SoSeparator() scale = coin.SoScale() text = coin.SoAsciiText() trans = coin.SoTranslation() rot = coin.SoRotationXYZ() rot.axis = coin.SoRotationXYZ.Z rot.angle.setValue(np.pi / 2) scale.scaleFactor = (self.text_scale, self.text_scale, self.text_scale) trans.translation = (i[0], i[1], 0.001) text.string = self.text_repr( interpolation(i[0]) * self.value_scale * self.scale[1]) textsep += [color, trans, scale, rot, text] self.grid += [textsep]
def onChanged(self, vobj, prop): if prop == "LineColor": if hasattr(vobj, "LineColor"): l = vobj.LineColor self.mat.diffuseColor.setValue([l[0], l[1], l[2]]) elif prop == "DrawStyle": if hasattr(vobj, "DrawStyle"): if vobj.DrawStyle == "Solid": self.linestyle.linePattern = 0xffff elif vobj.DrawStyle == "Dashed": self.linestyle.linePattern = 0xf00f elif vobj.DrawStyle == "Dotted": self.linestyle.linePattern = 0x0f0f else: self.linestyle.linePattern = 0xff88 elif prop == "LineWidth": if hasattr(vobj, "LineWidth"): self.linestyle.lineWidth = vobj.LineWidth elif prop in ["BubbleSize", "BubblePosition", "FontName", "FontSize"]: if hasattr(self, "bubbleset"): if self.bubbles: self.bubbleset.removeChild(self.bubbles) self.bubbles = None if vobj.Object.Shape: if vobj.Object.Shape.Edges: self.bubbles = coin.SoSeparator() self.bubblestyle = coin.SoDrawStyle() self.bubblestyle.linePattern = 0xffff self.bubbles.addChild(self.bubblestyle) self.bubbletexts = [] self.bubbledata = [] pos = ["Start"] if hasattr(vobj, "BubblePosition"): if vobj.BubblePosition in [ "Both", "Arrow left", "Arrow right", "Bar left", "Bar right" ]: pos = ["Start", "End"] elif vobj.BubblePosition == "None": pos = [] else: pos = [vobj.BubblePosition] for i in range(len(vobj.Object.Distances)): for p in pos: if hasattr( vobj.Object, "Limit") and vobj.Object.Limit.Value: verts = [ vobj.Object.Placement.inverse( ).multVec(vobj.Object.Shape.Edges[i]. Vertexes[0].Point), vobj.Object.Placement.inverse(). multVec(vobj.Object.Shape.Edges[ i + 1].Vertexes[0].Point) ] else: verts = [ vobj.Object.Placement.inverse( ).multVec(v.Point) for v in vobj.Object.Shape.Edges[i].Vertexes ] arrow = None if p == "Start": p1 = verts[0] p2 = verts[1] if vobj.BubblePosition.endswith("left"): arrow = True elif vobj.BubblePosition.endswith("right"): arrow = False else: p1 = verts[1] p2 = verts[0] if vobj.BubblePosition.endswith("left"): arrow = False elif vobj.BubblePosition.endswith("right"): arrow = True dv = p2.sub(p1) dv.normalize() if hasattr(vobj.BubbleSize, "Value"): rad = vobj.BubbleSize.Value / 2 else: rad = vobj.BubbleSize / 2 center = p2.add(Vector(dv).multiply(rad)) normal = vobj.Object.Placement.Rotation.multVec( Vector(0, 0, 1)) chord = dv.cross(normal) if arrow: p3 = p2.add( Vector(chord).multiply(rad / 2).negative()) if vobj.BubblePosition.startswith("Arrow"): p4 = p3.add( Vector(dv).multiply(rad * 2).negative()) p5 = p2.add( Vector(dv).multiply( rad).negative()).add( Vector(chord).multiply( rad * 1.5).negative()) pts = [ tuple(p3), tuple(p5), tuple(p4), tuple(p3) ] center = p5.add( Vector(chord).multiply(rad * 2.5)) else: p4 = p3.add( Vector(dv).multiply(rad / 2).negative()) p5 = p4.add( Vector(chord).multiply( rad * 1.5).negative()) p6 = p5.add( Vector(dv).multiply(rad / 2)) pts = [ tuple(p3), tuple(p6), tuple(p5), tuple(p4), tuple(p3) ] center = p5.add( Vector(chord).multiply(rad * 3)) coords = coin.SoCoordinate3() coords.point.setValues(0, len(pts), pts) line = coin.SoFaceSet() line.numVertices.setValue(-1) cir = Part.makePolygon(pts) cir.Placement = vobj.Object.Placement self.bubbledata.append(cir) elif arrow == False: p3 = p2.add( Vector(chord).multiply(rad / 2)) if vobj.BubblePosition.startswith("Arrow"): p4 = p3.add( Vector(dv).multiply(rad * 2).negative()) p5 = p2.add( Vector(dv).multiply( rad).negative()).add( Vector(chord).multiply( rad * 1.5)) pts = [ tuple(p3), tuple(p4), tuple(p5), tuple(p3) ] center = p5.add( Vector(chord).multiply( rad * 2.5).negative()) else: p4 = p3.add( Vector(dv).multiply(rad / 2).negative()) p5 = p4.add( Vector(chord).multiply(rad * 1.5)) p6 = p5.add( Vector(dv).multiply(rad / 2)) pts = [ tuple(p3), tuple(p4), tuple(p5), tuple(p6), tuple(p3) ] center = p5.add( Vector(chord).multiply( rad * 3).negative()) coords = coin.SoCoordinate3() coords.point.setValues(0, len(pts), pts) line = coin.SoFaceSet() line.numVertices.setValue(-1) cir = Part.makePolygon(pts) cir.Placement = vobj.Object.Placement self.bubbledata.append(cir) else: cir = Part.makeCircle(rad, center) buf = cir.writeInventor() try: cin = coin.SoInput() cin.setBuffer(buf) cob = coin.SoDB.readAll(cin) except Exception: # workaround for pivy SoInput.setBuffer() bug buf = buf.replace("\n", "") pts = re.findall( "point \[(.*?)\]", buf)[0] pts = pts.split(",") pc = [] for point in pts: v = point.strip().split() pc.append([ float(v[0]), float(v[1]), float(v[2]) ]) coords = coin.SoCoordinate3() coords.point.setValues(0, len(pc), pc) line = coin.SoLineSet() line.numVertices.setValue(-1) else: coords = cob.getChild(1).getChild( 0).getChild(2) line = cob.getChild(1).getChild( 0).getChild(3) cir.Placement = vobj.Object.Placement self.bubbledata.append(cir) self.bubbles.addChild(coords) self.bubbles.addChild(line) st = coin.SoSeparator() tr = coin.SoTransform() fs = rad * 1.5 if hasattr(vobj, "FontSize"): fs = vobj.FontSize.Value txpos = FreeCAD.Vector(center.x, center.y - fs / 2.5, center.z) tr.translation.setValue(tuple(txpos)) fo = coin.SoFont() fn = Draft.getParam("textfont", "Arial,Sans") if hasattr(vobj, "FontName"): if vobj.FontName: try: fn = str(vobj.FontName) except Exception: pass fo.name = fn fo.size = fs tx = coin.SoAsciiText() tx.justification = coin.SoText2.CENTER self.bubbletexts.append( (tx, vobj.Object.Placement.multVec(center))) st.addChild(tr) st.addChild(fo) st.addChild(tx) self.bubbles.addChild(st) self.bubbleset.addChild(self.bubbles) self.onChanged(vobj, "NumberingStyle") if prop in ["FontName", "FontSize"]: self.onChanged(vobj, "ShowLabel") elif prop in ["NumberingStyle", "StartNumber"]: if hasattr(self, "bubbletexts"): num = 0 if hasattr(vobj, "StartNumber"): if vobj.StartNumber > 1: num = vobj.StartNumber - 1 alt = False for t in self.bubbletexts: t[0].string = self.getNumber(vobj, num) num += 1 if hasattr(vobj, "BubblePosition"): if vobj.BubblePosition in [ "Both", "Arrow left", "Arrow right", "Bar left", "Bar right" ]: if not alt: num -= 1 alt = not alt elif prop in ["ShowLabel", "LabelOffset"]: if hasattr(self, "labels"): if self.labels: self.labelset.removeChild(self.labels) self.labels = None if hasattr(vobj, "ShowLabel") and hasattr(vobj.Object, "Labels"): if vobj.ShowLabel: self.labels = coin.SoSeparator() if hasattr(vobj.Object, "Limit") and vobj.Object.Limit.Value: n = len(vobj.Object.Shape.Edges) / 2 else: n = len(vobj.Object.Shape.Edges) for i in range(n): if len(vobj.Object.Labels) > i: if vobj.Object.Labels[i]: vert = vobj.Object.Shape.Edges[i].Vertexes[ 0].Point if hasattr(vobj, "LabelOffset"): pl = FreeCAD.Placement(vobj.LabelOffset) pl.Base = vert.add(pl.Base) st = coin.SoSeparator() tr = coin.SoTransform() fo = coin.SoFont() tx = coin.SoAsciiText() tx.justification = coin.SoText2.LEFT t = vobj.Object.Labels[i] if six.PY2 and isinstance(t, six.text_type): t = t.encode("utf8") tx.string.setValue(t) if hasattr(vobj, "FontSize"): fs = vobj.FontSize.Value elif hasattr(vobj.BubbleSize, "Value"): fs = vobj.BubbleSize.Value * 0.75 else: fs = vobj.BubbleSize * 0.75 tr.translation.setValue(tuple(pl.Base)) tr.rotation.setValue(pl.Rotation.Q) fn = Draft.getParam("textfont", "Arial,Sans") if hasattr(vobj, "FontName"): if vobj.FontName: try: fn = str(vobj.FontName) except Exception: pass fo.name = fn fo.size = fs st.addChild(tr) st.addChild(fo) st.addChild(tx) self.labels.addChild(st) self.labelset.addChild(self.labels)
def onChanged(self, vobj, prop): if prop == "LineColor": l = vobj.LineColor self.mat.diffuseColor.setValue([l[0], l[1], l[2]]) elif prop == "DrawStyle": if vobj.DrawStyle == "Solid": self.linestyle.linePattern = 0xffff elif vobj.DrawStyle == "Dashed": self.linestyle.linePattern = 0xf00f elif vobj.DrawStyle == "Dotted": self.linestyle.linePattern = 0x0f0f else: self.linestyle.linePattern = 0xff88 elif prop == "LineWidth": self.linestyle.lineWidth = vobj.LineWidth elif prop in ["BubbleSize", "BubblePosition", "FontName", "FontSize"]: if hasattr(self, "bubbleset"): if self.bubbles: self.bubbleset.removeChild(self.bubbles) self.bubbles = None if vobj.Object.Shape: if vobj.Object.Shape.Edges: self.bubbles = coin.SoSeparator() self.bubblestyle = coin.SoDrawStyle() self.bubblestyle.linePattern = 0xffff self.bubbles.addChild(self.bubblestyle) import Part, Draft self.bubbletexts = [] pos = ["Start"] if hasattr(vobj, "BubblePosition"): if vobj.BubblePosition == "Both": pos = ["Start", "End"] elif vobj.BubblePosition == "None": pos = [] else: pos = [vobj.BubblePosition] for i in range(len(vobj.Object.Shape.Edges)): for p in pos: verts = vobj.Object.Shape.Edges[i].Vertexes if p == "Start": p1 = verts[0].Point p2 = verts[1].Point else: p1 = verts[1].Point p2 = verts[0].Point dv = p2.sub(p1) dv.normalize() if hasattr(vobj.BubbleSize, "Value"): rad = vobj.BubbleSize.Value / 2 else: rad = vobj.BubbleSize / 2 center = p2.add(dv.scale(rad, rad, rad)) buf = Part.makeCircle(rad, center).writeInventor() try: cin = coin.SoInput() cin.setBuffer(buf) cob = coin.SoDB.readAll(cin) except: import re # workaround for pivy SoInput.setBuffer() bug buf = buf.replace("\n", "") pts = re.findall("point \[(.*?)\]", buf)[0] pts = pts.split(",") pc = [] for p in pts: v = p.strip().split() pc.append([ float(v[0]), float(v[1]), float(v[2]) ]) coords = coin.SoCoordinate3() coords.point.setValues(0, len(pc), pc) line = coin.SoLineSet() line.numVertices.setValue(-1) else: coords = cob.getChild(1).getChild( 0).getChild(2) line = cob.getChild(1).getChild( 0).getChild(3) self.bubbles.addChild(coords) self.bubbles.addChild(line) st = coin.SoSeparator() tr = coin.SoTransform() fs = rad * 1.5 if hasattr(vobj, "FontSize"): fs = vobj.FontSize.Value tr.translation.setValue( (center.x, center.y - fs / 2.5, center.z)) fo = coin.SoFont() fn = Draft.getParam("textfont", "Arial,Sans") if hasattr(vobj, "FontName"): if vobj.FontName: try: fn = str(vobj.FontName) except: pass fo.name = fn fo.size = fs tx = coin.SoAsciiText() tx.justification = coin.SoText2.CENTER self.bubbletexts.append(tx) st.addChild(tr) st.addChild(fo) st.addChild(tx) self.bubbles.addChild(st) self.bubbleset.addChild(self.bubbles) self.onChanged(vobj, "NumberingStyle") if prop in ["FontName", "FontSize"]: self.onChanged(vobj, "ShowLabel") elif prop in ["NumberingStyle", "StartNumber"]: if hasattr(self, "bubbletexts"): chars = "abcdefghijklmnopqrstuvwxyz" roman = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90), ('L', 50), ('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1)) num = 0 if hasattr(vobj, "StartNumber"): if vobj.StartNumber > 1: num = vobj.StartNumber - 1 alt = False for t in self.bubbletexts: if hasattr(vobj, "NumberingStyle"): if vobj.NumberingStyle == "1,2,3": t.string = str(num + 1) elif vobj.NumberingStyle == "01,02,03": t.string = str(num + 1).zfill(2) elif vobj.NumberingStyle == "001,002,003": t.string = str(num + 1).zfill(3) elif vobj.NumberingStyle == "A,B,C": result = "" base = num / 26 if base: result += chars[base].upper() remainder = num % 26 result += chars[remainder].upper() t.string = result elif vobj.NumberingStyle == "a,b,c": result = "" base = num / 26 if base: result += chars[base] remainder = num % 26 result += chars[remainder] t.string = result elif vobj.NumberingStyle == "I,II,III": result = "" n = num n += 1 for numeral, integer in roman: while n >= integer: result += numeral n -= integer t.string = result elif vobj.NumberingStyle == "L0,L1,L2": t.string = "L" + str(num) else: t.string = str(num + 1) num += 1 if hasattr(vobj, "BubblePosition"): if vobj.BubblePosition == "Both": if not alt: num -= 1 alt = not alt elif prop in ["ShowLabel", "LabelOffset"]: if hasattr(self, "labels"): if self.labels: self.labelset.removeChild(self.labels) self.labels = None if hasattr(vobj, "ShowLabel") and hasattr(vobj.Object, "Labels"): if vobj.ShowLabel: self.labels = coin.SoSeparator() for i in range(len(vobj.Object.Shape.Edges)): if len(vobj.Object.Labels) > i: if vobj.Object.Labels[i]: import Draft vert = vobj.Object.Shape.Edges[i].Vertexes[ 0].Point if hasattr(vobj, "LabelOffset"): pl = FreeCAD.Placement(vobj.LabelOffset) pl.Base = vert.add(pl.Base) st = coin.SoSeparator() tr = coin.SoTransform() fo = coin.SoFont() tx = coin.SoAsciiText() tx.justification = coin.SoText2.LEFT t = vobj.Object.Labels[i] if isinstance(t, unicode): t = t.encode("utf8") tx.string.setValue(t) if hasattr(vobj, "FontSize"): fs = vobj.FontSize.Value elif hasattr(vobj.BubbleSize, "Value"): fs = vobj.BubbleSize.Value * 0.75 else: fs = vobj.BubbleSize * 0.75 tr.translation.setValue(tuple(pl.Base)) tr.rotation.setValue(pl.Rotation.Q) fn = Draft.getParam("textfont", "Arial,Sans") if hasattr(vobj, "FontName"): if vobj.FontName: try: fn = str(vobj.FontName) except: pass fo.name = fn fo.size = fs st.addChild(tr) st.addChild(fo) st.addChild(tx) self.labels.addChild(st) self.labelset.addChild(self.labels)
def draw_Text_Wheel(vec=App.Vector(0.0, 0.0, 0.0), _color=FR_COLOR.FR_WHITE, setupRotation=[0, 0, 0], _rotation=[0.0, 0.0, 0.0, 0.0], _Scale=[1, 1, 1], LineWidth=1.0): try: TextScale = 0.04 txtCol = coin.SoBaseColor() # must be converted to SoBaseColor txtCol.rgb = _color txtXSo = coin.SoSeparator() # must be converted to SoBaseColor txtXSo.Name = "90Degree" txtXTransform = coin.SoTransform() txtXTransform.translation.setValue(5.5, 0.0, 0.0) txtXTransform.rotation.setValue(coin.SbVec3f(6.0, 0.0, 0.0), math.radians(0.0)) txtXTransform.scaleFactor.setValue(TextScale, TextScale, TextScale) textX = ["90.0°", ""] text3DX = coin.SoAsciiText() # Draw text in the 3D world text3DX.string.setValues([l.encode("utf8") for l in textX if l]) txtXSo.addChild(txtXTransform) txtXSo.addChild(txtCol) txtXSo.addChild(text3DX) txtXPSo = coin.SoSeparator() # must be converted to SoBaseColor txtXPSo.Name = "270Degree" txtXPTransform = coin.SoTransform() txtXPTransform.translation.setValue(0.0, -5.5, 0.0) txtXPTransform.rotation.setValue(coin.SbVec3f(0.0, 0.0, 0.0), math.radians(0.0)) txtXPTransform.scaleFactor.setValue(TextScale, TextScale, TextScale) textXP = ["270.0°", ""] text3DXP = coin.SoAsciiText() # Draw text in the 3D world text3DXP.string.setValues([l.encode("utf8") for l in textXP if l]) txtXPSo.addChild(txtXPTransform) txtXPSo.addChild(txtCol) txtXPSo.addChild(text3DXP) txtYSo = coin.SoSeparator() # must be converted to SoBaseColor txtYSo.Name = "0Degree" txtYTransform = coin.SoTransform() txtYTransform.translation.setValue(0.0, 5.5, 0.0) txtYTransform.rotation.setValue(coin.SbVec3f(0.0, 0.0, 0.0), math.radians(00.0)) txtYTransform.scaleFactor.setValue(TextScale, TextScale, TextScale) textY = ["0.0°", ""] text3DY = coin.SoAsciiText() # Draw text in the 3D world text3DY.string.setValues([l.encode("utf8") for l in textY if l]) txtYSo.addChild(txtYTransform) txtYSo.addChild(txtCol) txtYSo.addChild(text3DY) txtYPSo = coin.SoSeparator() # must be converted to SoBaseColor txtYPSo.Name = "180Degree" txtYPTransform = coin.SoTransform() txtYPTransform.translation.setValue(-6.0, 0.0, 0.0) txtYPTransform.rotation.setValue(coin.SbVec3f(0.0, 0.0, 0.0), math.radians(0.0)) txtYPTransform.scaleFactor.setValue(TextScale, TextScale, TextScale) textYP = ["180.0°", ""] text3DYP = coin.SoAsciiText() # Draw text in the 3D world text3DYP.string.setValues([l.encode("utf8") for l in textYP if l]) txtYPSo.addChild(txtYPTransform) txtYPSo.addChild(txtCol) txtYPSo.addChild(text3DYP) groupT = coin.SoSeparator() groupT.Name = "GroupT" groupT.addChild(txtXSo) groupT.addChild(txtXPSo) groupT.addChild(txtYSo) groupT.addChild(txtYPSo) txtRoot = coin.SoSeparator() txtRoot.Name = "AllText-coordination" txtrootTrans = coin.SoTransform() txtrootTrans.rotation.setValue(coin.SbVec3f(1.0, 0.0, 0.0), math.radians(0)) txtRoot.addChild(txtrootTrans) txtRoot.addChild(groupT) tRadiusX = coin.SbVec3f() tRadiusY = coin.SbVec3f() tRadiusZ = coin.SbVec3f() transfromX = coin.SoTransform() transfromY = coin.SoTransform() transfromZ = coin.SoTransform() tRadiusX.setValue(1, 0, 0) tRadiusY.setValue(0, 1, 0) tRadiusZ.setValue(0, 0, 1) transfromX.rotation.setValue(tRadiusX, math.radians(setupRotation[0])) transfromY.rotation.setValue(tRadiusY, math.radians(setupRotation[1])) transfromZ.rotation.setValue(tRadiusZ, math.radians(setupRotation[2])) SoSeparatorSetupX = coin.SoSeparator() SoSeparatorSetupX.Name = "SetupX" SoSeparatorSetupY = coin.SoSeparator() SoSeparatorSetupY.Name = "SetupY" SoSeparatorSetupZ = coin.SoSeparator() SoSeparatorSetupZ.Name = "SetupZ" SoSeparatorSetupX.addChild(transfromX) SoSeparatorSetupX.addChild(txtRoot) SoSeparatorSetupY.addChild(transfromY) SoSeparatorSetupY.addChild(SoSeparatorSetupX) SoSeparatorSetupZ.addChild(transfromZ) SoSeparatorSetupZ.addChild(SoSeparatorSetupY) tempR = coin.SbVec3f() tempR.setValue(_rotation[0], _rotation[1], _rotation[2]) rootTransform = coin.SoTransform() rootTransform.rotation.setValue(tempR, math.radians(_rotation[3])) rootTransform.scaleFactor.setValue(_Scale) material = coin.SoMaterial() material.diffuseColor.setValue(_color) material.emissiveColor.setValue(_color) material.transparency.setValue(0.0) root = coin.SoSeparator() root.Name = "RootText" transla = coin.SoTranslation() transla.translation.setValue([vec.x, vec.y, vec.z]) root.addChild(material) col1 = coin.SoBaseColor() # must be converted to SoBaseColor col1.rgb = _color root.addChild(rootTransform) root.addChild(col1) root.addChild(transla) root.addChild(SoSeparatorSetupZ) return root except Exception as err: App.Console.PrintError("'Wheel' Failed. " "{err}\n".format(err=str(err))) exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno)
def attach(self, vobj): '''Setup the scene sub-graph of the view provider''' self.Object = vobj.Object self.color = coin.SoBaseColor() self.font = coin.SoFont() self.font3d = coin.SoFont() self.text = coin.SoAsciiText() self.text3d = coin.SoText2() self.text.string = "d" # some versions of coin crash if string is not set self.text3d.string = "d" self.textpos = coin.SoTransform() self.text.justification = self.text3d.justification = coin.SoAsciiText.CENTER label = coin.SoSeparator() label.addChild(self.textpos) label.addChild(self.color) label.addChild(self.font) label.addChild(self.text) label3d = coin.SoSeparator() label3d.addChild(self.textpos) label3d.addChild(self.color) label3d.addChild(self.font3d) label3d.addChild(self.text3d) self.coord1 = coin.SoCoordinate3() self.trans1 = coin.SoTransform() self.coord2 = coin.SoCoordinate3() self.trans2 = coin.SoTransform() self.transDimOvershoot1 = coin.SoTransform() self.transDimOvershoot2 = coin.SoTransform() self.transExtOvershoot1 = coin.SoTransform() self.transExtOvershoot2 = coin.SoTransform() self.marks = coin.SoSeparator() self.marksDimOvershoot = coin.SoSeparator() self.marksExtOvershoot = coin.SoSeparator() self.drawstyle = coin.SoDrawStyle() self.line = coin.SoType.fromName("SoBrepEdgeSet").createInstance() self.coords = coin.SoCoordinate3() self.node = coin.SoGroup() self.node.addChild(self.color) self.node.addChild(self.drawstyle) self.lineswitch2 = coin.SoSwitch() self.lineswitch2.whichChild = -3 self.node.addChild(self.lineswitch2) self.lineswitch2.addChild(self.coords) self.lineswitch2.addChild(self.line) self.lineswitch2.addChild(self.marks) self.lineswitch2.addChild(self.marksDimOvershoot) self.lineswitch2.addChild(self.marksExtOvershoot) self.node.addChild(label) self.node3d = coin.SoGroup() self.node3d.addChild(self.color) self.node3d.addChild(self.drawstyle) self.lineswitch3 = coin.SoSwitch() self.lineswitch3.whichChild = -3 self.node3d.addChild(self.lineswitch3) self.lineswitch3.addChild(self.coords) self.lineswitch3.addChild(self.line) self.lineswitch3.addChild(self.marks) self.lineswitch3.addChild(self.marksDimOvershoot) self.lineswitch3.addChild(self.marksExtOvershoot) self.node3d.addChild(label3d) vobj.addDisplayMode(self.node, "2D") vobj.addDisplayMode(self.node3d, "3D") self.updateData(vobj.Object, "Start") self.onChanged(vobj, "FontSize") self.onChanged(vobj, "FontName") self.onChanged(vobj, "ArrowType") self.onChanged(vobj, "LineColor") self.onChanged(vobj, "DimOvershoot") self.onChanged(vobj, "ExtOvershoot")
def attach(self, vobj): """Set up the scene sub-graph of the viewprovider.""" self.Object = vobj.Object self.color = coin.SoBaseColor() if hasattr(vobj, "LineColor"): self.color.rgb.setValue(vobj.LineColor[0], vobj.LineColor[1], vobj.LineColor[2]) self.font = coin.SoFont() self.font3d = coin.SoFont() self.text = coin.SoAsciiText() # Can be oriented in 3D space self.text3d = coin.SoText2() # Faces the camera always # The text string needs to be initialized to something, # otherwise it may cause a crash of the system self.text.string = "d" self.text3d.string = "d" self.text.justification = coin.SoAsciiText.CENTER self.text3d.justification = coin.SoAsciiText.CENTER self.textpos = coin.SoTransform() label = coin.SoSeparator() label.addChild(self.textpos) label.addChild(self.color) label.addChild(self.font) label.addChild(self.text) label3d = coin.SoSeparator() label3d.addChild(self.textpos) label3d.addChild(self.color) label3d.addChild(self.font3d) label3d.addChild(self.text3d) self.coord1 = coin.SoCoordinate3() self.trans1 = coin.SoTransform() self.coord2 = coin.SoCoordinate3() self.trans2 = coin.SoTransform() self.marks = coin.SoSeparator() self.drawstyle = coin.SoDrawStyle() self.coords = coin.SoCoordinate3() self.arc = coin.SoType.fromName("SoBrepEdgeSet").createInstance() self.node = coin.SoGroup() self.node.addChild(self.color) self.node.addChild(self.drawstyle) self.node.addChild(self.coords) self.node.addChild(self.arc) self.node.addChild(self.marks) self.node.addChild(label) self.node3d = coin.SoGroup() self.node3d.addChild(self.color) self.node3d.addChild(self.drawstyle) self.node3d.addChild(self.coords) self.node3d.addChild(self.arc) self.node3d.addChild(self.marks) self.node3d.addChild(label3d) vobj.addDisplayMode(self.node, "2D") vobj.addDisplayMode(self.node3d, "3D") self.updateData(vobj.Object, None) self.onChanged(vobj, "FontSize") self.onChanged(vobj, "FontName") self.onChanged(vobj, "ArrowType") self.onChanged(vobj, "LineColor")
def draw_newlabel(text=[], prop: propertyValues = None): ''' Draw widgets label relative to the vectors given to the function The three angel will decide how the text will be rotated based on that vectors. ''' if text == '' or prop is None: raise ValueError # Nothing to do here try: p1 = prop.vectors[0] # if len(prop.vectors) >= 2: # p2 = prop.vectors[1] # direction= _translation = coin.SoTranslation() # coin.SoTransform() _transform = coin.SoTransform() _transformX = coin.SoTransform() _transformY = coin.SoTransform() _transformZ = coin.SoTransform() _transform.scaleFactor.setValue(prop.scale) # Only for scaling _translation.translation.setValue(coin.SbVec3f(p1)) _transformY.rotation.setValue(coin.SbVec3f(1, 0, 0), math.radians(prop.SetupRotation[0])) _transformX.rotation.setValue(coin.SbVec3f(0, 1, 0), math.radians(prop.SetupRotation[1])) _transformZ.rotation.setValue(coin.SbVec3f(0, 0, 1), math.radians(prop.SetupRotation[2])) font = coin.SoFont() font.size = prop.fontsize # Font size font.Name = prop.fontName # Font used _text3D = coin.SoAsciiText() # Draw text in the 3D world _text3D.string.setValues( text) #([l.encode("utf8") for l in text if l]) coinColor = coin.SoMaterial() # Font color coinColor.diffuseColor.setValue(prop.labelcolor) coinColor.emissiveColor.setValue(prop.labelcolor) root = coin.SoSeparator( ) # A Separator to separate the text from the drawing _textNodeX = coin.SoSeparator( ) # A Separator to Keep the rotation in X Axis _textNodeY = coin.SoSeparator( ) # A Separator to Keep the rotation in Y Axis _textNodeZ = coin.SoSeparator( ) # A Separator to Keep the rotation in Z Axis _textNodeX.addChild(_transformX) _textNodeY.addChild(_transformY) _textNodeZ.addChild(_transformZ) _textNodeX.addChild(coinColor) _textNodeX.addChild(font) _textNodeX.addChild(_text3D) _textNodeY.addChild(_textNodeX) _textNodeZ.addChild(_textNodeY) root.addChild(_translation) root.addChild(_transform) root.addChild(_textNodeZ) return root # Return the created SoSeparator that contains the text except Exception as err: App.Console.PrintError("'draw_label' Failed. " "{err}\n".format(err=str(err))) exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno)
def draw_label(text=[], prop: propertyValues = None): ''' Draw widgets label relative to the position with prop: Consist of several data for example: prop.vectors = [App.Vector(0, 0, 0), ] prop.linewidth = 1 prop.fontName = 'sans' prop.fontsize = 2 prop.labelcolor = constant.FR_COLOR.FR_BLACK prop.rotation = [0.0, 0.0, 0.0, 0.0] # Normal rotation like you have in FreeCAD objects prop.SetupRotation = [0.0, 0.0, 0.0] # in degrees . This is a pre-rotation during initialization prop.scale = [1.0, 1.0, 1.0] Example: from pivy import coin from PySide import QtCore,QtGui import fr_label_draw as l import fr_widget as w sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph() p=w.propertyValues() p.vectors=[App.Vector(20,20,0),App.Vector(0.0, 0.0, 0.0)] wl=l.draw_label(["My Label",],p) sg.addChild(wl) ''' if text == '' or prop is None: return # Nothing to do here try: delta = App.Vector(0, 0, 0) # You must cast the value or it will fail p1 = App.Vector(prop.vectors[0]) p2 = App.Vector(prop.vectors[1]) delta.x = p1.x + 3 delta.y = p1.y + 3 delta.z = p1.z # get spherical representation of the point(p2) #(r, thi, phi) = calculateLineSpherical(prop.vectors) xAng = math.radians(prop.SetupRotation[0]) yAng = math.radians(prop.SetupRotation[1]) zAng = math.radians(prop.SetupRotation[2]) _rootTrasnPOS = coin.SoTranslation() # location _rootRotation = coin.SoTransform() _transRotationX = coin.SoTransform() _transRotationY = coin.SoTransform() _transRotationZ = coin.SoTransform() _rootTrasnPOS.translation.setValue(coin.SbVec3f(delta)) tempRX = coin.SbVec3f() tempRX.setValue(1, 0, 0) tempRY = coin.SbVec3f() tempRY.setValue(0, 1, 0) tempRZ = coin.SbVec3f() tempRZ.setValue(0, 0, 1) _transRotationX.rotation.setValue(tempRX, xAng) _transRotationY.rotation.setValue(tempRY, yAng) _transRotationZ.rotation.setValue(tempRZ, zAng) _rootRotation.rotation.setValue( coin.SbVec3f(prop.rotation[0], prop.rotation[1], prop.rotation[2]), prop.rotation[3]) font = coin.SoFont() font.size = prop.fontsize # Font size font.Name = prop.fontName # Font used _text3D = coin.SoAsciiText() # Draw text in the 3D world _text3D.string.setValues([l.encode("utf8") for l in text if l]) coinColor = coin.SoMaterial() # Font color color = prop.labelcolor coinColor.diffuseColor.set1Value(0, coin.SbColor(*color)) #coinColor.diffuseColor.set1Value(0, coin.SbColor(*prop.labelcolor)) _textNode = coin.SoSeparator( ) # A Separator to separate the text from the drawing _textNode.addChild(font) _textNode.addChild(coinColor) _textNode.addChild(_text3D) xSoNod = coin.SoSeparator() # A Separator to Keep the rotation ySoNod = coin.SoSeparator() # A Separator to Keep the rotation zSoNod = coin.SoSeparator() # A Separator to Keep the rotation xSoNod.addChild(_transRotationX) xSoNod.addChild(_textNode) ySoNod.addChild(_transRotationY) ySoNod.addChild(xSoNod) zSoNod.addChild(_transRotationZ) zSoNod.addChild(ySoNod) root = coin.SoSeparator() root.addChild(_rootRotation) root.addChild(_rootTrasnPOS) root.addChild(zSoNod) return root # Return the created SoSeparator that contains the text except Exception as err: App.Console.PrintError("'draw_label' Failed. " "{err}\n".format(err=str(err))) exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno)
def attach(self, vobj): """Set up the scene sub-graph of the viewprovider.""" # Attributes of the Coin scenegraph self.arrow = coin.SoSeparator() self.arrowpos = coin.SoTransform() self.arrow.addChild(self.arrowpos) self.matline = coin.SoMaterial() self.drawstyle = coin.SoDrawStyle() self.drawstyle.style = coin.SoDrawStyle.LINES self.lcoords = coin.SoCoordinate3() self.line = coin.SoType.fromName("SoBrepEdgeSet").createInstance() self.mattext = coin.SoMaterial() self.textpos = coin.SoTransform() self.font = coin.SoFont() self.text2d = coin.SoText2() # Faces the camera always self.text3d = coin.SoAsciiText() # Can be oriented in 3D space self.fcoords = coin.SoCoordinate3() self.frame = coin.SoType.fromName("SoBrepEdgeSet").createInstance() self.lineswitch = coin.SoSwitch() self.symbol = gui_utils.dim_symbol() textdrawstyle = coin.SoDrawStyle() textdrawstyle.style = coin.SoDrawStyle.FILLED # The text string needs to be initialized to something, # otherwise it crashes self.text2d.string = self.text3d.string = "Label" self.text2d.justification = coin.SoText2.RIGHT self.text3d.justification = coin.SoAsciiText.RIGHT self.font.name = utils.get_param("textfont") switchnode = coin.SoSeparator() switchnode.addChild(self.line) self.lineswitch.addChild(switchnode) self.lineswitch.whichChild = 0 self.node2dtxt = coin.SoGroup() self.node2dtxt.addChild(self.font) self.node2dtxt.addChild(self.text2d) self.node2d = coin.SoGroup() self.node2d.addChild(self.matline) self.node2d.addChild(self.arrow) self.node2d.addChild(self.drawstyle) self.node2d.addChild(self.lcoords) self.node2d.addChild(self.lineswitch) self.node2d.addChild(self.mattext) self.node2d.addChild(textdrawstyle) self.node2d.addChild(self.textpos) self.node2d.addChild(self.node2dtxt) self.node2d.addChild(self.matline) self.node2d.addChild(self.drawstyle) self.node2d.addChild(self.fcoords) self.node2d.addChild(self.frame) self.node3dtxt = coin.SoGroup() self.node3dtxt.addChild(self.font) self.node3dtxt.addChild(self.text3d) self.node3d = coin.SoGroup() self.node3d.addChild(self.matline) self.node3d.addChild(self.arrow) self.node3d.addChild(self.drawstyle) self.node3d.addChild(self.lcoords) self.node3d.addChild(self.lineswitch) self.node3d.addChild(self.mattext) self.node3d.addChild(textdrawstyle) self.node3d.addChild(self.textpos) self.node3d.addChild(self.node3dtxt) self.node3d.addChild(self.matline) self.node3d.addChild(self.drawstyle) self.node3d.addChild(self.fcoords) self.node3d.addChild(self.frame) vobj.addDisplayMode(self.node2d, "2D text") vobj.addDisplayMode(self.node3d, "3D text") self.onChanged(vobj, "LineColor") self.onChanged(vobj, "TextColor") self.onChanged(vobj, "LineWidth") self.onChanged(vobj, "ArrowSize") self.onChanged(vobj, "Line")
def onChanged(self, vobj, prop): if prop == "LineColor": l = vobj.LineColor self.mat.diffuseColor.setValue([l[0],l[1],l[2]]) elif prop == "DrawStyle": if vobj.DrawStyle == "Solid": self.linestyle.linePattern = 0xffff elif vobj.DrawStyle == "Dashed": self.linestyle.linePattern = 0xf00f elif vobj.DrawStyle == "Dotted": self.linestyle.linePattern = 0x0f0f else: self.linestyle.linePattern = 0xff88 elif prop == "LineWidth": self.linestyle.lineWidth = vobj.LineWidth elif prop == "BubbleSize": if hasattr(self,"bubbleset"): if self.bubbles: self.bubbleset.removeChild(self.bubbles) self.bubbles = None if vobj.Object.Shape: if vobj.Object.Shape.Edges: self.bubbles = coin.SoSeparator() self.bubblestyle = coin.SoDrawStyle() self.bubblestyle.linePattern = 0xffff self.bubbles.addChild(self.bubblestyle) import Part,Draft self.bubbletexts = [] for i in range(len(vobj.Object.Shape.Edges)): verts = vobj.Object.Shape.Edges[i].Vertexes p1 = verts[0].Point p2 = verts[1].Point dv = p2.sub(p1) dv.normalize() if hasattr(vobj.BubbleSize,"Value"): rad = vobj.BubbleSize.Value/2 else: rad = vobj.BubbleSize/2 center = p2.add(dv.scale(rad,rad,rad)) buf = Part.makeCircle(rad,center).writeInventor() try: cin = coin.SoInput() cin.setBuffer(buf) cob = coin.SoDB.readAll(cin) except: import re # workaround for pivy SoInput.setBuffer() bug buf = buf.replace("\n","") pts = re.findall("point \[(.*?)\]",buf)[0] pts = pts.split(",") pc = [] for p in pts: v = p.strip().split() pc.append([float(v[0]),float(v[1]),float(v[2])]) coords = coin.SoCoordinate3() coords.point.setValues(0,len(pc),pc) line = coin.SoLineSet() line.numVertices.setValue(-1) else: coords = cob.getChild(1).getChild(0).getChild(2) line = cob.getChild(1).getChild(0).getChild(3) self.bubbles.addChild(coords) self.bubbles.addChild(line) st = coin.SoSeparator() tr = coin.SoTransform() tr.translation.setValue((center.x,center.y-rad/2,center.z)) fo = coin.SoFont() fo.name = Draft.getParam("textfont","Arial,Sans") fo.size = rad*1.5 tx = coin.SoAsciiText() tx.justification = coin.SoText2.CENTER self.bubbletexts.append(tx) st.addChild(tr) st.addChild(fo) st.addChild(tx) self.bubbles.addChild(st) self.bubbleset.addChild(self.bubbles) self.onChanged(vobj,"NumberingStyle") elif prop == "NumberingStyle": if hasattr(self,"bubbletexts"): chars = "abcdefghijklmnopqrstuvwxyz" roman=(('M',1000),('CM',900),('D',500),('CD',400), ('C',100),('XC',90),('L',50),('XL',40), ('X',10),('IX',9),('V',5),('IV',4),('I',1)) num = 0 for t in self.bubbletexts: if hasattr(vobj,"NumberingStyle"): if vobj.NumberingStyle == "1,2,3": t.string = str(num+1) elif vobj.NumberingStyle == "01,02,03": t.string = str(num+1).zfill(2) elif vobj.NumberingStyle == "001,002,003": t.string = str(num+1).zfill(3) elif vobj.NumberingStyle == "A,B,C": result = "" base = num/26 if base: result += chars[base].upper() remainder = num % 26 result += chars[remainder].upper() t.string = result elif vobj.NumberingStyle == "a,b,c": result = "" base = num/26 if base: result += chars[base] remainder = num % 26 result += chars[remainder] t.string = result elif vobj.NumberingStyle == "I,II,III": result = "" num += 1 for numeral, integer in roman: while num >= integer: result += numeral num -= integer t.string = result elif vobj.NumberingStyle == "L0,L1,L2": t.string = "L"+str(num) else: t.string = str(num+1) num += 1