-
Notifications
You must be signed in to change notification settings - Fork 9
/
lattice2Placement.py
362 lines (281 loc) · 16.1 KB
/
lattice2Placement.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#***************************************************************************
#* *
#* Copyright (c) 2015 - Victor Titov (DeepSOIC) *
#* <[email protected]> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
__title__="Placement feature module for lattice workbench for FreeCAD"
__author__ = "DeepSOIC"
__url__ = ""
import math
import FreeCAD as App
import Part
from lattice2Common import *
import lattice2BaseFeature
def makeLatticePlacement(name):
'''makePlacement(name): makes a Placement object.'''
return lattice2BaseFeature.makeLatticeFeature(name, LatticePlacement, ViewProviderLatticePlacement)
class LatticePlacement(lattice2BaseFeature.LatticeFeature):
"The Lattice Placement object"
_PlacementChoiceList = ['Custom','XY plane', 'XZ plane', 'YZ plane']
def derivedInit(self,obj):
self.Type = "LatticePlacement"
obj.addProperty("App::PropertyEnumeration","PlacementChoice","Lattice Placement","Choose one of standard placements here.")
obj.PlacementChoice = self._PlacementChoiceList
obj.addProperty("App::PropertyLength","Offset","Lattice Placement","Offset from origin for XY/XZ/YZ plane modes")
obj.addProperty("App::PropertyBool","FlipZ","Lattice Placement","Set to true to flip Z axis (also flips Y)")
obj.addProperty("App::PropertyBool","Invert","Lattice Placement","Invert the placement")
obj.ExposePlacement = True
def updateReadOnlyness(self, obj):
m = obj.PlacementChoice
obj.setEditorMode("Placement", 0 if m == "Custom" else 1)
obj.setEditorMode("Offset", 1 if m == "Custom" else 0)
def derivedExecute(self,obj):
# Fill in (update read-only) properties that are driven by the mode.
self.updateReadOnlyness(obj)
if obj.PlacementChoice == 'Custom':
pass
else:
rot = App.Rotation()
if obj.PlacementChoice == 'XY plane':
pass
elif obj.PlacementChoice == 'XZ plane':
rot.Q = (-1.0, 0.0, 0.0, -1.0) #will get normalized by FC automatically
elif obj.PlacementChoice == 'YZ plane':
rot.Q = (0.5, 0.5, 0.5, 0.5) #will get normalized by FC automatically
else:
raise ValueError("lattice2Placement: unsupported placement: "+obj.PlacementChoice)
obj.Placement = App.Placement(rot.multVec(App.Vector(0,0,obj.Offset)), rot)
if obj.FlipZ:
obj.Placement = obj.Placement.multiply(App.Placement(App.Vector(),App.Rotation(App.Vector(1,0,0),180)))
if obj.PlacementChoice == 'Custom':
obj.FlipZ = False
if obj.Invert:
obj.Placement = obj.Placement.inverse()
if obj.PlacementChoice == 'Custom':
obj.Invert = False
return [obj.Placement]
def makeLatticePlacementAx(name):
'''makePlacement(name): makes a Placement object.'''
return lattice2BaseFeature.makeLatticeFeature(name, LatticePlacementAx, ViewProviderLatticePlacement)
class LatticePlacementAx(lattice2BaseFeature.LatticeFeature):
"The Lattice Placement object, defined by axes directions"
def derivedInit(self,obj):
self.Type = "LatticePlacementAx"
obj.addProperty("App::PropertyEnumeration","Priority","Lattice Placement","Example: ZXY = ZDir followed strictly, XDir is a hint, YDir is ignored and computed from others.")
obj.Priority = ["XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"]
obj.addProperty("App::PropertyVector","XDir_wanted","Lattice Placement","Align X axis of placement with this direction.")
obj.addProperty("App::PropertyVector","YDir_wanted","Lattice Placement","Align Y axis of placement with this direction.")
obj.addProperty("App::PropertyVector","ZDir_wanted","Lattice Placement","Align Z axis of placement with this direction.")
obj.addProperty("App::PropertyVector","XDir_actual","Lattice Placement","Actual resulting direction of X axis of the placement.")
obj.addProperty("App::PropertyVector","YDir_actual","Lattice Placement","Actual resulting direction of Y axis of the placement.")
obj.addProperty("App::PropertyVector","ZDir_actual","Lattice Placement","Actual resulting direction of Z axis of the placement.")
obj.setEditorMode("XDir_actual", 1) #read-only
obj.setEditorMode("YDir_actual", 1) #read-only
obj.setEditorMode("ZDir_actual", 1) #read-only
obj.ExposePlacement = True
def derivedExecute(self,obj):
old_pos = App.Vector()
try:
old_pos = lattice2BaseFeature.getPlacementsList(obj, suppressWarning= True)[0].Base
except Exception:
pass #retrieving position may fail if the object is recomputed for the very first time
import lattice2GeomUtils
ori = lattice2GeomUtils.makeOrientationFromLocalAxesUni(obj.Priority, obj.XDir_wanted * 1.0, obj.YDir_wanted * 1.0, obj.ZDir_wanted * 1.0) # multiply vectors by 1.0 to copy them, to block mutation
plm = App.Placement(old_pos, ori)
obj.XDir_actual = ori.multVec(App.Vector(1,0,0))
obj.YDir_actual = ori.multVec(App.Vector(0,1,0))
obj.ZDir_actual = ori.multVec(App.Vector(0,0,1))
return [plm]
def makeLatticePlacementEuler(name):
'''makePlacement(name): makes a Placement object.'''
return lattice2BaseFeature.makeLatticeFeature(name, LatticePlacementEuler, ViewProviderLatticePlacement)
class LatticePlacementEuler(lattice2BaseFeature.LatticeFeature):
"The Lattice Placement object, defined by axes directions"
def derivedInit(self,obj):
self.Type = "LatticePlacementEuler"
obj.addProperty("App::PropertyAngle","Yaw","Lattice Placement","Rotation around Z axis")
obj.addProperty("App::PropertyAngle","Pitch","Lattice Placement","Rotation around Y axis")
obj.addProperty("App::PropertyAngle","Roll","Lattice Placement","Rotation around X axis")
obj.ExposePlacement = True
def derivedExecute(self,obj):
old_pos = App.Vector()
try:
old_pos = lattice2BaseFeature.getPlacementsList(obj, suppressWarning= True)[0].Base
except Exception:
pass #retrieving position may fail if the object is recomputed for the very first time
ori = App.Rotation(obj.Yaw, obj.Pitch, obj.Roll)
plm = App.Placement(old_pos, ori)
return [plm]
class ViewProviderLatticePlacement(lattice2BaseFeature.ViewProviderLatticeFeature):
def getIcon(self):
return getIconPath('Lattice2_Placement.svg')
# -------------------------- /document object --------------------------------------------------
# -------------------------- Gui command --------------------------------------------------
def CreateLatticePlacement(name,mode = 'Custom'):
sel = FreeCADGui.Selection.getSelectionEx()
FreeCAD.ActiveDocument.openTransaction("Create Lattice Placement")
FreeCADGui.addModule("lattice2Placement")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.addModule("lattice2Base.Autosize")
FreeCADGui.doCommand("f = lattice2Placement.makeLatticePlacement(name='"+name+"')")
FreeCADGui.doCommand("f.PlacementChoice = '"+mode+"'")
FreeCADGui.doCommand("f.Label = '"+mode+"'")
FreeCADGui.doCommand("f.Placement.Base = lattice2Base.Autosize.convenientPosition()")
FreeCADGui.doCommand("f.MarkerSize = lattice2Base.Autosize.convenientMarkerSize()")
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
FreeCADGui.doCommand("Gui.Selection.addSelection(f)")
FreeCADGui.doCommand("f = None")
FreeCAD.ActiveDocument.commitTransaction()
def CreateLatticePlacementAx(label, priority, XDir, YDir, ZDir):
sel = FreeCADGui.Selection.getSelectionEx()
FreeCAD.ActiveDocument.openTransaction("Create Lattice Placement")
FreeCADGui.addModule("lattice2Placement")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.addModule("lattice2Base.Autosize")
name = "PlacementAx"
FreeCADGui.doCommand("f = lattice2Placement.makeLatticePlacementAx(name='"+name+"')")
FreeCADGui.doCommand("f.Priority = "+repr(priority))
if XDir is not None and XDir.Length > DistConfusion:
FreeCADGui.doCommand("f.XDir_wanted = App.Vector"+repr(tuple(XDir)))
if YDir is not None and YDir.Length > DistConfusion:
FreeCADGui.doCommand("f.YDir_wanted = App.Vector"+repr(tuple(YDir)))
if ZDir is not None and ZDir.Length > DistConfusion:
FreeCADGui.doCommand("f.ZDir_wanted = App.Vector"+repr(tuple(ZDir)))
FreeCADGui.doCommand("f.Label = "+repr(label))
FreeCADGui.doCommand("f.Placement.Base = lattice2Base.Autosize.convenientPosition()")
FreeCADGui.doCommand("f.MarkerSize = lattice2Base.Autosize.convenientMarkerSize()")
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
FreeCADGui.doCommand("Gui.Selection.addSelection(f)")
FreeCADGui.doCommand("f = None")
FreeCAD.ActiveDocument.commitTransaction()
def CreateLatticePlacementEuler(name):
sel = FreeCADGui.Selection.getSelectionEx()
FreeCAD.ActiveDocument.openTransaction("Create Lattice Placement")
FreeCADGui.addModule("lattice2Placement")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.addModule("lattice2Base.Autosize")
FreeCADGui.doCommand("f = lattice2Placement.makeLatticePlacementEuler(name='"+name+"')")
FreeCADGui.doCommand("f.Placement.Base = lattice2Base.Autosize.convenientPosition()")
FreeCADGui.doCommand("f.MarkerSize = lattice2Base.Autosize.convenientMarkerSize()")
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
FreeCADGui.doCommand("Gui.Selection.addSelection(f)")
FreeCADGui.doCommand("f = None")
FreeCAD.ActiveDocument.commitTransaction()
class _CommandPlacement:
"Command to create Lattice Placement feature"
def __init__(self, mode = 'Custom'):
self.mode = mode
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_Placement_New.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Single Placement: ") + self.mode, # FIXME: not translation-friendly!
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Lattice Placement: Create lattice object with single item")}
def Activated(self):
FreeCADGui.Selection.clearSelection()
CreateLatticePlacement(name= "Placment", mode= self.mode)
if self.mode == "Custom":
FreeCADGui.runCommand("Std_Placement")
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
_listOfSubCommands = []
for mode in LatticePlacement._PlacementChoiceList:
cmdName = 'Lattice2_Placement' + mode
if FreeCAD.GuiUp:
FreeCADGui.addCommand(cmdName, _CommandPlacement(mode))
_listOfSubCommands.append(cmdName)
class _CommandPlacementAx:
"Command to create Lattice Placement by axes feature"
def __init__(self, menu_text, tooltip, label, priority, XDir = None, YDir = None, ZDir = None):
self.menu_text = menu_text
self.tooltip = tooltip
self.label = label
self.priority = priority
self.XDir = XDir
self.YDir = YDir
self.ZDir = ZDir
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_Placement_New.svg"),
'MenuText': "Single Placement: " + self.menu_text, # FIXME: not translation-friendly!
'Accel': "",
'ToolTip': self.tooltip}
def Activated(self):
try:
FreeCADGui.Selection.clearSelection()
CreateLatticePlacementAx(self.label, self.priority, self.XDir, self.YDir, self.ZDir)
except Exception as err:
msgError(err)
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
cmdName = "Lattice2_PlacementAx_AlongX"
if FreeCAD.GuiUp:
FreeCADGui.addCommand(cmdName, _CommandPlacementAx("along X","Single Placement with local X aligned along global X","Plm along X","XZY", XDir= App.Vector(1,0,0)))
_listOfSubCommands.append(cmdName)
cmdName = "Lattice2_PlacementAx_AlongY"
if FreeCAD.GuiUp:
FreeCADGui.addCommand(cmdName, _CommandPlacementAx("along Y","Single Placement with local X aligned along global Y","Plm along Y","XZY", XDir= App.Vector(0,1,0)))
_listOfSubCommands.append(cmdName)
cmdName = "Lattice2_PlacementAx_AlongZ"
if FreeCAD.GuiUp:
FreeCADGui.addCommand(cmdName, _CommandPlacementAx("along Z","Single Placement with local X aligned along global Z","Plm along Z","XZY", XDir= App.Vector(0,0,1)))
_listOfSubCommands.append(cmdName)
class _CommandPlacementEuler:
"Command to create Lattice Placement feature"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_Placement_New.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Single Placement: Euler angles"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Lattice Placement: driven by Euler angles (yaw, pitch, roll)")}
def Activated(self):
try:
FreeCADGui.Selection.clearSelection()
CreateLatticePlacementEuler(name= "PlacementEu")
except Exception as err:
msgError(err)
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
cmdName = "Lattice2_PlacementEuler"
if FreeCAD.GuiUp:
FreeCADGui.addCommand(cmdName, _CommandPlacementEuler())
_listOfSubCommands.append(cmdName)
import lattice2ArrayFromShape
_listOfSubCommands.extend(lattice2ArrayFromShape.exportedCommands_forSinglePlacement)
class GroupCommandPlacement:
def GetCommands(self):
global _listOfSubCommands
return tuple(_listOfSubCommands) # a tuple of command names that you want to group
def GetDefaultCommand(self): # return the index of the tuple of the default command. This method is optional and when not implemented '0' is used
return 0
def GetResources(self):
return { 'MenuText': 'Lattice Placement', 'ToolTip': 'Lattice Placement: Create lattice object with single item'}
def IsActive(self): # optional
return True
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_Placement_GroupCommand',GroupCommandPlacement())
exportedCommands = ['Lattice2_Placement_GroupCommand']
# -------------------------- /Gui command --------------------------------------------------