-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTDChainDistances.FCMacro
212 lines (175 loc) · 7.87 KB
/
TDChainDistances.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
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
"""
This macro creates chain measurements, as it aligns all distances to the same coordinate
It also re-centers the distance value.
If only a single distance measure is given, it will only be re-centered.
The first dimension selected will be the reference.
All other dimension must be of the same type!
The dimensions must be for parallel edges, otherwise the macro wont work.
"""
__Name__ = 'TDChainDistances'
__Comment__ = 'Create Chain Distances in TechDraw'
__Author__ = 'Sebastian Bachmann'
__Version__ = '1.0'
__Date__ = '2019-09-16'
__License__ = 'MIT'
__Web__ = 'https://github.com/reox/FreeCAD_Macros'
__Wiki__ = ''
__Icon__ = 'icons/TDChainDistances.svg'
__Help__ = 'Select any number of distances and use the macro. ' \
'The distances must be parallel. ' \
'The first distance is taken as reference.'
__Status__ = 'Stable'
__Requires__ = '>=0.19.18234; 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 math
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 is_above(a, b, p):
"""Checks if Point p is above the line a-b"""
# Extra check for division by zero -> vertical line
if (b.x - a.x) == 0:
return p.x < a.x
print(p.y, (((b.y - a.y) / (b.x - a.x) * (p.x - a.x)) + a.y))
if p.y > (((b.y - a.y) / (b.x - a.x) * (p.x - a.x)) + a.y):
return True
return False
def get_dim_vec(point):
"""
Returns the center of the line (m) which the dimension is measuring (a vector),
the normal vector (v) on the line of measurement (a unit vector)
and the orthogonal length to the dimension from the line.
All vectors are returned in model space.
"""
#FIXME: somewhere in this function there is a bug for Dimensions,
# that if one of the dimensions is close to the line itself, it will
# flip around. Usually, the second click on this macro resolves the issue though
# getLinearPoints() --> returns in model space
# dimension .X and .Y --> return in paper space (not scaled, need to divide by scale)
# makeCosmeticVertex --> requires scaled paper space
# a, b are in model space
a, b = point.getLinearPoints()
# get the center vector of the line in question
m = (a + b) / 2.0
# Get the current location of the dimension as a vector
# NOTE: has to be converted from paper space to model space by swapping Y axis
D = FreeCAD.Vector(point.X, -point.Y, 0)
# --> If you need to debug the current locations
#if len(point.OutList) > 1:
# print("Uhm, outlist has more entries than expected?!")
#view = point.OutList[0]
# plot the current location of the dimension
#view.makeCosmeticVertex(FreeCAD.Vector(point.X, point.Y, 0) / view.Scale)
# Plot the center of the line segment
#view.makeCosmeticVertex(FreeCAD.Vector(m.x, -m.y, 0) / view.Scale)
# get the line as a vector
# We want that a is the vector left from b
# As the sorting in the list might be different, we need to check that.
if a.x < b.x:
# Vector pointing from b to a, i.e. if a is left from b, the vector points
# in the negative direction
l = a - b
else:
if a.y < b.y:
# There might be the case that the line is straight up or down
l = a - b
else:
l = b - a
if l.x == 0 or point.Type == 'DistanceY':
# This is rather a DistanceY
if D.x < m.x:
return m, FreeCAD.Vector(-1, 0, 0), m.x - D.x
return m, FreeCAD.Vector(1, 0, 0), D.x - m.x
if l.y == 0 or point.Type == 'DistanceX':
# This is rather a DistanceX
if D.y < m.y:
return m, FreeCAD.Vector(0, -1, 0), m.y - D.y
return m, FreeCAD.Vector(0, 1, 0), D.y - m.y
# If not horizontal or vertical lines; get orthogonal vector on l
v = FreeCAD.Vector(-(l.x ** -1), (l.y ** -1), 0)
v.normalize() # Normalize as l might be any orthogonal vector
# We need to know if the dimension line is above or beyond the edge
if is_above(a, b, D):
v = -v
# Now, we calculate the vector between the center Point and the current
# location of the dimension
x = m - D
length = x.Length
# We need the angle between the vectors to calculate the distance
x.normalize()
# The actual length of v is now the cos(alpha) times |x|
length = math.fabs(x.dot(v)) * length
# This length times v gives the new point for the dimension
return m, v, length
def main():
sel = FreeCADGui.Selection.getSelection()
if not sel:
# Do nothing, empty selection
return
if not all(map(lambda x: x.isDerivedFrom("TechDraw::DrawViewDimension"), sel)):
msg("Error", "You need to only select DrawViewDimensions!")
return
reference = next(iter(sel))
if reference.Type not in ("DistanceX", "DistanceY", "Distance"):
msg("Error", "The dimension must be DistanceX, DistanceY or Distance!")
return
if not all(map(lambda x: x.Type == reference.Type, sel)):
msg("Error", "All dimensions must have the same type as the first selected")
return
if reference.Type == "Distance":
ref_m, ref_v, ref_length = get_dim_vec(reference)
# Make sure, all dimensions point into the same direction
# Some dimensions might be under the line, hence their vector is
# negative the reference
for s in sel:
_, check_v, _ = get_dim_vec(s)
# TODO: check if 0.000001 is a reasonable number; time will tell
if not ref_v.isEqual(check_v, 0.000001):
if not ref_v.isEqual(-check_v, 0.000001):
msg("Error", "{} point into different directions!".format(s.Name))
return
else:
ref_length = 0
ref_v = None
for s in sel:
m, cur_v, _ = get_dim_vec(s)
# Need to check if special cases, where Type==Distance is used for
# horizontal or vertical lines
if reference.Type == "DistanceY" or math.fabs(cur_v.x) == 1:
s.X = reference.X
s.Y = -m.y
elif reference.Type == "DistanceX" or math.fabs(cur_v.y) == 1:
s.Y = reference.Y
s.X = m.x
elif reference.Type == "Distance":
# For each line, must calculate the center and normal
# But the lines might be anywhere, hence we need to calculate the
# distance in direction of the normal vector between the reference
# and the current one.
x = m - ref_m
v = ref_v * (ref_length - (math.cos(x.getAngle(cur_v)) * x.Length))
s.X = m.x + v.x
s.Y = -(m.y + v.y)
if __name__ == "__main__":
main()