Created
May 14, 2020 08:48
-
-
Save tomkcook/f614099d626dae9c6e53a7a69d258335 to your computer and use it in GitHub Desktop.
Test code for FreeCAD highlighting and selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Molecule: | |
def __init__(self, obj): | |
''' Add two point properties ''' | |
obj.addProperty("App::PropertyVector","p1","Line","Start point") | |
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(5,0,0) | |
obj.Proxy = self | |
def onChanged(self, fp, prop): | |
pass | |
def execute(self, fp): | |
''' Print a short message when doing a recomputation, this method is mandatory ''' | |
pass | |
class ViewProviderMolecule: | |
def __init__(self, obj): | |
''' Set this object to the proxy object of the actual view provider ''' | |
self.constructed = False | |
obj.Proxy = self | |
self.ViewObject = obj | |
def attach(self, obj): | |
material = coin.SoMaterial() | |
material.diffuseColor = (1.0, 0.0, 0.0) | |
drawStyle = coin.SoDrawStyle() | |
drawStyle.pointSize.setValue(10) | |
drawStyle.style = coin.SoDrawStyle.LINES | |
self.coords = coin.SoCoordinate3() | |
self.coords.point.setValues(0, 2, [FreeCAD.Vector(0, 0, 0), FreeCAD.Vector(1, 0, 0)]) | |
obj.RootNode += self.coords | |
obj.RootNode += drawStyle | |
obj.RootNode += material | |
sep1=coin.SoSeparator() | |
sel1 = coin.SoType.fromName('SoBrepIndexedPointSet').createInstance() | |
# sel1.policy.setValue(coin.SoSelection.SHIFT) | |
sel1.ref() | |
sep1.addChild(sel1) | |
sel1.coordIndex.set1Value(0, 0) | |
sep2=coin.SoSeparator() | |
sel2 = coin.SoType.fromName('SoBrepIndexedPointSet').createInstance() | |
sel2.ref() | |
sep2.addChild(sel2) | |
sel2.coordIndex.set1Value(0, 1) | |
sep3=coin.SoSeparator() | |
sel3 = coin.SoType.fromName('SoBrepEdgeSet').createInstance() | |
sel3.ref() | |
sep3 += sel3 | |
sel3.coordIndex.setValues(0, 2, [0, 1]) | |
obj.RootNode.addChild(sep1) | |
obj.RootNode.addChild(sep2) | |
obj.RootNode.addChild(sep3) | |
self.updateData(obj.Object, 'p2') | |
self.sel1 = sel1 | |
self.sel2 = sel2 | |
self.sel3 = sel3 | |
self.constructed = True | |
def getDetailPath(self, subname, path, append): | |
vobj = self.ViewObject | |
if append: | |
path.append(vobj.RootNode) | |
path.append(vobj.SwitchNode) | |
mode = vobj.SwitchNode.whichChild.getValue() | |
if mode >= 0: | |
mode = vobj.SwitchNode.getChild(mode) | |
path.append(mode) | |
sub = Part.splitSubname(subname)[-1] | |
if sub == 'Atom1': | |
path.append(self.sel1) | |
elif sub == 'Atom2': | |
path.append(self.sel2) | |
elif sub == 'Line': | |
path.append(self.sel3) | |
else: | |
path.append(mode.getChild(0)) | |
return True | |
def getElementPicked(self, pp): | |
path = pp.getPath() | |
if path.findNode(self.sel1) >= 0: | |
return 'Atom1' | |
if path.findNode(self.sel2) >= 0: | |
return 'Atom2' | |
if path.findNode(self.sel3) >= 0: | |
return 'Line' | |
raise NotImplementedError | |
def updateData(self, fp, prop): | |
"If a property of the handled feature has changed we have the chance to handle this here" | |
# fp is the handled feature, prop is the name of the property that has changed | |
if not self.constructed: | |
return | |
if prop == "p1": | |
p = fp.getPropertyByName("p1") | |
self.coords.point.set1Value(0, p) | |
elif prop == "p2": | |
p = fp.getPropertyByName("p2") | |
self.coords.point.set1Value(1, p) | |
def getDisplayModes(self, obj): | |
return ['Shaded'] | |
def getDefaultDisplayMode(self): | |
return 'Shaded' | |
def setDisplayMode(self, mode): | |
return mode | |
def __getstate__(self): | |
return None | |
def __setstate__(self,state): | |
return None | |
def makeMolecule(): | |
FreeCAD.newDocument() | |
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Molecule") | |
Molecule(a) | |
ViewProviderMolecule(a.ViewObject) | |
FreeCAD.ActiveDocument.recompute() | |
makeMolecule() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment