forked from GreatDevelopers/BIM_Workbench
/
BimProject.py
343 lines (307 loc) · 17.8 KB
/
BimProject.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
#***************************************************************************
#* *
#* Copyright (c) 2017 Yorik van Havre <yorik@uncreated.net> *
#* *
#* 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 FreeCAD commands for the BIM workbench"""
import os,FreeCAD,FreeCADGui
def QT_TRANSLATE_NOOP(ctx,txt): return txt # dummy function for the QT translator
class BIM_Project:
def GetResources(self):
return {'Pixmap' : os.path.join(os.path.dirname(__file__),"icons","BIM_Project.svg"),
'MenuText': QT_TRANSLATE_NOOP("BIM_Levels", "Manage project..."),
'ToolTip' : QT_TRANSLATE_NOOP("BIM_Levels", "Setup your BIM project")}
def Activated(self):
# load dialog
from PySide import QtCore,QtGui
self.form = FreeCADGui.PySideUic.loadUi(os.path.join(os.path.dirname(__file__),"dialogProject.ui"))
# center the dialog over FreeCAD window
mw = FreeCADGui.getMainWindow()
self.form.move(mw.frameGeometry().topLeft() + mw.rect().center() - self.form.rect().center())
# set things up
import Arch
self.form.buildingUse.addItems(Arch.BuildingTypes)
self.form.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__),"icons","BIM_Project.svg")))
QtCore.QObject.connect(self.form.buttonAdd, QtCore.SIGNAL("clicked()"), self.addGroup)
QtCore.QObject.connect(self.form.buttonDel, QtCore.SIGNAL("clicked()"), self.delGroup)
QtCore.QObject.connect(self.form.buttonSave,QtCore.SIGNAL("clicked()"), self.savePreset)
QtCore.QObject.connect(self.form.presets,QtCore.SIGNAL("currentIndexChanged(QString)"), self.getPreset)
QtCore.QObject.connect(self.form.buttonOK,QtCore.SIGNAL("clicked()"), self.accept)
QtCore.QObject.connect(self.form.buttonCancel,QtCore.SIGNAL("clicked()"), self.reject)
self.fillPresets()
# show dialog
self.form.show()
def reject():
self.form.hide()
return True
def accept(self):
if self.form.groupNewDocument.isChecked() or (FreeCAD.ActiveDocument == None):
doc = FreeCAD.newDocument()
if self.form.projectName.text():
doc.Label = self.form.projectName.text()
FreeCAD.ActiveDocument = doc
if not FreeCAD.ActiveDocument:
FreeCAD.Console.PrintError("No active document, aborting.\n")
import Draft,Arch
site = None
outline = None
if self.form.groupSite.isChecked():
site = Arch.makeSite()
site.Label = self.form.siteName.text()
site.Address = self.form.siteAddress.text()
site.Longitude = self.form.siteLongitude.value()
site.Latitude = self.form.siteLatitude.value()
if hasattr(site,"NorthDeviation"):
site.NorthDeviation = self.form.siteDeviation.value()
elif hasattr(site,"Declination"):
site.Declination = self.form.siteDeviation.value()
site.Elevation = FreeCAD.Units.Quantity(self.form.siteElevation.text()).Value
if self.form.groupBuilding.isChecked():
building = Arch.makeBuilding()
if site:
site.Group = [building]
building.Label = self.form.buildingName.text()
building.BuildingType = self.form.buildingUse.currentText()
buildingWidth = FreeCAD.Units.Quantity(self.form.buildingWidth.text()).Value
buildingLength = FreeCAD.Units.Quantity(self.form.buildingLength.text()).Value
distVAxes = FreeCAD.Units.Quantity(self.form.distVAxes.text()).Value
distHAxes = FreeCAD.Units.Quantity(self.form.distHAxes.text()).Value
levelHeight = FreeCAD.Units.Quantity(self.form.levelHeight.text()).Value
color = self.form.lineColor.property("color").getRgbF()[:3]
if buildingWidth and buildingLength:
outline = Draft.makeRectangle(buildingLength,buildingWidth,face=False)
outline.Label = "Building Outline"
outline.ViewObject.DrawStyle = "Dashed"
outline.ViewObject.LineColor = color
outline.ViewObject.LineWidth = self.form.lineWidth.value()*2
grp = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup")
grp.Label = "Layout"
building.addObject(grp)
grp.addObject(outline)
if self.form.buildingName.text():
outtext = Draft.makeText([self.form.buildingName.text()],point=FreeCAD.Vector(Draft.getParam("textheight",0.20)*0.3,-Draft.getParam("textheight",0.20)*1.43,0))
outtext.Label = "Building Label"
outtext.ViewObject.TextColor = color
grp.addObject(outtext)
axisV = None
if self.form.countVAxes.value() and distVAxes:
axisV = Arch.makeAxis(num = self.form.countVAxes.value(), size = distVAxes, name="vaxis")
axisV.Label = "Vertical Axes"
axisV.ViewObject.BubblePosition = "Both"
axisV.ViewObject.LineWidth = self.form.lineWidth.value()
axisV.ViewObject.FontSize = Draft.getParam("textheight",0.20)
axisV.ViewObject.BubbleSize = Draft.getParam("textheight",0.20)*1.43
axisV.ViewObject.LineColor = color
if outline:
axisV.setExpression('Length', outline.Name+'.Height * 1.1')
axisV.setExpression('Placement.Base.y', outline.Name+'.Placement.Base.y - '+axisV.Name+'.Length * 0.05')
axisV.setExpression('Placement.Base.x', outline.Name+'.Placement.Base.x')
axisH = None
if self.form.countHAxes.value() and distHAxes:
axisH = Arch.makeAxis(num = self.form.countHAxes.value(), size = distHAxes, name="haxis")
axisH.Label = "Horizontal Axes"
axisH.ViewObject.BubblePosition = "Both"
axisH.ViewObject.NumberingStyle = "A,B,C"
axisH.ViewObject.LineWidth = self.form.lineWidth.value()
axisH.ViewObject.FontSize = Draft.getParam("textheight",0.20)
axisH.ViewObject.BubbleSize = Draft.getParam("textheight",0.20)*1.43
axisH.Placement.Rotation = FreeCAD.Rotation(FreeCAD.Vector(0,0,1),90)
axisH.ViewObject.LineColor = color
if outline:
axisH.setExpression('Length', outline.Name+'.Length * 1.1')
axisH.setExpression('Placement.Base.x', outline.Name+'.Placement.Base.x + '+axisH.Name+'.Length * 0.945')
axisH.setExpression('Placement.Base.y', outline.Name+'.Placement.Base.y')
if axisV and axisH:
axisG = Arch.makeAxisSystem([axisH,axisV])
axisG.Label = "Axes"
grp.addObject(axisG)
else:
if axisV:
grp.addObject(axisV)
if axisH:
grp.addObject(axisH)
if self.form.countLevels.value() and levelHeight:
h = 0
alabels = []
for i in range(self.form.countLevels.value()):
lev = Arch.makeFloor()
lev.Label = "Level "+str(i)
alabels.append(lev.Label)
lev.Height = levelHeight
lev.Placement.move(FreeCAD.Vector(0,0,h))
h += levelHeight
building.addObject(lev)
if self.form.levelsWP.isChecked():
prx = Draft.makeWorkingPlaneProxy(FreeCAD.Placement())
prx.Placement.move(FreeCAD.Vector(0,0,h))
lev.addObject(prx)
if self.form.levelsAxis.isChecked():
axisL = Arch.makeAxis(num = self.form.countLevels.value(), size = levelHeight, name="laxis")
axisL.Label = "Level Axes"
axisL.ViewObject.BubblePosition = "None"
axisL.ViewObject.LineWidth = self.form.lineWidth.value()
axisL.ViewObject.FontSize = Draft.getParam("textheight",0.20)
axisL.Placement.Rotation = FreeCAD.Rotation(FreeCAD.Vector (0.577350269189626, -0.5773502691896257, 0.5773502691896257),120)
axisL.ViewObject.LineColor = color
axisL.ViewObject.LabelOffset.Rotation = FreeCAD.Rotation(FreeCAD.Vector(1,0,0),90)
axisL.Labels = alabels
axisL.ViewObject.ShowLabel = True
if outline:
axisL.setExpression('Length', outline.Name+'.Length * 1.1')
axisL.setExpression('Placement.Base.x', outline.Name+'.Placement.Base.x + '+axisL.Name+'.Length * 0.945')
axisL.setExpression('Placement.Base.y', outline.Name+'.Placement.Base.y')
grp.addObject(axisL)
axisL.ViewObject.LabelOffset.Base = FreeCAD.Vector(-axisL.Length.Value + Draft.getParam("textheight",0.20)*0.43,0,Draft.getParam("textheight",0.20)*0.43)
self.form.hide()
FreeCAD.ActiveDocument.recompute()
if outline:
FreeCADGui.Selection.clearSelection()
FreeCADGui.Selection.addSelection(outline)
FreeCADGui.SendMsgToActiveView("ViewSelection")
FreeCADGui.Selection.clearSelection()
if hasattr(FreeCADGui,"Snapper"):
FreeCADGui.Snapper.show()
return True
def addGroup(self):
from PySide import QtCore,QtGui
it = QtGui.QListWidgetItem("New Group")
it.setFlags(it.flags() | QtCore.Qt.ItemIsEditable)
self.form.groupsList.addItem(it)
def delGroup(self):
r = self.form.groupsList.currentRow()
if r >= 0:
self.form.groupsList.takeItem(r)
def savePreset(self):
from PySide import QtCore,QtGui
import Arch
res = QtGui.QInputDialog.getText(None, "Save preset","Preset name:", QtGui.QLineEdit.Normal,"DefaultProject")
if res[1]:
name = res[0]
presetdir = os.path.join(FreeCAD.getUserAppDataDir(),"BIM")
if not os.path.isdir(presetdir):
os.mkdir(presetdir)
groups = []
for i in range(self.form.groupsList.count()):
groups.append(self.form.groupsList.item(i).text())
s = "# FreeCAD BIM Project setup preset "+name+"\n"
s += "groupNewDocument="+str(int(self.form.groupNewDocument.isChecked()))+"\n"
s += "projectName="+self.form.projectName.text()+"\n"
s += "groupSite="+str(int(self.form.groupSite.isChecked()))+"\n"
s += "siteName="+self.form.siteName.text()+"\n"
s += "siteAddress="+self.form.siteAddress.text()+"\n"
s += "siteLongitude="+str(self.form.siteLongitude.value())+"\n"
s += "siteLatitude="+str(self.form.siteLatitude.value())+"\n"
s += "siteDeviation="+str(self.form.siteDeviation.value())+"\n"
s += "siteElevation="+self.form.siteElevation.text()+"\n"
s += "groupBuilding="+str(int(self.form.groupBuilding.isChecked()))+"\n"
s += "buildingName="+self.form.buildingName.text()+"\n"
s += "buildingUse="+str(self.form.buildingUse.currentIndex())+"\n"
s += "buildingLength="+self.form.buildingLength.text()+"\n"
s += "buildingWidth="+self.form.buildingWidth.text()+"\n"
s += "countVAxes="+str(self.form.countVAxes.value())+"\n"
s += "distVAxes="+self.form.distVAxes.text()+"\n"
s += "countHAxes="+str(self.form.countHAxes.value())+"\n"
s += "distHAxes="+self.form.distHAxes.text()+"\n"
s += "countLevels="+str(self.form.countLevels.value())+"\n"
s += "levelHeight="+self.form.levelHeight.text()+"\n"
s += "lineWidth="+str(self.form.lineWidth.value())+"\n"
s += "lineColor="+str(self.form.lineColor.property("color").getRgbF()[:3])+"\n"
s += "groups="+";;".join(groups)+"\n"
s += "levelsWP="+str(int(self.form.levelsWP.isChecked()))+"\n"
s += "levelsAxis="+str(int(self.form.levelsAxis.isChecked()))+"\n"
f = open(os.path.join(presetdir,name+".txt"),"w")
f.write(s)
f.close()
self.fillPresets()
def fillPresets(self):
self.form.presets.clear()
self.form.presets.addItem("User preset...")
presetdir = os.path.join(FreeCAD.getUserAppDataDir(),"BIM")
if os.path.isdir(presetdir):
for f in os.listdir(presetdir):
if f.endswith(".txt"):
self.form.presets.addItem(os.path.splitext(f)[0])
def getPreset(self,preset):
import Arch
from PySide import QtGui
pfile = os.path.join(FreeCAD.getUserAppDataDir(),"BIM",preset+".txt")
if os.path.exists(pfile):
f = open(pfile,"r")
buf = f.read()
f.close()
lines = buf.split("\n")
for line in lines:
if line:
if line[0] != "#":
s = line.split("=")
if s[0] == "groupNewDocument":
self.form.groupNewDocument.setChecked(bool(int(s[1])))
elif s[0] == "projectName":
self.form.projectName.setText(s[1])
elif s[0] == "groupSite":
self.form.groupSite.setChecked(bool(int(s[1])))
elif s[0] == "siteName":
self.form.siteName.setText(s[1])
elif s[0] == "siteAddress":
self.form.siteAddress.setText(s[1])
elif s[0] == "siteLongitude":
self.form.siteLongitude.setValue(float(s[1]))
elif s[0] == "siteLatitude":
self.form.siteLatitude.setValue(float(s[1]))
elif s[0] == "siteDeviation":
self.form.siteDeviation.setValue(float(s[1]))
elif s[0] == "siteElevation":
self.form.siteElevation.setText(s[1])
elif s[0] == "groupBuilding":
self.form.groupBuilding.setChecked(bool(int(s[1])))
elif s[0] == "buildingName":
self.form.buildingName.setText(s[1])
elif s[0] == "buildingUse":
self.form.buildingUse.setCurrentIndex(int(s[1]))
elif s[0] == "buildingLength":
self.form.buildingLength.setText(s[1])
elif s[0] == "buildingWidth":
self.form.buildingWidth.setText(s[1])
elif s[0] == "countVAxes":
self.form.countVAxes.setValue(int(s[1]))
elif s[0] == "distVAxes":
self.form.distVAxes.setText(s[1])
elif s[0] == "countHAxes":
self.form.countHAxes.setValue(int(s[1]))
elif s[0] == "distHAxes":
self.form.distHAxes.setText(s[1])
elif s[0] == "countLevels":
self.form.countLevels.setValue(int(s[1]))
elif s[0] == "levelHeight":
self.form.levelHeight.setText(s[1])
elif s[0] == "lineWidth":
self.form.lineWidth.setValue(int(s[1]))
elif s[0] == "lineColor":
col = tuple([float(t) for t in s[1].strip("(").strip(")").split(",")])
col = QtGui.QColor.fromRgbF(*col)
self.form.lineColor.setProperty("color",col)
elif s[0] == "groups":
groups = s[1].split(";;")
self.form.groupsList.clear()
self.form.groupsList.addItems(groups)
elif s[0] == "levelsWP":
self.form.levelsWP.setChecked(bool(int(s[1])))
elif s[0] == "levelsAxis":
self.form.levelsAxis.setChecked(bool(int(s[1])))
FreeCADGui.addCommand('BIM_Project',BIM_Project())