-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerateDrawing.FCMacro
212 lines (193 loc) · 7.67 KB
/
GenerateDrawing.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
# Macro for automatic drawing generation
# with 3 normal projections and one isometric.
# Author: Milos Petrasinovic <mpetrasinovic@pr-dc.com>
# PR-DC, Republic of Serbia
# info@pr-dc.com
#
# --------------------
#
# Copyright (C) 2022 PRDC <info@pr-dc.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ----- PARAMS -----
paper = [420, 297] # A3 paper size
border = [10, 10, 400, 277] # border position ans size
table = [190, 74] # table size
dx = 10 # space between views along x axis
dy = 10 # space between views along y axis
author = "Miloš Petrašinović" # author name
drawn_date = "07.01.2022." # drawing drawn date
template_file = "test\\PRDC_template_A3.svg" # drawing template
# --------------------
__Name__ = 'GenerateDrawing'
__Comment__ = 'Macro for automatic drawing generation.'
__Author__ = 'Milos Petrasinovic <mpetrasinovic@pr-dc.com>'
__Version__ = '1.0.0'
__Date__ = '2022-01-07'
__License__ = 'GPL-3.0-or-later'
__Web__ = 'https://github.com/PR-DC/PRDC_GenerateDrawing_FC'
__Wiki__ = 'https://wiki.freecadweb.org/Macro_GenerateDrawing'
__Icon__ = 'GenerateDrawing.svg'
__Help__ = 'Open model and run the macro program!'
__Status__ = 'stable'
__Requires__ = 'Freecad >= 0.18'
__Communication__ = 'https://github.com/PR-DC/PRDC_GenerateDrawing_FC/issues/'
__Files__ = 'GenerateDrawing.svg'
import FreeCAD as App
import FreeCADGui as Gui
from PySide import QtGui
import Draft
import math
import os
if not os.path.isabs(template_file):
template_file = os.path.dirname(__file__) + '\\' + template_file
print(template_file)
doc = App.activeDocument()
doc_gui = Gui.activeDocument()
Gui.activateWorkbench("TechDrawWorkbench")
shapes = []
def GetShapes(objs, shapes):
for obj in objs:
if obj.TypeId == 'App::DocumentObjectGroup':
GetShapes(obj.OutList, shapes)
elif hasattr(obj, "Shape") and doc_gui.getObject(obj.Name).Visibility:
shapes.append(obj)
return shapes
if doc is not None:
shapes = GetShapes(doc.RootObjects, shapes)
if len(shapes):
if len(shapes) == 1:
model = shapes[0]
else:
model = doc.addObject("Part::Compound", "DrawingModel")
model.Links = shapes
doc.recompute()
# Obtain model bounding box
BB = model.Shape.BoundBox
X = BB.XMax-BB.XMin
Y = BB.YMax-BB.YMin
Z = BB.ZMax-BB.ZMin
Isometric = Draft.makeShape2DView(model, App.Vector(0.57735, 0.57735, 0.57735))
doc.recompute()
IBB = Isometric.Shape.BoundBox
if App.Version()[1] < "19":
Xrot = IBB.XMin+(IBB.XMax-IBB.XMin)/2
Yrot = IBB.YMin+(IBB.YMax-IBB.YMin)/2
Draft.rotate(Isometric, -120, center=App.Vector(Xrot, Yrot, 0), axis=App.Vector(0, 0, 1), copy=False)
doc.recompute()
IBB = Isometric.Shape.BoundBox
IsometricBB = [IBB.XMax-IBB.XMin, IBB.YMax-IBB.YMin]
doc.removeObject(Isometric.Name)
if len(shapes) > 1:
doc.removeObject(model.Name)
for obj in shapes:
doc_gui.getObject(obj.Name).Visibility = True
# Page scale
p1 = border[2]/(X+dx+Z)
p2 = border[3]/(Y+dy+Z)
if p1 < 1 or p2 < 1:
# Decrease
if p1 > p2:
s = math.ceil(1/p2)
else:
s = math.ceil(1/p1)
s = 1/s
scale_str = "1:"+str(s)
else:
# Increase
if p1 > p2:
s = math.floor(p2)
else:
s = math.floor(p1)
scale_str = str(s)+":1"
# Add page with template
page = doc.addObject('TechDraw::DrawPage', 'Page')
template = doc.addObject('TechDraw::DrawSVGTemplate', 'Template')
template.Template = template_file
page.Template = template
page.Scale = s
# Add project group views
views = doc.addObject('TechDraw::DrawProjGroup', 'ProjGroup')
views.AutoDistribute = False
page.addView(views)
views.Source = shapes
doc.recompute()
Top = views.addProjection('Front')
Top.Label = "Top"
views.Anchor.Direction = App.Vector(0, 0, 1)
if App.Version()[1] >= "19":
views.Anchor.XDirection = App.Vector(1, 0, 0)
views.Anchor.RotationVector = App.Vector(1, 0, 0)
views.Anchor.recompute()
views.ProjectionType = 'First Angle'
Front = views.addProjection('Top')
Front.Label = "Front"
Left = views.addProjection('Left')
Left.Label = "Left"
# Isometric view
Isometric = doc.addObject('TechDraw::DrawViewPart', 'Isometric')
page.addView(Isometric)
Isometric.Source = shapes
Isometric.ScaleType = "Custom"
Isometric.Scale = s/2
Isometric.Label = "Isometric"
Isometric.Direction = App.Vector(0.57735, 0.57735, 0.57735)
if App.Version()[1] >= "19":
Isometric.XDirection = App.Vector(0.57735, 0.57735, 0.57735)
else:
Isometric.Rotation = -120.00
Isometric.recompute()
# Views position
px0 = border[0]+dx
py0 = paper[1]-dy-border[1]
views.X = px0+s*X/2
views.Y = py0-s*Y/2
Front.Y = -(s*Y/2+dy+s*Z/2)
Left.X = s*X/2+dx+s*Z/2
Isometric.X = paper[0]-border[0]-dx-s/2*IsometricBB[0]/2
Isometric.Y = border[1]+table[1]+dy+s/2*IsometricBB[1]/2
# Change drawing text fields
template.setEditFieldContent("AUTHOR_NAME", author)
template.setEditFieldContent("AUTHOR_DATE", drawn_date)
template.setEditFieldContent("DRAWING_SCALE", scale_str)
template.setEditFieldContent("PROJECT_NAME", "PROJECT NAME")
template.setEditFieldContent("DRAWING_TITLE", "DRAWING TITLE")
template.setEditFieldContent("PCS_NUMBER", "X pcs")
template.setEditFieldContent("MATERIAL_NAME", "x")
template.setEditFieldContent("FINISH_TYPE", "As Produced")
template.setEditFieldContent("TOLERANCE_VALUE", "xxx")
template.setEditFieldContent("SUPERVISOR_NAME", "Supervisor")
template.setEditFieldContent("SUPERVISOR_DATE", "xxx")
template.setEditFieldContent("DESIGNER_NAME", "Designer")
template.setEditFieldContent("DESIGNER_DATE", "xxx")
template.setEditFieldContent("PAGE_SIZE", "A3")
template.setEditFieldContent("REV_NUMBER", "X")
template.setEditFieldContent("PART_WEIGHT", "XXX")
template.setEditFieldContent("SHEET_NUMBER", "1/1")
doc.recompute()
doc_gui.getObject(page.Name).show()
Gui.updateGui()
else:
msgBox = QtGui.QMessageBox()
msgBox.setWindowTitle("Error")
msgBox.setText("No shapes for drawing!")
msgBox.exec_()
App.Console.PrintError("\nError: No shapes for drawing!\n")
else:
msgBox = QtGui.QMessageBox()
msgBox.setWindowTitle("Error")
msgBox.setText("No active document!")
msgBox.exec_()
App.Console.PrintError("\nError: No active document!\n")