forked from Zolko-123/FreeCAD_Assembly4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariablesLib.py
223 lines (184 loc) · 7.43 KB
/
VariablesLib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
# coding: utf-8
#
# placeDatumCmd.py
from PySide import QtGui, QtCore
import FreeCADGui as Gui
import FreeCAD as App
import Part, math, re
from libAsm4 import *
"""
+-----------------------------------------------+
| main class |
+-----------------------------------------------+
"""
class addVariable( QtGui.QDialog ):
"My tool object"
def __init__(self):
super(addVariable,self).__init__()
self.Variables = []
self.allowedProperties = [ 'App::PropertyBool',
'App::PropertyBoolList',
'App::PropertyInteger',
'App::PropertyIntegerList',
'App::PropertyFloat',
'App::PropertyFloatList',
'App::PropertyString',
'App::PropertyEnumeration',
#'App::PropertyLink',
'App::PropertyXLink',
'App::PropertyVector',
'App::PropertyMatrix',
'App::PropertyPlacement',
'App::PropertyColor',
'App::PropertyFile']
def GetResources(self):
return {"MenuText": "Add Variable",
"ToolTip": "Add Variable",
"Pixmap" : os.path.join( iconPath , 'Asm4_Variables.svg')
}
def IsActive(self):
# is there an active document ?
if App.ActiveDocument:
# is something selected ?
selObj = self.checkSelection()
if selObj != None:
return True
return False
def checkSelection(self):
selectedObj = None
# check that it's an Assembly4 'Model'
if App.ActiveDocument.getObject('Model') and App.ActiveDocument.getObject('Model').TypeId=='App::Part':
model = App.ActiveDocument.getObject('Model')
# check that something is selected
if Gui.Selection.getSelection():
# set the (first) selected object as global variable
selection = Gui.Selection.getSelection()[0]
# if the root Model is selected it's OK
if selection == model:
selectedObj = model
# check that the selected object is a Datum CS or Point type
if selection.TypeId=='App::FeaturePython' and selection.Name=='Variables':
selectedObj = selection
# if nothing is selected then it's also OK
else:
selectedObj = model
# now we should be safe
return selectedObj
"""
+-----------------------------------------------+
| the real stuff |
+-----------------------------------------------+
"""
def Activated(self):
# check that we have selected a PartDesign::CoordinateSystem
self.Variables = App.ActiveDocument.getObject('Variables')
# Now we can draw the UI
self.drawUI()
self.show()
# get all supported Property types
for prop in self.Variables.supportedProperties():
if prop in self.allowedProperties:
self.typeList.addItem(prop)
# find the Float property
propFloat = self.typeList.findText( 'App::PropertyFloat' )
# if not found
if propFloat == -1:
self.typeList.setCurrentIndex( 0 )
else:
self.typeList.setCurrentIndex( propFloat )
"""
+-----------------------------------------------+
| OK |
+-----------------------------------------------+
"""
def onOK(self):
# var property type
propType = self.typeList.currentText()
# if the variable's name is defined
varName = self.varName.text()
if varName:
# add it to the Variables group of the Variables FeaturePython object
var = self.Variables.addProperty( propType, varName, 'Variables' )
# if the variable's value is defined and it's a float
varValue = self.varValue.value()
if varValue and propType=='App::PropertyFloat':
setattr( self.Variables, varName, varValue )
self.close()
else:
return
"""
+-----------------------------------------------+
| Cancel |
+-----------------------------------------------+
"""
def onCancel(self):
self.close()
"""
+-----------------------------------------------+
| defines the UI, only static elements |
+-----------------------------------------------+
"""
def drawUI(self):
# Our main window will be a QDialog
self.setWindowTitle('Add Variable')
self.setWindowIcon( QtGui.QIcon( os.path.join( iconPath , 'FreeCad.svg' ) ) )
self.setMinimumSize(470, 330)
self.resize(470,330)
self.setModal(False)
# make this dialog stay above the others, always visible
self.setWindowFlags( QtCore.Qt.WindowStaysOnTopHint )
# Variable Type
self.typeLabel = QtGui.QLabel(self)
self.typeLabel.setText("Type")
self.typeLabel.move(10,25)
# combobox showing all available App::PropertyType
self.typeList = QtGui.QComboBox(self)
self.typeList.move(110,20)
self.typeList.setMinimumSize(350, 1)
# Variable Name
self.nameLabel = QtGui.QLabel(self)
self.nameLabel.setText("Name")
self.nameLabel.move(10,65)
# the document containing the linked object
self.varName = QtGui.QLineEdit(self)
self.varName.setMinimumSize(350, 1)
self.varName.move(110,60)
# Variable Value
self.valueLabel = QtGui.QLabel(self)
self.valueLabel.setText("Value")
self.valueLabel.move(10,105)
# the document containing the linked object
self.varValue = QtGui.QDoubleSpinBox(self)
self.varValue.setMinimumSize(350, 1)
self.varValue.setRange( -10000.0, 10000.0 )
self.varValue.setValue( 10.0 )
self.varValue.move(110,100)
# Documentation
self.docLabel = QtGui.QLabel(self)
self.docLabel.setText("Document")
self.docLabel.move(10,145)
# Create a line that will contain full expression for the expression engine
self.expression = QtGui.QLineEdit(self)
self.expression.setMinimumSize(350, 100)
self.expression.move(110, 140)
# Buttons
#
# Cancel button
self.CancelButton = QtGui.QPushButton('Cancel', self)
self.CancelButton.setAutoDefault(False)
self.CancelButton.move(10, 290)
# OK button
self.OKButton = QtGui.QPushButton('OK', self)
self.OKButton.setAutoDefault(True)
self.OKButton.move(380, 290)
self.OKButton.setDefault(True)
# Actions
self.CancelButton.clicked.connect(self.onCancel)
self.OKButton.clicked.connect(self.onOK)
"""
+-----------------------------------------------+
| add the command to the workbench |
+-----------------------------------------------+
"""
Gui.addCommand( 'Asm4_addVariable', addVariable() )