5
5
__license__ = "LGPL 2.1"
6
6
__doc__ = "An example for a workbench feature"
7
7
8
- import PythonWorkbenchTemplate
9
8
import os
10
9
import FreeCADGui
11
10
import FreeCAD
12
- from FreeCAD import Vector
13
- import Part
14
11
12
+ __dir__ = os .path .dirname (__file__ )
13
+ __iconpath__ = os .path .join (__dir__ , 'feature1.svg' )
15
14
16
15
class Feature1Worker :
17
16
def __init__ (self ,
@@ -44,7 +43,7 @@ def __init__(self, vobj):
44
43
45
44
def getIcon (self ):
46
45
'''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" ))
46
+ return __iconpath__
48
47
49
48
def attach (self , vobj ):
50
49
'''Setup the scene sub-graph of the view provider, this method is mandatory'''
@@ -67,29 +66,79 @@ def onChanged(self, fp, prop):
67
66
'''Here we can do something when a single property got changed'''
68
67
pass
69
68
69
+ def setEdit (self , vobj = None , mode = 0 ):
70
+ '''Enter edit mode when double clicking onto the feature. Optional.'''
71
+ # Create a task panel UI
72
+ self .panel = Feature1TaskPanel (self .Object )
73
+ FreeCADGui .Control .showDialog (self .panel )
74
+ return True
75
+
70
76
def __getstate__ (self ):
71
77
'''When saving the document this object gets stored using Python's json module.\
72
78
Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\
73
79
to return a tuple of all serializable objects or None.'''
74
80
return None
75
81
76
- def __setstate__ (self ,state ):
82
+ def __setstate__ (self , state ):
77
83
'''When restoring the serialized object from document we have the chance to set some internals here.\
78
84
Since no data were serialized nothing needs to be done here.'''
79
85
return None
86
+
80
87
88
+ class Feature1TaskPanel :
89
+ def __init__ (self , fp ):
90
+ self .fp = fp
91
+ # this will create a Qt widget from our ui file
92
+ self .form = FreeCADGui .PySideUic .loadUi (os .path .join (__dir__ , 'feature1.ui' ))
93
+ # connect controls from the .ui file to class methods
94
+ self .form .pushButtonSelect .pressed .connect (self ._selectPart )
95
+ self .form .radioButtonRed .released .connect (self ._changeColor )
96
+ self .form .radioButtonGreen .released .connect (self ._changeColor )
97
+ if self .fp .Base :
98
+ self .form .labelSelected .setText (self .fp .Base .Name )
81
99
100
+ def accept (self ):
101
+ '''called when the OK button in the task panel is pressed'''
102
+ self .fp .Base = FreeCAD .ActiveDocument .getObject (self .form .labelSelected .text ())
103
+ self .fp .Green = self .form .radioButtonGreen .isChecked ()
104
+
105
+ redrawFeature1 (self .fp )
106
+ FreeCADGui .ActiveDocument .resetEdit ()
107
+ return True
108
+
109
+ def reject (self ):
110
+ '''called when the Cancel button in the task panel is pressed'''
111
+ FreeCADGui .ActiveDocument .resetEdit ()
112
+ return True
113
+
114
+ def _selectPart (self ):
115
+ '''called when the Select Part button defined in the .ui file is pressed'''
116
+ selection = FreeCADGui .Selection .getSelectionEx ()
117
+ if len (selection ) > 0 :
118
+ self .fp .Base = FreeCAD .ActiveDocument .getObject (selection [0 ].ObjectName )
119
+ self .form .labelSelected .setText (selection [0 ].ObjectName )
120
+
121
+ def _changeColor (self ):
122
+ '''called when a radio button defined in the .ui file is pressed'''
123
+ self .fp .Green = self .form .radioButtonGreen .isChecked ()
124
+ changeFeature1Color (self .fp )
125
+
126
+
82
127
def redrawFeature1 (fp ):
83
128
# check plausibility of all parameters
84
129
if not fp .Base :
85
130
return
86
131
87
132
# fp.Shape contains the newly created object
88
133
fp .Shape = fp .Base .Shape .copy ()
134
+ fp .Placement = fp .Base .Placement
135
+ # place the new object on top of the selected one
136
+ fp .Placement .Base .z += fp .Base .Shape .BoundBox .ZLength
137
+
89
138
changeFeature1Color (fp )
90
139
91
140
92
- def changeFeature1Color (fp ):
141
+ def changeFeature1Color (fp ):
93
142
if fp .Green :
94
143
fp .ViewObject .ShapeColor = (0.00 , 1.00 , 0.00 )
95
144
else :
@@ -122,9 +171,9 @@ def IsActive(self):
122
171
123
172
def GetResources (self ):
124
173
'''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" ) ,
174
+ return {'Pixmap' : __iconpath__ ,
126
175
'Accel' : "" , # a default shortcut (optional)
127
176
'MenuText' : "Feature1" ,
128
- 'ToolTip' : "A template for a workbench feature" }
177
+ 'ToolTip' : __doc__ }
129
178
130
179
FreeCADGui .addCommand ('Feature1' , Feature1 ())
0 commit comments