def reset_planes(self): '''put the clip planes to an initial position''' ids = self.root.ids ids['cp_green'].plane.setValue( coin.SbPlane(coin.SbVec3f(-1., 0., 0.), -200)) ids['cp_blue_b'].plane.setValue( coin.SbPlane(coin.SbVec3f(-1., 0., 0.), -140)) ids['cp_blue_a'].plane.setValue( coin.SbPlane(coin.SbVec3f(0., 1., 0.), -80))
def setup_clipping_plane(self, vobj): """Set-up the clipping plane of the 3dView. This method is called when the property CutView or the object Placement changes. """ sg = Gui.ActiveDocument.ActiveView.getSceneGraph() if self.clip: sg.removeChild(self.clip) self.clip = None '''for o in Draft.getGroupContents(vobj.Object.Objects,walls=True): if hasattr(o.ViewObject,"Lighting"): o.ViewObject.Lighting = "One side"''' # prefer keeping interior lighty self.clip = coin.SoClipPlane() self.clip.on.setValue(True) norm = vobj.Object.Proxy.getNormal(vobj.Object) mp = vobj.Object.Shape.CenterOfMass mp = DraftVecUtils.project(mp, norm) dist = mp.Length #- 0.1 # to not clip exactly on the section object norm = norm.negative() marg = 1 if hasattr(vobj, "CutMargin"): marg = vobj.CutMargin.Value if mp.getAngle(norm) > 1: dist += marg dist = -dist else: dist -= marg plane = coin.SbPlane(coin.SbVec3f(norm.x, norm.y, norm.z), dist) self.clip.plane.setValue(plane) sg.insertChild(self.clip, 0)
def _enableClipPlane(self, obj, enable, placement=None, offset=0.0): """Enables or disables clipping for an object. Placement specifies the plane (plane is placement's XY plane), and should be in global CS. Offset shifts the plane; positive offset reveals more material, negative offset hides more material.""" if not hasattr(obj, 'getGlobalPlacement'): print(" {obj} has no attribute 'getGlobalPlacement'".format( obj=obj.Name)) return #can't clip these yet... skip them for now. Example object: Draft Label node = self._getClipplaneNode(obj.ViewObject, make_if_missing=enable) if node is None: if enable: App.Console.PrintError( "TempoVis: failed to set clip plane to {obj}.\n".format( obj=obj.Name)) return if placement is not None: from pivy import coin plm_local = obj.getGlobalPlacement().multiply( obj.Placement.inverse()) # placement of CS the object is in plm_plane = plm_local.inverse().multiply(placement) normal = plm_plane.Rotation.multVec(App.Vector(0, 0, -1)) basepoint = plm_plane.Base + normal * (-offset) normal_coin = coin.SbVec3f(*tuple(normal)) basepoint_coin = coin.SbVec3f(*tuple(basepoint)) node.plane.setValue(coin.SbPlane(normal_coin, basepoint_coin)) node.on.setValue(enable)
def createClippingPlane(clip: ClippingPlane): """ Creates a clipping plane `clip` on the scene graph of FreeCADGui """ global clipPlanes if (clip is None or clip.direction is None or clip.location is None): return False sceneGraph = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph() # create plane that gets added to the scene graph normal = coin.SbVec3f(convertToFreeCADUnits(distance=clip.direction.x), convertToFreeCADUnits(distance=clip.direction.y), convertToFreeCADUnits(distance=clip.direction.z)) ref = coin.SbVec3f(convertToFreeCADUnits(distance=clip.location.x), convertToFreeCADUnits(distance=clip.location.y), convertToFreeCADUnits(distance=clip.location.z)) plane = coin.SbPlane(normal, ref) clipplane = coin.SoClipPlane() clipplane.plane.setValue(plane) # add the clipping plane to the scenegraph sceneGraph.insertChild(clipplane, 0) clipPlanes.append(clipplane) return True
def run_blue_a(self): '''run animation''' ids = self.root.ids for i in range(-40, 20): ids['cp_blue_a'].plane.setValue( coin.SbPlane(coin.SbVec3f(0., 1., 0.), i * 2)) Gui.updateGui() time.sleep(0.01)
def run_green(self): '''run animation green''' ids = self.root.ids for i in range(-40, 14): ids['cp_green'].plane.setValue( coin.SbPlane(coin.SbVec3f(-1., 0., 0.), i * 5)) Gui.updateGui() time.sleep(0.01)
def apply_data(self, val): enable, pldef = val vp = self.doc.getObject(self.objname).ViewObject cp = getClipPlaneNode(vp, make_if_missing= True if enable else False) if cp is None and not enable: return if enable: from pivy import coin v, d = pldef cp.plane.setValue(coin.SbPlane(coin.SbVec3f(*v), d)) cp.on.setValue(enable)
def getPoint(pX, pY): render_manager = Gui.ActiveDocument.ActiveView.getViewer( ).getSoRenderManager() cam = render_manager.getCamera() ws = render_manager.getWindowSize().getValue() ar = 1.0 * ws[0] / ws[1] vol = cam.getViewVolume() line = coin.SbLine(*vol.projectPointToLine(coin.SbVec2f(pX * ar - 0.5, pY))) normal = coin.SbVec3f(0, 0, 1) center = coin.SbVec3f(0, 0, 0) plane = coin.SbPlane(normal, center) point = plane.intersect(line) return (FreeCAD.Vector(point.getValue()))
def main(): app = QtWidgets.QApplication(sys.argv) viewer = quarter.QuarterWidget() # build a scene (sphere, cube) plane = coin.SbPlane(coin.SbVec3f(0, 0, 1), coin.SbVec3f(0, 0, 0)) root = coin.SoSeparator() myCallback = coin.SoCallback() cap = CapPlane(plane, root) myCallback.setCallback(myCallbackRoutine, cap) root += myCallback, coin.SoSphere() viewer.setSceneGraph(root) viewer.setBackgroundColor(coin.SbColor(.5, .5, .5)) viewer.setWindowTitle("GL stencil buffer") viewer.show() sys.exit(app.exec_())
def get_point_on_screen(render_manager, screen_pos, normal="camera", point=None): """get coordinates from pixel position""" pCam = render_manager.getCamera() vol = pCam.getViewVolume() point = point or coin.SbVec3f(0, 0, 0) if normal == "camera": plane = vol.getPlane(10) normal = plane.getNormal() elif normal == "x": normal = coin.SbVec3f(1, 0, 0) elif normal == "y": normal = coin.SbVec3f(0, 1, 0) elif normal == "z": normal = coin.SbVec3f(0, 0, 1) normal.normalize() x, y = screen_pos vp = render_manager.getViewportRegion() size = vp.getViewportSize() dX, dY = size fRatio = vp.getViewportAspectRatio() pX = float(x) / float(vp.getViewportSizePixels()[0]) pY = float(y) / float(vp.getViewportSizePixels()[1]) if (fRatio > 1.0): pX = (pX - 0.5 * dX) * fRatio + 0.5 * dX elif (fRatio < 1.0): pY = (pY - 0.5 * dY) / fRatio + 0.5 * dY plane = coin.SbPlane(normal, point) line = coin.SbLine(*vol.projectPointToLine(coin.SbVec2f(pX, pY))) pt = plane.intersect(line) return pt
def onChanged(self, vobj, prop): #print(vobj.Object.Label," - ",prop) if prop == "ShapeColor": if hasattr(vobj, "ShapeColor"): l = vobj.ShapeColor self.mat.diffuseColor.setValue([l[0], l[1], l[2]]) elif prop == "LineWidth": if hasattr(vobj, "LineWidth"): self.dst.lineWidth = vobj.LineWidth elif prop == "FontName": if hasattr(vobj, "FontName"): if vobj.FontName: if sys.version_info.major < 3: self.fon.name = vobj.FontName.encode("utf8") else: self.fon.name = vobj.FontName elif prop in ["FontSize", "DisplayOffset", "OriginOffset"]: if hasattr(vobj, "FontSize") and hasattr( vobj, "DisplayOffset") and hasattr(vobj, "OriginOffset"): fs = vobj.FontSize.Value if fs: self.fon.size = fs b = vobj.DisplayOffset.Base self.tra.translation.setValue( [b.x + fs / 8, b.y, b.z + fs / 8]) r = vobj.DisplayOffset.Rotation self.tra.rotation.setValue(r.Q) if vobj.OriginOffset: self.lco.point.setValues([[b.x - fs, b.y, b.z], [b.x + fs, b.y, b.z], [b.x, b.y - fs, b.z], [b.x, b.y + fs, b.z], [b.x, b.y, b.z - fs], [b.x, b.y, b.z + fs]]) else: self.lco.point.setValues([[-fs, 0, 0], [fs, 0, 0], [0, -fs, 0], [0, fs, 0], [0, 0, -fs], [0, 0, fs]]) elif prop in ["OverrideUnit", "ShowUnit", "ShowLevel", "ShowLabel"]: if hasattr(vobj, "OverrideUnit") and hasattr( vobj, "ShowUnit") and hasattr( vobj, "ShowLevel") and hasattr(vobj, "ShowLabel"): z = vobj.Object.Placement.Base.z + vobj.Object.LevelOffset.Value q = FreeCAD.Units.Quantity(z, FreeCAD.Units.Length) txt = "" if vobj.ShowLabel: txt += vobj.Object.Label if vobj.ShowLevel: if txt: txt += " " if z >= 0: txt += "+" if vobj.OverrideUnit: u = vobj.OverrideUnit else: u = q.getUserPreferred()[2] try: q = q.getValueAs(u) except: q = q.getValueAs(q.getUserPreferred()[2]) d = FreeCAD.ParamGet( "User parameter:BaseApp/Preferences/Units").GetInt( "Decimals", 0) fmt = "{0:." + str(d) + "f}" if not vobj.ShowUnit: u = "" txt += fmt.format(float(q)) + str(u) if not txt: txt = " " # empty texts make coin crash... if isinstance(txt, unicode): txt = txt.encode("utf8") self.txt.string.setValue(txt) elif prop in [ "ChildrenOverride", "ChildenLineWidth", "ChildrenLineColor", "ChildrenShapeColor", "ChildrenTransparency" ]: if hasattr(vobj, "ChildrenOverride") and vobj.ChildrenOverride: props = [ "ChildenLineWidth", "ChildrenLineColor", "ChildrenShapeColor", "ChildrenTransparency" ] for child in vobj.Object.Group: for prop in props: if hasattr(vobj, prop) and hasattr( child.ViewObject, prop[8:]) and not hasattr( child, "ChildrenOverride"): setattr(child.ViewObject, prop[8:], getattr(vobj, prop)) elif prop in ["CutView", "CutMargin"]: if hasattr(vobj, "CutView") and FreeCADGui.ActiveDocument.ActiveView: sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph() if vobj.CutView: from pivy import coin if self.clip: sg.removeChild(self.clip) self.clip = None for o in Draft.getGroupContents(vobj.Object.Group, walls=True): if hasattr(o.ViewObject, "Lighting"): o.ViewObject.Lighting = "One side" self.clip = coin.SoClipPlane() self.clip.on.setValue(True) norm = vobj.Object.Placement.multVec( FreeCAD.Vector(0, 0, 1)) mp = vobj.Object.Placement.Base mp = DraftVecUtils.project(mp, norm) dist = mp.Length #- 0.1 # to not clip exactly on the section object norm = norm.negative() marg = 1 if hasattr(vobj, "CutMargin"): marg = vobj.CutMargin.Value if mp.getAngle(norm) > 1: dist += marg dist = -dist else: dist -= marg plane = coin.SbPlane(coin.SbVec3f(norm.x, norm.y, norm.z), dist) self.clip.plane.setValue(plane) sg.insertChild(self.clip, 0) else: if self.clip: sg.removeChild(self.clip) self.clip = None for o in Draft.getGroupContents(vobj.Object.Group, walls=True): if hasattr(o.ViewObject, "Lighting"): o.ViewObject.Lighting = "Two side" elif prop == "Visibility": # turn clipping off when turning the object off if hasattr(vobj, "Visibility") and not (vobj.Visibility) and hasattr( vobj, "CutView"): vobj.CutView = False elif prop == "SaveInventor": self.writeInventor(vobj.Object)
def onChanged(self, vobj, prop): if prop == "LineColor": l = vobj.LineColor self.mat1.diffuseColor.setValue([l[0], l[1], l[2]]) self.mat2.diffuseColor.setValue([l[0], l[1], l[2]]) elif prop == "Transparency": if hasattr(vobj, "Transparency"): self.mat2.transparency.setValue(vobj.Transparency / 100.0) elif prop in ["DisplayLength", "DisplayHeight", "ArrowSize"]: if hasattr(vobj, "DisplayLength"): ld = vobj.DisplayLength.Value / 2 hd = vobj.DisplayHeight.Value / 2 elif hasattr(vobj, "DisplaySize"): # old objects ld = vobj.DisplaySize.Value / 2 hd = vobj.DisplaySize.Value / 2 else: ld = 1 hd = 1 verts = [] fverts = [] for v in [[-ld, -hd], [ld, -hd], [ld, hd], [-ld, hd]]: if hasattr(vobj, "ArrowSize"): l1 = vobj.ArrowSize.Value if vobj.ArrowSize.Value > 0 else 0.1 else: l1 = 0.1 l2 = l1 / 3 pl = FreeCAD.Placement(vobj.Object.Placement) p1 = pl.multVec(Vector(v[0], v[1], 0)) p2 = pl.multVec(Vector(v[0], v[1], -l1)) p3 = pl.multVec(Vector(v[0] - l2, v[1], -l1 + l2)) p4 = pl.multVec(Vector(v[0] + l2, v[1], -l1 + l2)) p5 = pl.multVec(Vector(v[0], v[1] - l2, -l1 + l2)) p6 = pl.multVec(Vector(v[0], v[1] + l2, -l1 + l2)) verts.extend([[p1.x, p1.y, p1.z], [p2.x, p2.y, p2.z]]) fverts.append([p1.x, p1.y, p1.z]) verts.extend([[p2.x, p2.y, p2.z], [p3.x, p3.y, p3.z], [p4.x, p4.y, p4.z], [p2.x, p2.y, p2.z]]) verts.extend([[p2.x, p2.y, p2.z], [p5.x, p5.y, p5.z], [p6.x, p6.y, p6.z], [p2.x, p2.y, p2.z]]) verts.extend(fverts + [fverts[0]]) self.lcoords.point.setValues(verts) self.fcoords.point.setValues(fverts) elif prop == "LineWidth": self.drawstyle.lineWidth = vobj.LineWidth elif prop == "CutView": if hasattr(vobj, "CutView") and FreeCADGui.ActiveDocument.ActiveView: sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph() if vobj.CutView: if self.clip: sg.removeChild(self.clip) self.clip = None for o in Draft.getGroupContents(vobj.Object.Objects, walls=True): if hasattr(o.ViewObject, "Lighting"): o.ViewObject.Lighting = "One side" self.clip = coin.SoClipPlane() self.clip.on.setValue(True) norm = vobj.Object.Proxy.getNormal(vobj.Object) mp = vobj.Object.Shape.CenterOfMass mp = DraftVecUtils.project(mp, norm) dist = mp.Length #- 0.1 # to not clip exactly on the section object norm = norm.negative() if mp.getAngle(norm) > 1: dist += 1 dist = -dist else: dist -= 0.1 plane = coin.SbPlane(coin.SbVec3f(norm.x, norm.y, norm.z), dist) self.clip.plane.setValue(plane) sg.insertChild(self.clip, 0) else: if self.clip: sg.removeChild(self.clip) self.clip = None return
def main(self): controller = Leap.Controller() # Current orientation r0 = FreeCAD.Gui.activeView().getCameraNode().orientation.getValue( ).getValue() # Initial variables prev = controller.frame() first = True rt = [] # pt = [] clip = coin.SoClipPlane() clipping = False # Loop while self.running: frame = controller.frame() if frame.id > prev.id: if len(frame.hands) > 0: # we need a hand hand = frame.hands[0] if len(frame.hands) > 1: for h in frame.hands: if h.is_left: if h.id < hand.id: hand = h else: # is_right if hand.is_right and h.id < hand.id: hand = h if hand.grab_strength > STRENGTH: if first: # Init first = False r0 = FreeCAD.Gui.activeView().getCameraNode( ).orientation.getValue().getValue() # p0 = FreeCAD.Gui.activeView().getCameraNode().position.getValue().getValue() # Basis 1 d1 = FreeCAD.Vector(hand.direction) n1 = FreeCAD.Vector(hand.palm_normal) # p1 = FreeCAD.Vector(hand.palm_position) else: # Basis 2 d2 = FreeCAD.Vector(hand.direction) n2 = FreeCAD.Vector(hand.palm_normal) # p2 = FreeCAD.Vector(hand.palm_position) # Previous rt0 = rt # pt0 = pt # Rotation r1 = FreeCAD.Rotation(d1, d2) nt = r1.multVec(n1) r2 = FreeCAD.Rotation(nt, n2) rtni = r2.multiply(r1) rt = rtni.inverted() # Translation # pt = FreeCAD.Vector(p0) - YOLO * FreeCAD.Rotation(r0[0],r0[1],r0[2],r0[3]).multiply(rt).multVec(p2 - p1) # Filter noise try: rtf = coin.SbRotation.slerp(rt0, rt, 0.5) # ptf = 1/2 * (pt0 + pt) except: rtf = rt # ptf = pt # FreeCAD FreeCAD.Gui.activeView().getCameraNode( ).orientation = coin.SbRotation( rtf.Q) * coin.SbRotation(r0) # FreeCAD.Gui.activeView().getCameraNode().position = coin.SbVec3f(ptf) # FreeCAD.Gui.SendMsgToActiveView("ViewFit") else: if not first: # first == False first = True FreeCAD.Gui.SendMsgToActiveView("ViewFit") if FixedVector(hand.palm_velocity).magnitude( ) > HISPEED and hand.grab_strength < WEAKNESS: # Fruit Ninja Mode if not clipping: FreeCAD.Console.PrintMessage( "Caution: Entering Fruit Ninja Mode\n") r0 = FreeCAD.Gui.activeView().getCameraNode( ).orientation.getValue().getValue() direction = coin.SbRotation(r0).multVec( coin.SbVec3f(hand.palm_normal.to_tuple())) clip.plane.setValue( coin.SbPlane(direction, OFFSET) ) # set this to control the clipping plane FreeCAD.Gui.activeView().getSceneGraph( ).insertChild(clip, 0) clipping = True else: FreeCAD.Console.PrintMessage( "Back to Safety\n") FreeCAD.Gui.activeView().getSceneGraph( ).removeChild(clip) clipping = False time.sleep(4 * DELAY) else: if not first: # first == False first = True FreeCAD.Gui.SendMsgToActiveView("ViewFit") # Update variables prev = frame # Refresh UI FreeCAD.Gui.updateGui() time.sleep(DELAY) FreeCAD.Gui.activeView().getSceneGraph().removeChild(clip)