/
ArchRebar.py
285 lines (261 loc) · 12.4 KB
/
ArchRebar.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
#***************************************************************************
#* *
#* Copyright (c) 2013 *
#* 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 *
#* *
#***************************************************************************
import FreeCAD,Draft,ArchComponent,DraftVecUtils,ArchCommands
from FreeCAD import Vector
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore, QtGui
from DraftTools import translate
from PySide.QtCore import QT_TRANSLATE_NOOP
else:
# \cond
def translate(ctxt,txt):
return txt
def QT_TRANSLATE_NOOP(ctxt,txt):
return txt
# \endcond
## @package ArchRebar
# \ingroup ARCH
# \brief The Rebar object and tools
#
# This module provides tools to build Rebar objects.
# Rebars (or Reinforcing Bars) are metallic bars placed
# inside concrete strutures to reinforce them.
__title__="FreeCAD Rebar"
__author__ = "Yorik van Havre"
__url__ = "http://www.freecadweb.org"
def makeRebar(baseobj=None,sketch=None,diameter=None,amount=1,offset=None,name="Rebar"):
"""makeRebar([baseobj,sketch,diameter,amount,offset,name]): adds a Reinforcement Bar object
to the given structural object, using the given sketch as profile."""
p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch")
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython",name)
obj.Label = translate("Arch",name)
_Rebar(obj)
if FreeCAD.GuiUp:
_ViewProviderRebar(obj.ViewObject)
if baseobj and sketch:
if hasattr(sketch,"Support"):
if sketch.Support:
if isinstance(sketch.Support,tuple):
if sketch.Support[0] == baseobj:
sketch.Support = None
elif sketch.Support == baseobj:
sketch.Support = None
obj.Base = sketch
if FreeCAD.GuiUp:
sketch.ViewObject.hide()
p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch")
if p.GetBool("archRemoveExternal",False):
a = baseobj.Armatures
a.append(obj)
baseobj.Armatures = a
else:
import Arch
host = getattr(Arch,"make"+Draft.getType(baseobj))(baseobj)
a = host.Armatures
a.append(obj)
host.Armatures = a
if diameter:
obj.Diameter = diameter
else:
obj.Diameter = p.GetFloat("RebarDiameter",6)
obj.Amount = amount
if offset:
obj.OffsetStart = offset
obj.OffsetEnd = offset
else:
obj.OffsetStart = p.GetFloat("RebarOffset",30)
obj.OffsetEnd = p.GetFloat("RebarOffset",30)
ArchCommands.fixDAG(obj)
return obj
class _CommandRebar:
"the Arch Rebar command definition"
def GetResources(self):
return {'Pixmap' : 'Arch_Rebar',
'MenuText': QT_TRANSLATE_NOOP("Arch_Rebar","Rebar"),
'Accel': "R, B",
'ToolTip': QT_TRANSLATE_NOOP("Arch_Rebar","Creates a Reinforcement bar from the selected face of a structural object")}
def IsActive(self):
return not FreeCAD.ActiveDocument is None
def Activated(self):
sel = FreeCADGui.Selection.getSelectionEx()
if sel:
obj = sel[0].Object
if Draft.getType(obj) == "Structure":
if len(sel) > 1:
sk = sel[1].Object
if Draft.getType(sk) == "Sketch":
# we have a base object and a sketch: create the rebar now
FreeCAD.ActiveDocument.openTransaction(translate("Arch","Create Rebar"))
FreeCADGui.addModule("Arch")
FreeCADGui.doCommand("Arch.makeRebar(FreeCAD.ActiveDocument."+obj.Name+",FreeCAD.ActiveDocument."+sk.Name+")")
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
return
else:
# we have only a base object: open the sketcher
FreeCADGui.activateWorkbench("SketcherWorkbench")
FreeCADGui.runCommand("Sketcher_NewSketch")
FreeCAD.ArchObserver = ArchComponent.ArchSelectionObserver(obj,FreeCAD.ActiveDocument.Objects[-1],hide=False,nextCommand="Arch_Rebar")
FreeCADGui.Selection.addObserver(FreeCAD.ArchObserver)
return
elif Draft.getType(obj) == "Sketch":
# we have only the sketch: extract the base object from it
if hasattr(obj,"Support"):
if obj.Support:
if len(obj.Support) != 0:
sup = obj.Support[0][0]
else:
print "Arch: error: couldn't extract a base object"
return
FreeCAD.ActiveDocument.openTransaction(translate("Arch","Create Rebar"))
FreeCADGui.addModule("Arch")
FreeCADGui.doCommand("Arch.makeRebar(FreeCAD.ActiveDocument."+sup.Name+",FreeCAD.ActiveDocument."+obj.Name+")")
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
return
else:
print("Arch: error: couldn't extract a base object")
return
FreeCAD.Console.PrintMessage(translate("Arch","Please select a base face on a structural object\n"))
FreeCADGui.Control.showDialog(ArchComponent.SelectionTaskPanel())
FreeCAD.ArchObserver = ArchComponent.ArchSelectionObserver(nextCommand="Arch_Rebar")
FreeCADGui.Selection.addObserver(FreeCAD.ArchObserver)
class _Rebar(ArchComponent.Component):
"A parametric reinforcement bar (rebar) object"
def __init__(self,obj):
ArchComponent.Component.__init__(self,obj)
obj.addProperty("App::PropertyLength","Diameter","Arch",QT_TRANSLATE_NOOP("App::Property","The diameter of the bar"))
obj.addProperty("App::PropertyLength","OffsetStart","Arch",QT_TRANSLATE_NOOP("App::Property","The distance between the border of the beam and the fist bar (concrete cover)."))
obj.addProperty("App::PropertyLength","OffsetEnd","Arch",QT_TRANSLATE_NOOP("App::Property","The distance between the border of the beam and the last bar (concrete cover)."))
obj.addProperty("App::PropertyInteger","Amount","Arch",QT_TRANSLATE_NOOP("App::Property","The amount of bars"))
obj.addProperty("App::PropertyLength","Spacing","Arch",QT_TRANSLATE_NOOP("App::Property","The spacing between the bars"))
obj.addProperty("App::PropertyVector","Direction","Arch",QT_TRANSLATE_NOOP("App::Property","The direction to use to spread the bars. Keep (0,0,0) for automatic direction."))
obj.addProperty("App::PropertyFloat","Rounding","Arch",QT_TRANSLATE_NOOP("App::Property","The fillet to apply to the angle of the base profile. This value is multiplied by the bar diameter."))
self.Type = "Rebar"
obj.setEditorMode("Spacing",1)
def getBaseAndAxis(self,wire):
"returns a base point and orientation axis from the base wire"
import DraftGeomUtils
if wire:
e = wire.Edges[0]
v = DraftGeomUtils.vec(e).normalize()
return e.Vertexes[0].Point,v
if obj.Base:
if obj.Base.Shape:
if obj.Base.Shape.Wires:
e = obj.Base.Shape.Wires[0].Edges[0]
v = DraftGeomUtils.vec(e).normalize()
return e.Vertexes[0].Point,v
return None,None
def execute(self,obj):
if self.clone(obj):
return
if len(obj.InList) != 1:
return
if Draft.getType(obj.InList[0]) != "Structure":
return
if not obj.InList[0].Shape:
return
if not obj.Base:
return
if not obj.Base.Shape:
return
if not obj.Base.Shape.Wires:
return
if not obj.Diameter.Value:
return
if not obj.Amount:
return
father = obj.InList[0]
wire = obj.Base.Shape.Wires[0]
if hasattr(obj,"Rounding"):
#print(obj.Rounding)
if obj.Rounding:
radius = obj.Rounding * obj.Diameter.Value
import DraftGeomUtils
wire = DraftGeomUtils.filletWire(wire,radius)
bpoint, bvec = self.getBaseAndAxis(wire)
if not bpoint:
return
axis = obj.Base.Placement.Rotation.multVec(FreeCAD.Vector(0,0,-1))
size = (ArchCommands.projectToVector(father.Shape.copy(),axis)).Length
if hasattr(obj,"Direction"):
if not DraftVecUtils.isNull(obj.Direction):
axis = FreeCAD.Vector(obj.Direction) #.normalize()
# don't normalize so the vector can also be used to determine the distance
size = axis.Length
#print(axis)
#print(size)
if (obj.OffsetStart.Value + obj.OffsetEnd.Value) > size:
return
# all tests ok!
pl = obj.Placement
import Part
circle = Part.makeCircle(obj.Diameter.Value/2,bpoint,bvec)
circle = Part.Wire(circle)
try:
bar = wire.makePipeShell([circle],True,False,2)
except Part.OCCError:
print("Arch: error sweeping rebar profile along the base sketch")
return
# building final shape
shapes = []
if obj.Amount == 1:
offset = DraftVecUtils.scaleTo(axis,size/2)
bar.translate(offset)
shapes.append(bar)
if hasattr(obj,"Spacing"):
obj.Spacing = 0
else:
if obj.OffsetStart.Value:
baseoffset = DraftVecUtils.scaleTo(axis,obj.OffsetStart.Value)
else:
baseoffset = None
interval = size - (obj.OffsetStart.Value + obj.OffsetEnd.Value)
interval = interval / (obj.Amount - 1)
vinterval = DraftVecUtils.scaleTo(axis,interval)
for i in range(obj.Amount):
if i == 0:
if baseoffset:
bar.translate(baseoffset)
shapes.append(bar)
else:
bar = bar.copy()
bar.translate(vinterval)
shapes.append(bar)
if hasattr(obj,"Spacing"):
obj.Spacing = interval
if shapes:
obj.Shape = Part.makeCompound(shapes)
obj.Placement = pl
class _ViewProviderRebar(ArchComponent.ViewProviderComponent):
"A View Provider for the Rebar object"
def __init__(self,vobj):
ArchComponent.ViewProviderComponent.__init__(self,vobj)
vobj.ShapeColor = ArchCommands.getDefaultColor("Rebar")
def getIcon(self):
import Arch_rc
return ":/icons/Arch_Rebar_Tree.svg"
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Arch_Rebar',_CommandRebar())