/
WorkingPlane.py
851 lines (746 loc) · 31.2 KB
/
WorkingPlane.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
## @package WorkingPlane
# \ingroup DRAFT
# \brief This module handles the Working Plane and grid of the Draft module.
#
# This module provides the plane class which provides a virtual working plane
# in FreeCAD and a couple of utility functions.
"""
This module provides the plane class which provides a virtual working plane
in FreeCAD and a couple of utility functions.
The Working Plane is mostly intended to be used in the Draft Workbench
to draw 2D objects.
"""
# ***************************************************************************
# * *
# * Copyright (c) 2009, 2010 *
# * Ken Cline <cline@frii.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, math, DraftVecUtils
from FreeCAD import Vector
__title__ = "FreeCAD Working Plane utility"
__author__ = "Ken Cline"
__url__ = "http://www.freecadweb.org"
class plane:
"""A WorkPlane object.
Attributes
----------
doc : App::Document
The active document. Reset view when `doc` changes.
weak : bool
It is `True` if the plane has been defined by `setup()`
or has been reset.
u : Base::Vector3
An axis (vector) that helps define the working plane.
v : Base::Vector3
An axis (vector) that helps define the working plane.
axis : Base::Vector3
A vector that is supposed to be perpendicular to `u` and `v`;
it is helpful although redundant.
position : Base::Vector3
A point throught which the plane goes through,
that helps define the working plane.
stored : bool
A placeholder for a stored state.
"""
def __init__(self, u=Vector(1, 0, 0), v=Vector(0, 1, 0), w=Vector(0, 0, 1), pos=Vector(0, 0, 0)):
"""Initialize the working plane.
Parameters
----------
u : Base::Vector3, optional
An axis (vector) that helps define the working plane.
It defaults to `(1, 0, 0)`, or the +X axis.
v : Base::Vector3, optional
An axis (vector) that helps define the working plane.
It defaults to `(0, 1, 0)`, or the +Y axis.
w : Base::Vector3, optional
An axis that is supposed to be perpendicular to `u` and `v`;
it is redundant.
It defaults to `(0, 0, 1)`, or the +Z axis.
pos : Base::Vector3, optional
A point through which the plane goes through.
It defaults to the origin `(0, 0, 0)`.
"""
# keep track of active document. Reset view when doc changes.
self.doc = None
self.weak = True
self.u = u
self.v = v
self.axis = w
self.position = pos
# a placeholder for a stored state
self.stored = None
def __repr__(self):
"""Show the string representation of the object."""
return "Workplane x="+str(DraftVecUtils.rounded(self.u))+" y="+str(DraftVecUtils.rounded(self.v))+" z="+str(DraftVecUtils.rounded(self.axis))
def copy(self):
"""Return a new plane that is a copy of the present object."""
return plane(u=self.u, v=self.v, w=self.axis, pos=self.position)
def offsetToPoint(self, p, direction=None):
"""Return the signed distance from a point to the plane.
Parameters
----------
p : Base::Vector3
The external point to consider.
direction : Base::Vector3, optional
The unit vector that indicates the direction of the distance.
It defaults to `None`, which then uses the `plane.axis` (normal)
value, meaning that the measured distance is perpendicular
to the plane.
Returns
-------
float
The distance from the point to the plane.
Notes
-----
The signed distance `d`, from `p` to the plane, is such that
::
x = p + d*direction,
where `x` is a point that lies on the plane.
The `direction` is a unit vector that specifies the direction
in which the distance is measured.
It defaults to `plane.axis`,
meaning that it is the perpendicular distance.
A picture will help explain the computation
::
p
//|
/ / |
d / / | axis
/ / |
/ / |
-------- plane -----x-----c-----a--------
The points are as follows
* `p` is an arbitraty point outside the plane.
* `c` is a known point on the plane,
for example, `plane.position`.
* `x` is the intercept on the plane from `p` in
the desired `direction`.
* `a` is the perpendicular intercept on the plane,
i.e. along `plane.axis`.
The distance is calculated through the dot product
of the vector `pc` (going from point `p` to point `c`,
both of which are known) with the unit vector `direction`
(which is provided or defaults to `plane.axis`).
::
d = pc . direction
d = (c - p) . direction
**Warning:** this implementation doesn't calculate the entire
distance `|xp|`, only the distance `|pc|` projected onto `|xp|`.
Trigonometric relationships
---------------------------
In 2D the distances can be calculated by trigonometric relationships
::
|ap| = |cp| cos(apc) = |xp| cos(apx)
Then the desired distance is `d = |xp|`
::
|xp| = |cp| cos(apc) / cos(apx)
The cosines can be obtained from the definition of the dot product
::
A . B = |A||B| cos(angleAB)
If one vector is a unit vector
::
A . uB = |A| cos(angleAB)
cp . axis = |cp| cos(apc)
and if both vectors are unit vectors
::
uA . uB = cos(angleAB).
direction . axis = cos(apx)
Then
::
d = (cp . axis) / (direction . axis)
**Note:** for 2D these trigonometric operations
produce the full `|xp|` distance.
"""
if direction == None: direction = self.axis
return direction.dot(self.position.sub(p))
def projectPoint(self, p, direction=None):
"""Project a point onto the plane, by default orthogonally.
Parameters
----------
p : Base::Vector3
The point to project.
direction : Base::Vector3, optional
The unit vector that indicates the direction of projection.
It defaults to `None`, which then uses the `plane.axis` (normal)
value, meaning that the point is projected perpendicularly
to the plane.
Returns
-------
Base::Vector3
The projected vector, scaled to the appropriate distance.
"""
if not direction:
direction = self.axis
lp = self.getLocalCoords(p)
gp = self.getGlobalCoords(Vector(lp.x, lp.y, 0))
a = direction.getAngle(gp.sub(p))
if a > math.pi/2:
direction = direction.negative()
a = math.pi - a
ld = self.getLocalRot(direction)
gd = self.getGlobalRot(Vector(ld.x, ld.y, 0))
hyp = abs(math.tan(a) * lp.z)
return gp.add(DraftVecUtils.scaleTo(gd, hyp))
def projectPointOld(self, p, direction=None):
"""Project a point onto the plane. OBSOLETE.
Parameters
----------
p : Base::Vector3
The point to project.
direction : Base::Vector3, optional
The unit vector that indicates the direction of projection.
It defaults to `None`, which then uses the `plane.axis` (normal)
value, meaning that the point is projected perpendicularly
to the plane.
Returns
-------
Base::Vector3
The projected point,
or the original point if the angle between the `direction`
and the `plane.axis` is 90 degrees.
"""
if not direction:
direction = self.axis
t = Vector(direction)
#t.normalize()
a = round(t.getAngle(self.axis), DraftVecUtils.precision())
pp = round((math.pi)/2, DraftVecUtils.precision())
if a == pp:
return p
t.multiply(self.offsetToPoint(p, direction))
return p.add(t)
def alignToPointAndAxis(self, point, axis, offset=0, upvec=None):
"""Align the working plane to a point and an axis (vector).
Set `v` as the cross product of `axis` with `(1, 0, 0)` or `+X`,
and `u` as `v` rotated -90 degrees around the `axis`.
Also set `weak` to `False`.
Parameters
----------
point : Base::Vector3
The new `position` of the plane, adjusted by
the `offset`.
axis : Base::Vector3
A vector whose unit vector will be used as the new `axis`
of the plane.
If it is very close to the `X` or `-X` axes,
it will use this axis exactly, and will adjust `u` and `v`
to `+Y` and `+Z`, or `-Y` and `+Z`, respectively.
offset : float, optional
Defaults to zero. A value which will be used to offset
the plane in the direction of its `axis`.
upvec : Base::Vector3, optional
Defaults to `None`.
If it exists, its unit vector will be used as `v`,
and will set `u` as the cross product of `v` with `axis`.
"""
self.doc = FreeCAD.ActiveDocument
self.axis = axis
self.axis.normalize()
if axis.getAngle(Vector(1, 0, 0)) < 0.00001:
self.axis = Vector(1, 0, 0)
self.u = Vector(0, 1, 0)
self.v = Vector(0, 0, 1)
elif axis.getAngle(Vector(-1, 0, 0)) < 0.00001:
self.axis = Vector(-1, 0, 0)
self.u = Vector(0, -1, 0)
self.v = Vector(0, 0, 1)
elif upvec:
self.v = upvec
self.v.normalize()
self.u = self.v.cross(self.axis)
else:
self.v = axis.cross(Vector(1, 0, 0))
self.v.normalize()
self.u = DraftVecUtils.rotate(self.v, -math.pi/2, self.axis)
offsetVector = Vector(axis)
offsetVector.multiply(offset)
self.position = point.add(offsetVector)
self.weak = False
# FreeCAD.Console.PrintMessage("(position = " + str(self.position) + ")\n")
# FreeCAD.Console.PrintMessage("Current workplane: x="+str(DraftVecUtils.rounded(self.u))+" y="+str(DraftVecUtils.rounded(self.v))+" z="+str(DraftVecUtils.rounded(self.axis))+"\n")
def alignToPointAndAxis_SVG(self, point, axis, offset=0):
"""Align the working plane to a point and an axis (vector).
It aligns `u` and `v` based on the magnitude of the components
of `axis`.
Also set `weak` to `False`.
Parameters
----------
point : Base::Vector3
The new `position` of the plane, adjusted by
the `offset`.
axis : Base::Vector3
A vector whose unit vector will be used as the new `axis`
of the plane.
The magnitudes of the `x`, `y`, `z` components of the axis
determine the orientation of `u` and `v` of the plane.
offset : float, optional
Defaults to zero. A value which will be used to offset
the plane in the direction of its `axis`.
Cases
-----
The `u` and `v` are always calculated the same
* `u` is the cross product of the positive or negative of `axis`
with a `reference vector`.
::
u = [+1|-1] axis.cross(ref_vec)
* `v` is `u` rotated 90 degrees around `axis`.
Whether the `axis` is positive or negative, and which reference
vector is used, depends on the absolute values of the `x`, `y`, `z`
components of the `axis` unit vector.
#. If `x > y`, and `y > z`
The reference vector is +Z
::
u = -1 axis.cross(+Z)
#. If `y > z`, and `z >= x`
The reference vector is +X.
::
u = -1 axis.cross(+X)
#. If `y >= x`, and `x > z`
The reference vector is +Z.
::
u = +1 axis.cross(+Z)
#. If `x > z`, and `z >= y`
The reference vector is +Y.
::
u = +1 axis.cross(+Y)
#. If `z >= y`, and `y > x`
The reference vector is +X.
::
u = +1 axis.cross(+X)
#. otherwise
The reference vector is +Y.
::
u = -1 axis.cross(+Y)
"""
self.doc = FreeCAD.ActiveDocument
self.axis = axis
self.axis.normalize()
ref_vec = Vector(0.0, 1.0, 0.0)
if ((abs(axis.x) > abs(axis.y)) and (abs(axis.y) > abs(axis.z))):
ref_vec = Vector(0.0, 0., 1.0)
self.u = axis.negative().cross(ref_vec)
self.u.normalize()
self.v = DraftVecUtils.rotate(self.u, math.pi/2, self.axis)
#projcase = "Case new"
elif ((abs(axis.y) > abs(axis.z)) and (abs(axis.z) >= abs(axis.x))):
ref_vec = Vector(1.0, 0.0, 0.0)
self.u = axis.negative().cross(ref_vec)
self.u.normalize()
self.v = DraftVecUtils.rotate(self.u, math.pi/2, self.axis)
#projcase = "Y>Z, View Y"
elif ((abs(axis.y) >= abs(axis.x)) and (abs(axis.x) > abs(axis.z))):
ref_vec = Vector(0.0, 0., 1.0)
self.u = axis.cross(ref_vec)
self.u.normalize()
self.v = DraftVecUtils.rotate(self.u, math.pi/2, self.axis)
#projcase = "ehem. XY, Case XY"
elif ((abs(axis.x) > abs(axis.z)) and (abs(axis.z) >= abs(axis.y))):
self.u = axis.cross(ref_vec)
self.u.normalize()
self.v = DraftVecUtils.rotate(self.u, math.pi/2, self.axis)
#projcase = "X>Z, View X"
elif ((abs(axis.z) >= abs(axis.y)) and (abs(axis.y) > abs(axis.x))):
ref_vec = Vector(1.0, 0., 0.0)
self.u = axis.cross(ref_vec)
self.u.normalize()
self.v = DraftVecUtils.rotate(self.u, math.pi/2, self.axis)
#projcase = "Y>X, Case YZ"
else:
self.u = axis.negative().cross(ref_vec)
self.u.normalize()
self.v = DraftVecUtils.rotate(self.u, math.pi/2, self.axis)
#projcase = "else"
#spat_vec = self.u.cross(self.v)
#spat_res = spat_vec.dot(axis)
#FreeCAD.Console.PrintMessage(projcase + " spat Prod = " + str(spat_res) + "\n")
offsetVector = Vector(axis); offsetVector.multiply(offset)
self.position = point.add(offsetVector)
self.weak = False
# FreeCAD.Console.PrintMessage("(position = " + str(self.position) + ")\n")
# FreeCAD.Console.PrintMessage("Current workplane: x="+str(DraftVecUtils.rounded(self.u))+" y="+str(DraftVecUtils.rounded(self.v))+" z="+str(DraftVecUtils.rounded(self.axis))+"\n")
def alignToCurve(self, shape, offset=0):
"""Align plane to curve. NOT YET IMPLEMENTED.
Parameters
----------
shape : Part.Shape
A curve that will serve to align the plane.
It can be an `'Edge'` or `'Wire'`.
offset : float
Defaults to zero. A value which will be used to offset
the plane in the direction of its `axis`.
Returns
-------
False
Returns `False` if the shape is null.
Currently it always returns `False`.
"""
if shape.isNull():
return False
elif shape.ShapeType == 'Edge':
#??? TODO: process curve here. look at shape.edges[0].Curve
return False
elif shape.ShapeType == 'Wire':
#??? TODO: determine if edges define a plane
return False
else:
return False
def alignToEdges(self, edges):
"""Align plane to two edges.
Uses the two points of the first edge to define the direction
of the unit vector `u`, the other two points of the other edge
to define the other unit vector `v`, and then the cross product
of `u` with `v` to define the `axis`.
Parameters
----------
edges : list
A list of two edges.
Returns
-------
False
Return `False` if `edges` is a list of more than 2 elements.
"""
# use a list of edges to find a plane position
if len(edges) > 2:
return False
# for axes systems, we suppose the 2 first edges are parallel
# ??? TODO: exclude other cases first
v1 = edges[0].Vertexes[-1].Point.sub(edges[0].Vertexes[0].Point)
v2 = edges[1].Vertexes[0].Point.sub(edges[0].Vertexes[0].Point)
v3 = v1.cross(v2)
v1.normalize()
v2.normalize()
v3.normalize()
#print v1,v2,v3
self.u = v1
self.v = v2
self.axis = v3
def alignToFace(self, shape, offset=0):
"""Align the plane to a face.
It uses the center of mass of the face as `position`,
and its normal in the center of the face as `axis`,
then calls `alignToPointAndAxis(position, axis, offset)`.
If the face is a quadrilateral, then it adjusts the position
of the plane according to its reported X direction and Y direction.
Also set `weak` to `False`.
Parameter
--------
shape : Part.Shape
A shape of type `'Face'`.
offset : float
Defaults to zero. A value which will be used to offset
the plane in the direction of its `axis`.
Returns
-------
bool
`True` if the operation was succesful, and `False` if the shape
is not a `'Face'`.
See Also
--------
alignToPointAndAxis, DraftGeomUtils.getQuad
"""
# Set face to the unique selected face, if found
if shape.ShapeType == 'Face':
self.alignToPointAndAxis(shape.Faces[0].CenterOfMass, shape.Faces[0].normalAt(0, 0), offset)
import DraftGeomUtils
q = DraftGeomUtils.getQuad(shape)
if q:
self.u = q[1]
self.v = q[2]
if not DraftVecUtils.equals(self.u.cross(self.v), self.axis):
self.u = q[2]
self.v = q[1]
if DraftVecUtils.equals(self.u, Vector(0, 0, 1)):
# the X axis is vertical: rotate 90 degrees
self.u, self.v = self.v.negative(), self.u
elif DraftVecUtils.equals(self.u, Vector(0, 0, -1)):
self.u, self.v = self.v, self.u.negative()
self.weak = False
return True
else:
return False
def alignTo3Points(self, p1, p2, p3, offset=0):
"""Align the plane to three points.
It makes a closed quadrilateral face with the three points,
and then calls `alignToFace(shape, offset)`.
Parameter
---------
p1 : Base::Vector3
The first point.
p2 : Base::Vector3
The second point.
p3 : Base::Vector3
The third point.
offset : float
Defaults to zero. A value which will be used to offset
the plane in the direction of its `axis`.
Returns
-------
bool
`True` if the operation was succesful, and `False` otherwise.
"""
import Part
w = Part.makePolygon([p1, p2, p3, p1])
f = Part.Face(w)
return self.alignToFace(f, offset)
def alignToSelection(self, offset=0):
"""Align the plane to a selection if it defines a plane.
If the selection uniquely defines a plane it will be used.
Currently it only works with one object selected, a `'Face'`.
It extracts the shape of the object or subobject
and then calls `alignToFace(shape, offset)`.
Parameter
---------
offset : float
Defaults to zero. A value which will be used to offset
the plane in the direction of its `axis`.
Returns
-------
bool
`True` if the operation was succesful, and `False` otherwise.
It returns `False` if the selection has no elements,
or if it has more than one element,
or if the object is not derived from `'Part::Feature'`
or if the object doesn't have a `Shape`.
To do
-----
The method returns `False` if the selection list has more than
one element.
The method should search the list for objects like faces, points,
edges, wires, etc., and call the appropriate aligning submethod.
The method could work for curves (`'Edge'` or `'Wire'`) but
`alignToCurve()` isn't fully implemented.
See also
--------
alignToFace, alignToCurve
"""
import FreeCADGui
sex = FreeCADGui.Selection.getSelectionEx(FreeCAD.ActiveDocument.Name)
if len(sex) == 0:
return False
elif len(sex) == 1:
if not sex[0].Object.isDerivedFrom("Part::Feature") \
or not sex[0].Object.Shape:
return False
return self.alignToCurve(sex[0].Object.Shape, offset) \
or self.alignToFace(sex[0].Object.Shape, offset) \
or (len(sex[0].SubObjects) == 1 and self.alignToFace(sex[0].SubObjects[0], offset))
else:
# len(sex) > 2, look for point and line, three points, etc.
return False
def setup(self, direction=None, point=None, upvec=None):
'''If working plane is undefined, define it!'''
if self.weak:
if direction and point:
self.alignToPointAndAxis(point, direction, 0, upvec)
else:
try:
import FreeCADGui
from pivy import coin
rot = FreeCADGui.ActiveDocument.ActiveView.getCameraNode().getField("orientation").getValue()
upvec = Vector(rot.multVec(coin.SbVec3f(0, 1, 0)).getValue())
vdir = FreeCADGui.ActiveDocument.ActiveView.getViewDirection()
if (vdir.getAngle(self.axis) > 0.001) and (vdir.getAngle(self.axis) < 3.14159):
# don't change the WP if it is already perpendicular to the current view
self.alignToPointAndAxis(Vector(0, 0, 0), vdir.negative(), 0, upvec)
except:
pass
self.weak = True
def reset(self):
self.doc = None
self.weak = True
def getRotation(self):
"returns a placement describing the working plane orientation ONLY"
m = DraftVecUtils.getPlaneRotation(self.u, self.v, self.axis)
p = FreeCAD.Placement(m)
# Arch active container
if FreeCAD.GuiUp:
import FreeCADGui
if FreeCADGui.ActiveDocument.ActiveView:
a = FreeCADGui.ActiveDocument.ActiveView.getActiveObject("Arch")
if a:
p = a.Placement.inverse().multiply(p)
return p
def getPlacement(self, rotated=False):
"returns the placement of the working plane"
if rotated:
m = FreeCAD.Matrix(
self.u.x, self.axis.x, -self.v.x, self.position.x,
self.u.y, self.axis.y, -self.v.y, self.position.y,
self.u.z, self.axis.z, -self.v.z, self.position.z,
0.0, 0.0, 0.0, 1.0)
else:
m = FreeCAD.Matrix(
self.u.x, self.v.x, self.axis.x, self.position.x,
self.u.y, self.v.y, self.axis.y, self.position.y,
self.u.z, self.v.z, self.axis.z, self.position.z,
0.0, 0.0, 0.0, 1.0)
p = FreeCAD.Placement(m)
# Arch active container if based on App Part
#if FreeCAD.GuiUp:
# import FreeCADGui
# a = FreeCADGui.ActiveDocument.ActiveView.getActiveObject("Arch")
# if a:
# p = a.Placement.inverse().multiply(p)
return p
def getNormal(self):
n = self.axis
# Arch active container if based on App Part
#if FreeCAD.GuiUp:
# import FreeCADGui
# a = FreeCADGui.ActiveDocument.ActiveView.getActiveObject("Arch")
# if a:
# n = a.Placement.inverse().Rotation.multVec(n)
return n
def setFromPlacement(self, pl, rebase=False):
"sets the working plane from a placement (rotaton ONLY, unless rebase=True)"
rot = FreeCAD.Placement(pl).Rotation
self.u = rot.multVec(FreeCAD.Vector(1, 0, 0))
self.v = rot.multVec(FreeCAD.Vector(0, 1, 0))
self.axis = rot.multVec(FreeCAD.Vector(0, 0, 1))
if rebase:
self.position = pl.Base
def inverse(self):
"inverts the direction of the working plane"
self.u = self.u.negative()
self.axis = self.axis.negative()
def save(self):
"stores the current plane state"
self.stored = [self.u, self.v, self.axis, self.position, self.weak]
def restore(self):
"restores a previously saved plane state, if exists"
if self.stored:
self.u = self.stored[0]
self.v = self.stored[1]
self.axis = self.stored[2]
self.position = self.stored[3]
self.weak = self.stored[4]
self.stored = None
def getLocalCoords(self, point):
"returns the coordinates of a given point on the working plane"
pt = point.sub(self.position)
xv = DraftVecUtils.project(pt, self.u)
x = xv.Length
if xv.getAngle(self.u) > 1:
x = -x
yv = DraftVecUtils.project(pt, self.v)
y = yv.Length
if yv.getAngle(self.v) > 1:
y = -y
zv = DraftVecUtils.project(pt, self.axis)
z = zv.Length
if zv.getAngle(self.axis) > 1:
z = -z
return Vector(x, y, z)
def getGlobalCoords(self, point):
"returns the global coordinates of the given point, taken relatively to this working plane"
vx = Vector(self.u).multiply(point.x)
vy = Vector(self.v).multiply(point.y)
vz = Vector(self.axis).multiply(point.z)
pt = (vx.add(vy)).add(vz)
return pt.add(self.position)
def getLocalRot(self, point):
"Same as getLocalCoords, but discards the WP position"
xv = DraftVecUtils.project(point, self.u)
x = xv.Length
if xv.getAngle(self.u) > 1:
x = -x
yv = DraftVecUtils.project(point, self.v)
y = yv.Length
if yv.getAngle(self.v) > 1:
y = -y
zv = DraftVecUtils.project(point, self.axis)
z = zv.Length
if zv.getAngle(self.axis) > 1:
z = -z
return Vector(x, y, z)
def getGlobalRot(self, point):
"Same as getGlobalCoords, but discards the WP position"
vx = Vector(self.u).multiply(point.x)
vy = Vector(self.v).multiply(point.y)
vz = Vector(self.axis).multiply(point.z)
pt = (vx.add(vy)).add(vz)
return pt
def getClosestAxis(self, point):
"returns which of the workingplane axes is closest from the given vector"
ax = point.getAngle(self.u)
ay = point.getAngle(self.v)
az = point.getAngle(self.axis)
bx = point.getAngle(self.u.negative())
by = point.getAngle(self.v.negative())
bz = point.getAngle(self.axis.negative())
b = min(ax, ay, az, bx, by, bz)
if b in [ax, bx]:
return "x"
elif b in [ay, by]:
return "y"
elif b in [az, bz]:
return "z"
else:
return None
def isGlobal(self):
"returns True if the plane axes are equal to the global axes"
if self.u != Vector(1, 0, 0):
return False
if self.v != Vector(0, 1, 0):
return False
if self.axis != Vector(0, 0, 1):
return False
return True
def isOrtho(self):
"returns True if the plane axes are following the global axes"
if round(self.u.getAngle(Vector(0, 1, 0)), 6) in [0, -1.570796, 1.570796, -3.141593, 3.141593, -4.712389, 4.712389, 6.283185]:
if round(self.v.getAngle(Vector(0, 1, 0)), 6) in [0, -1.570796, 1.570796, -3.141593, 3.141593, -4.712389, 4.712389, 6.283185]:
if round(self.axis.getAngle(Vector(0, 1, 0)), 6) in [0, -1.570796, 1.570796, -3.141593, 3.141593, -4.712389, 4.712389, 6.283185]:
return True
return False
def getDeviation(self):
"returns the deviation angle between the u axis and the horizontal plane"
proj = Vector(self.u.x, self.u.y, 0)
if self.u.getAngle(proj) == 0:
return 0
else:
norm = proj.cross(self.u)
return DraftVecUtils.angle(self.u, proj, norm)
def getPlacementFromPoints(points):
"returns a placement from a list of 3 or 4 vectors"
pl = plane()
try:
pl.position = points[0]
pl.u = (points[1].sub(points[0]).normalize())
pl.v = (points[2].sub(points[0]).normalize())
if len(points) == 4:
pl.axis = (points[3].sub(points[0]).normalize())
else:
pl.axis = ((pl.u).cross(pl.v)).normalize()
except:
return None
p = pl.getPlacement()
del pl
return p
def getPlacementFromFace(face, rotated=False):
"returns a placement from a face"
pl = plane()
try:
pl.alignToFace(face)
except:
return None
p = pl.getPlacement(rotated)
del pl
return p