-
Notifications
You must be signed in to change notification settings - Fork 6
/
TDThread.FCMacro
129 lines (101 loc) · 4.84 KB
/
TDThread.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
"""
This Macro is intended to be used in the TechDraw Workbench,
it paints a thread according to DIN ISO 6410-1:1993 around a hole.
THIS ONLY WORKS IN A VERY RECENT VERSION (0.19.21099)!!!
"""
__Name__ = 'TDThread'
__Comment__ = 'Paint threads for holes in TechDraw'
__Author__ = 'Sebastian Bachmann'
__Version__ = '1.0'
__Date__ = '2020-05-07'
__License__ = 'MIT'
__Web__ = 'https://github.com/reox/FreeCAD_Macros'
__Wiki__ = ''
__Icon__ = 'icons/TDThread.svg'
__Help__ = 'Select a hole and run the macro'
__Status__ = 'Stable'
__Requires__ = '>=0.19.21099; 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
import TechDraw
def msg(title, message):
diag = QMessageBox(QMessageBox.Warning, title, message)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
diag.exec_()
def paint_thread(offset=3):
"""
Paint a thread around a circle in TD
:param float offset: additional units to draw centerlines outwards in both directions
"""
# FIXME: Need to get the correct values here!
# Maybe one method would be to have a table of common values and select the ones which are
# closest to the hole.
thread_depth = 2 # additional size which will be added to the radius
thread_size = 'M16' # What will be written in the dimension
# 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
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
# 04.1 --> dash dot, small
center_style = (4, cur_view.ViewObject.IsoWidth, (0.0, 0.0, 0.0, 1.0), True)
# 01.1 --> full line, small
thread_style = (1, cur_view.ViewObject.IsoWidth, (0.0, 0.0, 0.0, 1.0), True)
for i in indices:
curve = cur_view.getEdgeByIndex(i).Curve
# If we use this coordinate in makeComsmeticVertex, we get the vertex at the center.
# X: positive direction to the right of the screen
# Y: positive direction to the top of the screen
center = curve.Location
radius = curve.Radius
# DIN ISO 6410-1:1993: 3/4 circle, opening and position variable
edge = cur_view.makeCosmeticCircleArc(center, radius + thread_depth, 110, 20)
cur_view.getCosmeticEdge(edge).Format = thread_style
# Make dimension
r_vec = App.Vector(radius + thread_depth, 0, 0)
v1 = center + r_vec
v2 = center - r_vec
dim = TechDraw.makeDistanceDim(cur_view, 'DistanceX', v1, v2)
# NOTE: scaling has to be applied for dimensions!
dim.X = center.x * cur_view.Scale
# TODO: Actually we need to take into account the size between the text and the line..
# Not sure how we can get this programatically
dim.Y = (center.y + radius + 3*offset) * cur_view.Scale
dim.Arbitrary = True
dim.FormatSpec = thread_size
# create centermark
e = cur_view.makeCosmeticLine(center + App.Vector(0, radius + offset), center - App.Vector(0, radius + offset))
cur_view.getCosmeticEdge(e).Format = center_style
e = cur_view.makeCosmeticLine(center + App.Vector(radius + offset, 0), center - App.Vector(radius + offset, 0))
cur_view.getCosmeticEdge(e).Format = center_style
cur_view.requestPaint()
# Recompute
App.activeDocument().recompute(None,True,True)
if __name__ == "__main__":
paint_thread()