-
Notifications
You must be signed in to change notification settings - Fork 9
/
lattice2PDPattern.py
376 lines (316 loc) · 15.5 KB
/
lattice2PDPattern.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#***************************************************************************
#* *
#* Copyright (c) 2018 - 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__="Lattice PartDesign Pattern object: a partdesign pattern based on Lattice array."
__author__ = "DeepSOIC"
__url__ = ""
import FreeCAD as App
import Part
from lattice2Common import *
import lattice2BaseFeature
import lattice2Executer
from lattice2ShapeCopy import shallowCopy, transformCopy_Smart
from lattice2PopulateCopies import DereferenceArray
class FeatureUnsupportedError(RuntimeError):
pass
class NotPartDesignFeatureError(RuntimeError):
pass
class FeatureFailure(RuntimeError):
pass
class ScopeError(RuntimeError):
pass
class MultiTransformSettings(object):
selfintersections = False #if True, take care of intersections between occurrences. If False, optimize assuming occurrences do not intersect.
sign_override = +1 #+1 for keep sign, -1 for invert, +2 for force positive, -2 for force negative
def makeFeature():
'''makeFeature(): makes a PartDesignPattern object.'''
obj = activeBody().newObject("PartDesign::FeaturePython","LatticePattern")
LatticePDPattern(obj)
if FreeCAD.GuiUp:
ViewProviderLatticePDPattern(obj.ViewObject)
return obj
def getBodySequence(body, skipfirst = False):
visited = set()
result = []
curfeature = body.Tip
while True:
if curfeature in visited:
raise ValueError("Feature sequence is looped in {body}".format(body= body.Name))
if curfeature is None:
break
if not curfeature.isDerivedFrom('PartDesign::Feature'):
break
if not body.hasObject(curfeature):
break
if curfeature.isDerivedFrom('PartDesign::FeatureBase'):
#base feature for body. Do not include.
break
visited.add(curfeature)
result.insert(0, curfeature)
curfeature = curfeature.BaseFeature
if skipfirst:
result.pop(0)
return result
def feature_sign(feature, raise_if_unsupported = False):
"""feature_sign(feature, raise_if_unsupported = False): returns +1 for additive PD features, -1 for subtractive PD features, and 0 for the remaining (unsupported)"""
additive_types = [
'PartDesign::Pad',
'PartDesign::Revolution',
]
subtractive_types = [
'PartDesign::Pocket',
'PartDesign::Groove',
'PartDesign::Hole',
]
def unsupported():
if raise_if_unsupported:
raise FeatureUnsupportedError("Feature {name} is neither additive nor subtractive. Unsupported.".format(name= feature.Name))
else:
return 0
if not feature.isDerivedFrom('PartDesign::Feature'):
raise NotPartDesignFeatureError("Feature {name} is not a PartDesign feature. Unsupported.".format(name= feature.Name))
if hasattr(feature, 'AddSubType'): #part-o-magic; possibly PartDesign future
t = feature.AddSubType
if t == 'Additive':
return +1
elif t == 'Subtractive':
return -1
else:
return unsupported()
typ = feature.TypeId
if typ in additive_types:
return 1
if typ in subtractive_types:
return -1
if 'Additive' in typ:
return +1
if 'Subtractive' in typ:
return -1
if typ == 'PartDesign::Boolean':
t = feature.Type
if t == 'Fuse':
return +1
elif t == 'Cut':
return -1
else:
return unsupported()
return unsupported()
def getFeatureShapes(feature):
sign = feature_sign(feature, raise_if_unsupported= True)
if hasattr(feature, 'AddSubShape'):
sh = shallowCopy(feature.AddSubShape)
sh.Placement = feature.Placement
return [(sign, sh)]
elif feature.isDerivedFrom('PartDesign::Boolean'):
return [(sign, obj.Shape) for obj in feature.Group]
else:
raise FeatureUnsupportedError("Feature {name} is not supported.".format(name= feature.Name))
def is_supported(feature):
if hasattr(feature, 'Proxy') and hasattr(feature.Proxy, 'applyTransformed'):
return True
try:
sign = feature_sign(feature, raise_if_unsupported= True)
return True
except (FeatureUnsupportedError, NotPartDesignFeatureError):
return False
def applyFeature(baseshape, feature, transforms, mts):
if hasattr(feature, 'Proxy') and hasattr(feature.Proxy, 'applyTransformed'):
return feature.Proxy.applyTransformed(feature, baseshape, transforms, mts)
task = getFeatureShapes(feature)
for sign,featureshape in task:
actionshapes = []
for transform in transforms:
actionshapes.append(shallowCopy(featureshape, transform))
if mts.selfintersections:
pass #to fuse the shapes to baseshape one by one
else:
actionshapes = [Part.Compound(actionshapes)] #to fuse all at once, saving for computing intersections between the occurrences of the feature
for actionshape in actionshapes:
assert(sign != 0)
realsign = sign * mts.sign_override
if abs(mts.sign_override) == +2:
realsign = int(mts.sign_override / 2)
if realsign > 0:
baseshape = baseshape.fuse(actionshape)
elif realsign < 0:
baseshape = baseshape.cut(actionshape)
if baseshape.isNull():
raise FeatureFailure('applying {name} failed - returned shape is null'.format(name= feature.Name))
return baseshape
class LatticePDPattern(object):
def __init__(self,obj):
obj.addProperty('App::PropertyLinkListGlobal','FeaturesToCopy',"Lattice Pattern","Features to be copied (can be a body)")
obj.addProperty('App::PropertyLinkGlobal','PlacementsFrom',"Lattice Pattern","Reference placement (placement that marks where the original feature is)")
obj.addProperty('App::PropertyLink','PlacementsTo',"Lattice Pattern","Target placements")
obj.addProperty('App::PropertyEnumeration','Referencing',"Lattice Pattern","Reference placement mode (sets what to grab the feature by).")
obj.Referencing = ['Origin','First item', 'Last item', 'Use PlacementsFrom']
obj.addProperty('App::PropertyBool', 'IgnoreUnsupported', "Lattice Pattern", "Skip unsupported features such as fillets, instead of throwing errors")
obj.addProperty('App::PropertyBool', 'SkipFirstInBody', "Lattice Pattern", "Skip first body feature (which may be used as support for the important features).")
obj.addProperty('App::PropertyEnumeration', 'SignOverride', "Lattice Pattern", "Use it to change Pockets into Pads.")
obj.SignOverride = ['keep', 'invert', 'as additive', 'as subtractive']
obj.addProperty('App::PropertyBool', 'Selfintersections', "Lattice Pattern", "If True, take care of intersections between occurrences. If False, you get a slight speed boost.")
obj.addProperty('App::PropertyBool', 'Refine', "PartDesign", "If True, remove redundant edges after this operation.")
obj.Refine = getParamPDRefine()
obj.addProperty('App::PropertyBool', 'SingleSolid', "PartDesign", "If True, discard solids not joined with the base.")
obj.Proxy = self
def execute(self, selfobj):
if selfobj.BaseFeature is None:
baseshape = Part.Compound([])
else:
baseshape = selfobj.BaseFeature.Shape
mts = MultiTransformSettings()
mts.sign_override = {'keep': +1, 'invert': -1, 'as additive': +2 , 'as subtractive': -2}[selfobj.SignOverride]
mts.selfintersections = selfobj.Selfintersections
result = self.applyTransformed(selfobj, baseshape, None, mts)
if selfobj.SingleSolid:
# not proper implementation, but should do for majority of cases: pick the largest solid.
vmax = 0
vmax_solid = None
for s in result.Solids:
v = s.Volume
if v > vmax:
vmax = v
vmax_solid = s
if vmax_solid is None:
raise ValueError("No solids in result. Maybe the result is corrupted because of failed BOP, or all the material was removed in the end.")
result = vmax_solid
if selfobj.SingleSolid or len(result.Solids) == 1:
result = transformCopy_Smart(result.Solids[0], selfobj.Placement)
if selfobj.Refine:
result = result.removeSplitter()
selfobj.Shape = result
def applyTransformed(self, selfobj, baseshape, transforms, mts):
featurelist = []
has_bodies = False
has_features = False
for lnk in selfobj.FeaturesToCopy:
if lnk.isDerivedFrom('PartDesign::Body'):
featurelist.extend(getBodySequence(lnk, skipfirst= selfobj.SkipFirstInBody))
has_bodies = True
else:
featurelist.append(lnk)
has_features = True
#check cross-links
if selfobj.Referencing == 'Use PlacementsFrom':
body_ref = bodyOf(selfobj.PlacementsFrom)
else:
body_ref = bodyOf(selfobj)
for feature in featurelist:
if bodyOf(feature) is not body_ref:
raise ScopeError('Reference placement and the feature are not in the same body (use Shapebinder or Ghost to bring the placement in).')
placements = lattice2BaseFeature.getPlacementsList(selfobj.PlacementsTo, selfobj)
placements = DereferenceArray(selfobj, placements, selfobj.PlacementsFrom, selfobj.Referencing)
if selfobj.Referencing == 'First item' and transforms is None:
placements.pop(0) #to not repeat the feature where it was applied already
elif selfobj.Referencing == 'Last item' and transforms is None:
placements.pop() #to not repeat the feature where it was applied already
if not transforms is None:
newplacements = []
for transform in transforms:
newplacements += [transform.multiply(plm) for plm in placements]
placements = newplacements
for feature in featurelist:
try:
baseshape = applyFeature(baseshape, feature, placements, mts)
except FeatureUnsupportedError as err:
if not selfobj.IgnoreUnsupported:
raise
else:
App.Console.PrintLog('{name} is unsupported, skipped.\n'.format(name= feature.Name))
return baseshape
def __getstate__(self):
return None
def __setstate__(self,state):
return None
def dumps(self):
return None
def loads(self,state):
return None
class ViewProviderLatticePDPattern:
"A View Provider for the Lattice PartDesign Pattern object"
def __init__(self,vobj):
vobj.Proxy = self
def getIcon(self):
return getIconPath("Lattice2_PDPattern.svg")
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
def __getstate__(self):
return None
def __setstate__(self,state):
return None
def dumps(self):
return None
def loads(self,state):
return None
def claimChildren(self):
weakparenting = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Lattice2").GetBool("WeakParenting", True)
if weakparenting:
return []
return [self.Object.PlacementsTo]
def onDelete(self, feature, subelements): # subelements is a tuple of strings
return True
# -------------------------- /document object --------------------------------------------------
# -------------------------- Gui command --------------------------------------------------
def CreateLatticePDPattern(features, latticeObjFrom, latticeObjTo, refmode):
FreeCADGui.addModule("lattice2PDPattern")
FreeCADGui.addModule("lattice2Executer")
#fill in properties
FreeCADGui.doCommand("f = lattice2PDPattern.makeFeature()")
reprfeatures = ', '.join(['App.ActiveDocument.'+f.Name for f in features])
FreeCADGui.doCommand("f.FeaturesToCopy = [{features}]".format(features= reprfeatures))
FreeCADGui.doCommand("f.PlacementsTo = App.ActiveDocument."+latticeObjTo.Name)
if latticeObjFrom is not None:
FreeCADGui.doCommand("f.PlacementsFrom = App.ActiveDocument."+latticeObjFrom.Name)
FreeCADGui.doCommand("f.Referencing = "+repr(refmode))
#execute
FreeCADGui.doCommand("lattice2Executer.executeFeature(f)")
#hide something
FreeCADGui.doCommand("f.PlacementsTo.ViewObject.hide()")
FreeCADGui.doCommand("f.BaseFeature.ViewObject.hide()")
#finalize
FreeCADGui.doCommand("Gui.Selection.addSelection(f)")
FreeCADGui.doCommand("f = None")
def cmdPDPattern():
sel = FreeCADGui.Selection.getSelectionEx()
(lattices, shapes) = lattice2BaseFeature.splitSelection(sel)
if len(shapes) > 0 and len(lattices) == 2:
FreeCAD.ActiveDocument.openTransaction("Lattice Pattern")
latticeFrom = lattices[0]
latticeTo = lattices[1]
CreateLatticePDPattern([so.Object for so in shapes], latticeFrom.Object, latticeTo.Object,'Use PlacementsFrom')
deselect(sel)
FreeCAD.ActiveDocument.commitTransaction()
elif len(shapes) > 0 and len(lattices) == 1:
FreeCAD.ActiveDocument.openTransaction("Lattice Pattern")
latticeTo = lattices[0]
CreateLatticePDPattern([so.Object for so in shapes], None, latticeTo.Object,'First item')
deselect(sel)
FreeCAD.ActiveDocument.commitTransaction()
else:
raise SelectionError("Bad selection",
"Please select either:\n"
" one or more PartDesign features, and one or two placements/arrays \n"
"or\n"
" a template body and two placements/arrays, one from selected body and one from active body."
)
# command defined in lattice2PDPatternCommand.py