/
PathDressupHoldingTags.py
1427 lines (1238 loc) · 58.4 KB
/
PathDressupHoldingTags.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2016 sliptonic <shopinthewoods@gmail.com> *
# * *
# * 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 *
# * *
# ***************************************************************************
import FreeCAD
import FreeCADGui
import Draft
import DraftGeomUtils
import Path
import PathScripts.PathLog as PathLog
import PathScripts.PathPreferencesPathDressup as PathPreferencesPathDressup
import Part
import copy
import math
from PathScripts import PathUtils
from PathScripts.PathGeom import PathGeom
from PathScripts.PathPreferences import PathPreferences
from PySide import QtCore
"""Holding Tags Dressup object and FreeCAD command"""
# Qt tanslation handling
def translate(text, context = "PathDressup_HoldingTags", disambig=None):
return QtCore.QCoreApplication.translate(context, text, disambig)
LOG_MODULE = 'PathDressupHoldingTags'
#PathLog.setLevel(PathLog.Level.DEBUG, LOG_MODULE)
if FreeCAD.GuiUp:
from pivy import coin
from PySide import QtCore
def debugEdge(edge, prefix, force = False):
if force or PathLog.getLevel(LOG_MODULE) == PathLog.Level.DEBUG:
pf = edge.valueAt(edge.FirstParameter)
pl = edge.valueAt(edge.LastParameter)
if type(edge.Curve) == Part.Line or type(edge.Curve) == Part.LineSegment:
print("%s %s((%.2f, %.2f, %.2f) - (%.2f, %.2f, %.2f))" % (prefix, type(edge.Curve), pf.x, pf.y, pf.z, pl.x, pl.y, pl.z))
else:
pm = edge.valueAt((edge.FirstParameter+edge.LastParameter)/2)
print("%s %s((%.2f, %.2f, %.2f) - (%.2f, %.2f, %.2f) - (%.2f, %.2f, %.2f))" % (prefix, type(edge.Curve), pf.x, pf.y, pf.z, pm.x, pm.y, pm.z, pl.x, pl.y, pl.z))
def debugMarker(vector, label, color = None, radius = 0.5):
if PathLog.getLevel(LOG_MODULE) == PathLog.Level.DEBUG:
obj = FreeCAD.ActiveDocument.addObject("Part::Sphere", label)
obj.Label = label
obj.Radius = radius
obj.Placement = FreeCAD.Placement(vector, FreeCAD.Rotation(FreeCAD.Vector(0,0,1), 0))
if color:
obj.ViewObject.ShapeColor = color
def debugCylinder(vector, r, height, label, color = None):
if PathLog.getLevel(LOG_MODULE) == PathLog.Level.DEBUG:
obj = FreeCAD.ActiveDocument.addObject("Part::Cylinder", label)
obj.Label = label
obj.Radius = r
obj.Height = height
obj.Placement = FreeCAD.Placement(vector, FreeCAD.Rotation(FreeCAD.Vector(0,0,1), 0))
obj.ViewObject.Transparency = 90
if color:
obj.ViewObject.ShapeColor = color
def debugCone(vector, r1, r2, height, label, color = None):
if PathLog.getLevel(LOG_MODULE) == PathLog.Level.DEBUG:
obj = FreeCAD.ActiveDocument.addObject("Part::Cone", label)
obj.Label = label
obj.Radius1 = r1
obj.Radius2 = r2
obj.Height = height
obj.Placement = FreeCAD.Placement(vector, FreeCAD.Rotation(FreeCAD.Vector(0,0,1), 0))
obj.ViewObject.Transparency = 90
if color:
obj.ViewObject.ShapeColor = color
class HoldingTagsPreferences:
DefaultHoldingTagWidth = 'DefaultHoldingTagWidth'
DefaultHoldingTagHeight = 'DefaultHoldingTagHeight'
DefaultHoldingTagAngle = 'DefaultHoldingTagAngle'
DefaultHoldingTagRadius = 'DefaultHoldingTagRadius'
DefaultHoldingTagCount = 'DefaultHoldingTagCount'
@classmethod
def defaultWidth(cls, ifNotSet):
value = PathPreferences.preferences().GetFloat(cls.DefaultHoldingTagWidth, ifNotSet)
if value == 0.0:
return ifNotSet
return value
@classmethod
def defaultHeight(cls, ifNotSet):
value = PathPreferences.preferences().GetFloat(cls.DefaultHoldingTagHeight, ifNotSet)
if value == 0.0:
return ifNotSet
return value
@classmethod
def defaultAngle(cls, ifNotSet = 45.0):
value = PathPreferences.preferences().GetFloat(cls.DefaultHoldingTagAngle, ifNotSet)
if value < 10.0:
return ifNotSet
return value
@classmethod
def defaultCount(cls, ifNotSet = 4):
value = PathPreferences.preferences().GetUnsigned(cls.DefaultHoldingTagCount, ifNotSet)
if value < 2:
return float(ifNotSet)
return float(value)
@classmethod
def defaultRadius(cls, ifNotSet = 0.0):
return PathPreferences.preferences().GetFloat(cls.DefaultHoldingTagRadius, ifNotSet)
def __init__(self):
self.form = FreeCADGui.PySideUic.loadUi(":/preferences/PathDressupHoldingTags.ui")
self.label = translate('Holding Tags')
def loadSettings(self):
self.form.ifWidth.setText(FreeCAD.Units.Quantity(self.defaultWidth(0), FreeCAD.Units.Length).UserString)
self.form.ifHeight.setText(FreeCAD.Units.Quantity(self.defaultHeight(0), FreeCAD.Units.Length).UserString)
self.form.dsbAngle.setValue(self.defaultAngle())
self.form.ifRadius.setText(FreeCAD.Units.Quantity(self.defaultRadius(), FreeCAD.Units.Length).UserString)
self.form.sbCount.setValue(self.defaultCount())
def saveSettings(self):
pref = PathPreferences.preferences()
pref.SetFloat(self.DefaultHoldingTagWidth, FreeCAD.Units.Quantity(self.form.ifWidth.text()).Value)
pref.SetFloat(self.DefaultHoldingTagHeight, FreeCAD.Units.Quantity(self.form.ifHeight.text()).Value)
pref.SetFloat(self.DefaultHoldingTagAngle, self.form.dsbAngle.value())
pref.SetFloat(self.DefaultHoldingTagRadius, FreeCAD.Units.Quantity(self.form.ifRadius.text()))
pref.SetUnsigned(self.DefaultHoldingTagCount, self.form.sbCount.value())
@classmethod
def preferencesPage(cls):
return HoldingTagsPreferences()
class Tag:
def __init__(self, x, y, width, height, angle, radius, enabled=True):
PathLog.track("%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d" % (x, y, width, height, angle, radius, enabled))
self.x = x
self.y = y
self.width = math.fabs(width)
self.height = math.fabs(height)
self.actualHeight = self.height
self.angle = math.fabs(angle)
self.radius = radius
self.enabled = enabled
self.isSquare = False
def fullWidth(self):
return 2 * self.toolRadius + self.width
def originAt(self, z):
return FreeCAD.Vector(self.x, self.y, z)
def bottom(self):
return self.z
def top(self):
return self.z + self.actualHeight
def createSolidsAt(self, z, R):
self.z = z
self.toolRadius = R
r1 = self.fullWidth() / 2
self.r1 = r1
self.r2 = r1
height = self.height * 1.01
radius = 0
if self.angle == 90 and height > 0:
# cylinder
self.isSquare = True
self.solid = Part.makeCylinder(r1, height)
radius = min(min(self.radius, r1), self.height)
PathLog.debug("Part.makeCone(%f, %f)" % (r1, height))
elif self.angle > 0.0 and height > 0.0:
# cone
rad = math.radians(self.angle)
tangens = math.tan(rad)
dr = height / tangens
if dr < r1:
# with top
r2 = r1 - dr
s = height / math.sin(rad)
radius = min(r2, s) * math.tan((math.pi - rad)/2) * 0.95
else:
# triangular
r2 = 0
height = r1 * tangens * 1.01
self.actualHeight = height
self.r2 = r2
PathLog.debug("Part.makeCone(%f, %f, %f)" % (r1, r2, height))
self.solid = Part.makeCone(r1, r2, height)
else:
# degenerated case - no tag
PathLog.debug("Part.makeSphere(%f / 10000)" % (r1))
self.solid = Part.makeSphere(r1 / 10000)
if not R == 0: # testing is easier if the solid is not rotated
angle = -PathGeom.getAngle(self.originAt(0)) * 180 / math.pi
PathLog.debug("solid.rotate(%f)" % angle)
self.solid.rotate(FreeCAD.Vector(0,0,0), FreeCAD.Vector(0,0,1), angle)
orig = self.originAt(z - 0.01 * self.actualHeight)
PathLog.debug("solid.translate(%s)" % orig)
self.solid.translate(orig)
radius = min(self.radius, radius)
self.realRadius = radius
if radius != 0:
PathLog.debug("makeFillet(%.4f)" % radius)
self.solid = self.solid.makeFillet(radius, [self.solid.Edges[0]])
def filterIntersections(self, pts, face):
if type(face.Surface) == Part.Cone or type(face.Surface) == Part.Cylinder or type(face.Surface) == Part.Toroid:
PathLog.track("it's a cone/cylinder, checking z")
return filter(lambda pt: pt.z >= self.bottom() and pt.z <= self.top(), pts)
if type(face.Surface) == Part.Plane:
PathLog.track("it's a plane, checking R")
c = face.Edges[0].Curve
if (type(c) == Part.Circle):
return filter(lambda pt: (pt - c.Center).Length <= c.Radius or PathGeom.isRoughly((pt - c.Center).Length, c.Radius), pts)
print("==== we got a %s" % face.Surface)
def isPointOnEdge(self, pt, edge):
param = edge.Curve.parameter(pt)
if edge.FirstParameter <= param <= edge.LastParameter:
return True
if edge.LastParameter <= param <= edge.FirstParameter:
return True
if PathGeom.isRoughly(edge.FirstParameter, param) or PathGeom.isRoughly(edge.LastParameter, param):
return True
#print("-------- X %.2f <= %.2f <=%.2f (%.2f, %.2f, %.2f) %.2f:%.2f" % (edge.FirstParameter, param, edge.LastParameter, pt.x, pt.y, pt.z, edge.Curve.parameter(edge.valueAt(edge.FirstParameter)), edge.Curve.parameter(edge.valueAt(edge.LastParameter))))
p1 = edge.Vertexes[0]
f1 = edge.Curve.parameter(FreeCAD.Vector(p1.X, p1.Y, p1.Z))
p2 = edge.Vertexes[1]
f2 = edge.Curve.parameter(FreeCAD.Vector(p2.X, p2.Y, p2.Z))
return False
def nextIntersectionClosestTo(self, edge, solid, refPt):
ef = edge.valueAt(edge.FirstParameter)
em = edge.valueAt((edge.FirstParameter+edge.LastParameter)/2)
el = edge.valueAt(edge.LastParameter)
#print("-------- intersect %s (%.2f, %.2f, %.2f) - (%.2f, %.2f, %.2f) - (%.2f, %.2f, %.2f) refp=(%.2f, %.2f, %.2f)" % (type(edge.Curve), ef.x, ef.y, ef.z, em.x, em.y, em.z, el.x, el.y, el.z, refPt.x, refPt.y, refPt.z))
vertexes = edge.common(solid).Vertexes
if vertexes:
return sorted(vertexes, key=lambda v: (v.Point - refPt).Length)[0].Point
return None
def intersects(self, edge, param):
if self.enabled:
if edge.valueAt(edge.FirstParameter).z < self.top() or edge.valueAt(edge.LastParameter).z < self.top():
return self.nextIntersectionClosestTo(edge, self.solid, edge.valueAt(param))
return None
def bbEdges(self):
edges = []
for i in range(12):
p1, p2 = self.solid.BoundBox.getEdge(i)
edges.append(Part.Edge(Part.LineSegment(p1, p2)))
return edges
def bbShow(self):
for e in self.bbEdges():
Part.show(e)
class MapWireToTag:
def __init__(self, edge, tag, i, segm, maxZ):
debugEdge(edge, 'MapWireToTag(%.2f, %.2f, %.2f)' % (i.x, i.y, i.z))
self.tag = tag
self.segm = segm
self.maxZ = maxZ
if PathGeom.pointsCoincide(edge.valueAt(edge.FirstParameter), i):
tail = edge
self.commands = []
debugEdge(tail, '.........=')
elif PathGeom.pointsCoincide(edge.valueAt(edge.LastParameter), i):
debugEdge(edge, '++++++++ .')
self.commands = PathGeom.cmdsForEdge(edge, segm=segm)
tail = None
else:
e, tail = PathGeom.splitEdgeAt(edge, i)
debugEdge(e, '++++++++ .')
self.commands = PathGeom.cmdsForEdge(e, segm=segm)
debugEdge(tail, '.........-')
self.initialEdge = edge
self.tail = tail
self.edges = []
self.entry = i
if tail:
PathLog.debug("MapWireToTag(%s - %s)" % (i, tail.valueAt(tail.FirstParameter)))
else:
PathLog.debug("MapWireToTag(%s - )" % i)
self.complete = False
self.haveProblem = False
def addEdge(self, edge):
debugEdge(edge, '..........')
self.edges.append(edge)
def needToFlipEdge(self, edge, p):
if PathGeom.pointsCoincide(edge.valueAt(edge.LastParameter), p):
return True, edge.valueAt(edge.FirstParameter)
return False, edge.valueAt(edge.LastParameter)
def isEntryOrExitStrut(self, e):
p1 = e.valueAt(e.FirstParameter)
p2 = e.valueAt(e.LastParameter)
if PathGeom.pointsCoincide(p1, self.entry) and p2.z >= self.entry.z:
return 1
if PathGeom.pointsCoincide(p2, self.entry) and p1.z >= self.entry.z:
return 1
if PathGeom.pointsCoincide(p1, self.exit) and p2.z >= self.exit.z:
return 2
if PathGeom.pointsCoincide(p2, self.exit) and p1.z >= self.exit.z:
return 2
return 0
def cleanupEdges(self, edges):
# want to remove all edges from the wire itself, and all internal struts
PathLog.track("+cleanupEdges")
PathLog.debug(" edges:")
for e in edges:
debugEdge(e, ' ')
PathLog.debug(":")
self.edgesCleanup = [copy.copy(edges)]
# remove any edge that has a point inside the tag solid
# and collect all edges that are connected to the entry and/or exit
self.entryEdges = []
self.exitEdges = []
self.edgePoints = []
for e in copy.copy(edges):
p1 = e.valueAt(e.FirstParameter)
p2 = e.valueAt(e.LastParameter)
self.edgePoints.append(p1)
self.edgePoints.append(p2)
if self.tag.solid.isInside(p1, PathGeom.Tolerance, False) or self.tag.solid.isInside(p2, PathGeom.Tolerance, False):
edges.remove(e)
debugEdge(e, '......... X0', False)
else:
if PathGeom.pointsCoincide(p1, self.entry) or PathGeom.pointsCoincide(p2, self.entry):
self.entryEdges.append(e)
if PathGeom.pointsCoincide(p1, self.exit) or PathGeom.pointsCoincide(p2, self.exit):
self.exitEdges.append(e)
self.edgesCleanup.append(copy.copy(edges))
# if there are no edges connected to entry/exit, it means the plunge in/out is vertical
# we need to add in the missing segment and collect the new entry/exit edges.
if not self.entryEdges:
print("fill entryEdges ...")
self.realEntry = sorted(self.edgePoints, key=lambda p: (p - self.entry).Length)[0]
self.entryEdges = filter(lambda e: PathGeom.edgeConnectsTo(e, self.realEntry), edges)
edges.append(Part.Edge(Part.LineSegment(self.entry, self.realEntry)))
else:
self.realEntry = None
if not self.exitEdges:
print("fill exitEdges ...")
self.realExit = sorted(self.edgePoints, key=lambda p: (p - self.exit).Length)[0]
self.exitEdges = filter(lambda e: PathGeom.edgeConnectsTo(e, self.realExit), edges)
edges.append(Part.Edge(Part.LineSegment(self.realExit, self.exit)))
else:
self.realExit = None
self.edgesCleanup.append(copy.copy(edges))
# if there are 2 edges attached to entry/exit, throw away the one that is "lower"
if len(self.entryEdges) > 1:
debugEdge(self.entryEdges[0], ' entry[0]', False)
debugEdge(self.entryEdges[1], ' entry[1]', False)
if self.entryEdges[0].BoundBox.ZMax < self.entryEdges[1].BoundBox.ZMax:
edges.remove(self.entryEdges[0])
debugEdge(e, '......... X1', False)
else:
edges.remove(self.entryEdges[1])
debugEdge(e, '......... X2', False)
if len(self.exitEdges) > 1:
debugEdge(self.exitEdges[0], ' exit[0]', False)
debugEdge(self.exitEdges[1], ' exit[1]', False)
if self.exitEdges[0].BoundBox.ZMax < self.exitEdges[1].BoundBox.ZMax:
if self.exitEdges[0] in edges:
edges.remove(self.exitEdges[0])
debugEdge(e, '......... X3', False)
else:
if self.exitEdges[1] in edges:
edges.remove(self.exitEdges[1])
debugEdge(e, '......... X4', False)
self.edgesCleanup.append(copy.copy(edges))
return edges
def orderAndFlipEdges(self, edges):
PathLog.track("entry(%.2f, %.2f, %.2f), exit(%.2f, %.2f, %.2f)" % (self.entry.x, self.entry.y, self.entry.z, self.exit.x, self.exit.y, self.exit.z))
self.edgesOrder = []
outputEdges = []
p0 = self.entry
lastP = p0
while edges:
#print("(%.2f, %.2f, %.2f) %d %d" % (p0.x, p0.y, p0.z))
for e in edges:
p1 = e.valueAt(e.FirstParameter)
p2 = e.valueAt(e.LastParameter)
if PathGeom.pointsCoincide(p1, p0):
outputEdges.append((e, False))
edges.remove(e)
lastP = None
p0 = p2
debugEdge(e, ">>>>> no flip")
break
elif PathGeom.pointsCoincide(p2, p0):
outputEdges.append((e, True))
edges.remove(e)
lastP = None
p0 = p1
debugEdge(e, ">>>>> flip")
break
else:
debugEdge(e, "<<<<< (%.2f, %.2f, %.2f)" % (p0.x, p0.y, p0.z))
if lastP == p0:
self.edgesOrder.append(outputEdges)
self.edgesOrder.append(edges)
print('ordered edges:')
for e, flip in outputEdges:
debugEdge(e, ' %c ' % ('<' if flip else '>'), False)
print('remaining edges:')
for e in edges:
debugEdge(e, ' ', False)
raise ValueError("No connection to %s" % (p0))
elif lastP:
PathLog.debug("xxxxxx (%.2f, %.2f, %.2f) (%.2f, %.2f, %.2f)" % (p0.x, p0.y, p0.z, lastP.x, lastP.y, lastP.z))
else:
PathLog.debug("xxxxxx (%.2f, %.2f, %.2f) -" % (p0.x, p0.y, p0.z))
lastP = p0
PathLog.track("-")
return outputEdges
def isStrut(self, edge):
p1 = PathGeom.xy(edge.valueAt(edge.FirstParameter))
p2 = PathGeom.xy(edge.valueAt(edge.LastParameter))
return PathGeom.pointsCoincide(p1, p2)
def shell(self):
if len(self.edges) > 1:
wire = Part.Wire(self.initialEdge)
else:
edge = self.edges[0]
if PathGeom.pointsCoincide(edge.valueAt(edge.FirstParameter), self.finalEdge.valueAt(self.finalEdge.FirstParameter)):
wire = Part.Wire(self.finalEdge)
elif hasattr(self, 'initialEdge') and PathGeom.pointsCoincide(edge.valueAt(edge.FirstParameter), self.initialEdge.valueAt(self.initialEdge.FirstParameter)):
wire = Part.Wire(self.initialEdge)
else:
wire = Part.Wire(edge)
for edge in self.edges[1:]:
if PathGeom.pointsCoincide(edge.valueAt(edge.FirstParameter), self.finalEdge.valueAt(self.finalEdge.FirstParameter)):
wire.add(self.finalEdge)
else:
wire.add(edge)
shell = wire.extrude(FreeCAD.Vector(0, 0, self.tag.height + 1))
return shell.removeShape(filter(lambda f: PathGeom.isRoughly(f.Area, 0), shell.Faces))
def commandsForEdges(self):
if self.edges:
shape = self.shell().common(self.tag.solid)
commands = []
rapid = None
for e,flip in self.orderAndFlipEdges(self.cleanupEdges(shape.Edges)):
debugEdge(e, '++++++++ %s' % ('<' if flip else '>'), False)
p1 = e.valueAt(e.FirstParameter)
p2 = e.valueAt(e.LastParameter)
if self.tag.isSquare and (PathGeom.isRoughly(p1.z, self.maxZ) or p1.z > self.maxZ) and (PathGeom.isRoughly(p2.z, self.maxZ) or p2.z > self.maxZ):
rapid = p1 if flip else p2
else:
if rapid:
commands.append(Path.Command('G0', {'X': rapid.x, 'Y': rapid.y, 'Z': rapid.z}))
rapid = None
commands.extend(PathGeom.cmdsForEdge(e, flip, False, self.segm))
if rapid:
commands.append(Path.Command('G0', {'X': rapid.x, 'Y': rapid.y, 'Z': rapid.z}))
rapid = None
return commands
return []
def add(self, edge):
self.tail = None
self.finalEdge = edge
if self.tag.solid.isInside(edge.valueAt(edge.LastParameter), PathGeom.Tolerance, True):
self.addEdge(edge)
else:
i = self.tag.intersects(edge, edge.LastParameter)
if not i:
self.offendingEdge = edge
debugEdge(edge, 'offending Edge:', False)
o = self.tag.originAt(self.tag.z)
print('originAt: (%.2f, %.2f, %.2f)' % (o.x, o.y, o.z))
i = edge.valueAt(edge.FirstParameter)
if PathGeom.pointsCoincide(i, edge.valueAt(edge.FirstParameter)):
self.tail = edge
else:
e, tail = PathGeom.splitEdgeAt(edge, i)
self.addEdge(e)
self.tail = tail
self.exit = i
self.complete = True
self.commands.extend(self.commandsForEdges())
def mappingComplete(self):
return self.complete
class _RapidEdges:
def __init__(self, rapid):
self.rapid = rapid
def isRapid(self, edge):
if type(edge.Curve) == Part.Line or type(edge.Curve) == Part.LineSegment:
v0 = edge.Vertexes[0]
v1 = edge.Vertexes[1]
for r in self.rapid:
r0 = r.Vertexes[0]
r1 = r.Vertexes[1]
if PathGeom.isRoughly(r0.X, v0.X) and PathGeom.isRoughly(r0.Y, v0.Y) and PathGeom.isRoughly(r0.Z, v0.Z) and PathGeom.isRoughly(r1.X, v1.X) and PathGeom.isRoughly(r1.Y, v1.Y) and PathGeom.isRoughly(r1.Z, v1.Z):
return True
return False
class PathData:
def __init__(self, obj):
PathLog.track(obj.Base.Name)
self.obj = obj
self.wire, rapid = PathGeom.wireForPath(obj.Base.Path)
self.rapid = _RapidEdges(rapid)
self.edges = self.wire.Edges
self.base = self.findBottomWire(self.edges)
# determine overall length
self.length = self.base.Length
def findBottomWire(self, edges):
(minZ, maxZ) = self.findZLimits(edges)
self.minZ = minZ
self.maxZ = maxZ
bottom = [e for e in edges if PathGeom.isRoughly(e.Vertexes[0].Point.z, minZ) and PathGeom.isRoughly(e.Vertexes[1].Point.z, minZ)]
wire = Part.Wire(bottom)
if wire.isClosed():
return wire
# if we get here there are already holding tags, or we're not looking at a profile
# let's try and insert the missing pieces - another day
raise ValueError("Selected path doesn't seem to be a Profile operation.")
def findZLimits(self, edges):
# not considering arcs and spheres in Z direction, find the highes and lowest Z values
minZ = 99999999999
maxZ = -99999999999
for e in edges:
if self.rapid.isRapid(e):
continue
for v in e.Vertexes:
if v.Point.z < minZ:
minZ = v.Point.z
if v.Point.z > maxZ:
maxZ = v.Point.z
return (minZ, maxZ)
def shortestAndLongestPathEdge(self):
edges = sorted(self.base.Edges, key=lambda e: e.Length)
return (edges[0], edges[-1])
def generateTags(self, obj, count, width=None, height=None, angle=None, radius=None, spacing=None):
PathLog.track(count, width, height, angle, spacing)
#for e in self.base.Edges:
# debugMarker(e.Vertexes[0].Point, 'base', (0.0, 1.0, 1.0), 0.2)
if spacing:
tagDistance = spacing
else:
tagDistance = self.base.Length / (count if count else 4)
W = width if width else self.defaultTagWidth()
H = height if height else self.defaultTagHeight()
A = angle if angle else self.defaultTagAngle()
R = radius if radius else self.defaultTagRadius()
# start assigning tags on the longest segment
(shortestEdge, longestEdge) = self.shortestAndLongestPathEdge()
startIndex = 0
for i in range(0, len(self.base.Edges)):
edge = self.base.Edges[i]
PathLog.debug(' %d: %.2f' % (i, edge.Length))
if edge.Length == longestEdge.Length:
startIndex = i
break
startEdge = self.base.Edges[startIndex]
startCount = int(startEdge.Length / tagDistance)
if (longestEdge.Length - shortestEdge.Length) > shortestEdge.Length:
startCount = int(startEdge.Length / tagDistance) + 1
lastTagLength = (startEdge.Length + (startCount - 1) * tagDistance) / 2
currentLength = startEdge.Length
minLength = min(2. * W, longestEdge.Length)
PathLog.debug("length=%.2f shortestEdge=%.2f(%.2f) longestEdge=%.2f(%.2f) minLength=%.2f" % (self.base.Length, shortestEdge.Length, shortestEdge.Length/self.base.Length, longestEdge.Length, longestEdge.Length / self.base.Length, minLength))
PathLog.debug(" start: index=%-2d count=%d (length=%.2f, distance=%.2f)" % (startIndex, startCount, startEdge.Length, tagDistance))
PathLog.debug(" -> lastTagLength=%.2f)" % lastTagLength)
PathLog.debug(" -> currentLength=%.2f)" % currentLength)
edgeDict = { startIndex: startCount }
for i in range(startIndex + 1, len(self.base.Edges)):
edge = self.base.Edges[i]
(currentLength, lastTagLength) = self.processEdge(i, edge, currentLength, lastTagLength, tagDistance, minLength, edgeDict)
for i in range(0, startIndex):
edge = self.base.Edges[i]
(currentLength, lastTagLength) = self.processEdge(i, edge, currentLength, lastTagLength, tagDistance, minLength, edgeDict)
tags = []
for (i, count) in edgeDict.iteritems():
edge = self.base.Edges[i]
PathLog.debug(" %d: %d" % (i, count))
#debugMarker(edge.Vertexes[0].Point, 'base', (1.0, 0.0, 0.0), 0.2)
#debugMarker(edge.Vertexes[1].Point, 'base', (0.0, 1.0, 0.0), 0.2)
if 0 != count:
distance = (edge.LastParameter - edge.FirstParameter) / count
for j in range(0, count):
tag = edge.Curve.value((j+0.5) * distance)
tags.append(Tag(tag.x, tag.y, W, H, A, R, True))
return tags
def processEdge(self, index, edge, currentLength, lastTagLength, tagDistance, minLength, edgeDict):
tagCount = 0
currentLength += edge.Length
if edge.Length >= minLength:
while lastTagLength + tagDistance < currentLength:
tagCount += 1
lastTagLength += tagDistance
if tagCount > 0:
PathLog.debug(" index=%d -> count=%d" % (index, tagCount))
edgeDict[index] = tagCount
else:
PathLog.debug(" skipping=%-2d (%.2f)" % (index, edge.Length))
return (currentLength, lastTagLength)
def defaultTagHeight(self):
if hasattr(self.obj, 'Base') and hasattr(self.obj.Base, 'StartDepth') and hasattr(self.obj.Base, 'FinalDepth'):
pathHeight = (self.obj.Base.StartDepth - self.obj.Base.FinalDepth).Value
else:
pathHeight = self.maxZ - self.minZ
height = HoldingTagsPreferences.defaultHeight(pathHeight / 2)
if height > pathHeight:
return pathHeight
return height
def defaultTagWidth(self):
width = self.shortestAndLongestPathEdge()[1].Length / 10
return HoldingTagsPreferences.defaultWidth(width)
def defaultTagAngle(self):
return HoldingTagsPreferences.defaultAngle()
def defaultTagRadius(self):
return HoldingTagsPreferences.defaultRadius()
def sortedTags(self, tags):
ordered = []
for edge in self.base.Edges:
ts = [t for t in tags if DraftGeomUtils.isPtOnEdge(t.originAt(self.minZ), edge)]
for t in sorted(ts, key=lambda t: (t.originAt(self.minZ) - edge.valueAt(edge.FirstParameter)).Length):
tags.remove(t)
ordered.append(t)
# disable all tags that are not on the base wire.
for tag in tags:
PathLog.notice("Tag #%d not on base wire - disabling\n" % len(ordered))
tag.enabled = False
ordered.append(tag)
return ordered
def pointIsOnPath(self, p):
for e in self.edges:
if DraftGeomUtils.isPtOnEdge(p, e):
return True
return False
class ObjectDressup:
def __init__(self, obj):
self.obj = obj
obj.addProperty("App::PropertyLink", "Base","Base", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "The base path to modify"))
obj.addProperty("App::PropertyLength", "Width", "Tag", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "Width of tags."))
obj.addProperty("App::PropertyLength", "Height", "Tag", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "Height of tags."))
obj.addProperty("App::PropertyAngle", "Angle", "Tag", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "Angle of tag plunge and ascent."))
obj.addProperty("App::PropertyLength", "Radius", "Tag", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "Radius of the fillet for the tag."))
obj.addProperty("App::PropertyVectorList", "Positions", "Tag", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "Locations of insterted holding tags"))
obj.addProperty("App::PropertyIntegerList", "Disabled", "Tag", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "Ids of disabled holding tags"))
obj.addProperty("App::PropertyInteger", "SegmentationFactor", "Tag", QtCore.QT_TRANSLATE_NOOP("PathDressup_HoldingTags", "Factor determining the # segments used to approximate rounded tags."))
obj.Proxy = self
def __getstate__(self):
return None
def __setstate__(self, state):
return None
def generateTags(self, obj, count):
if hasattr(self, "pathData"):
self.tags = self.pathData.generateTags(obj, count, obj.Width.Value, obj.Height.Value, obj.Angle, obj.Radius.Value, None)
obj.Positions = [tag.originAt(self.pathData.minZ) for tag in self.tags]
obj.Disabled = []
return False
else:
self.setup(obj, count)
self.execute(obj)
return True
def isValidTagStartIntersection(self, edge, i):
if PathGeom.pointsCoincide(i, edge.valueAt(edge.LastParameter)):
return False
p1 = edge.valueAt(edge.FirstParameter)
p2 = edge.valueAt(edge.LastParameter)
if PathGeom.pointsCoincide(PathGeom.xy(p1), PathGeom.xy(p2)):
# if this vertical goes up, it can't be the start of a tag intersection
if p1.z < p2.z:
return False
return True
def createPath(self, obj, pathData, tags):
PathLog.track()
commands = []
lastEdge = 0
lastTag = 0
sameTag = None
t = 0
inters = None
edge = None
segm = 50
if hasattr(obj, 'SegmentationFactor'):
segm = obj.SegmentationFactor
if segm <= 0:
segm = 50
obj.SegmentationFactor = 50
self.mappers = []
mapper = None
while edge or lastEdge < len(pathData.edges):
PathLog.debug("------- lastEdge = %d/%d.%d/%d" % (lastEdge, lastTag, t, len(tags)))
if not edge:
edge = pathData.edges[lastEdge]
debugEdge(edge, "======= new edge: %d/%d" % (lastEdge, len(pathData.edges)))
lastEdge += 1
sameTag = None
if mapper:
mapper.add(edge)
if mapper.mappingComplete():
commands.extend(mapper.commands)
edge = mapper.tail
mapper = None
else:
edge = None
if edge:
tIndex = (t + lastTag) % len(tags)
t += 1
i = tags[tIndex].intersects(edge, edge.FirstParameter)
if i and self.isValidTagStartIntersection(edge, i):
mapper = MapWireToTag(edge, tags[tIndex], i, segm, pathData.maxZ)
self.mappers.append(mapper)
edge = mapper.tail
if not mapper and t >= len(tags):
# gone through all tags, consume edge and move on
if edge:
debugEdge(edge, '++++++++')
if pathData.rapid.isRapid(edge):
v = edge.Vertexes[1]
commands.append(Path.Command('G0', {'X': v.X, 'Y': v.Y, 'Z': v.Z}))
else:
commands.extend(PathGeom.cmdsForEdge(edge, segm=segm))
edge = None
t = 0
#for cmd in commands:
# print(cmd)
return Path.Path(commands)
def problems(self):
return filter(lambda m: m.haveProblem, self.mappers)
def createTagsPositionDisabled(self, obj, positionsIn, disabledIn):
rawTags = []
for i, pos in enumerate(positionsIn):
tag = Tag(pos.x, pos.y, obj.Width.Value, obj.Height.Value, obj.Angle, obj.Radius, not i in disabledIn)
tag.createSolidsAt(self.pathData.minZ, self.toolRadius)
rawTags.append(tag)
# disable all tags that intersect with their previous tag
prev = None
tags = []
positions = []
disabled = []
for i, tag in enumerate(self.pathData.sortedTags(rawTags)):
if tag.enabled:
if prev:
if prev.solid.common(tag.solid).Faces:
PathLog.notice("Tag #%d intersects with previous tag - disabling\n" % i)
PathLog.debug("this tag = %d [%s]" % (i, tag.solid.BoundBox))
tag.enabled = False
elif self.pathData.edges:
e = self.pathData.edges[0]
p0 = e.valueAt(e.FirstParameter)
p1 = e.valueAt(e.LastParameter)
if tag.solid.isInside(p0, PathGeom.Tolerance, True) or tag.solid.isInside(p1, PathGeom.Tolerance, True):
PathLog.notice("Tag #%d intersects with starting point - disabling\n" % i)
tag.enabled = False
if tag.enabled:
prev = tag
PathLog.debug("previousTag = %d [%s]" % (i, prev))
else:
disabled.append(i)
tags.append(tag)
positions.append(tag.originAt(self.pathData.minZ))
return (tags, positions, disabled)
def execute(self, obj):
#import cProfile
#pr = cProfile.Profile()
#pr.enable()
self.doExecute(obj)
#pr.disable()
#pr.print_stats()
def doExecute(self,obj):
if not obj.Base:
return
if not obj.Base.isDerivedFrom("Path::Feature"):
return
if not obj.Base.Path:
return
if not obj.Base.Path.Commands:
return
pathData = self.setup(obj)
if not pathData:
print("execute - no pathData")
return
self.tags = []
if hasattr(obj, "Positions"):
self.tags, positions, disabled = self.createTagsPositionDisabled(obj, obj.Positions, obj.Disabled)
if obj.Disabled != disabled:
PathLog.debug("Updating properties.... %s vs. %s" % (obj.Disabled, disabled))
obj.Positions = positions
obj.Disabled = disabled
if not self.tags:
print("execute - no tags")
obj.Path = obj.Base.Path
return
self.processTags(obj)
def processTags(self, obj):
tagID = 0
if PathLog.getLevel(LOG_MODULE) == PathLog.Level.DEBUG:
for tag in self.tags:
tagID += 1
if tag.enabled:
PathLog.debug("x=%s, y=%s, z=%s" % (tag.x, tag.y, self.pathData.minZ))
#debugMarker(FreeCAD.Vector(tag.x, tag.y, self.pathData.minZ), "tag-%02d" % tagID , (1.0, 0.0, 1.0), 0.5)
#if tag.angle != 90:
# debugCone(tag.originAt(self.pathData.minZ), tag.r1, tag.r2, tag.actualHeight, "tag-%02d" % tagID)
#else:
# debugCylinder(tag.originAt(self.pathData.minZ), tag.fullWidth()/2, tag.actualHeight, "tag-%02d" % tagID)
obj.Path = self.createPath(obj, self.pathData, self.tags)
def setup(self, obj, generate=False):
PathLog.debug("setup")
self.obj = obj
try:
pathData = PathData(obj)
except ValueError:
PathLog.error(translate("Cannot insert holding tags for this path - please select a Profile path\n"))
return None
self.toolRadius = 5
toolLoad = PathUtils.getLastToolLoad(obj)
if toolLoad is None or toolLoad.ToolNumber == 0:
self.toolRadius = 5
else:
tool = PathUtils.getTool(obj, toolLoad.ToolNumber)
if not tool or tool.Diameter == 0:
self.toolRadius = 5
else:
self.toolRadius = tool.Diameter / 2
self.pathData = pathData
if generate:
obj.Height = self.pathData.defaultTagHeight()
obj.Width = self.pathData.defaultTagWidth()
obj.Angle = self.pathData.defaultTagAngle()
obj.Radius = self.pathData.defaultTagRadius()
count = HoldingTagsPreferences.defaultCount()
self.generateTags(obj, count)
return self.pathData
def setXyEnabled(self, triples):
PathLog.track()
if not hasattr(self, 'pathData'):
self.setup(self.obj)
positions = []
disabled = []
for i, (x, y, enabled) in enumerate(triples):
#print("%d: (%.2f, %.2f) %d" % (i, x, y, enabled))
positions.append(FreeCAD.Vector(x, y, 0))
if not enabled:
disabled.append(i)
self.tags, self.obj.Positions, self.obj.Disabled = self.createTagsPositionDisabled(self.obj, positions, disabled)
self.processTags(self.obj)
def pointIsOnPath(self, obj, point):
if not hasattr(self, 'pathData'):
self.setup(obj)
return self.pathData.pointIsOnPath(point)
@classmethod
def preferencesPage(cls):
return HoldingTagsPreferences()
PathPreferencesPathDressup.RegisterDressup(ObjectDressup)
class TaskPanel:
DataX = QtCore.Qt.ItemDataRole.UserRole
DataY = QtCore.Qt.ItemDataRole.UserRole + 1
DataZ = QtCore.Qt.ItemDataRole.UserRole + 2
DataID = QtCore.Qt.ItemDataRole.UserRole + 3
def __init__(self, obj, viewProvider, jvoVisibility=None):
self.obj = obj
self.obj.Proxy.obj = obj
self.viewProvider = viewProvider
self.form = QtGui.QWidget()
self.formTags = FreeCADGui.PySideUic.loadUi(":/panels/HoldingTagsEdit.ui")
self.formPoint = FreeCADGui.PySideUic.loadUi(":/panels/PointEdit.ui")
self.layout = QtGui.QVBoxLayout(self.form)
#self.form.setGeometry(self.formTags.geometry())
self.form.setWindowTitle(self.formTags.windowTitle())
self.form.setSizePolicy(self.formTags.sizePolicy())
self.formTags.setParent(self.form)
self.formPoint.setParent(self.form)
self.layout.addWidget(self.formTags)
self.layout.addWidget(self.formPoint)
self.formPoint.hide()
self.jvo = PathUtils.findParentJob(obj).ViewObject
if jvoVisibility is None:
FreeCAD.ActiveDocument.openTransaction(translate("Edit HoldingTags Dress-up"))
self.jvoVisible = self.jvo.isVisible()
if self.jvoVisible:
self.jvo.hide()
else:
self.jvoVisible = jvoVisibility
self.pt = FreeCAD.Vector(0, 0, 0)
def reject(self):
FreeCAD.ActiveDocument.abortTransaction()
self.cleanup()
def accept(self):
self.getFields()
FreeCAD.ActiveDocument.commitTransaction()
self.cleanup()
FreeCAD.ActiveDocument.recompute()