-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlattice2AttachablePlacement.py
274 lines (222 loc) · 13.1 KB
/
lattice2AttachablePlacement.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
#***************************************************************************
#* *
#* Copyright (c) 2016 - 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__="Attachable placement feature module for lattice workbench for FreeCAD"
__author__ = "DeepSOIC"
__url__ = ""
import math
import FreeCAD as App
import Part
from lattice2Common import *
import lattice2Compatibility as Compat
import lattice2BaseFeature
import lattice2Subsequencer as Subsequencer
EDIT_ATTACHMENT = 56 # Viewprovider edit mode number
def makeAttachablePlacement(name):
'''makeAttachablePlacement(name): makes an attachable Placement object.'''
if Compat.attach_extension_era:
obj = lattice2BaseFeature.makeLatticeFeature(name, AttachablePlacement, ViewProviderAttachablePlacement, no_disable_attacher= True)
else:
#obsolete!
obj = FreeCAD.ActiveDocument.addObject("Part::AttachableObjectPython",name)
AttachablePlacement(obj)
if FreeCAD.GuiUp:
ViewProviderAttachablePlacement(obj.ViewObject)
return obj
class AttachableFeature(lattice2BaseFeature.LatticeFeature):
"Base class for attachable features"
def derivedInit(self,obj):
if Compat.attach_extension_era:
if not obj.hasExtension('Part::AttachExtension'): #PartDesign-related hack: the placement already has attachextension if created in PD
if Compat.no_extension_proxy_era:
obj.addExtension('Part::AttachExtensionPython')
else:
obj.addExtension('Part::AttachExtensionPython', None)
def onDocumentRestored(self, selfobj):
#PartDesign-related hack: this dummy override disables disabling of attacher
pass
class AttachablePlacement(AttachableFeature):
"Attachable Lattice Placement object"
def derivedInit(self,selfobj):
super(AttachablePlacement, self).derivedInit(selfobj)
selfobj.ExposePlacement = True
selfobj.setEditorMode('ExposePlacement', 1) #read-only
def derivedExecute(self,selfobj):
selfobj.ExposePlacement = True
selfobj.positionBySupport()
return [selfobj.Placement]
class ViewProviderAttachableFeature(lattice2BaseFeature.ViewProviderLatticeFeature):
always_edit_attachment = False
def setEdit(self,vobj,mode):
if not (mode == EDIT_ATTACHMENT or (mode == 0 and self.always_edit_attachment)): raise NotImplementedError()
import PartGui
import FreeCADGui as Gui
PartGui.AttachmentEditor.editAttachment(self.Object,
callback_OK= lambda: Gui.ActiveDocument.resetEdit(),
callback_Cancel= lambda: Gui.ActiveDocument.resetEdit())
return True
def unsetEdit(self,vobj,mode):
if not (mode == EDIT_ATTACHMENT or (mode == 0 and self.always_edit_attachment)): raise NotImplementedError()
import FreeCADGui as Gui
Gui.Control.closeDialog()
return True
def setupContextMenu(self,vobj,menu):
from PySide import QtCore,QtGui
action1 = QtGui.QAction(QtGui.QIcon(":/icons/Part_Attachment.svg"),"Attachment...",menu)
QtCore.QObject.connect(action1,QtCore.SIGNAL("triggered()"), lambda: FreeCADGui.ActiveDocument.setEdit(vobj, 56))
menu.addAction(action1)
menu.setDefaultAction(action1)
class ViewProviderAttachablePlacement(ViewProviderAttachableFeature):
always_edit_attachment = True
def getIcon(self):
return getIconPath('Lattice2_AttachablePlacement.svg')
def makeLatticeAttachedPlacementSubsequence(name):
'''makeLatticeAttachedPlacementSubsequence(name): makes a AttachedPlacementSubsequence object.'''
return lattice2BaseFeature.makeLatticeFeature(name, AttachedPlacementSubsequence, ViewProviderAttachedPlacementSubsequence)
class AttachedPlacementSubsequence(lattice2BaseFeature.LatticeFeature):
"Array Maker from Attachable Lattice Placement"
def derivedInit(self,obj):
self.Type = "AttachablePlacementSubsequence"
obj.ExposePlacement = False
obj.setEditorMode("ExposePlacement", 1) #read-only
obj.addProperty("App::PropertyLink", "Base", "Lattice Attached Placement Subsequence", "Link to Lattice Attached Placement, which is to be subsequenced.")
obj.addProperty("App::PropertyString", "RefIndexFilter","Lattice Attached Placement Subsequence","Sets which references of attachment to cycle through children. '0000' = no cycle, '1000' = cycle only ref1. '' = cycle all if possible")
obj.addProperty("App::PropertyEnumeration", "CycleMode","Lattice Attached Placement Subsequence", "How to cycle through children. Open = advance each link till one reaches the end of array. Periodic = if array end reached, continue from begin if any children left.")
obj.CycleMode = ['Open','Periodic']
def derivedExecute(self,obj):
attacher = Part.AttachEngine(screen(obj.Base).AttacherType)
attacher.readParametersFromFeature(screen(obj.Base))
i_filt_str = obj.RefIndexFilter
ifilt = None if i_filt_str == "" else [i for i in range(len(i_filt_str)) if int(i_filt_str[i]) != 0]
sublinks = Subsequencer.Subsequence_auto(attacher.References,
loop= ('Till end' if obj.CycleMode == 'Open' else 'All around'),
index_filter= ifilt)
plms = []
for lnkval in sublinks:
attacher.References = lnkval
plms.append(attacher.calculateAttachedPlacement(screen(obj.Base).Placement))
return plms
class ViewProviderAttachedPlacementSubsequence(lattice2BaseFeature.ViewProviderLatticeFeature):
def getIcon(self):
return getIconPath('Lattice2_AttachedPlacementSubsequence.svg')
def claimChildren(self):
return [screen(self.Object.Base)]
# -------------------------- /document object --------------------------------------------------
# -------------------------- Gui command --------------------------------------------------
def editNewAttachment(f):
def accepted(f):
App.ActiveDocument.commitTransaction()
FreeCADGui.Selection.clearSelection()
FreeCADGui.Selection.addSelection(f)
import PartGui
PartGui.AttachmentEditor.editAttachment(f, take_selection= True,
create_transaction= False,
callback_OK= lambda f=f: accepted(f),
callback_Cancel= lambda: App.ActiveDocument.abortTransaction())
def CreateAttachablePlacement(name):
sel = FreeCADGui.Selection.getSelectionEx()
FreeCAD.ActiveDocument.openTransaction("Create Attachable Placement")
FreeCADGui.addModule("lattice2AttachablePlacement")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.addModule("lattice2Base.Autosize")
FreeCADGui.addModule("PartGui")
FreeCADGui.doCommand("f = lattice2AttachablePlacement.makeAttachablePlacement(name='"+name+"')")
FreeCADGui.doCommand("f.Placement.Base = lattice2Base.Autosize.convenientPosition()")
FreeCADGui.doCommand("f.MarkerSize = lattice2Base.Autosize.convenientMarkerSize()")
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
FreeCADGui.doCommand("lattice2AttachablePlacement.editNewAttachment(f)")
#FreeCAD.ActiveDocument.commitTransaction()
def cmdCreateAttachedPlacementSubsequence(name):
sel = FreeCADGui.Selection.getSelectionEx()
FreeCAD.ActiveDocument.openTransaction("Array an attached placement")
FreeCADGui.addModule("lattice2AttachablePlacement")
FreeCADGui.addModule("lattice2Executer")
FreeCADGui.doCommand("f = lattice2AttachablePlacement.makeLatticeAttachedPlacementSubsequence(name='"+name+"')")
FreeCADGui.doCommand("f.Base = App.ActiveDocument."+sel[0].Object.Name)
FreeCADGui.doCommand("f.Base.ViewObject.hide()")
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
FreeCAD.ActiveDocument.commitTransaction()
FreeCADGui.doCommand("Gui.Selection.addSelection(f)")
deselect(sel)
class CommandAttachablePlacement:
"Command to create Lattice Placement feature"
def __init__(self):
pass
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_AttachablePlacement.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Attached Placement") ,
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Attached Placement: create Lattice Placement attached to geometry")}
def Activated(self):
try:
CreateAttachablePlacement(name= "Placment")
except Exception as err:
msgError(err)
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
class CommandAttachedPlacementSubsequence:
"Command to convert a attached placement into an array"
def __init__(self):
pass
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_AttachedPlacementSubsequence.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Array an attached placement") ,
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_Placement","Attached Placement: makes an array of placements from an attached placement by cycling attachment references...")}
def Activated(self):
try:
sel = FreeCADGui.Selection.getSelectionEx()
if len(sel) == 0:
infoMessage("Attached Placement Subsequence",
"Attached Placement Subsequence feature: makes an array of placements from an attached placement by cycling attachment references through children of an array the placement is attached to."+
"\n\nPlease select an attached placement object, first. Then invoke this tool. Adjust the properties of the created object if necessary." )
else:
if len(sel)!=1:
raise SelectionError("PlacementSubsequence", "Please select just one object, an attached placement. You have selected {num}.".format(num= len(sel)))
cmdCreateAttachedPlacementSubsequence(name= "PlacementSubsequence")
except Exception as err:
msgError(err)
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
if FreeCAD.GuiUp:
FreeCADGui.addCommand("Lattice2_AttachedPlacement", CommandAttachablePlacement())
FreeCADGui.addCommand("Lattice2_AttachedPlacementSubsequence", CommandAttachedPlacementSubsequence())
class CommandAttachedPlacementGroup:
def GetCommands(self):
return ("Lattice2_AttachedPlacement","Lattice2_AttachedPlacementSubsequence")
def GetDefaultCommand(self): # return the index of the tuple of the default command.
return 0
def GetResources(self):
return { 'MenuText': 'Attached Placement:',
'ToolTip': 'Attached Placement (group): tools to work with attached placement objects.'}
def IsActive(self): # optional
return App.ActiveDocument is not None
if FreeCAD.GuiUp:
FreeCADGui.addCommand("Lattice2_AttachedPlacement_Group", CommandAttachedPlacementGroup())
exportedCommands = ["Lattice2_AttachedPlacement_Group"]
# -------------------------- /Gui command --------------------------------------------------