-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathboolean_mesh.py
362 lines (248 loc) · 9.9 KB
/
boolean_mesh.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
'''Base FeaturePython implementation that can handle boolean operations with meshes'''
import FreeCAD
import Mesh
from base_lithophane_processor import BaseLithophaneProcessor
from utils.resource_utils import iconPath
import utils.qtutils as qtutils
from utils import preferences
MODE_MAPPING = {
'Additive': 'union',
'Subtractive': 'difference'
}
ICON_MAPPING = {
'Additive': iconPath('BooleanMeshFeatureAdd.svg'),
'Subtractive': iconPath('BooleanMeshFeatureSubtract.svg')
}
class BooleanMeshProcessor(BaseLithophaneProcessor):
def __init__(self, description, checkExecutionFunction, extractMeshFunction, processingStepsFunction):
super(BooleanMeshProcessor, self).__init__(description)
self.result = None
self.checkExecutionFunction = checkExecutionFunction
self.extractMeshFunction = extractMeshFunction
self.processingStepsFunction = processingStepsFunction
def checkExecution(self):
return self.checkExecutionFunction()
def processingDone(self, obj, params):
self.result = self.extractMeshFunction(obj, params)
def getProcessingSteps(self, obj):
return self.processingStepsFunction(obj)
class BooleanMeshFeature(object):
def __init__(self, obj, base, mode):
obj.Proxy = self
self.setProperties(obj)
obj.Base = base
obj.Mode = mode
obj.Base.ViewObject.Visibility = False
def execute(self, obj):
import MeshPart
base = obj.Base
if hasattr(base, 'Mesh') and isinstance(base, Mesh.Mesh):
self.mesh = base
elif hasattr(base, 'Shape') and base.Shape:
self.mesh = MeshPart.meshFromShape(base.Shape.copy(False))
else:
self.mesh = MeshPart.meshFromShape(base.copy(False))
def getMesh(self):
if not self.mesh:
self.execute(self.Object)
return self.mesh
def applyOperationToMesh(self, mesh):
if not self.Object.Enabled:
return mesh
import OpenSCADUtils
openscadOperation = MODE_MAPPING[self.Object.Mode]
return OpenSCADUtils.meshoptempfile(openscadOperation, (mesh, self.getMesh()))
def setProperties(self, obj):
self.Object = obj
self.mesh = None
pl = obj.PropertiesList
if not 'Base' in pl:
obj.addProperty("App::PropertyLink", "Base", "Boolean",
"The geometry to apply to the mesh")
if not 'Mode' in pl:
obj.addProperty("App::PropertyEnumeration", "Mode", "Boolean",
"The type of operation to apply to the mesh")
obj.Mode = ['Additive', 'Subtractive']
if not 'Enabled' in pl:
obj.addProperty("App::PropertyBool", "Enabled", "Boolean",
"When False, the operation will not be applied").Enabled = True
def onDocumentRestored(self, obj):
self.setProperties(obj)
def __getstate__(self):
'''We do not store any data for now'''
pass
def __setstate__(self, state):
'''We do not store any data for now'''
pass
class ViewProviderBooleanMeshFeature(object):
def __init__(self, vobj):
vobj.Proxy = self
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
from pivy import coin
self.coinNode = coin.SoGroup()
vobj.addDisplayMode(self.coinNode, "Standard")
def onChanged(self, vp, prop):
pass
def claimChildren(self):
return [self.Object.Base]
def getIcon(self):
if not self.Object.Enabled:
return iconPath('BooleanMeshFeatureDisabled.svg')
return ICON_MAPPING[self.Object.Mode]
def getDisplayModes(self, obj):
return ["Standard"]
def getDefaultDisplayMode(self):
return "Standard"
def __getstate__(self):
'''We do not store any data for now'''
pass
def __setstate__(self, state):
'''We do not store any data for now'''
pass
class BooleanMesh(object):
def __init__(self, obj):
obj.Proxy = self
self.setProperties(obj)
def getDescription(self):
raise NotImplementedError
def getIcon(self):
return None
def checkBaseMeshExecution(self):
if self.Object.LithophaneImage is None:
qtutils.showInfo('No LithophaneImage linked',
'Please link a lithophane Image to the mesh to caculate the geometry')
return None
return self.Object.LithophaneImage.Proxy
def extractBaseMesh(self, obj, params):
raise NotImplementedError
def getBaseProcessingSteps(self, obj):
raise NotImplementedError
def getResultName(self, obj):
if not obj.LithophaneImage:
return "Result"
return obj.LithophaneImage.Name + '_Result'
def execute(self, obj):
if not obj.Result:
m = FreeCAD.ActiveDocument.addObject(
"Mesh::Feature", self.getResultName(obj))
obj.Result = m
resultMesh = Mesh.Mesh()
description = self.getDescription()
baseMeshProcessor = BooleanMeshProcessor(
description + " (Base)", self.checkBaseMeshExecution, self.extractBaseMesh, self.getBaseProcessingSteps)
baseMeshProcessor.execute(obj)
if preferences.useBlenderForBooleanOperations():
mesh = self.executeBlender(baseMeshProcessor.result, obj)
else:
mesh = self.executeOpenSCAD(baseMeshProcessor.result, obj)
resultMesh.addMesh(mesh)
obj.Result.Mesh = resultMesh
def executeOpenSCAD(self, basemesh, obj):
if len(obj.Features) == 0:
return basemesh
mesh = basemesh
for meshFeature in obj.Features:
mesh = meshFeature.Proxy.applyOperationToMesh(mesh)
return mesh
def executeBlender(self, basemesh, obj):
if len(obj.Features) == 0:
return basemesh
from blender import blender_processor
operations = [(feature.Proxy.getMesh(), feature.Mode, feature.Name)
for feature in obj.Features if feature.Enabled]
# no operations enabled
if len(operations) == 0:
return basemesh
return blender_processor.applyBooleanOperations(basemesh, operations)
def addAdditiveFeature(self, base):
createFeature(self.Object, base, 'Additive')
def addSubtractiveFeature(self, base):
createFeature(self.Object, base, 'Subtractive')
def setProperties(self, obj):
self.Object = obj
self.isBooleanMesh = True
pl = obj.PropertiesList
if not 'LithophaneImage' in pl:
obj.addProperty("App::PropertyLink", "LithophaneImage", "Image",
"The image used to build the geometry")
if not 'Result' in pl:
obj.addProperty("App::PropertyLink", "Result", "Mesh",
"The mesh that stores the final result")
if not 'Features' in pl:
obj.addProperty("App::PropertyLinkList", "Features", "Feature",
"The boolean operations to apply to the mesh")
def onDocumentRestored(self, obj):
self.setProperties(obj)
def __getstate__(self):
'''We do not store any data for now'''
pass
def __setstate__(self, state):
'''We do not store any data for now'''
pass
class ViewProviderBooleanMesh():
def __init__(self, vobj):
vobj.Proxy = self
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
self.BooleanMesh = self.Object.Proxy
from pivy import coin
self.coinNode = coin.SoGroup()
vobj.addDisplayMode(self.coinNode, "Standard")
def onChanged(self, vp, prop):
if prop == 'Visibility':
self.Object.Result.ViewObject.Visibility = vp.Visibility
def onDelete(self, vp, subelements):
if self.Object.Result:
FreeCAD.ActiveDocument.removeObject(self.Object.Result.Name)
return True
def claimChildren(self):
children = []
children.extend(self.Object.Features)
children.append(self.Object.Result)
return children
def getDisplayModes(self, obj):
return ["Standard"]
def getDefaultDisplayMode(self):
return "Standard"
def getIcon(self):
return self.BooleanMesh.getIcon()
def __getstate__(self):
'''We do not store any data for now'''
pass
def __setstate__(self, state):
'''We do not store any data for now'''
pass
def createFeature(booleanMesh, base, mode):
feature = FreeCAD.ActiveDocument.addObject(
"App::FeaturePython", base.Label + "_" + mode)
BooleanMeshFeature(feature, base, mode)
ViewProviderBooleanMeshFeature(feature.ViewObject)
features = booleanMesh.Features
features.append(feature)
booleanMesh.Features = features
if __name__ == '__main__':
class DummyMesh(BooleanMesh):
def __init__(self, obj):
super(DummyMesh, self).__init__(obj)
def getDescription(self):
return 'Dummy'
def checkBaseMeshExecution(self):
return '<ignore>'
def getBaseProcessingSteps(self, obj):
return []
def extractBaseMesh(self, obj, params):
return Mesh.createBox()
booleanMesh = FreeCAD.ActiveDocument.addObject(
"App::FeaturePython", 'DummyMesh')
DummyMesh(booleanMesh)
ViewProviderBooleanMesh(booleanMesh.ViewObject)
additiveBox = App.ActiveDocument.addObject("Part::Box", "AdditiveBox")
createFeature(booleanMesh, additiveBox, 'Additive')
subtractiveBox = App.ActiveDocument.addObject(
"Part::Box", "SubtractiveBox")
subtractiveBox.Placement = App.Placement(
App.Vector(-10, -10, -10), App.Rotation(App.Vector(0, 0, 1), 0))
createFeature(booleanMesh, subtractiveBox, 'Subtractive')