-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCurvedSegment.py
528 lines (430 loc) · 22.3 KB
/
CurvedSegment.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# -*- coding: utf-8 -*-
__title__ = "CurvedSegment"
__author__ = "Christian Bergmann"
__license__ = "LGPL 2.1"
__doc__ = "Interpolates a 3D shape between two 2D curves and optional hullcurves"
import os
import FreeCAD
from FreeCAD import Vector
import Part
import CurvedShapes
import math
if FreeCAD.GuiUp:
import FreeCADGui
epsilon = CurvedShapes.epsilon
class CurvedSegment:
def __init__(self,
fp, # FeaturePython
shape1 = None,
shape2 = None,
hullcurves=[],
normalShape1=Vector(0,0,0),
normalShape2=Vector(0,0,0),
items=2,
surface=False,
solid=False,
InterpolationPoints=16,
Twist = 0.0,
TwistReverse = False,
Distribution = 'linear',
DistributionReverse = False,
LoftMaxDegree=5,
MaxLoftSize=16,
Path=None,
ForceInterpolated = False):
CurvedShapes.addObjectProperty(fp,"App::PropertyLink", "Shape1", "CurvedSegment", "The first object of the segment").Shape1 = shape1
CurvedShapes.addObjectProperty(fp,"App::PropertyLink", "Shape2", "CurvedSegment", "The last object of the segment").Shape2 = shape2
CurvedShapes.addObjectProperty(fp,"App::PropertyLinkList", "Hullcurves", "CurvedSegment", "Bounding curves").Hullcurves = hullcurves
CurvedShapes.addObjectProperty(fp,"App::PropertyVector", "NormalShape1", "CurvedSegment", "Direction axis of Shape1").NormalShape1 = normalShape1
CurvedShapes.addObjectProperty(fp,"App::PropertyVector", "NormalShape2", "CurvedSegment", "Direction axis of Shape2").NormalShape1 = normalShape2
CurvedShapes.addObjectProperty(fp,"App::PropertyInteger", "Items", "CurvedSegment", "Nr. of items between the segments").Items = items
CurvedShapes.addObjectProperty(fp,"App::PropertyBool", "makeSurface","CurvedSegment", "Make a surface").makeSurface = surface
CurvedShapes.addObjectProperty(fp,"App::PropertyBool", "makeSolid","CurvedSegment", "Make a solid").makeSolid = solid
CurvedShapes.addObjectProperty(fp,"App::PropertyInteger", "InterpolationPoints", "CurvedSegment", "Unequal edges will be split into this number of points").InterpolationPoints = InterpolationPoints
CurvedShapes.addObjectProperty(fp,"App::PropertyFloat", "Twist","CurvedSegment", "Compensates a rotation between Shape1 and Shape2").Twist = Twist
CurvedShapes.addObjectProperty(fp,"App::PropertyBool", "TwistReverse","CurvedSegment", "Reverses the rotation of one Shape").TwistReverse = TwistReverse
CurvedShapes.addObjectProperty(fp,"App::PropertyEnumeration", "Distribution", "CurvedSegment", "Algorithm for distance between elements")
CurvedShapes.addObjectProperty(fp,"App::PropertyBool", "DistributionReverse", "CurvedSegment", "Reverses direction of Distribution algorithm").DistributionReverse = DistributionReverse
CurvedShapes.addObjectProperty(fp,"App::PropertyInteger", "LoftMaxDegree", "CurvedSegment", "Max Degree for Surface or Solid").LoftMaxDegree = LoftMaxDegree
CurvedShapes.addObjectProperty(fp,"App::PropertyInteger", "MaxLoftSize", "CurvedSegment", "Max Size of a Loft in Segments.").MaxLoftSize = MaxLoftSize
CurvedShapes.addObjectProperty(fp,"App::PropertyLink", "Path", "CurvedSegment", "Sweep path").Path = Path
CurvedShapes.addObjectProperty(fp,"App::PropertyBool", "ForceInterpolated","CurvedSegment", "Force Interpolation of sketches").ForceInterpolated = ForceInterpolated
fp.Distribution = ['linear', 'parabolic', 'x³', 'sinusoidal', 'asinusoidal', 'elliptic']
fp.Distribution = Distribution
self.doScaleXYZ = []
self.doScaleXYZsum = [False, False, False]
self.update = True
fp.Proxy = self
def execute(self, fp):
if not self.update:
return
if not fp.Shape1 or not hasattr(fp.Shape1, "Shape") or len(fp.Shape1.Shape.Edges) == 0:
return
if not fp.Shape2 or not hasattr(fp.Shape2, "Shape") or len(fp.Shape2.Shape.Edges) == 0:
return
if fp.InterpolationPoints <= 1:
return
try:
self.update = False
if fp.NormalShape1 == Vector(0,0,0):
fp.NormalShape1 = CurvedShapes.getNormal(fp.Shape1)
if fp.NormalShape2 == Vector(0,0,0):
fp.NormalShape2 = CurvedShapes.getNormal(fp.Shape2)
self.doScaleXYZ = []
self.doScaleXYZsum = [False, False, False]
for h in fp.Hullcurves:
bbox = h.Shape.BoundBox
doScale = [False, False, False]
if bbox.XLength > epsilon:
doScale[0] = True
self.doScaleXYZsum[0] = True
if bbox.YLength > epsilon:
doScale[1] = True
self.doScaleXYZsum[1] = True
if bbox.ZLength > epsilon:
doScale[2] = True
self.doScaleXYZsum[2] = True
self.doScaleXYZ.append(doScale)
if fp.Items > 0:
self.makeRibs(fp)
self.update = True
except Exception as ex:
self.update = True
raise ex
def onChanged(self, fp, prop):
if not hasattr(fp, 'LoftMaxDegree'):
CurvedShapes.addObjectProperty(fp, "App::PropertyInteger", "LoftMaxDegree", "CurvedSegment", "Max Degree for Surface or Solid", init_val=5) # backwards compatibility - this upgrades older documents
if not hasattr(fp, 'MaxLoftSize'):
CurvedShapes.addObjectProperty(fp,"App::PropertyInteger", "MaxLoftSize", "CurvedSegment", "Max Size of a Loft in Segments.", init_val=-1) # backwards compatibility - this upgrades older documents
if not hasattr(fp, 'Path'):
CurvedShapes.addObjectProperty(fp,"App::PropertyLink", "Path", "CurvedSegment", "Sweep path", init_val=None) # backwards compatibility - this upgrades older documents
if not hasattr(fp, 'ForceInterpolated'):
CurvedShapes.addObjectProperty(fp,"App::PropertyBool", "ForceInterpolated","CurvedSegment", "Force Interpolation of sketches", init_val=False) # backwards compatibility - this upgrades older documents
def makeRibs(self, fp):
interpolate = False
if fp.ForceInterpolated or len(fp.Shape1.Shape.Edges) != len(fp.Shape2.Shape.Edges):
interpolate = True
else:
for e in range(0, len(fp.Shape1.Shape.Edges)):
edge1 = fp.Shape1.Shape.Edges[e]
edge2 = fp.Shape2.Shape.Edges[e]
curve1 = edge1.Curve.toBSpline(edge1.FirstParameter, edge1.LastParameter)
curve2 = edge2.Curve.toBSpline(edge2.FirstParameter, edge2.LastParameter)
poles1 = curve1.getPoles()
poles2 = curve2.getPoles()
if len(poles1) != len(poles2):
interpolate = True
break
makeStartEnd = fp.makeSurface or fp.makeSolid
if interpolate:
ribs = makeRibsInterpolate(fp, fp.Items, False, makeStartEnd)
else:
ribs = makeRibsSameShape(fp, fp.Items, False, makeStartEnd)
self.rescaleRibs(fp, ribs)
if fp.makeSurface or fp.makeSolid:
fp.Shape = CurvedShapes.makeSurfaceSolid(ribs, fp.makeSolid, maxDegree=fp.LoftMaxDegree, maxLoftSize=fp.MaxLoftSize)
else:
fp.Shape = Part.makeCompound(ribs)
def rescaleRibs(self, fp, ribs):
if (fp.makeSurface or fp.makeSolid) and fp.Path is None and abs(fp.Twist)<=epsilon:
start = 1
end = len(ribs) - 1
items = fp.Items + 3
else:
start = 0
end = len(ribs)
items = fp.Items + 1
maxlen = 0
edgelen = []
if fp.Path is not None:
edges = Part.__sortEdges__(fp.Path.Shape.Edges)
for edge in edges:
maxlen += edge.Length
edgelen.append(edge.Length)
bc0=fp.Shape1.Shape.Placement.Base
bc1=fp.Shape2.Shape.Placement.Base # makes rotating assymetric shapes easier - taking sketch origin into account
for i in range(start, end):
d = CurvedShapes.distribute(i / items, fp.Distribution, fp.DistributionReverse)
normal = CurvedShapes.vectorMiddle(fp.NormalShape1, fp.NormalShape2, d)
#Draft.makeLine(ribs[i].BoundBox.Center, ribs[i].BoundBox.Center + normal)
ribs[i] = ribs[i].rotate(bc0+d*(bc1-bc0), normal, fp.Twist * d)
direction = normal
if maxlen>0:
plen = d * maxlen
for edge in edges:
if plen > edge.Length:
plen -= edge.Length
else:
param = edge.getParameterByLength(plen)
direction = edge.tangentAt(param)
posvec = edge.valueAt(param)
rotaxis = normal.cross(direction)
angle = math.degrees(normal.getAngle(direction))
if rotaxis.Length>epsilon:
ribs[i] = ribs[i].rotate(bc0+d*(bc1-bc0), rotaxis, angle)
ribs[i].Placement.Base = posvec
if len(fp.Hullcurves) > 0:
bbox = CurvedShapes.boundbox_from_intersect(fp.Hullcurves, ribs[i].BoundBox.Center, direction, self.doScaleXYZ)
if bbox:
ribs[i] = CurvedShapes.scaleByBoundbox(ribs[i], bbox, self.doScaleXYZsum, copy=False)
#background compatibility
CurvedSegmentWorker = CurvedSegment
def vectorMiddlePlane(vec1, vec2, fraction, plane):
line = Part.makeLine(vec1, vec2)
isec = plane.intersect(line.Curve)
if not isec or len(isec[0]) != 1:
return CurvedShapes.vectorMiddle(vec1, vec2, fraction)
return CurvedShapes.PointVec(isec[0][0])
def vectorMiddlePlaneNormal1(vec1, vec2, normalShape1, normalShape2):
rota90 = FreeCAD.Rotation(normalShape1.cross(normalShape2), 90)
normal90 = rota90.multVec(normalShape1)
plane1 = Part.Plane(vec1, normal90)
line2 = Part.makeLine(vec2, vec2 + normalShape2)
p1 = vec1
isec = plane1.intersect(line2.Curve)
if isec and len(isec[0]) == 1:
p1 = CurvedShapes.PointVec(isec[0][0])
return p1
def vectorMiddlePlaneNormal(vec1, vec2, fraction, normalShape1, normalShape2):
if fraction == 0:
return vec1
if fraction == 1:
return vec2
p1 = vectorMiddlePlaneNormal1(vec1, vec2, normalShape1, normalShape2)
p2 = vectorMiddlePlaneNormal1(vec2, vec1, normalShape2, normalShape1)
return CurvedShapes.vectorMiddle(p1, p2, 0.5)
def getMidPlane(fp, fraction):
midvec = CurvedShapes.vectorMiddle(fp.Shape1.Shape.BoundBox.Center, fp.Shape2.Shape.BoundBox.Center, fraction)
midnorm = CurvedShapes.vectorMiddle(fp.NormalShape1, fp.NormalShape2, fraction)
return Part.Plane(midvec, midnorm)
def makeRibsSameShape(fp, items, alongNormal, makeStartEnd = False):
ribs = []
base1=fp.Shape1.Placement.Base
base2=fp.Shape2.Placement.Base
offset=base2-base1
if makeStartEnd and ((fp.Path is not None) or (abs(fp.Twist)>epsilon)):
start=0
end=items+2
else:
start=1
end=items+1
for i in range(start, end):
if hasattr(fp, "Distribution"):
fraction = CurvedShapes.distribute(i / (items + 1), fp.Distribution, fp.DistributionReverse)
else:
fraction = i / (items + 1)
plane = getMidPlane(fp, fraction)
edges = []
curves = []
edges2 = fp.Shape2.Shape.Edges.copy()
if fp.TwistReverse:
edges2.reverse()
for e in range(0, len(fp.Shape1.Shape.Edges)):
edge1 = fp.Shape1.Shape.Edges[e]
edge2 = edges2[e]
curve1 = edge1.Curve.toBSpline(edge1.FirstParameter, edge1.LastParameter)
curve2 = edge2.Curve.toBSpline(edge2.FirstParameter, edge2.LastParameter)
poles1 = curve1.getPoles()
poles2 = curve2.getPoles()
newpoles = []
for p in range(len(poles1)):
if alongNormal:
newpoles.append(vectorMiddlePlaneNormal(poles1[p], poles2[p], fraction, fp.NormalShape1, fp.NormalShape2)-fraction*offset)
else:
newpoles.append(vectorMiddlePlane(poles1[p], poles2[p], fraction, plane)-fraction*offset)
# coordinate has fraction*offset substracted to force the shape to be centered on itself, important for later rotation on path
newcurve = Part.BSplineCurve()
newcurve.buildFromPolesMultsKnots(newpoles,
curve1.getMultiplicities(),
curve1.getKnots(),
curve1.isPeriodic(),
curve1.Degree,
curve1.getWeights(),
curve1.isRational())
rib = newcurve.toShape()
curves.append(rib)
edges += rib.Edges
try:
wire = Part.Wire(edges)
ribs.append(wire)
except Exception as ex:
if len(curves) == 1:
ribs.append(curves[0])
else:
ribs.append(Part.makeCompound(curves))
ribs[-1].Placement.Base=fraction*offset #place the whole rib in the right place instead
if makeStartEnd and fp.Path is None and abs(fp.Twist)<=epsilon:
ribs.insert(0, fp.Shape1.Shape)
ribs.append(fp.Shape2.Shape)
return ribs
def makeRibsInterpolate(fp, items, alongNormal, makeStartEnd = False):
s1=fp.Shape1.Shape.toNurbs()
s2=fp.Shape2.Shape.toNurbs()
len1 = len(s1.Edges)
len2 = len(s2.Edges)
nr_edges = int(len1 * len2 / math.gcd(len1, len2))
pointslist1 = EdgesToPoints(s1, int(nr_edges / len1), int(fp.InterpolationPoints))
pointslist2 = EdgesToPoints(s2, int(nr_edges / len2), int(fp.InterpolationPoints), fp.TwistReverse)
ribs = []
if makeStartEnd:
start = 0
end = items + 2
else:
start = 1
end = items + 1
base1=s1.Placement.Base
base2=s2.Placement.Base
offset=base2-base1
for i in range(start, end):
if hasattr(fp, "Distribution"):
fraction = CurvedShapes.distribute(i / (items + 1), fp.Distribution, fp.DistributionReverse)
else:
fraction = i / (items + 1)
plane = getMidPlane(fp, fraction)
newshape = []
for l in range(0, len(pointslist1)):
points1 = pointslist1[l]
points2 = pointslist2[l]
if(fp.TwistReverse):
points2.reverse()
newpoles = []
for p in range(0, fp.InterpolationPoints):
if alongNormal:
newpoles.append(vectorMiddlePlaneNormal(points1[p], points2[p], fraction, fp.NormalShape1, fp.NormalShape2)-fraction*offset)
else:
newpoles.append(vectorMiddlePlane(points1[p], points2[p], fraction, plane)-fraction*offset)
# coordinate has fraction*offset substracted to force the shape to be centered on itself, important for later rotation on path
bc = Part.BSplineCurve()
bc.approximate(newpoles)
newshape.append(bc)
if len(newshape) == 1:
sh = newshape[0].toShape()
try:
wire = Part.Wire(sh.Edges)
ribs.append(wire)
except Exception as ex:
ribs.append(sh)
else:
shapes = []
for n in newshape:
shapes.append(n.toShape())
comp = Part.makeCompound(shapes)
try:
wire = Part.Wire(comp.Edges)
ribs.append(wire)
except Exception as ex:
ribs.append(comp)
ribs[-1].Placement.Base=fraction*offset #place the whole rib in the right place instead
return ribs
def EdgesToPoints(shape, nr_frac, points_per_edge, twistReverse = False):
edges = []
sortedEdges=sum(Part.sortEdges(shape.Edges),[])
if twistReverse:
sortedEdges.reverse()
if nr_frac == 1:
edges = sortedEdges
else:
for edge in sortedEdges:
edge1 = edge
for f in range(0, nr_frac - 1):
wires1 = edge1.split(edge1.FirstParameter + (edge1.LastParameter - edge1.FirstParameter) / nr_frac)
edges.append(wires1.Edges[0])
edge1 = wires1.Edges[1]
edges.append(wires1.Edges[1])
llpoints = []
for edge in edges:
edge.Placement = shape.Placement
llpoints.append(edge.discretize(points_per_edge))
return llpoints
class CurvedSegmentViewProvider:
def __init__(self, vfp):
vfp.Proxy = self
self.Object = vfp.Object
def getIcon(self):
if self.Object.Path:
return (os.path.join(CurvedShapes.get_module_path(), "Resources", "icons", "CurvedPathSegment.svg"))
else:
return (os.path.join(CurvedShapes.get_module_path(), "Resources", "icons", "curvedSegment.svg"))
def attach(self, vfp):
self.Object = vfp.Object
self.onChanged(vfp,"Shape1")
def claimChildren(self):
return [self.Object.Shape1, self.Object.Shape2, self.Object.Path] + self.Object.Hullcurves
def onDelete(self, feature, subelements):
return True
def onChanged(self, fp, prop):
pass
def loads(self, state):
return None
def dumps(self):
return None
def __getstate__(self):
return None
def __setstate__(self,state):
return None
if FreeCAD.GuiUp:
class CurvedSegmentCommand():
def Activated(self):
FreeCADGui.doCommand("import CurvedShapes")
selection = FreeCADGui.Selection.getSelectionEx()
options = ""
for sel in selection:
if sel == selection[0]:
FreeCADGui.doCommand("shape1 = FreeCAD.ActiveDocument.getObject('%s')"%(selection[0].ObjectName))
options += "Shape1=shape1, "
elif sel == selection[1]:
FreeCADGui.doCommand("shape2 = FreeCAD.ActiveDocument.getObject('%s')"%(selection[1].ObjectName))
options += "Shape2=shape2, "
FreeCADGui.doCommand("hullcurves = []");
options += "Hullcurves=hullcurves, "
else:
FreeCADGui.doCommand("hullcurves.append(FreeCAD.ActiveDocument.getObject('%s'))"%(sel.ObjectName))
FreeCADGui.doCommand("CurvedShapes.makeCurvedSegment(%sItems=16, Surface=False, Solid=False)"%(options))
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", "curvedSegment.svg"),
'Accel' : "", # a default shortcut (optional)
'MenuText': "Curved Segment",
'ToolTip' : __doc__ }
class CurvedPathSegmentCommand():
def Activated(self):
FreeCADGui.doCommand("import CurvedShapes")
selection = FreeCADGui.Selection.getSelectionEx()
options = ""
for sel in selection:
if sel == selection[0]:
FreeCADGui.doCommand("shape1 = FreeCAD.ActiveDocument.getObject('%s')"%(selection[0].ObjectName))
options += "Shape1=shape1, "
elif sel == selection[1]:
FreeCADGui.doCommand("shape2 = FreeCAD.ActiveDocument.getObject('%s')"%(selection[1].ObjectName))
options += "Shape2=shape2, "
elif sel == selection[2]:
FreeCADGui.doCommand("path = FreeCAD.ActiveDocument.getObject('%s')"%(selection[2].ObjectName))
options += "Path=path, "
FreeCADGui.doCommand("hullcurves = []");
options += "Hullcurves=hullcurves, "
else:
FreeCADGui.doCommand("hullcurves.append(FreeCAD.ActiveDocument.getObject('%s'))"%(sel.ObjectName))
FreeCADGui.doCommand("CurvedShapes.makeCurvedSegment(%sItems=16, Surface=False, Solid=False)"%(options))
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", "CurvedPathSegment.svg"),
'Accel' : "", # a default shortcut (optional)
'MenuText': "Curved Path Segment",
'ToolTip' : __doc__ + " along a path" }
FreeCADGui.addCommand('CurvedSegment', CurvedSegmentCommand())
FreeCADGui.addCommand('CurvedPathSegment', CurvedPathSegmentCommand())