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 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 _getClipplaneNode(self, viewprovider, make_if_missing = True): from pivy import coin sa = coin.SoSearchAction() sa.setType(coin.SoClipPlane.getClassTypeId()) sa.setName('TVClipPlane') sa.traverse(viewprovider.RootNode) if sa.isFound() and sa.getPath().getLength() == 1: return sa.getPath().getTail() elif not sa.isFound(): if not make_if_missing: return None clipplane = coin.SoClipPlane() viewprovider.RootNode.insertChild(clipplane, 0) clipplane.setName('TVClipPlane') clipplane.on.setValue(False) #make sure the plane is not activated by default return clipplane
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)