def dim_dash(p1, p2): """Return a SoSeparator with a line used to make dimension dashes. It is used by `dim_symbol` to create line end symbols like `'Tick-2'`, `'DimOvershoot'`, and `'ExtOvershoot'` dashes. Parameters ---------- p1: tuple of three floats or Base::Vector3 A point to define a line vertex. p2: tuple of three floats or Base::Vector3 A point to define a line vertex. Returns ------- Coin.SoSeparator A Coin object with a `SoLineSet` created from `p1` and `p2` as vertices. """ dash = coin.SoSeparator() v = coin.SoVertexProperty() v.vertex.set1Value(0, p1) v.vertex.set1Value(1, p2) line = coin.SoLineSet() line.vertexProperty = v dash.addChild(line) return dash
def mesh_sep(vertices, polygons, color=(1,1,0), draw_lines=False): _vertices = vertices _polygons = [] _lines = [] for i in polygons: _polygons += i _lines += i _lines.append(i[0]) _polygons.append(-1) _lines.append(-1) sep = coin.SoSeparator() vertex_property = coin.SoVertexProperty() face_set = coin.SoIndexedFaceSet() shape_hint = coin.SoShapeHints() shape_hint.vertexOrdering = coin.SoShapeHints.COUNTERCLOCKWISE shape_hint.creaseAngle = coin.M_PI / 3 face_mat = coin.SoMaterial() face_mat.diffuseColor = color vertex_property.vertex.setValues(0, len(_vertices), _vertices) face_set.coordIndex.setValues(0, len(_polygons), list(_polygons)) vertex_property.materialBinding = coin.SoMaterialBinding.PER_VERTEX_INDEXED sep += shape_hint, vertex_property, face_mat, face_set if draw_lines: line_set = coin.SoIndexedLineSet() line_set.coordIndex.setValues(0, len(_lines), list(_lines)) line_mat = coin.SoMaterial() line_mat.diffuseColor = (.0, .0, .0) sep += line_mat, line_set return sep
def draw_line(p1, p2, color, LineWidth): try: dash = coin.SoSeparator() v = coin.SoVertexProperty() v.vertex.set1Value(0, p1) v.vertex.set1Value(1, p2) coords = coin.SoTransform() line = coin.SoLineSet() line.vertexProperty = v style = coin.SoDrawStyle() style.lineWidth = LineWidth dash.addChild(style) col1 = coin.SoBaseColor() # must be converted to SoBaseColor col1.rgb = color dash.addChild(col1) dash.addChild(line) dash.addChild(coords) return dash except Exception as err: App.Console.PrintError("'makeIt' 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 dim_dash(p1, p2, color, LineWidth): dash = coin.SoSeparator() v = coin.SoVertexProperty() v.vertex.set1Value(0, p1) v.vertex.set1Value(1, p2) line = coin.SoLineSet() line.vertexProperty = v style = coin.SoDrawStyle() style.lineWidth = LineWidth my_transparency = coin.SoMaterial() my_transparency.transparency.setValue(0.5) dash.addChild(style) dash.addChild(my_transparency) dash.addChild(color) dash.addChild(line) return dash
def simple_poly_mesh(verts, poly, color=None): color = color or COLORS["grey"] _vertices = [list(v) for v in verts] _polygons = [] for pol in poly: _polygons += list(pol) + [-1] sep = coin.SoSeparator() vertex_property = coin.SoVertexProperty() face_set = coin.SoIndexedFaceSet() shape_hint = coin.SoShapeHints() shape_hint.vertexOrdering = coin.SoShapeHints.COUNTERCLOCKWISE shape_hint.creaseAngle = np.pi / 3 face_mat = coin.SoMaterial() face_mat.diffuseColor = color vertex_property.vertex.setValues(0, len(_vertices), _vertices) face_set.coordIndex.setValues(0, len(_polygons), list(_polygons)) vertex_property.materialBinding = coin.SoMaterialBinding.PER_VERTEX_INDEXED sep += [shape_hint, vertex_property, face_mat, face_set] return sep
def dim_dash(p1, p2, color, LineWidth): dash = coin.SoSeparator() pick_style = coin.SoPickStyle() pick_style.style.setValue(coin.SoPickStyle.UNPICKABLE) #This will disallow to be picked by mouse click v = coin.SoVertexProperty() v.vertex.set1Value(0, p1) v.vertex.set1Value(1, p2) line = coin.SoLineSet() line.vertexProperty = v style = coin.SoDrawStyle() style.lineWidth = LineWidth my_transparency = coin.SoMaterial() my_transparency.transparency.setValue(0.5) dash.addChild(pick_style) #This will disallow to be picked by mouse click dash.addChild(style) dash.addChild(my_transparency) dash.addChild(color) dash.addChild(line) return dash
def simple_quad_mesh(points, num_u, num_v, colors=None): msh_sep = coin.SoSeparator() msh = coin.SoQuadMesh() vertexproperty = coin.SoVertexProperty() vertexproperty.vertex.setValues(0, len(points), points) msh.verticesPerRow = num_u msh.verticesPerColumn = num_v if colors: vertexproperty.materialBinding = coin.SoMaterialBinding.PER_VERTEX for i in range(len(colors)): vertexproperty.orderedRGBA.set1Value( i, coin.SbColor(colors[i]).getPackedValue()) msh.vertexProperty = vertexproperty shape_hint = coin.SoShapeHints() shape_hint.vertexOrdering = coin.SoShapeHints.COUNTERCLOCKWISE shape_hint.creaseAngle = np.pi / 3 msh_sep += [shape_hint, msh] return msh_sep
def draw_box(vertices=[], color=(0.0, 0.0, 0.0), LineWidth=1): """ Draw any box. This will be the base of all multi-point drawing. Curves, and arc is not here. """ if len(vertices) < 4: raise ValueError('Vertices must be 4') dash = coin.SoSeparator() v = coin.SoVertexProperty() coords = coin.SoTransform() p1 = vertices[0] p2 = vertices[1] p3 = vertices[2] p4 = vertices[3] square = coin.SbBox3f(p1, p2, p3, p4) square.vertexProperty = v style = coin.SoDrawStyle() style.lineWidth = LineWidth dash.addChild(style) dash.addChild(color) dash.addChild(square) dash.addChild(coords) return draw_square
def __init__(self, layout=0): node = Gui.ActiveDocument.ActiveView.getSceneGraph() bb = coin.SoVRMLBillboard() b = coin.SoSphere() t = coin.SoTransform() t.translation.setValue(FreeCAD.Vector(0, 0, 10)) mat = coin.SoMaterial() # mat.diffuseColor.setValue([1.,0.,1.]) mat.emissiveColor.setValue([1., 1., 0.]) ssa = coin.SoSeparator() ssa.addChild(t) ssa.addChild(bb) node.addChild(ssa) if layout == 0: vertices = [ (30, 20, 0), (30, 50, 0), (0, 50, 0), (0, 0, 0), ] if layout == 1: vertices = [ (30, 80, 0), (0, 80, 0), (0, 0, 0), ] if layout == 2: vertices = [(60, 80, 0), (0, 80, 0), (0, 0, 0), (60, 0, 0)] vc = len(vertices) numvertices = (vc, vc) myVertexProperty = coin.SoVertexProperty() myVertexProperty.vertex.setValues(0, vc, vertices) # Define the FaceSet myFaceSet = coin.SoFaceSet() myFaceSet.vertexProperty = myVertexProperty myFaceSet.numVertices.setValues(0, 1, numvertices) ss = coin.SoSeparator() bb.addChild(ss) africaSep = coin.SoSeparator() africaTranslate = coin.SoTranslation() font = coin.SoFont() font.size = 20 africaText = coin.SoText2() africaTranslate.translation = (2., 40.0, 1.) # africaText.string = "AFRICA" africaText.string.setValues( ["Station", "", "Road 66", "AB - CD", "EF"]) bb.addChild(africaSep) textmat = coin.SoMaterial() textmat.diffuseColor.setValue([0., 0., 0.]) # mat.emissiveColor.setValue([1.,1.,0.]) africaSep.addChild(font) africaSep.addChild(textmat) africaSep.addChild(africaTranslate) africaSep.addChild(africaText) ss.addChild(mat) ss.addChild(myFaceSet) ss.addChild(b) self.placement = t self.material = mat self.face = ss self.parentN = node self.billboard = ssa self.text = africaText
def dim_symbol(symbol=None, invert=False): """Return the specified dimension symbol. Parameters ---------- symbol: int, optional It defaults to `None`, in which it gets the value from the parameter database, `get_param("dimsymbol", 0)`. A numerical value defines different markers * 0, `SoSphere` * 1, `SoSeparator` with a `SoLineSet`, a circle (in fact a 24 sided polygon) * 2, `SoSeparator` with a `soCone` * 3, `SoSeparator` with a `SoFaceSet` * 4, `SoSeparator` with a `SoLineSet`, calling `dim_dash` * Otherwise, `SoSphere` invert: bool, optional It defaults to `False`. If it is `True` and `symbol=2`, the cone will be rotated -90 degrees around the Z axis, otherwise the rotation is positive, +90 degrees. Returns ------- Coin.SoNode A `Coin.SoSphere`, or `Coin.SoSeparator` (circle, cone, face, line) that will be used as a dimension symbol. """ if symbol is None: symbol = utils.get_param("dimsymbol", 0) if symbol == 0: # marker = coin.SoMarkerSet() # marker.markerIndex = 80 # Returning a sphere means that the bounding box will # be 3-dimensional; a marker will always be planar seen from any # orientation but it currently doesn't work correctly marker = coin.SoSphere() return marker elif symbol == 1: marker = coin.SoSeparator() v = coin.SoVertexProperty() for i in range(25): ang = math.radians(i * 15) v.vertex.set1Value(i, (math.sin(ang), math.cos(ang), 0)) p = coin.SoLineSet() p.vertexProperty = v marker.addChild(p) return marker elif symbol == 2: marker = coin.SoSeparator() t = coin.SoTransform() t.translation.setValue((0, -2, 0)) t.center.setValue((0, 2, 0)) if invert: t.rotation.setValue(coin.SbVec3f((0, 0, 1)), -math.pi / 2) else: t.rotation.setValue(coin.SbVec3f((0, 0, 1)), math.pi / 2) c = coin.SoCone() c.height.setValue(4) marker.addChild(t) marker.addChild(c) return marker elif symbol == 3: marker = coin.SoSeparator() # hints are required otherwise only the bottom of the face is colored h = coin.SoShapeHints() h.vertexOrdering = h.COUNTERCLOCKWISE c = coin.SoCoordinate3() c.point.setValues([(-1, -2, 0), (0, 2, 0), (1, 2, 0), (0, -2, 0)]) f = coin.SoFaceSet() marker.addChild(h) marker.addChild(c) marker.addChild(f) return marker elif symbol == 4: return dim_dash((-1.5, -1.5, 0), (1.5, 1.5, 0)) else: _wrn( translate("draft", "Symbol not implemented. Using a default symbol.")) return coin.SoSphere()
def makeFrame(self, frame_labels): """ Method which makes a Coin3D frame to show a current pose in a RobRotation. A frame is made from 3 red, green and blue arrows representing X, Y and Z. Arrows are each constructed from a shaft and an arrowhead. Their dimensions and other attributes are unassigned as they are extracted from appropriate `RobRotation` properties. Returns: A SoSeparator with the frame shown in the FreeCAD View. """ # make a generic shaft from 0 in Y direction shaft_vertices = coin.SoVertexProperty() shaft_vertices.vertex.setNum(2) shaft_vertices.vertex.set1Value(0, 0, 0, 0) self.frame_shaft = coin.SoLineSet() self.frame_shaft.vertexProperty.setValue(shaft_vertices) self.frame_shaft.numVertices.setNum(1) self.frame_shaft.numVertices.setValue(2) # make a generic conic arrowhead oriented in Y axis direction and # move it at the end of the shaft self.frame_arrowhead_translation = coin.SoTranslation() self.frame_arrowhead_cone = coin.SoCone() self.frame_arrowhead = coin.SoSwitch() self.frame_arrowhead.addChild(self.frame_arrowhead_translation) self.frame_arrowhead.addChild(self.frame_arrowhead_cone) # make rotations to rotate prepared shaft and arrowhead for Y axis # direction also to X and Z rot_y2x = coin.SoRotation() rot_y2x.rotation.setValue(coin.SbRotation(coin.SbVec3f(0, 1, 0), coin.SbVec3f(1, 0, 0))) rot_y2z = coin.SoRotation() rot_y2z.rotation.setValue(coin.SbRotation(coin.SbVec3f(0, 1, 0), coin.SbVec3f(0, 0, 1))) # prepare colors for X,Y,Z which will correspond to R,G,B as customary self.frame_color_x = coin.SoPackedColor() self.frame_color_y = coin.SoPackedColor() self.frame_color_z = coin.SoPackedColor() # make complete colored and rotated arrows x_arrow = coin.SoSeparator() x_arrow.addChild(rot_y2x) x_arrow.addChild(self.frame_color_x) x_arrow.addChild(self.frame_shaft) x_arrow.addChild(self.frame_arrowhead) x_arrow.addChild(frame_labels[0]) y_arrow = coin.SoSeparator() y_arrow.addChild(self.frame_color_y) y_arrow.addChild(self.frame_shaft) y_arrow.addChild(self.frame_arrowhead) y_arrow.addChild(frame_labels[1]) z_arrow = coin.SoSeparator() z_arrow.addChild(rot_y2z) z_arrow.addChild(self.frame_color_z) z_arrow.addChild(self.frame_shaft) z_arrow.addChild(self.frame_arrowhead) z_arrow.addChild(frame_labels[2]) # prepare draw style to control shaft width self.frame_drawstyle = coin.SoDrawStyle() # make complete frame and it to shaded display mode separated_frame = coin.SoSeparator() separated_frame.addChild(self.frame_drawstyle) separated_frame.addChild(x_arrow) separated_frame.addChild(y_arrow) separated_frame.addChild(z_arrow) return separated_frame
def makeFrame(self, frame_labels): # make a generic shaft from 0 in Y direction shaft_vertices = coin.SoVertexProperty() shaft_vertices.vertex.setNum(2) shaft_vertices.vertex.set1Value(0, 0, 0, 0) self.frame_shaft = coin.SoLineSet() self.frame_shaft.vertexProperty.setValue(shaft_vertices) self.frame_shaft.numVertices.setNum(1) self.frame_shaft.numVertices.setValue(2) # make a generic conic arrowhead oriented in Y axis direction and # move it at the end of the shaft self.frame_arrowhead_translation = coin.SoTranslation() self.frame_arrowhead_cone = coin.SoCone() self.frame_arrowhead = coin.SoSwitch() self.frame_arrowhead.addChild(self.frame_arrowhead_translation) self.frame_arrowhead.addChild(self.frame_arrowhead_cone) # make rotations to rotate prepared shaft and arrowhead for Y axis # direction also to X and Z rot_y2x = coin.SoRotation() rot_y2x.rotation.setValue( coin.SbRotation(coin.SbVec3f(0, 1, 0), coin.SbVec3f(1, 0, 0))) rot_y2z = coin.SoRotation() rot_y2z.rotation.setValue( coin.SbRotation(coin.SbVec3f(0, 1, 0), coin.SbVec3f(0, 0, 1))) # prepare colors for X,Y,Z which will correspond to R,G,B as customary self.frame_color_x = coin.SoPackedColor() self.frame_color_y = coin.SoPackedColor() self.frame_color_z = coin.SoPackedColor() # make complete colored and rotated arrows x_arrow = coin.SoSeparator() x_arrow.addChild(rot_y2x) x_arrow.addChild(self.frame_color_x) x_arrow.addChild(self.frame_shaft) x_arrow.addChild(self.frame_arrowhead) x_arrow.addChild(frame_labels[0]) y_arrow = coin.SoSeparator() y_arrow.addChild(self.frame_color_y) y_arrow.addChild(self.frame_shaft) y_arrow.addChild(self.frame_arrowhead) y_arrow.addChild(frame_labels[1]) z_arrow = coin.SoSeparator() z_arrow.addChild(rot_y2z) z_arrow.addChild(self.frame_color_z) z_arrow.addChild(self.frame_shaft) z_arrow.addChild(self.frame_arrowhead) z_arrow.addChild(frame_labels[2]) # prepare draw style to control shaft width self.frame_drawstyle = coin.SoDrawStyle() # make complete frame and it to shaded display mode separated_frame = coin.SoSeparator() separated_frame.addChild(self.frame_drawstyle) separated_frame.addChild(x_arrow) separated_frame.addChild(y_arrow) separated_frame.addChild(z_arrow) return separated_frame