-
Notifications
You must be signed in to change notification settings - Fork 5
/
TDEdgeIntersection.FCMacro
104 lines (84 loc) · 3.72 KB
/
TDEdgeIntersection.FCMacro
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
"""
This Macro is intended to be used in the TechDraw Workbench,
to find the intersection point of two Edges inside a DrawViewPart.
Select two Egdes and run the Macro. The cosmetic vertex will be created
at the intersection point.
"""
__Name__ = 'TDEdgeIntersection'
__Comment__ = 'Inserts a cosmetic vertex at the position where two lines intersect'
__Author__ = 'Sebastian Bachmann'
__Version__ = '1.0'
__Date__ = '2020-05-07'
__License__ = 'MIT'
__Web__ = 'https://github.com/reox/FreeCAD_Macros'
__Wiki__ = ''
__Icon__ = 'icons/EdgeIntersection.svg'
__Help__ = 'Select two straight lines and run the macro'
__Status__ = 'Stable'
__Requires__ = '>=0.19.20943; py3 only'
__Communication__ = 'https://github.com/reox/FreeCAD_Macros/issues'
# Copyright (c) 2019, Sebastian Bachmann <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import numpy as np
from PySide.QtGui import QMessageBox
from PySide import QtCore
def msg(title, message):
diag = QMessageBox(QMessageBox.Warning, title, message)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
diag.exec_()
def find_intersection():
"""
Finds the intersection point of two edges
And creates a cosmetic vertex there
"""
# Get the current view:
cur_view = Gui.Selection.getSelectionEx()[0].Object
if not cur_view.isDerivedFrom("TechDraw::DrawViewPart"):
msg("Wrong Selection", "Must select Edges of DrawViewPart!")
return
# Get the names of the selected edges:
elem = Gui.Selection.getSelectionEx()[0].SubElementNames
if not all(map(lambda x: x.startswith('Edge'), elem)) or len(elem) != 2:
msg("Wrong Selection", "You have not selected two edges!")
return
try:
indices = map(lambda x: int(x.replace('Edge', '')), elem)
except ValueError as e:
msg("Something is wrong", "Can not parse Edge indices: {}".format(e))
return
(p, q), (r, s) = list(map(lambda i: (i[0].Point, i[1].Point), map(lambda a:
cur_view.getEdgeByIndex(a).Vertexes, indices)))
# Want to solve: p + t(q - p) = r + v(s - r)
# t and v are costants, p,q,r,s are vectors
a = np.array([[q.x - p.x, -s.x + r.x], [q.y - p.y, -s.y + r.y]])
b = np.array([r.x - p.x, r.y - p.y])
try:
t, v = np.linalg.solve(a, b)
except np.linalg.LinAlgError:
# usually singular matrix -> det(a) = 0 -> parallel lines
msg("Singular Matrix", "The lines are probably parallel, no intersection possible!")
else:
x = p.x + t * (q.x - p.x)
y = r.y + v * (s.y - r.y)
res = FreeCAD.Vector(x, y, 0)
# Add the Vertex
cur_view.makeCosmeticVertex(res)
# Recompute
App.activeDocument().recompute(None,True,True)
if __name__ == "__main__":
find_intersection()