forked from chbergmann/CurvedShapesWorkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSurfaceCut.py
183 lines (133 loc) · 5.98 KB
/
SurfaceCut.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
# -*- coding: utf-8 -*-
__title__ = "SurfaceCut"
__author__ = "Christian Bergmann"
__license__ = "LGPL 2.1"
__doc__ = "Create 3D shapes from 2D curves"
import os
import FreeCADGui
import FreeCAD
from FreeCAD import Vector
import Part
import CurvedShapes
epsilon = CurvedShapes.epsilon
class SurfaceCutWorker:
def __init__(self, obj, Surfaces=[], Normal=Vector(0, 0, 1), Position=0, Face=False, Simplify=0):
obj.addProperty("App::PropertyLinkList", "Surfaces", "SurfaceCut", "List of objects with a surface").Surfaces = Surfaces
obj.addProperty("App::PropertyVector", "Normal", "SurfaceCut", "Normal vector of the cut plane").Normal = Normal
obj.addProperty("App::PropertyVector", "Position", "SurfaceCut", "Position of the cut plane relative to Surfaces").Position = Position
obj.addProperty("App::PropertyBool", "Face", "SurfaceCut", "make a face").Face = Face
obj.addProperty("App::PropertyQuantity", "Simplify", "SurfaceCut", "if >0, discretize each edge to this number of poles and approximate.").Simplify = Simplify
self.update = True
obj.Proxy = self
def execute(self, fp):
self.cutSurfaces(fp)
self.makeFace(fp)
def makeFace(self, fp):
if fp.Face and len(fp.Shape.Wires) > 0:
face = Part.makeFace(fp.Shape.Wires, "Part::FaceMakerSimple")
fp.Shape = face
def onChanged(self, fp, prop):
props = ["Surfaces", "Position", "Normal", "Simplify"]
if prop in props:
self.execute(fp)
if prop == "Face":
if fp.Face == True:
self.makeFace(fp)
else:
fp.Shape = Part.Compound(fp.Shape.Wires)
def cutSurfaces(self, fp):
edges=list()
if len(fp.Surfaces) == 1:
vOffset = fp.Surfaces[0].Placement.Base
else:
bbox = None
for obj in fp.Surfaces:
if not bbox:
bbox = obj.Shape.BoundBox
else:
bbox = bbox.united(obj.Shape.BoundBox)
vOffset = Vector(bbox.XMin, bbox.YMin, bbox.ZMin)
vOffset += fp.Position
origin = Vector(0,0,0)
off = origin.distanceToPlane(vOffset, fp.Normal) * -1
for obj in fp.Surfaces:
for wire in obj.Shape.slice(fp.Normal, off):
edges += wire.Edges
if fp.Simplify > 0:
edges = self.removeEdgeComplexity(fp, edges)
edges = self.removeDoubles(edges)
comp = Part.Compound(edges)
comp.connectEdgesToWires(False, 1e-7)
fp.Shape = comp
def removeEdgeComplexity(self, fp, edges):
newedges = []
for e in edges:
if fp.Simplify > 0 and type(e.Curve) == Part.BSplineCurve and e.Curve.NbPoles > fp.Simplify:
poles = e.discretize(Deflection = 1.0)
bc = Part.BSplineCurve()
bc.approximate(poles)
newedges.append(bc.toShape().Edges[0])
else:
newedges.append(e)
return newedges
def removeDoubles(self, edges):
newedges = []
for e in edges:
found = False
for e2 in edges:
if e != e2 and self.isSameEdge(e, e2):
found = True
break
if not found:
newedges.append(e)
return newedges
def isSameEdge(self, e1, e2):
pol1 = e1.discretize(Deflection = 1.0)
pol2 = e2.discretize(Deflection = 1.0)
if len(pol1) != len(pol2): return False
equal = True
for n in range(len(pol1)):
v = pol1[n] - pol2[n]
if v.Length > epsilon: equal = False
if equal: return True
for n in range(len(pol1)):
v = pol1[n] - pol2[len(pol1) - n - 1]
if v.Length > epsilon: return False
return True
class SurfaceCutViewProvider:
def __init__(self, vfp):
vfp.Proxy = self
self.Object = vfp.Object
def getIcon(self):
return (os.path.join(CurvedShapes.get_module_path(), "Resources", "icons", "surfaceCut.svg"))
def attach(self, vobj):
self.Object = vobj.Object
def claimChildren(self):
return self.Object.Surfaces
def onDelete(self, feature, subelements):
return True
def onChanged(self, fp, prop):
pass
class SurfaceCut():
def Activated(self):
import SurfaceCut
FreeCADGui.doCommand("import CurvedShapes")
selection = FreeCADGui.Selection.getSelectionEx()
FreeCADGui.doCommand("curves = []")
for sel in selection:
FreeCADGui.doCommand("curves.append(FreeCAD.ActiveDocument.getObject('%s'))"%(sel.ObjectName))
FreeCADGui.doCommand("CurvedShapes.cutSurfaces(curves, Normal = FreeCAD.Vector(0, 0, 1), Position=FreeCAD.Vector(0,0,0), Face=False, Simplify=False)")
FreeCAD.ActiveDocument.recompute()
def IsActive(self):
"""Here you can define if the command must be active or not (greyed) if certain conditions
are met or not. This function is optional."""
if FreeCAD.ActiveDocument:
return(True)
else:
return(False)
def GetResources(self):
return {'Pixmap' : os.path.join(CurvedShapes.get_module_path(), "Resources", "icons", "surfaceCut.svg"),
'Accel' : "", # a default shortcut (optional)
'MenuText': "Surface Cut",
'ToolTip' : "Creates a wire by cutting through surfaces" }
FreeCADGui.addCommand('SurfaceCut', SurfaceCut())