-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimWrappedTools.py
583 lines (501 loc) · 19.6 KB
/
BimWrappedTools.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# -*- coding: utf8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2017 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 *
# * *
# ***************************************************************************
"""This module contains BIM wrappers for commands from other workbenches"""
import os
import FreeCAD
import DraftVecUtils
from BimTranslateUtils import *
from draftguitools import gui_dimensions
from draftguitools import gui_shape2dview
class BIM_Project:
def __init__(self):
try:
import ifc_tools
except:
self.NativeIFC = False
else:
self.NativeIFC = True
def GetResources(self):
if self.NativeIFC:
icon = os.path.join(
os.path.dirname(__file__), "icons", "Arch_Project_IFC.svg"
)
descr = QT_TRANSLATE_NOOP(
"BIM_Project", "Create an empty NativeIFC project"
)
else:
icon = os.path.join(os.path.dirname(__file__), "icons", "Arch_Project.svg")
descr = QT_TRANSLATE_NOOP("BIM_Project", "Create an empty BIM project")
return {
"Pixmap": icon,
"MenuText": QT_TRANSLATE_NOOP("BIM_Project", "Project"),
"ToolTip": descr,
}
def Activated(self):
if self.NativeIFC:
import ifc_tools
project = ifc_tools.create_document_object(FreeCAD.ActiveDocument)
project.Modified = True
FreeCAD.ActiveDocument.recompute()
else:
import FreeCADGui
FreeCADGui.runCommand("Arch_Project")
class BIM_Builder:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "Part_Shapebuilder.svg"
),
"MenuText": QT_TRANSLATE_NOOP("Part_Builder", "Shape builder..."),
"ToolTip": QT_TRANSLATE_NOOP(
"Part_Builder", "Advanced utility to create shapes"
),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_Builder")
class BIM_Offset2D:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "Part_Offset2D.svg"
),
"MenuText": QT_TRANSLATE_NOOP("Part_Offset2D", "2D Offset..."),
"ToolTip": QT_TRANSLATE_NOOP(
"Part_Offset2D", "Utility to offset planar shapes"
),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_Offset2D")
class BIM_Extrude:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "Part_Extrude.svg"
),
"MenuText": QT_TRANSLATE_NOOP("Part_Extrude", "Extrude..."),
"ToolTip": QT_TRANSLATE_NOOP("Part_Extrude", "Extrude a selected sketch"),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_Extrude")
class BIM_Cut:
def GetResources(self):
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "Part_Cut.svg"),
"MenuText": QT_TRANSLATE_NOOP("BIM_Cut", "Difference"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Cut", "Make a difference between two shapes"
),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_Cut")
class BIM_Fuse:
def GetResources(self):
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "Part_Fuse.svg"),
"MenuText": QT_TRANSLATE_NOOP("Part_Fuse", "Union"),
"ToolTip": QT_TRANSLATE_NOOP("Part_Fuse", "Make a union of several shapes"),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_Fuse")
class BIM_Common:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "Part_Common.svg"
),
"MenuText": QT_TRANSLATE_NOOP("Part_Common", "Intersection"),
"ToolTip": QT_TRANSLATE_NOOP(
"Part_Common", "Make an intersection of two shapes"
),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_Common")
class BIM_Compound:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "Part_Compound.svg"
),
"MenuText": QT_TRANSLATE_NOOP("Part_Compound", "Make compound"),
"ToolTip": QT_TRANSLATE_NOOP(
"Part_Compound", "Make a compound of several shapes"
),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_Compound")
class BIM_SimpleCopy:
def GetResources(self):
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "Tree_Part.svg"),
"MenuText": QT_TRANSLATE_NOOP("Part_SimpleCopy", "Create simple copy"),
"ToolTip": QT_TRANSLATE_NOOP(
"Part_SimpleCopy", "Create a simple non-parametric copy"
),
}
def Activated(self):
import FreeCADGui
import PartGui
FreeCADGui.runCommand("Part_SimpleCopy")
class BIM_TDPage:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "techdraw-PageDefault.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_TDPage", "Page"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_TDPage", "Creates a new TechDraw page from a template"
),
}
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
def Activated(self):
from PySide import QtCore, QtGui
import TechDraw
templatedir = FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Mod/BIM"
).GetString("TDTemplateDir", "")
if not templatedir:
templatedir = None
filename = QtGui.QFileDialog.getOpenFileName(
QtGui.QApplication.activeWindow(),
translate("BIM", "Select page template"),
templatedir,
"SVG file (*.svg)",
)
if filename:
filename = filename[0]
name = os.path.splitext(os.path.basename(filename))[0]
FreeCAD.ActiveDocument.openTransaction("Create page")
page = FreeCAD.ActiveDocument.addObject("TechDraw::DrawPage", "Page")
page.Label = name
template = FreeCAD.ActiveDocument.addObject(
"TechDraw::DrawSVGTemplate", "Template"
)
template.Template = filename
template.Label = translate("BIM", "Template")
page.Template = template
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM").SetString(
"TDTemplateDir", filename.replace("\\", "/")
)
for txt in ["scale", "Scale", "SCALE", "scaling", "Scaling", "SCALING"]:
if txt in page.Template.EditableTexts:
val = page.Template.EditableTexts[txt]
if val:
if ":" in val:
val.replace(":", "/")
if "/" in val:
try:
page.Scale = eval(val)
except:
pass
else:
break
else:
try:
page.Scale = float(val)
except:
pass
else:
break
else:
page.Scale = FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Mod/BIM"
).GetFloat("DefaultPageScale", 0.01)
FreeCAD.ActiveDocument.recompute()
class BIM_TDArchView:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "techdraw-ArchView.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_TDArchView", "View"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_TDArchView",
"Creates a TechDraw view from a section plane or 2D objects",
),
}
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
def Activated(self):
import FreeCADGui
import Draft
sections = []
page = None
drafts = []
for obj in FreeCADGui.Selection.getSelection():
t = Draft.getType(obj)
if t == "SectionPlane":
sections.append(obj)
elif t == "TechDraw::DrawPage":
page = obj
else:
drafts.append(obj)
if not page:
pages = FreeCAD.ActiveDocument.findObjects(Type="TechDraw::DrawPage")
if pages:
page = pages[0]
if (not page) or ((not sections) and (not drafts)):
FreeCAD.Console.PrintError(
translate(
"BIM",
"No section view or draft objects selected, or no page selected, or no page found in document",
)
+ "\n"
)
return
FreeCAD.ActiveDocument.openTransaction("Create view")
for section in sections:
view = FreeCAD.ActiveDocument.addObject(
"TechDraw::DrawViewArch", "ArchView"
)
view.Label = section.Label
view.Source = section
page.addView(view)
if page.Scale:
view.Scale = page.Scale
for draft in drafts:
view = FreeCAD.ActiveDocument.addObject(
"TechDraw::DrawViewDraft", "DraftView"
)
view.Label = draft.Label
view.Source = draft
page.addView(view)
if page.Scale:
view.Scale = page.Scale
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
class BIM_ImagePlane:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "Image_CreateImagePlane.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_ImagePlane", "Image plane"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_ImagePlane", "Creates a plane from an image"
),
}
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
def Activated(self):
import FreeCADGui
from PySide import QtCore, QtGui
try:
import DraftTrackers
except Exception:
import draftguitools.gui_trackers as DraftTrackers
self.tracker = DraftTrackers.rectangleTracker()
self.basepoint = None
self.opposite = None
(filename, _filter) = QtGui.QFileDialog.getOpenFileName(
QtGui.QApplication.activeWindow(),
translate("BIM", "Select image"),
None,
translate("BIM", "Image file (*.png *.jpg *.bmp)"),
)
if filename:
self.filename = filename
im = QtGui.QImage(self.filename)
self.proportion = float(im.height()) / float(im.width())
if hasattr(FreeCADGui, "Snapper"):
FreeCADGui.Snapper.getPoint(
callback=self.PointCallback, movecallback=self.MoveCallback
)
def MoveCallback(self, point, snapinfo):
if point and self.basepoint and (point != self.basepoint):
chord = point.sub(self.basepoint)
length = DraftVecUtils.project(chord, self.tracker.u).Length
height = length * self.proportion
self.opposite = (
FreeCAD.Vector(self.tracker.u)
.multiply(length)
.add(FreeCAD.Vector(self.tracker.v).multiply(height))
)
self.opposite = self.basepoint.add(self.opposite)
self.tracker.update(self.opposite)
def PointCallback(self, point, snapinfo):
import FreeCADGui
import Image
if not point:
# cancelled
self.tracker.off()
return
elif not self.basepoint:
# this is our first clicked point, nothing to do just yet
self.basepoint = point
self.tracker.setorigin(point)
self.tracker.on()
FreeCADGui.Snapper.getPoint(
last=point, callback=self.PointCallback, movecallback=self.MoveCallback
)
else:
# this is our second point
self.tracker.off()
midpoint = self.basepoint.add(
self.opposite.sub(self.basepoint).multiply(0.5)
)
rotation = FreeCAD.DraftWorkingPlane.getRotation().Rotation
diagonal = self.opposite.sub(self.basepoint)
length = DraftVecUtils.project(diagonal, FreeCAD.DraftWorkingPlane.u).Length
height = DraftVecUtils.project(diagonal, FreeCAD.DraftWorkingPlane.v).Length
FreeCAD.ActiveDocument.openTransaction("Create image plane")
image = FreeCAD.activeDocument().addObject(
"Image::ImagePlane", "ImagePlane"
)
image.Label = os.path.splitext(os.path.basename(self.filename))[0]
image.ImageFile = self.filename
image.Placement = FreeCAD.Placement(midpoint, rotation)
image.XSize = length
image.YSize = height
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
class BIM_DimensionAligned(gui_dimensions.Dimension):
def __init__(self):
super(BIM_DimensionAligned, self).__init__()
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_DimensionAligned.svg"
),
"MenuText": QT_TRANSLATE_NOOP("BIM_DimensionAligned", "Aligned dimension"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_DimensionAligned", "Create an aligned dimension"
),
"Accel": "D, I",
}
class BIM_DimensionHorizontal(gui_dimensions.Dimension):
def __init__(self):
super(BIM_DimensionHorizontal, self).__init__()
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_DimensionHorizontal.svg"
),
"MenuText": QT_TRANSLATE_NOOP(
"BIM_DimensionHorizontal", "Horizontal dimension"
),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_DimensionHorizontal", "Create an horizontal dimension"
),
"Accel": "D, H",
}
def Activated(self):
self.dir = FreeCAD.DraftWorkingPlane.u
super(BIM_DimensionHorizontal, self).Activated()
class BIM_DimensionVertical(gui_dimensions.Dimension):
def __init__(self):
super(BIM_DimensionVertical, self).__init__()
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_DimensionVertical.svg"
),
"MenuText": QT_TRANSLATE_NOOP(
"BIM_DimensionVertical", "Vertical dimension"
),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_DimensionVertical", "Create a vertical dimension"
),
"Accel": "D, V",
}
def Activated(self):
self.dir = FreeCAD.DraftWorkingPlane.v
super(BIM_DimensionVertical, self).Activated()
class BIM_Text:
def GetResources(self):
import Draft_rc
return {
"Pixmap": "Draft_Text",
"MenuText": QT_TRANSLATE_NOOP("BIM_Text", "Text"),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_Text", "Create a text in the current 3D view or TechDraw page"
),
"Accel": "T, E",
}
def Activated(self):
import FreeCADGui
import draftutils.utils as utils
self.view = FreeCADGui.ActiveDocument.ActiveView
if hasattr(self.view, "getPage") and self.view.getPage():
self.text = ""
FreeCADGui.draftToolBar.sourceCmd = self
FreeCADGui.draftToolBar.taskUi()
FreeCADGui.draftToolBar.textUi()
else:
FreeCADGui.runCommand("Draft_Text")
def createObject(self):
import TechDraw
if self.text:
page = self.view.getPage()
pagescale = page.Scale
if not pagescale:
pagescale = 1
anno = FreeCAD.ActiveDocument.addObject(
"TechDraw::DrawViewAnnotation", "Annotation"
)
anno.Text = self.text
page.addView(anno)
param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
anno.TextSize = param.GetFloat("textheight", 10) * pagescale
anno.Font = param.GetString("textfont", "Sans")
c = param.GetUnsigned("DefaultTextColor", 255)
r = ((c >> 24) & 0xFF) / 255.0
g = ((c >> 16) & 0xFF) / 255.0
b = ((c >> 8) & 0xFF) / 255.0
anno.TextColor = (r, g, b)
self.finish()
def finish(self, arg=False):
import FreeCADGui
FreeCADGui.draftToolBar.sourceCmd = None
FreeCADGui.draftToolBar.offUi()
class BIM_Shape2DView(gui_shape2dview.Shape2DView):
def GetResources(self):
d = super(BIM_Shape2DView, self).GetResources()
d["Pixmap"] = "Arch_BuildingPart_Tree"
d["MenuText"] = QT_TRANSLATE_NOOP("BIM_Shape2DView", "Shape-based view")
return d