/
DraftSnap.py
1478 lines (1342 loc) · 64.8 KB
/
DraftSnap.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
#***************************************************************************
#* *
#* Copyright (c) 2011 *
#* Yorik van Havre <yorik@uncreated.net> *
#* *
#* 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__="FreeCAD Draft Snap tools"
__author__ = "Yorik van Havre"
__url__ = "http://www.freecadweb.org"
## @package DraftSnap
# \ingroup DRAFT
# \brief Snapping system used by Draft & Arch workbenches
#
# This module provides tools to handle point snapping and
# everything that goes with it (toolbar buttons, cursor icons, etc)
import FreeCAD, FreeCADGui, math, Draft, DraftGui, DraftTrackers, DraftVecUtils, itertools
from collections import OrderedDict
from FreeCAD import Vector
from pivy import coin
from PySide import QtCore,QtGui
class Snapper:
"""The Snapper objects contains all the functionality used by draft
and arch module to manage object snapping. It is responsible for
finding snap points and displaying snap markers. Usually You
only need to invoke it's snap() function, all the rest is taken
care of.
3 functions are useful for the scriptwriter: snap(), constrain()
or getPoint() which is an all-in-one combo.
The individual snapToXXX() functions return a snap definition in
the form [real_point,marker_type,visual_point], and are not
meant to be used directly, they are all called when necessary by
the general snap() function.
The Snapper lives inside FreeCADGui once the Draft module has been
loaded.
"""
def __init__(self):
self.activeview = None
self.lastObj = [None,None]
self.maxEdges = 0
self.radius = 0
self.constraintAxis = None
self.basepoint = None
self.affinity = None
self.mask = None
self.cursorMode = None
if Draft.getParam("maxSnap",0):
self.maxEdges = Draft.getParam("maxSnapEdges",0)
self.snapStyle = Draft.getParam("snapStyle",0)
# we still have no 3D view when the draft module initializes
self.tracker = None
self.extLine = None
self.grid = None
self.constrainLine = None
self.trackLine = None
self.extLine2 = None
self.radiusTracker = None
self.dim1 = None
self.dim2 = None
self.snapInfo = None
self.lastSnappedObject = None
self.active = True
self.forceGridOff = False
self.lastExtensions = []
# the trackers are stored in lists because there can be several views, each with its own set
self.trackers = [[],[],[],[],[],[],[],[],[],[]] # view, grid, snap, extline, radius, dim1, dim2, trackLine, extline2, crosstrackers
self.polarAngles = [90,45]
self.selectMode = False
self.holdTracker = None
self.holdPoints = []
self.running = False
self.callbackClick = None
self.callbackMove = None
self.snapObjectIndex = 0
# the snapmarker has "dot","circle" and "square" available styles
if self.snapStyle:
self.mk = OrderedDict([('passive', 'empty'),
('extension', 'empty'),
('parallel', 'empty'),
('grid', 'quad'),
('endpoint', 'quad'),
('midpoint', 'quad'),
('perpendicular','quad'),
('angle', 'quad'),
('center', 'quad'),
('ortho', 'quad'),
('intersection', 'quad'),
('special', 'quad')])
else:
self.mk = OrderedDict([('passive', 'circle'),
('extension', 'circle'),
('parallel', 'circle'),
('grid', 'circle'),
('endpoint', 'dot'),
('midpoint', 'square'),
('perpendicular','dot'),
('angle', 'square'),
('center', 'dot'),
('ortho', 'dot'),
('intersection', 'dot'),
('special', 'dot')])
self.cursors = OrderedDict([('passive', ':/icons/Snap_Near.svg'),
('extension', ':/icons/Snap_Extension.svg'),
('parallel', ':/icons/Snap_Parallel.svg'),
('grid', ':/icons/Snap_Grid.svg'),
('endpoint', ':/icons/Snap_Endpoint.svg'),
('midpoint', ':/icons/Snap_Midpoint.svg'),
('perpendicular', ':/icons/Snap_Perpendicular.svg'),
('angle', ':/icons/Snap_Angle.svg'),
('center', ':/icons/Snap_Center.svg'),
('ortho', ':/icons/Snap_Ortho.svg'),
('intersection', ':/icons/Snap_Intersection.svg'),
('special', ':/icons/Snap_Special.svg')])
def cstr(self, lastpoint, constrain, point):
"constrains if needed"
if constrain or self.mask:
fpt = self.constrain(point,lastpoint)
else:
self.unconstrain()
fpt = point
if self.radiusTracker:
self.radiusTracker.update(fpt)
return fpt
def snap(self,screenpos,lastpoint=None,active=True,constrain=False,noTracker=False):
"""snap(screenpos,lastpoint=None,active=True,constrain=False,noTracker=False): returns a snapped
point from the given (x,y) screenpos (the position of the mouse cursor), active is to
activate active point snapping or not (passive), lastpoint is an optional
other point used to draw an imaginary segment and get additional snap locations. Constrain can
be True to constrain the point against the closest working plane axis.
Screenpos can be a list, a tuple or a coin.SbVec2s object. If noTracker is True,
the tracking line is not displayed."""
if self.running:
# do not allow concurrent runs
return None
self.running = True
global Part, DraftGeomUtils
import Part, DraftGeomUtils
self.spoint = None
if not hasattr(self,"toolbar"):
self.makeSnapToolBar()
mw = FreeCADGui.getMainWindow()
bt = mw.findChild(QtGui.QToolBar,"Draft Snap")
if not bt:
mw.addToolBar(self.toolbar)
else:
if Draft.getParam("showSnapBar",True):
bt.show()
self.snapInfo = None
# type conversion if needed
if isinstance(screenpos,list):
screenpos = tuple(screenpos)
elif isinstance(screenpos,coin.SbVec2s):
screenpos = tuple(screenpos.getValue())
elif not isinstance(screenpos,tuple):
print("snap needs valid screen position (list, tuple or sbvec2s)")
self.running = False
return None
# setup trackers if needed
self.setTrackers()
# show the grid if it's off (new view, for ex)
if self.grid and Draft.getParam("grid",True):
self.grid.on()
# getting current snap Radius
self.radius = self.getScreenDist(Draft.getParam("snapRange", 8),screenpos)
if self.radiusTracker:
self.radiusTracker.update(self.radius)
self.radiusTracker.off()
# activate snap
oldActive = False
if Draft.getParam("alwaysSnap",True):
oldActive = active
active = True
if not self.active:
active = False
self.setCursor('passive')
if self.tracker:
self.tracker.off()
if self.extLine2:
self.extLine2.off()
if self.extLine:
self.extLine.off()
if self.trackLine:
self.trackLine.off()
if self.dim1:
self.dim1.off()
if self.dim2:
self.dim2.off()
point = self.getApparentPoint(screenpos[0],screenpos[1])
# setup a track line if we got a last point
if lastpoint and self.trackLine:
self.trackLine.p1(lastpoint)
# checking if parallel to one of the edges of the last objects or to a polar direction
eline = None
if active:
point,eline = self.snapToPolar(point,lastpoint)
point,eline = self.snapToExtensions(point,lastpoint,constrain,eline)
objectsUnderCursor = Draft.get3DView().getObjectsInfo((screenpos[0],screenpos[1]))
if objectsUnderCursor:
if self.snapObjectIndex >= len(objectsUnderCursor):
self.snapObjectIndex = 0
self.snapInfo = objectsUnderCursor[self.snapObjectIndex]
if self.snapInfo and "Component" in self.snapInfo:
return self.snapToObject(lastpoint, active, constrain, eline, point, oldActive)
# nothing has been snapped
# check for grid snap and ext crossings
if active:
epoint = self.snapToCrossExtensions(point)
if epoint:
point = epoint
else:
point = self.snapToGrid(point)
fp = self.cstr(lastpoint, constrain, point)
if self.trackLine and lastpoint and (not noTracker):
self.trackLine.p2(fp)
self.trackLine.on()
# set the arch point tracking
if lastpoint:
self.setArchDims(lastpoint,fp)
self.spoint = fp
self.running = False
return fp
def cycleSnapObject(self):
self.snapObjectIndex = self.snapObjectIndex + 1
def snapToObject(self, lastpoint, active, constrain, eline, point, oldActive):
# we have an object to snap to
parent = self.snapInfo.get('ParentObject',None)
if parent:
subname = self.snapInfo['SubName']
obj = parent.getSubObject(subname,retType=1)
else:
obj = FreeCAD.ActiveDocument.getObject(self.snapInfo['Object'])
parent = obj
subname = self.snapInfo['Component']
if not obj:
self.spoint = cstr(point)
self.running = False
return self.spoint
snaps = []
self.lastSnappedObject = obj
if hasattr(obj.ViewObject,"Selectable"):
if not obj.ViewObject.Selectable:
self.spoint = self.cstr(lastpoint, constrain, point)
self.running = False
return self.spoint
if not active:
# passive snapping
snaps = [self.snapToVertex(self.snapInfo)]
else:
# first stick to the snapped object
s = self.snapToVertex(self.snapInfo)
if s:
point = s[0]
snaps = [s]
# active snapping
comp = self.snapInfo['Component']
shape = Part.getShape(parent,subname,
needSubElement=True,noElementMap=True)
if not shape.isNull():
snaps.extend(self.snapToSpecials(obj,lastpoint,eline))
if Draft.getType(obj) == "Polygon":
# special snapping for polygons: add the center
snaps.extend(self.snapToPolygon(obj))
if (not self.maxEdges) or (len(shape.Edges) <= self.maxEdges):
if "Edge" in comp:
# we are snapping to an edge
en = int(comp[4:])-1
if len(shape.Edges) > en:
edge = shape.Edges[en]
snaps.extend(self.snapToEndpoints(edge))
snaps.extend(self.snapToMidpoint(edge))
snaps.extend(self.snapToPerpendicular(edge,lastpoint))
snaps.extend(self.snapToIntersection(edge))
snaps.extend(self.snapToElines(edge,eline))
et = DraftGeomUtils.geomType(edge)
if et == "Circle":
# the edge is an arc, we have extra options
snaps.extend(self.snapToAngles(edge))
snaps.extend(self.snapToCenter(edge))
elif et == "Ellipse":
# extra ellipse options
snaps.extend(self.snapToCenter(edge))
elif "Face" in comp:
en = int(comp[4:])-1
if len(shape.Faces) > en:
face = shape.Faces[en]
snaps.extend(self.snapToFace(face))
elif "Vertex" in comp:
# directly snapped to a vertex
snaps.append(self.snapToVertex(self.snapInfo,active=True))
elif comp == '':
# workaround for the new view provider
snaps.append(self.snapToVertex(self.snapInfo,active=True))
else:
# all other cases (face, etc...) default to passive snap
snapArray = [self.snapToVertex(self.snapInfo)]
elif Draft.getType(obj) == "Dimension":
# for dimensions we snap to their 2 points:
snaps.extend(self.snapToDim(obj))
elif Draft.getType(obj) == "Axis":
for edge in obj.Shape.Edges:
snaps.extend(self.snapToEndpoints(edge))
snaps.extend(self.snapToIntersection(edge))
elif Draft.getType(obj) == "Mesh":
# for meshes we only snap to vertices
snaps.extend(self.snapToEndpoints(obj.Mesh))
elif Draft.getType(obj) == "Points":
# for points we only snap to points
snaps.extend(self.snapToEndpoints(obj.Points))
elif Draft.getType(obj) in ["WorkingPlaneProxy","BuildingPart"]:
# snap to the center of WPProxies and BuildingParts
snaps.append([obj.Placement.Base,'endpoint',self.toWP(obj.Placement.Base)])
elif Draft.getType(obj) == "SectionPlane":
# snap to corners of section planes
snaps.extend(self.snapToEndpoints(obj.Shape))
# updating last objects list
if not self.lastObj[1]:
self.lastObj[1] = obj.Name
elif self.lastObj[1] != obj.Name:
self.lastObj[0] = self.lastObj[1]
self.lastObj[1] = obj.Name
if not snaps:
self.spoint = self.cstr(lastpoint, constrain, point)
self.running = False
if self.trackLine and lastpoint:
self.trackLine.p2(self.spoint)
self.trackLine.on()
return self.spoint
# calculating the nearest snap point
shortest = 1000000000000000000
origin = Vector(self.snapInfo['x'],self.snapInfo['y'],self.snapInfo['z'])
winner = None
fp = point
for snap in snaps:
if (not snap) or (snap[0] == None):
pass
#print("debug: Snapper: invalid snap point: ",snaps)
else:
delta = snap[0].sub(origin)
if delta.Length < shortest:
shortest = delta.Length
winner = snap
if winner:
# see if we are out of the max radius, if any
if self.radius:
dv = point.sub(winner[2])
if (dv.Length > self.radius):
if (not oldActive) and self.isEnabled("passive"):
winner = self.snapToVertex(self.snapInfo)
# setting the cursors
if self.tracker and not self.selectMode:
self.tracker.setCoords(winner[2])
self.tracker.setMarker(self.mk[winner[1]])
self.tracker.on()
# setting the trackline
fp = self.cstr(lastpoint, constrain, winner[2])
if self.trackLine and lastpoint:
self.trackLine.p2(fp)
self.trackLine.on()
# set the cursor
self.setCursor(winner[1])
# set the arch point tracking
if lastpoint:
self.setArchDims(lastpoint,fp)
# return the final point
self.spoint = fp
self.running = False
return self.spoint
def toWP(self,point):
"projects the given point on the working plane, if needed"
if self.isEnabled("WorkingPlane"):
if hasattr(FreeCAD,"DraftWorkingPlane"):
return FreeCAD.DraftWorkingPlane.projectPoint(point)
return point
def getApparentPoint(self,x,y):
"returns a 3D point, projected on the current working plane"
view = Draft.get3DView()
pt = view.getPoint(x,y)
if self.mask != "z":
if hasattr(FreeCAD,"DraftWorkingPlane"):
if view.getCameraType() == "Perspective":
camera = view.getCameraNode()
p = camera.getField("position").getValue()
dv = pt.sub(Vector(p[0],p[1],p[2]))
else:
dv = view.getViewDirection()
return FreeCAD.DraftWorkingPlane.projectPoint(pt,dv)
return pt
def snapToDim(self,obj):
snaps = []
if obj.ViewObject:
if hasattr(obj.ViewObject.Proxy,"p2") and hasattr(obj.ViewObject.Proxy,"p3"):
snaps.append([obj.ViewObject.Proxy.p2,'endpoint',self.toWP(obj.ViewObject.Proxy.p2)])
snaps.append([obj.ViewObject.Proxy.p3,'endpoint',self.toWP(obj.ViewObject.Proxy.p3)])
return snaps
def snapToExtensions(self,point,last,constrain,eline):
"returns a point snapped to extension or parallel line to last object, if any"
tsnap = self.snapToHold(point)
if tsnap:
if self.tracker and not self.selectMode:
self.tracker.setCoords(tsnap[2])
self.tracker.setMarker(self.mk[tsnap[1]])
self.tracker.on()
if self.extLine:
self.extLine.p1(tsnap[0])
self.extLine.p2(tsnap[2])
self.extLine.on()
self.setCursor(tsnap[1])
return tsnap[2],eline
if self.isEnabled("extension"):
tsnap = self.snapToExtOrtho(last,constrain,eline)
if tsnap:
if (tsnap[0].sub(point)).Length < self.radius:
if self.tracker and not self.selectMode:
self.tracker.setCoords(tsnap[2])
self.tracker.setMarker(self.mk[tsnap[1]])
self.tracker.on()
if self.extLine:
self.extLine.p2(tsnap[2])
self.extLine.on()
self.setCursor(tsnap[1])
return tsnap[2],eline
else:
tsnap = self.snapToExtPerpendicular(last)
if tsnap:
if (tsnap[0].sub(point)).Length < self.radius:
if self.tracker and not self.selectMode:
self.tracker.setCoords(tsnap[2])
self.tracker.setMarker(self.mk[tsnap[1]])
self.tracker.on()
if self.extLine:
self.extLine.p2(tsnap[2])
self.extLine.on()
self.setCursor(tsnap[1])
return tsnap[2],eline
for o in [self.lastObj[1],self.lastObj[0]]:
if o and (self.isEnabled('extension') or self.isEnabled('parallel')):
ob = FreeCAD.ActiveDocument.getObject(o)
if ob:
if ob.isDerivedFrom("Part::Feature"):
edges = ob.Shape.Edges
if Draft.getType(ob) == "Wall":
for so in [ob]+ob.Additions:
if Draft.getType(so) == "Wall":
if so.Base:
edges.extend(so.Base.Shape.Edges)
edges.reverse()
if (not self.maxEdges) or (len(edges) <= self.maxEdges):
for e in edges:
if DraftGeomUtils.geomType(e) == "Line":
np = self.getPerpendicular(e,point)
if not DraftGeomUtils.isPtOnEdge(np,e):
if (np.sub(point)).Length < self.radius:
if self.isEnabled('extension'):
if np != e.Vertexes[0].Point:
p0 = e.Vertexes[0].Point
if self.tracker and not self.selectMode:
self.tracker.setCoords(np)
self.tracker.setMarker(self.mk['extension'])
self.tracker.on()
if self.extLine:
if self.snapStyle:
dv = np.sub(p0)
self.extLine.p1(p0.add(dv.multiply(0.5)))
else:
self.extLine.p1(p0)
self.extLine.p2(np)
self.extLine.on()
self.setCursor('extension')
ne = Part.LineSegment(p0,np).toShape()
# storing extension line for intersection calculations later
if len(self.lastExtensions) == 0:
self.lastExtensions.append(ne)
elif len(self.lastExtensions) == 1:
if not DraftGeomUtils.areColinear(ne,self.lastExtensions[0]):
self.lastExtensions.append(self.lastExtensions[0])
self.lastExtensions[0] = ne
else:
if (not DraftGeomUtils.areColinear(ne,self.lastExtensions[0])) and \
(not DraftGeomUtils.areColinear(ne,self.lastExtensions[1])):
self.lastExtensions[1] = self.lastExtensions[0]
self.lastExtensions[0] = ne
return np,ne
else:
if self.isEnabled('parallel'):
if last:
ve = DraftGeomUtils.vec(e)
if not DraftVecUtils.isNull(ve):
de = Part.LineSegment(last,last.add(ve)).toShape()
np = self.getPerpendicular(de,point)
if (np.sub(point)).Length < self.radius:
if self.tracker and not self.selectMode:
self.tracker.setCoords(np)
self.tracker.setMarker(self.mk['parallel'])
self.tracker.on()
self.setCursor('parallel')
return np,de
return point,eline
def snapToCrossExtensions(self,point):
"snaps to the intersection of the last 2 extension lines"
if self.isEnabled('extension'):
if len(self.lastExtensions) == 2:
np = DraftGeomUtils.findIntersection(self.lastExtensions[0],self.lastExtensions[1],True,True)
if np:
for p in np:
dv = point.sub(p)
if (self.radius == 0) or (dv.Length <= self.radius):
if self.tracker and not self.selectMode:
self.tracker.setCoords(p)
self.tracker.setMarker(self.mk['intersection'])
self.tracker.on()
self.setCursor('intersection')
if self.extLine and self.extLine2:
if DraftVecUtils.equals(self.extLine.p1(),self.lastExtensions[0].Vertexes[0].Point):
p0 = self.lastExtensions[1].Vertexes[0].Point
else:
p0 = self.lastExtensions[0].Vertexes[0].Point
if self.snapStyle:
dv = p.sub(p0)
self.extLine2.p1(p0.add(dv.multiply(0.5)))
else:
self.extLine2.p1(p0)
self.extLine2.p2(p)
self.extLine.p2(p)
self.extLine2.on()
return p
return None
def snapToPolar(self,point,last):
"snaps to polar lines from the given point"
if self.isEnabled('ortho') and (not self.mask):
if last:
vecs = []
if hasattr(FreeCAD,"DraftWorkingPlane"):
ax = [FreeCAD.DraftWorkingPlane.u,
FreeCAD.DraftWorkingPlane.v,
FreeCAD.DraftWorkingPlane.axis]
else:
ax = [FreeCAD.Vector(1,0,0),
FreeCAD.Vector(0,1,0),
FreeCAD.Vector(0,0,1)]
for a in self.polarAngles:
if a == 90:
vecs.extend([ax[0],ax[0].negative()])
vecs.extend([ax[1],ax[1].negative()])
else:
v = DraftVecUtils.rotate(ax[0],math.radians(a),ax[2])
vecs.extend([v,v.negative()])
v = DraftVecUtils.rotate(ax[1],math.radians(a),ax[2])
vecs.extend([v,v.negative()])
for v in vecs:
if not DraftVecUtils.isNull(v):
try:
de = Part.LineSegment(last,last.add(v)).toShape()
except Part.OCCError:
return point,None
np = self.getPerpendicular(de,point)
if ((self.radius == 0) and (point.sub(last).getAngle(v) < 0.087)) \
or ((np.sub(point)).Length < self.radius):
if self.tracker and not self.selectMode:
self.tracker.setCoords(np)
self.tracker.setMarker(self.mk['parallel'])
self.tracker.on()
self.setCursor('ortho')
return np,de
return point,None
def snapToGrid(self,point):
"returns a grid snap point if available"
if self.grid:
if self.grid.Visible:
if self.isEnabled("grid"):
np = self.grid.getClosestNode(point)
if np:
dv = point.sub(np)
if (self.radius == 0) or (dv.Length <= self.radius):
if self.tracker and not self.selectMode:
self.tracker.setCoords(np)
self.tracker.setMarker(self.mk['grid'])
self.tracker.on()
self.setCursor('grid')
return np
return point
def snapToEndpoints(self,shape):
"returns a list of enpoints snap locations"
snaps = []
if self.isEnabled("endpoint"):
if hasattr(shape,"Vertexes"):
for v in shape.Vertexes:
snaps.append([v.Point,'endpoint',self.toWP(v.Point)])
elif hasattr(shape,"Point"):
snaps.append([shape.Point,'endpoint',self.toWP(shape.Point)])
elif hasattr(shape,"Points"):
if len(shape.Points) and hasattr(shape.Points[0],"Vector"):
for v in shape.Points:
snaps.append([v.Vector,'endpoint',self.toWP(v.Vector)])
else:
for v in shape.Points:
snaps.append([v,'endpoint',self.toWP(v)])
return snaps
def snapToMidpoint(self,shape):
"returns a list of midpoints snap locations"
snaps = []
if self.isEnabled("midpoint"):
if isinstance(shape,Part.Edge):
mp = DraftGeomUtils.findMidpoint(shape)
if mp:
snaps.append([mp,'midpoint',self.toWP(mp)])
return snaps
def snapToPerpendicular(self,shape,last):
"returns a list of perpendicular snap locations"
snaps = []
if self.isEnabled("perpendicular"):
if last:
if isinstance(shape,Part.Edge):
if DraftGeomUtils.geomType(shape) == "Line":
np = self.getPerpendicular(shape,last)
elif DraftGeomUtils.geomType(shape) == "Circle":
dv = last.sub(shape.Curve.Center)
dv = DraftVecUtils.scaleTo(dv,shape.Curve.Radius)
np = (shape.Curve.Center).add(dv)
elif DraftGeomUtils.geomType(shape) == "BSplineCurve":
try:
pr = shape.Curve.parameter(last)
np = shape.Curve.value(pr)
except:
return snaps
else:
return snaps
snaps.append([np,'perpendicular',self.toWP(np)])
return snaps
def snapToOrtho(self,shape,last,constrain):
"returns a list of ortho snap locations"
snaps = []
if self.isEnabled("ortho"):
if constrain:
if isinstance(shape,Part.Edge):
if last:
if DraftGeomUtils.geomType(shape) == "Line":
if self.constraintAxis:
tmpEdge = Part.LineSegment(last,last.add(self.constraintAxis)).toShape()
# get the intersection points
pt = DraftGeomUtils.findIntersection(tmpEdge,shape,True,True)
if pt:
for p in pt:
snaps.append([p,'ortho',self.toWP(p)])
return snaps
def snapToExtOrtho(self,last,constrain,eline):
"returns an ortho X extension snap location"
if self.isEnabled("extension") and self.isEnabled("ortho"):
if constrain and last and self.constraintAxis and self.extLine:
tmpEdge1 = Part.LineSegment(last,last.add(self.constraintAxis)).toShape()
tmpEdge2 = Part.LineSegment(self.extLine.p1(),self.extLine.p2()).toShape()
# get the intersection points
pt = DraftGeomUtils.findIntersection(tmpEdge1,tmpEdge2,True,True)
if pt:
return [pt[0],'ortho',pt[0]]
if eline:
try:
tmpEdge2 = Part.LineSegment(self.extLine.p1(),self.extLine.p2()).toShape()
# get the intersection points
pt = DraftGeomUtils.findIntersection(eline,tmpEdge2,True,True)
if pt:
return [pt[0],'ortho',pt[0]]
except:
return None
return None
def snapToHold(self,point):
"returns a snap location that is orthogonal to hold points or, if possible, at crossings"
if not self.holdPoints:
return None
if hasattr(FreeCAD,"DraftWorkingPlane"):
u = FreeCAD.DraftWorkingPlane.u
v = FreeCAD.DraftWorkingPlane.v
else:
u = FreeCAD.Vector(1,0,0)
v = FreeCAD.Vector(0,1,0)
if len(self.holdPoints) > 1:
# first try mid points
if self.isEnabled("midpoint"):
l = list(self.holdPoints)
for p1,p2 in itertools.combinations(l,2):
p3 = p1.add((p2.sub(p1)).multiply(0.5))
if (p3.sub(point)).Length < self.radius:
return [p1,'midpoint',p3]
# then try int points
ipoints = []
l = list(self.holdPoints)
while len(l) > 1:
p1 = l.pop()
for p2 in l:
i1 = DraftGeomUtils.findIntersection(p1,p1.add(u),p2,p2.add(v),True,True)
if i1:
ipoints.append([p1,i1[0]])
i2 = DraftGeomUtils.findIntersection(p1,p1.add(v),p2,p2.add(u),True,True)
if i2:
ipoints.append([p1,i2[0]])
for p in ipoints:
if (p[1].sub(point)).Length < self.radius:
return [p[0],'ortho',p[1]]
# then try to stick to a line
for p in self.holdPoints:
d = DraftGeomUtils.findDistance(point,[p,p.add(u)])
if d:
if d.Length < self.radius:
fp = point.add(d)
return [p,'extension',fp]
d = DraftGeomUtils.findDistance(point,[p,p.add(v)])
if d:
if d.Length < self.radius:
fp = point.add(d)
return [p,'extension',fp]
return None
def snapToExtPerpendicular(self,last):
"returns a perpendicular X extension snap location"
if self.isEnabled("extension") and self.isEnabled("perpendicular"):
if last and self.extLine:
if self.extLine.p1() != self.extLine.p2():
tmpEdge = Part.LineSegment(self.extLine.p1(),self.extLine.p2()).toShape()
np = self.getPerpendicular(tmpEdge,last)
return [np,'perpendicular',np]
return None
def snapToElines(self,e1,e2):
"returns a snap location at the infinite intersection of the given edges"
snaps = []
if self.isEnabled("intersection") and self.isEnabled("extension"):
if e1 and e2:
# get the intersection points
pts = DraftGeomUtils.findIntersection(e1,e2,True,True)
if pts:
for p in pts:
snaps.append([p,'intersection',self.toWP(p)])
return snaps
def snapToAngles(self,shape):
"returns a list of angle snap locations"
snaps = []
if self.isEnabled("angle"):
rad = shape.Curve.Radius
pos = shape.Curve.Center
for i in [0,30,45,60,90,120,135,150,180,210,225,240,270,300,315,330]:
ang = math.radians(i)
cur = Vector(math.sin(ang)*rad+pos.x,math.cos(ang)*rad+pos.y,pos.z)
snaps.append([cur,'angle',self.toWP(cur)])
return snaps
def snapToCenter(self,shape):
"returns a list of center snap locations"
snaps = []
if self.isEnabled("center"):
pos = shape.Curve.Center
c = self.toWP(pos)
if hasattr(shape.Curve,"Radius"):
rad = shape.Curve.Radius
for i in [15,37.5,52.5,75,105,127.5,142.5,165,195,217.5,232.5,255,285,307.5,322.5,345]:
ang = math.radians(i)
cur = Vector(math.sin(ang)*rad+pos.x,math.cos(ang)*rad+pos.y,pos.z)
snaps.append([cur,'center',c])
else:
snaps.append([c,'center',c])
return snaps
def snapToFace(self,shape):
"returns a face center snap location"
snaps = []
if self.isEnabled("center"):
pos = shape.CenterOfMass
c = self.toWP(pos)
snaps.append([pos,'center',c])
return snaps
def snapToIntersection(self,shape):
"returns a list of intersection snap locations"
snaps = []
if self.isEnabled("intersection"):
# get the stored objects to calculate intersections
if self.lastObj[0]:
obj = FreeCAD.ActiveDocument.getObject(self.lastObj[0])
if obj:
if obj.isDerivedFrom("Part::Feature") or (Draft.getType(obj) == "Axis"):
if (not self.maxEdges) or (len(obj.Shape.Edges) <= self.maxEdges):
import Part
for e in obj.Shape.Edges:
# get the intersection points
try:
if self.isEnabled("WorkingPlane") and hasattr(e,"Curve") and isinstance(e.Curve,(Part.Line,Part.LineSegment)) and hasattr(shape,"Curve") and isinstance(shape.Curve,(Part.Line,Part.LineSegment)):
# get apparent intersection (lines projected on WP)
p1 = self.toWP(e.Vertexes[0].Point)
p2 = self.toWP(e.Vertexes[-1].Point)
p3 = self.toWP(shape.Vertexes[0].Point)
p4 = self.toWP(shape.Vertexes[-1].Point)
pt = DraftGeomUtils.findIntersection(p1,p2,p3,p4,True,True)
else:
pt = DraftGeomUtils.findIntersection(e,shape)
if pt:
for p in pt:
snaps.append([p,'intersection',self.toWP(p)])
except:
pass
# some curve types yield an error when trying to read their types...
return snaps
def snapToPolygon(self,obj):
"returns a list of polygon center snap locations"
snaps = []
if self.isEnabled("center"):
c = obj.Placement.Base
for edge in obj.Shape.Edges:
p1 = edge.Vertexes[0].Point
p2 = edge.Vertexes[-1].Point
v1 = p1.add((p2-p1).scale(.25,.25,.25))
v2 = p1.add((p2-p1).scale(.75,.75,.75))
snaps.append([v1,'center',self.toWP(c)])
snaps.append([v2,'center',self.toWP(c)])
return snaps
def snapToVertex(self,info,active=False):
p = Vector(info['x'],info['y'],info['z'])
if active:
if self.isEnabled("passive"):
return [p,'endpoint',self.toWP(p)]
else:
return []
elif self.isEnabled("passive"):
return [p,'passive',p]
else:
return []
def snapToSpecials(self,obj,lastpoint=None,eline=None):
"returns special snap locations, if any"
snaps = []
if self.isEnabled("special"):
if (Draft.getType(obj) == "Wall"):
# special snapping for wall: snap to its base shape if it is linear
if obj.Base:
if not obj.Base.Shape.Solids:
for v in obj.Base.Shape.Vertexes:
snaps.append([v.Point,'special',self.toWP(v.Point)])
elif (Draft.getType(obj) == "Structure"):
# special snapping for struct: only to its base point
if obj.Base:
if not obj.Base.Shape.Solids:
for v in obj.Base.Shape.Vertexes:
snaps.append([v.Point,'special',self.toWP(v.Point)])
else:
b = obj.Placement.Base
snaps.append([b,'special',self.toWP(b)])
if obj.ViewObject.ShowNodes:
for edge in obj.Proxy.getNodeEdges(obj):
snaps.extend(self.snapToEndpoints(edge))
snaps.extend(self.snapToMidpoint(edge))
snaps.extend(self.snapToPerpendicular(edge,lastpoint))
snaps.extend(self.snapToIntersection(edge))
snaps.extend(self.snapToElines(edge,eline))
elif hasattr(obj,"SnapPoints"):
for p in obj.SnapPoints:
p2 = obj.Placement.multVec(p)
snaps.append([p2,'special',p2])
return snaps
def getScreenDist(self,dist,cursor):
"returns a distance in 3D space from a screen pixels distance"
view = Draft.get3DView()
p1 = view.getPoint(cursor)
p2 = view.getPoint((cursor[0]+dist,cursor[1]))
return (p2.sub(p1)).Length
def getPerpendicular(self,edge,pt):
"returns a point on an edge, perpendicular to the given point"
dv = pt.sub(edge.Vertexes[0].Point)
nv = DraftVecUtils.project(dv,DraftGeomUtils.vec(edge))
np = (edge.Vertexes[0].Point).add(nv)
return np
def setArchDims(self,p1,p2):
"show arch dimensions between 2 points"
if self.isEnabled("Dimensions"):
if not self.dim1:
self.dim1 = DraftTrackers.archDimTracker(mode=2)
if not self.dim2:
self.dim2 = DraftTrackers.archDimTracker(mode=3)
self.dim1.p1(p1)
self.dim2.p1(p1)
self.dim1.p2(p2)
self.dim2.p2(p2)
if self.dim1.Distance:
self.dim1.on()
if self.dim2.Distance:
self.dim2.on()
def setCursor(self,mode=None):
"setCursor(self,mode=None): sets or resets the cursor to the given mode or resets"
if self.selectMode:
mw = FreeCADGui.getMainWindow()
for w in mw.findChild(QtGui.QMdiArea).findChildren(QtGui.QWidget):
if w.metaObject().className() == "SIM::Coin3D::Quarter::QuarterWidget":
w.unsetCursor()
self.cursorMode = None
elif not mode:
mw = FreeCADGui.getMainWindow()
for w in mw.findChild(QtGui.QMdiArea).findChildren(QtGui.QWidget):
if w.metaObject().className() == "SIM::Coin3D::Quarter::QuarterWidget":
w.unsetCursor()
self.cursorMode = None
else:
if mode != self.cursorMode:
baseicon = QtGui.QPixmap(":/icons/Draft_Cursor.svg")