-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimBox.py
353 lines (319 loc) · 14.3 KB
/
BimBox.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# ***************************************************************************
# * *
# * Copyright (c) 2018 Yorik van Havre <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
from __future__ import print_function
"""This module contains FreeCAD commands for the BIM workbench"""
import os
import FreeCAD
from BimTranslateUtils import *
class BIM_Box:
def GetResources(self):
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "BIM_Box.svg"),
"MenuText": QT_TRANSLATE_NOOP("BIM_Box", "Box"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Box", "Graphically creates a generic box in the current document"
),
}
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
def Activated(self):
import FreeCADGui
try:
import DraftTrackers
except Exception:
import draftguitools.gui_trackers as DraftTrackers
# here we will store our points
self.points = []
# we build a special cube tracker which is a list of 4 rectangle trackers
self.cubetracker = []
self.LengthValue = 0
self.WidthValue = 0
self.HeightValue = 0
self.currentpoint = None
for i in range(4):
self.cubetracker.append(DraftTrackers.rectangleTracker())
if hasattr(FreeCADGui, "Snapper"):
FreeCADGui.Snapper.getPoint(
callback=self.PointCallback,
movecallback=self.MoveCallback,
extradlg=self.taskbox(),
)
def MoveCallback(self, point, snapinfo):
import DraftVecUtils
self.currentpoint = point
if len(self.points) == 1:
# we have the base point already
self.Length.setText(
FreeCAD.Units.Quantity(
str(self.points[-1].sub(point).Length) + "mm"
).UserString
)
self.Length.selectAll()
self.Length.setFocus()
elif len(self.points) == 2:
# now we already have our base line, we update the 1st rectangle
p = point
v1 = point.sub(self.points[1])
v4 = v1.cross(self.points[1].sub(self.points[0]))
if v4 and v4.Length:
n = (self.points[1].sub(self.points[0])).cross(v4)
if n and n.Length:
n = DraftVecUtils.project(v1, n)
p = self.points[1].add(n)
self.cubetracker[0].p3(p)
self.Width.setText(
FreeCAD.Units.Quantity(
str(self.cubetracker[0].getSize()[1]) + "mm"
).UserString
)
self.Width.selectAll()
self.Width.setFocus()
elif len(self.points) == 3:
# we must first find our height point by projecting on the normal
w = DraftVecUtils.project(point.sub(self.cubetracker[0].p3()), self.normal)
# then we update all rectangles
self.cubetracker[1].p3((self.cubetracker[0].p2()).add(w))
self.cubetracker[2].p3((self.cubetracker[0].p4()).add(w))
self.cubetracker[3].p1((self.cubetracker[0].p1()).add(w))
self.cubetracker[3].p3((self.cubetracker[0].p3()).add(w))
self.Height.setText(FreeCAD.Units.Quantity(str(w.Length) + "mm").UserString)
self.Height.selectAll()
self.Height.setFocus()
def PointCallback(self, point, snapinfo):
import FreeCADGui
if not point:
# cancelled
if hasattr(FreeCAD, "DraftWorkingPlane"):
FreeCAD.DraftWorkingPlane.restore()
FreeCADGui.Snapper.setGrid()
for c in self.cubetracker:
c.off()
return
if len(self.points) == 0:
# this is our first clicked point, nothing to do just yet
FreeCADGui.Snapper.getPoint(
last=point,
callback=self.PointCallback,
movecallback=self.MoveCallback,
extradlg=self.taskbox(),
)
elif len(self.points) == 1:
# this is our second point
# we turn on only one of the rectangles
baseline = point.sub(self.points[0])
self.cubetracker[0].setPlane(baseline)
self.cubetracker[0].p1(self.points[0])
self.cubetracker[0].on()
FreeCADGui.Snapper.getPoint(
last=point,
callback=self.PointCallback,
movecallback=self.MoveCallback,
extradlg=self.taskbox(),
)
elif len(self.points) == 2:
# this is our third point
# we can get the cubes Z axis from our first rectangle
self.normal = self.cubetracker[0].getNormal()
# we can therefore define the (u,v) planes of all rectangles
u = self.cubetracker[0].u
v = self.cubetracker[0].v
self.cubetracker[1].setPlane(u, self.normal)
self.cubetracker[2].setPlane(u, self.normal)
self.cubetracker[3].setPlane(u, v)
# and the origin points of the vertical rectangles
self.cubetracker[1].p1(self.cubetracker[0].p1())
self.cubetracker[2].p1(self.cubetracker[0].p3())
# finally we turn all rectangles on
for r in self.cubetracker:
r.on()
if hasattr(FreeCAD, "DraftWorkingPlane"):
FreeCAD.DraftWorkingPlane.save()
FreeCAD.DraftWorkingPlane.position = self.cubetracker[0].p3()
FreeCAD.DraftWorkingPlane.u = (
self.cubetracker[0].p4().sub(self.cubetracker[0].p3())
).normalize()
FreeCAD.DraftWorkingPlane.v = FreeCAD.Vector(self.normal).normalize()
FreeCAD.DraftWorkingPlane.axis = (
self.cubetracker[0].p2().sub(self.cubetracker[0].p3())
).normalize()
FreeCADGui.Snapper.setGrid()
FreeCADGui.Snapper.getPoint(
last=self.cubetracker[0].p3(),
callback=self.PointCallback,
movecallback=self.MoveCallback,
extradlg=self.taskbox(),
)
elif len(self.points) == 3:
# finally we have all our points. Let's create the actual cube
cube = FreeCAD.ActiveDocument.addObject("Part::Box", "Cube")
cube.Length = self.LengthValue
cube.Width = self.WidthValue
cube.Height = self.HeightValue
# we get 3 points that define our cube orientation
p1 = self.cubetracker[0].p1()
p2 = self.cubetracker[0].p2()
p3 = self.cubetracker[0].p4()
import WorkingPlane
cube.Placement = WorkingPlane.getPlacementFromPoints([p1, p2, p3])
if hasattr(FreeCAD, "DraftWorkingPlane"):
FreeCAD.DraftWorkingPlane.restore()
FreeCADGui.Snapper.setGrid()
for c in self.cubetracker:
c.off()
FreeCAD.ActiveDocument.recompute()
self.points.append(point)
def taskbox(self):
"sets up a taskbox widget"
from PySide import QtCore, QtGui
import FreeCADGui
wid = QtGui.QWidget()
ui = FreeCADGui.UiLoader()
wid.setWindowTitle(translate("BIM", "Box dimensions"))
grid = QtGui.QGridLayout(wid)
label1 = QtGui.QLabel(translate("BIM", "Length"))
self.Length = ui.createWidget("Gui::InputField")
self.Length.setText(FreeCAD.Units.Quantity(self.LengthValue, FreeCAD.Units.Length).UserString)
grid.addWidget(label1, 0, 0, 1, 1)
grid.addWidget(self.Length, 0, 1, 1, 1)
if self.LengthValue:
self.Length.setEnabled(False)
label2 = QtGui.QLabel(translate("BIM", "Width"))
self.Width = ui.createWidget("Gui::InputField")
self.Width.setText(FreeCAD.Units.Quantity(self.WidthValue, FreeCAD.Units.Length).UserString)
grid.addWidget(label2, 1, 0, 1, 1)
grid.addWidget(self.Width, 1, 1, 1, 1)
if self.WidthValue or (not self.LengthValue):
self.Width.setEnabled(False)
label3 = QtGui.QLabel(translate("BIM", "Height"))
self.Height = ui.createWidget("Gui::InputField")
self.Height.setText(FreeCAD.Units.Quantity(self.HeightValue, FreeCAD.Units.Length).UserString)
grid.addWidget(label3, 2, 0, 1, 1)
grid.addWidget(self.Height, 2, 1, 1, 1)
if not self.WidthValue:
self.Height.setEnabled(False)
QtCore.QObject.connect(
self.Length, QtCore.SIGNAL("valueChanged(double)"), self.setLength
)
QtCore.QObject.connect(
self.Width, QtCore.SIGNAL("valueChanged(double)"), self.setWidth
)
QtCore.QObject.connect(
self.Height, QtCore.SIGNAL("valueChanged(double)"), self.setHeight
)
QtCore.QObject.connect(
self.Length, QtCore.SIGNAL("returnPressed()"), self.setLengthUI
)
QtCore.QObject.connect(
self.Width, QtCore.SIGNAL("returnPressed()"), self.setWidthUI
)
QtCore.QObject.connect(
self.Height, QtCore.SIGNAL("returnPressed()"), self.setHeightUI
)
return wid
def setLength(self, d):
self.LengthValue = d
def setWidth(self, d):
self.WidthValue = d
def setHeight(self, d):
self.HeightValue = d
def setLengthUI(self):
import FreeCADGui
if (len(self.points) == 1) and self.currentpoint and self.LengthValue:
baseline = self.currentpoint.sub(self.points[0])
baseline.normalize()
baseline.multiply(self.LengthValue)
p2 = self.points[0].add(baseline)
self.points.append(p2)
self.cubetracker[0].setPlane(baseline)
self.cubetracker[0].p1(self.points[0])
self.cubetracker[0].on()
FreeCADGui.Snapper.getPoint(
last=p2,
callback=self.PointCallback,
movecallback=self.MoveCallback,
extradlg=self.taskbox(),
)
def setWidthUI(self):
import FreeCADGui
if (len(self.points) == 2) and self.currentpoint and self.WidthValue:
self.normal = self.cubetracker[0].getNormal()
if self.normal:
n = (self.points[1].sub(self.points[0])).cross(self.normal)
if n and n.Length:
n.normalize()
n.multiply(self.WidthValue)
p2 = self.points[1].add(n)
self.cubetracker[0].p3(p2)
self.points.append(p2)
u = self.cubetracker[0].u
v = self.cubetracker[0].v
self.cubetracker[1].setPlane(u, self.normal)
self.cubetracker[2].setPlane(u, self.normal)
self.cubetracker[3].setPlane(u, v)
self.cubetracker[1].p1(self.cubetracker[0].p1())
self.cubetracker[2].p1(self.cubetracker[0].p3())
for r in self.cubetracker:
r.on()
if hasattr(FreeCAD, "DraftWorkingPlane"):
FreeCAD.DraftWorkingPlane.save()
FreeCAD.DraftWorkingPlane.position = self.cubetracker[0].p3()
FreeCAD.DraftWorkingPlane.u = (
self.cubetracker[0].p4().sub(self.cubetracker[0].p3())
).normalize()
FreeCAD.DraftWorkingPlane.v = self.normal
FreeCAD.DraftWorkingPlane.axis = (
self.cubetracker[0].p2().sub(self.cubetracker[0].p3())
).normalize()
FreeCADGui.Snapper.setGrid()
FreeCADGui.Snapper.getPoint(
last=p2,
callback=self.PointCallback,
movecallback=self.MoveCallback,
extradlg=self.taskbox(),
)
def setHeightUI(self):
import FreeCADGui
if (len(self.points) == 3) and self.HeightValue:
cube = FreeCAD.ActiveDocument.addObject("Part::Box", "Cube")
cube.Length = self.LengthValue
cube.Width = self.WidthValue
cube.Height = self.HeightValue
# we get 3 points that define our cube orientation
p1 = self.cubetracker[0].p1()
p2 = self.cubetracker[0].p2()
p3 = self.cubetracker[0].p4()
FreeCADGui.Snapper.off()
import WorkingPlane
cube.Placement = WorkingPlane.getPlacementFromPoints([p1, p2, p3])
if hasattr(FreeCAD, "DraftWorkingPlane"):
FreeCAD.DraftWorkingPlane.restore()
FreeCADGui.Snapper.setGrid()
for c in self.cubetracker:
c.off()
FreeCADGui.Snapper.getPoint()
FreeCADGui.Snapper.off()
if hasattr(FreeCADGui, "draftToolBar"):
FreeCADGui.draftToolBar.offUi()
FreeCAD.ActiveDocument.recompute()