def shape2shape(name, geom): ''' Convert from gegede shape to a SoShape ''' obj = geom.get_shape(name) if type(obj).__name__ == 'Box': box = coin.SoCube() box.width = 2.0 * obj.dx.to(spatial_units).magnitude box.height = 2.0 * obj.dy.to(spatial_units).magnitude box.depth = 2.0 * obj.dz.to(spatial_units).magnitude return box if type(obj).__name__ == 'Tubs': tub = coin.SoCylinder() tub.radius = obj.rmax.to(spatial_units).magnitude tub.height = 0.5 * obj.dz.to(spatial_units).magnitude # fixme: no built-in support of rmin, nor start/stop angles - need to make boolean return tub if type(obj).__name__ == 'Sphere': sph = coin.SoSphere() sph.radius = obj.rmax.to(spatial_units).magnitude # fixme: no built-in support of rmin, nor start/stop angles - need to make boolean return sph if type(obj).__name__ == 'Boolean': print( 'WARNING: Boolean shapes are not supported, simply using the first shape for %s' % obj.name) return shape2shape(obj.first, geom) raise ValueError('Unsupported shape for scenegraph: "%s"' % type(obj).__name__)
def make_mother_daughter(): ''' Make a cube with two cylinders inside. ''' tube_shape = coin.SoCylinder() tube_shape.radius = 0.25 tube_shape.height = 1.5 tube_mat = coin.SoMaterial() tube_mat.ambientColor = (.33, .22, .27) tube_mat.diffuseColor = (.78, .57, .11) tube_mat.specularColor = (.99, .94, .81) tube_mat.shininess = .28 tube_mat.transparency = 0.5 tube_lv = coin.SoSeparator() tube_lv.addChild(tube_mat) tube_lv.addChild(tube_shape) tube_tr1 = coin.SoTransform() tube_tr1.translation = (+0.75, 0, 0) tube_tr2 = coin.SoTransform() tube_tr2.translation = (-0.75, 0, 0) tube_pv1 = coin.SoSeparator() tube_pv1.addChild(tube_tr1) tube_pv1.addChild(tube_lv) tube_pv2 = coin.SoSeparator() tube_pv2.addChild(tube_tr2) tube_pv2.addChild(tube_lv) cube_lv = coin.SoSeparator() cube_lv.addChild(tube_pv1) cube_lv.addChild(tube_pv2) cube_mat = coin.SoMaterial() cube_mat.ambientColor = (.2, .2, .2) cube_mat.diffuseColor = (.6, .6, .6) cube_mat.specularColor = (.5, .5, .5) cube_mat.shininess = .5 cube_shape = coin.SoCube() cube_shape.width = 3.0 # x cube_shape.height = 2.0 # y cube_shape.depth = 1.0 # z, out of screen cube_lv.addChild(cube_mat) cube_lv.addChild(cube_shape) return cube_lv
def createABar(self, _vector, _color, _length, _rotation): color = coin.SoBaseColor() transform = coin.SoTransform() tempR = coin.SbVec3f() tempR.setValue(_rotation[0], _rotation[1], _rotation[2]) transform.rotation.setValue(tempR, math.radians(_rotation[3])) color.rgb = _color cylinder = coin.SoCylinder() cylinder.height = _length cylinder.radius = self.barRadius transBar = coin.SoTranslation() transBar.translation.setValue(_vector) barLine = coin.SoSeparator() barLine.addChild(color) barLine.addChild(transBar) barLine.addChild(transform) barLine.addChild(cylinder) return barLine
def draw_AllParts(vec=App.Vector(0, 0, 0), Ptype: str = "", _color=FR_COLOR.FR_RED, setupRotation=[0, 0, 0, 0], _rotation=[0, 0, 0, 0], _Scale=[1, 1, 1], LineWidth=1): style = coin.SoDrawStyle() style.lineWidth = LineWidth objectToDraw = coin.SoSeparator() theObject = coin.SoCylinder() transtheObject = coin.SoTransform() # theObject cylinder tempC = coin.SbVec3f() if Ptype == "Center": objectToDraw.Name = "Center" tempC.setValue(1, 0, 0) transtheObject.rotation.setValue(tempC, math.radians(90)) theObject.radius = 4.5 theObject.height = 0.65 col1 = coin.SoBaseColor() # must be converted to SoBaseColor col1.rgb = FR_COLOR.FR_GLASS elif Ptype == "Xaxis": objectToDraw.Name = "Xaxis" tempC.setValue(0, 0, 1) transtheObject.rotation.setValue(tempC, math.radians(90)) theObject.radius = 0.25 theObject.height = 20 col1 = coin.SoBaseColor() # must be converted to SoBaseColor col1.rgb = FR_COLOR.FR_RED elif Ptype == "Yaxis": objectToDraw.Name = "Yaxis" tempC.setValue(0, 0, 1) transtheObject.rotation.setValue(tempC, math.radians(0)) theObject.radius = 0.25 theObject.height = 20 col1 = coin.SoBaseColor() # must be converted to SoBaseColor col1.rgb = FR_COLOR.FR_GREENYELLOW elif Ptype == "45axis": objectToDraw.Name = "45axis" tempC.setValue(0, 0, 1) transtheObject.rotation.setValue(tempC, math.radians(45)) theObject.radius = 0.25 theObject.height = 14 col1 = coin.SoBaseColor() # must be converted to SoBaseColor col1.rgb = FR_COLOR.FR_BLUEVIOLET elif Ptype == "135axis": objectToDraw.Name = "135Axis" tempC.setValue(0, 0, 1) transtheObject.rotation.setValue(tempC, math.radians(135)) theObject.radius = 0.25 theObject.height = 14 col1 = coin.SoBaseColor() # must be converted to SoBaseColor col1.rgb = FR_COLOR.FR_ORANGE objectToDraw.addChild(col1) objectToDraw.addChild(transtheObject) objectToDraw.addChild(theObject) tRadiusX = coin.SbVec3f() tRadiusY = coin.SbVec3f() tRadiusZ = coin.SbVec3f() tRadiusX.setValue(1, 0, 0) tRadiusY.setValue(0, 1, 0) tRadiusZ.setValue(0, 0, 1) tempTransform_x = coin.SoTransform() tempTransform_y = coin.SoTransform() tempTransform_z = coin.SoTransform() tempTransform_x.rotation.setValue(tRadiusX, math.radians(setupRotation[0])) tempTransform_y.rotation.setValue(tRadiusY, math.radians(setupRotation[1])) tempTransform_z.rotation.setValue(tRadiusZ, math.radians(setupRotation[2])) SoSeparatorSetupX = coin.SoSeparator() SoSeparatorSetupY = coin.SoSeparator() SoSeparatorSetupZ = coin.SoSeparator() SoSeparatorSetupX.addChild(tempTransform_x) SoSeparatorSetupX.addChild(objectToDraw) SoSeparatorSetupY.addChild(tempTransform_y) SoSeparatorSetupY.addChild(SoSeparatorSetupX) SoSeparatorSetupZ.addChild(tempTransform_z) 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) root = coin.SoSeparator() root.Name = "RootAxisDrawing" transla = coin.SoTranslation() transla.translation.setValue([vec.x, vec.y, vec.z]) material = coin.SoMaterial() if (Ptype == "Center"): material.transparency.setValue(0.80) else: material.transparency.setValue(0.0) material.diffuseColor.setValue(_color) material.emissiveColor.setValue(_color) root.addChild(transla) root.addChild(rootTransform) #root.addChild(material) root.addChild(SoSeparatorSetupZ) return root
def navi(): '''navigator startup''' mw = QtGui.QApplication #widget.setCursor(QtCore.Qt.SizeAllCursor) #cursor ausblenden #mw.setOverrideCursor(QtCore.Qt.BlankCursor) # FreeCADGui.activateWorkbench("NoneWorkbench") mw.setOverrideCursor(QtCore.Qt.PointingHandCursor) ef = EventFilter() ef.laenge = 0.0 ef.breite = 0.0 ef.campos = FreeCAD.Vector(0, 0, 20000) # ef.output.hide() ef.mouseMode = False ef.firstCall = True ef.mode = "turn" ef.navi = myNavigatorWidget(ef) ef.speed = 100 ef.direction = 0.5 * math.pi ef.roll = 0 #-------------- # get a jpg filename # jpgfilename = QtGui.QFileDialog.getOpenFileName(QtGui.qApp.activeWindow(),'Open image file','*.jpg') fn = '/home/microelly2/FCB/b175_camera_controller/winter.jpg' fn = os.path.dirname(__file__) + "/../pics/winter.jpg" sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph() col = coin.SoBaseColor() #col.rgb=(1,0,0) trans = coin.SoTranslation() trans.translation.setValue([0, 0, 0]) myCustomNode = coin.SoSeparator() #myCustomNode.addChild(col) if 0 or False: cub = coin.SoCylinder() cub.radius.setValue(3000) cub.height.setValue(4000) cub.parts.set("SIDES") s = coin.SoRotationXYZ() s.angle.setValue(1.5708) s.axis.setValue(0) myCustomNode.addChild(s) s = coin.SoRotationXYZ() s.angle.setValue(math.pi) s.axis.setValue(1) myCustomNode.addChild(s) else: cub = coin.SoSphere() cub.radius.setValue(10000000) s = coin.SoRotationXYZ() s.angle.setValue(1.5708) s.axis.setValue(0) myCustomNode.addChild(s) s = coin.SoRotationXYZ() s.angle.setValue(math.pi) s.axis.setValue(1) myCustomNode.addChild(s) if False: l = coin.SoDirectionalLight() l.direction.setValue(coin.SbVec3f(0, 1, 0)) l.color.setValue(coin.SbColor(0, 0, 1)) myCustomNode.addChild(l) l = coin.SoDirectionalLight() l.direction.setValue(coin.SbVec3f(0, -1, 0)) l.color.setValue(coin.SbColor(0, 1, 1)) myCustomNode.addChild(l) l = coin.SoDirectionalLight() l.direction.setValue(coin.SbVec3f(0, 0, 1)) l.color.setValue(coin.SbColor(1, 0, 0)) myCustomNode.addChild(l) l = coin.SoDirectionalLight() l.direction.setValue(coin.SbVec3f(0, 0, -1)) l.color.setValue(coin.SbColor(0.6, 0.6, 1)) myCustomNode.addChild(l) l = coin.SoSpotLight() l.direction.setValue(coin.SbVec3f(1, 0, 1)) l.color.setValue(coin.SbColor(0, 1, 0)) l.location.setValue(coin.SbVec3f(0, 0, 0)) # l.cutOffAngle.setValue(0.01) # l.dropOffRate.setValue(1) myCustomNode.addChild(l) #myCustomNode.addChild(trans) myCustomNode.addChild(cub) sg.addChild(myCustomNode) tex = coin.SoTexture2() tex.filename = fn myCustomNode.insertChild(tex, 0) #--------------- ef.background = myCustomNode ef.tex = tex FreeCAD.eventfilter = ef mw.installEventFilter(ef) FreeCAD.eventfilter.on_key_press = on_keypress2 FreeCAD.eventfilter.on_move = on_move3 FreeCAD.eventfilter.on_clicks = on_clicks3 FreeCAD.eventfilter.on_windowslist = on_windowslist2 on_keypress2(FreeCAD.eventfilter, 'O') view = FreeCADGui.activeDocument().activeView() FreeCADGui.ActiveDocument.ActiveView.setAnimationEnabled(False) mgr = view.getViewer().getSoRenderManager() mgr.setAutoClipping(0) FreeCAD.ActiveDocument.recompute() FreeCADGui.updateGui() return ef
def attach(self, vobj): r"""Setup the scene sub-graph of the view provider, this method is mandatory See Also -------- https://www.freecadweb.org/wiki/Scripted_objects """ debug("AnchorViewProvider/attach") self.shaded = coin.SoSeparator() self.wireframe = coin.SoSeparator() # group u cone and cylinder self.group_u = coin.SoSeparator() self.color_u = coin.SoBaseColor() self.group_cyl_u = coin.SoSeparator() self.transform_cyl_u = coin.SoTransform() self.group_cone_u = coin.SoSeparator() self.transform_cone_u = coin.SoTransform() # group v cone and cylinder self.group_v = coin.SoSeparator() self.transform_v = coin.SoTransform() self.color_v = coin.SoBaseColor() self.group_cyl_v = coin.SoSeparator() self.transform_cyl_v = coin.SoTransform() self.group_cone_v = coin.SoSeparator() self.transform_cone_v = coin.SoTransform() # global self.scale = coin.SoScale() self.scale.scaleFactor.setValue(1., 1., 1.) self.transform = coin.SoTransform() # arrow dimensions self.arrow_length = 1 cone_cyl_ratio = 0.2 cyl_radius_ratio = 0.05 cone_base_radius_ratio = 0.1 # The cylinder is created from its middle at the origin # -> compensation self.transform_cyl_u.translation.setValue( (0., self.arrow_length * (1 - cone_cyl_ratio) / 2, 0.)) self.transform_cone_u.translation.setValue( (0., self.arrow_length * (1 - cone_cyl_ratio) + self.arrow_length * cone_cyl_ratio / 2, 0.)) self.transform_cyl_v.translation.setValue( (0., self.arrow_length * (1 - cone_cyl_ratio) / 2, 0.)) self.transform_cone_v.translation.setValue( (0., self.arrow_length * (1 - cone_cyl_ratio) + self.arrow_length * cone_cyl_ratio / 2, 0.)) # put v at 90 degrees self.transform_v.center.setValue((0, 0, 0)) self.transform_v.rotation.setValue(coin.SbVec3f((1, 0, 0)), math.pi / 2) # Cone and cylinder creation from dimensions cone_u = coin.SoCone() cone_u.height.setValue(self.arrow_length * cone_cyl_ratio) cone_u.bottomRadius.setValue(self.arrow_length * cone_base_radius_ratio) cylinder_u = coin.SoCylinder() cylinder_u.radius.setValue(self.arrow_length * cyl_radius_ratio) cylinder_u.height.setValue(self.arrow_length * (1 - cone_cyl_ratio)) cone_v = coin.SoCone() cone_v.height.setValue(self.arrow_length * cone_cyl_ratio) cone_v.bottomRadius.setValue(self.arrow_length * cone_base_radius_ratio) cylinder_v = coin.SoCylinder() cylinder_v.radius.setValue(self.arrow_length * cyl_radius_ratio) cylinder_v.height.setValue(self.arrow_length * (1 - cone_cyl_ratio)) # group_cyl_u self.group_cyl_u.addChild(self.transform_cyl_u) self.group_cyl_u.addChild(cylinder_u) # group_cone_u self.group_cone_u.addChild(self.transform_cone_u) self.group_cone_u.addChild(cone_u) # group_u self.group_u.addChild(self.color_u) self.group_u.addChild(self.group_cyl_u) self.group_u.addChild(self.group_cone_u) # group_cyl_v self.group_cyl_v.addChild(self.transform_cyl_v) self.group_cyl_v.addChild(cylinder_v) # group_cone_v self.group_cone_v.addChild(self.transform_cone_v) self.group_cone_v.addChild(cone_v) # group_v self.group_v.addChild(self.transform_v) self.group_v.addChild(self.color_v) self.group_v.addChild(self.group_cyl_v) self.group_v.addChild(self.group_cone_v) # ** shaded ** self.shaded.addChild(self.transform) self.shaded.addChild(self.scale) self.shaded.addChild(self.group_u) self.shaded.addChild(self.group_v) vobj.addDisplayMode(self.shaded, "Shaded") # ** wireframe ** style = coin.SoDrawStyle() style.style = coin.SoDrawStyle.LINES self.wireframe.addChild(style) self.wireframe.addChild(self.transform) self.wireframe.addChild(self.scale) self.wireframe.addChild(self.group_u) self.wireframe.addChild(self.group_v) vobj.addDisplayMode(self.wireframe, "Wireframe") self.onChanged(vobj, "ColorU") self.onChanged(vobj, "ColorV")