-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTDTangentialMeasure.FCMacro
146 lines (118 loc) · 5.23 KB
/
TDTangentialMeasure.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
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
"""
This Macro is intended to be used in the TechDraw Workbench,
to find the tangential points on two circles, which are parallel to a given reference.
Select three edges: two circles and a "axis" line.
"""
__Name__ = 'TDTangentialMeasure'
__Comment__ = 'Create a tangential measure on two arcs by aligning to an axis'
__Author__ = 'Sebastian Bachmann'
__Version__ = '0.9'
__Date__ = '2020-05-07'
__License__ = 'MIT'
__Web__ = 'https://github.com/reox/FreeCAD_Macros'
__Wiki__ = ''
__Icon__ = 'icons/TDTangentialMeasure.svg'
__Help__ = 'Select two arcs where the tangential measure shall be applied ' \
'and an additional straight line which will be the axis. ' \
'The macro will create four cosmetic vertices: two at the arcs ' \
'and two at the opposing sites in order to apply a distance measure.'
__Status__ = 'Beta'
__Requires__ = '>=0.19.20943; py3 only'
__Communication__ = 'https://github.com/reox/FreeCAD_Macros/issues'
# Copyright (c) 2020, 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_tangential():
"""
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) != 3:
msg("Wrong Selection", "You have not selected three 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
# Edge -> Curve (Circle) -> Location + Radius
# -> Curve (Line) -> Direction
items = list(map(lambda a: cur_view.getEdgeByIndex(a), indices))
points = []
directions = []
radii = []
dir = None
for item in items:
if item.Curve.TypeId == 'Part::GeomCircle':
v1, v2 = item.Vertexes
# Calculate the "direction vector" of the circle
p = (item.Curve.Location - v1.Point) + (item.Curve.Location - v2.Point)
p.normalize()
points.append(item.Curve.Location)
directions.append(p)
radii.append(item.Curve.Radius)
elif item.Curve.TypeId == 'Part::GeomLine':
dir = item.Curve.Direction
else:
print("Where do I belong?", item.Curve.TypeId)
if dir is None or len(points) != 2:
msg("Wrong Selection", "Please select two arcs and a straight line!")
return
p1, p2 = points
d1, d2 = directions
r1, r2 = radii
def dir_sign(v1, v2):
"""Calculate the directional sign of two vectors"""
a = v1.getAngle(v2)
# TODO: is this correct?! I thought that we would return 1 here and -1 otherwise...
# Probably a problem because of the two coordinate system which are used in TD
if a < np.pi / 2.0 or a >= (3.0/2.0) * np.pi:
return -1
return 1
# Can be enabled to create a vertex at the arc's center
#cur_view.makeCosmeticVertex(p1)
#cur_view.makeCosmeticVertex(p2)
# Tangential points in direction of axis
vert1 = p1 + dir_sign(dir, d1) * dir * r1
vert2 = p2 + dir_sign(dir, d2) * dir * r2
cur_view.makeCosmeticVertex(vert1)
cur_view.makeCosmeticVertex(vert2)
# We can "fake" the measurement by creating the point which can be used to measure the distance
distance = dir.dot(vert1 - vert2)
vert3 = vert2 + (dir * distance)
cur_view.makeCosmeticVertex(vert3)
# Also create the other vertex, so we have an option to choose
vert4 = vert1 - (dir * distance)
cur_view.makeCosmeticVertex(vert4)
# Recompute
App.activeDocument().recompute(None,True,True)
if __name__ == "__main__":
find_tangential()