|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +__title__ = "Feature1" |
| 4 | +__author__ = "Christian Bergmann" |
| 5 | +__license__ = "LGPL 2.1" |
| 6 | +__doc__ = "An example for a workbench feature" |
| 7 | + |
| 8 | +import PythonWorkbenchTemplate |
| 9 | +import os |
| 10 | +import FreeCADGui |
| 11 | +import FreeCAD |
| 12 | +from FreeCAD import Vector |
| 13 | +import Part |
| 14 | + |
| 15 | + |
| 16 | +class Feature1Worker: |
| 17 | + def __init__(self, |
| 18 | + fp, # an instance of Part::FeaturePython |
| 19 | + base = None, |
| 20 | + green = False): |
| 21 | + fp.addProperty("App::PropertyLink", "Base", "Feature1", "This object will be modified by this feature").Base = base |
| 22 | + fp.addProperty("App::PropertyBool", "Green", "Feature1", "Colorize the feature green").Green = green |
| 23 | + |
| 24 | + fp.Proxy = self |
| 25 | + |
| 26 | + def execute(self, fp): |
| 27 | + '''Do something when doing a recomputation, this method is mandatory''' |
| 28 | + redrawFeature1(fp) |
| 29 | + |
| 30 | + def onChanged(self, fp, prop): |
| 31 | + '''Do something when a property has changed''' |
| 32 | + if prop == "Base": |
| 33 | + redrawFeature1(fp) |
| 34 | + |
| 35 | + if prop == "Green": |
| 36 | + changeFeature1Color(fp) |
| 37 | + |
| 38 | + |
| 39 | +class Feature1ViewProvider: |
| 40 | + def __init__(self, vobj): |
| 41 | + '''Set this object to the proxy object of the actual view provider''' |
| 42 | + vobj.Proxy = self |
| 43 | + self.Object = vobj.Object |
| 44 | + |
| 45 | + def getIcon(self): |
| 46 | + '''Return the icon which will appear in the tree view. This method is optional and if not defined a default icon is shown.''' |
| 47 | + return (os.path.join(PythonWorkbenchTemplate.get_module_path(), "Resources", "icons", "feature1.svg")) |
| 48 | + |
| 49 | + def attach(self, vobj): |
| 50 | + '''Setup the scene sub-graph of the view provider, this method is mandatory''' |
| 51 | + self.Object = vobj.Object |
| 52 | + self.onChanged(vobj, "Base") |
| 53 | + |
| 54 | + def updateData(self, fp, prop): |
| 55 | + '''If a property of the handled feature has changed we have the chance to handle this here''' |
| 56 | + pass |
| 57 | + |
| 58 | + def claimChildren(self): |
| 59 | + '''Return a list of objects that will be modified by this feature''' |
| 60 | + return [self.Object.Base] |
| 61 | + |
| 62 | + def onDelete(self, feature, subelements): |
| 63 | + '''Here we can do something when the feature will be deleted''' |
| 64 | + return True |
| 65 | + |
| 66 | + def onChanged(self, fp, prop): |
| 67 | + '''Here we can do something when a single property got changed''' |
| 68 | + pass |
| 69 | + |
| 70 | + def __getstate__(self): |
| 71 | + '''When saving the document this object gets stored using Python's json module.\ |
| 72 | + Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\ |
| 73 | + to return a tuple of all serializable objects or None.''' |
| 74 | + return None |
| 75 | + |
| 76 | + def __setstate__(self,state): |
| 77 | + '''When restoring the serialized object from document we have the chance to set some internals here.\ |
| 78 | + Since no data were serialized nothing needs to be done here.''' |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def redrawFeature1(fp): |
| 83 | + # check plausibility of all parameters |
| 84 | + if not fp.Base: |
| 85 | + return |
| 86 | + |
| 87 | + # fp.Shape contains the newly created object |
| 88 | + fp.Shape = fp.Base.Shape.copy() |
| 89 | + changeFeature1Color(fp) |
| 90 | + |
| 91 | + |
| 92 | +def changeFeature1Color(fp): |
| 93 | + if fp.Green: |
| 94 | + fp.ViewObject.ShapeColor = (0.00, 1.00, 0.00) |
| 95 | + else: |
| 96 | + fp.ViewObject.ShapeColor = (1.00, 0.00, 0.00) |
| 97 | + |
| 98 | + |
| 99 | +class Feature1(): |
| 100 | + '''This class will be loaded when the workbench is activated in FreeCAD. You must restart FreeCAD to apply changes in this class''' |
| 101 | + |
| 102 | + def Activated(self): |
| 103 | + '''Will be called when the feature is executed.''' |
| 104 | + # Generate commands in the FreeCAD python console to create Feature1 |
| 105 | + FreeCADGui.doCommand("import PythonWorkbenchTemplate") |
| 106 | + |
| 107 | + selection = FreeCADGui.Selection.getSelectionEx() |
| 108 | + if len(selection) > 0: |
| 109 | + FreeCADGui.doCommand("base = FreeCAD.ActiveDocument.getObject('%s')"%(selection[0].ObjectName)) |
| 110 | + FreeCADGui.doCommand("PythonWorkbenchTemplate.makeFeature1(base)") |
| 111 | + else: |
| 112 | + FreeCADGui.doCommand("PythonWorkbenchTemplate.makeFeature1()") |
| 113 | + |
| 114 | + |
| 115 | + def IsActive(self): |
| 116 | + """Here you can define if the command must be active or not (greyed) if certain conditions |
| 117 | + are met or not. This function is optional.""" |
| 118 | + if FreeCAD.ActiveDocument: |
| 119 | + return(True) |
| 120 | + else: |
| 121 | + return(False) |
| 122 | + |
| 123 | + def GetResources(self): |
| 124 | + '''Return the icon which will appear in the tree view. This method is optional and if not defined a default icon is shown.''' |
| 125 | + return {'Pixmap' : os.path.join(PythonWorkbenchTemplate.get_module_path(), "Resources", "icons", "feature1.svg"), |
| 126 | + 'Accel' : "", # a default shortcut (optional) |
| 127 | + 'MenuText': "Feature1", |
| 128 | + 'ToolTip' : "A template for a workbench feature" } |
| 129 | + |
| 130 | +FreeCADGui.addCommand('Feature1', Feature1()) |
0 commit comments