/
ArchRebar.py
242 lines (221 loc) · 10.8 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
#***************************************************************************
#* *
#* 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,FreeCADGui,Draft,ArchComponent,DraftVecUtils,ArchCommands
from FreeCAD import Vector
from PyQt4 import QtCore
from DraftTools import translate
__title__="FreeCAD Rebar"
__author__ = "Yorik van Havre"
__url__ = "http://www.freecadweb.org"
def makeRebar(baseobj,sketch,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)
_Rebar(obj)
_ViewProviderRebar(obj.ViewObject)
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
sketch.ViewObject.hide()
a = baseobj.Armatures
a.append(obj)
baseobj.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)
obj.ViewObject.ShapeColor = ArchCommands.getDefaultColor("Rebar")
return obj
class _CommandRebar:
"the Arch Rebar command definition"
def GetResources(self):
return {'Pixmap' : 'Arch_Rebar',
'MenuText': QtCore.QT_TRANSLATE_NOOP("Arch_Rebar","Rebar"),
'Accel': "R, B",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Arch_Rebar","Creates a Reinforcement bar from the selected face of a structural object")}
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(str(translate("Arch","Create Rebar")))
FreeCADGui.doCommand("import 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 isinstance(obj.Support,tuple):
sup = obj.Support[0]
else:
sup = obj.Support
FreeCAD.ActiveDocument.openTransaction(str(translate("Arch","Create Rebar")))
FreeCADGui.doCommand("import 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(str(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","The diameter of the bar")
obj.addProperty("App::PropertyLength","OffsetStart","Arch","The distance between the border of the beam and the fist bar (concrete cover).")
obj.addProperty("App::PropertyLength","OffsetEnd","Arch","The distance between the border of the beam and the last bar (concrete cover).")
obj.addProperty("App::PropertyInteger","Amount","Arch","The amount of bars")
obj.addProperty("App::PropertyLength","Spacing","Arch","The spacing between the bars")
obj.addProperty("App::PropertyVector","Direction","Arch","The direction to use to spread the bars. Keep (0,0,0) for automatic direction.")
obj.addProperty("App::PropertyFloat","Rounding","Arch","The fillet to apply to the angle of the base profile. This value is multiplied by the bar diameter.")
self.Type = "Component"
obj.setEditorMode("Spacing",1)
def getBaseAndAxis(self,obj):
"returns a base point and orientation axis from the base sketch"
if obj.Base:
if obj.Base.Shape:
if obj.Base.Shape.Wires:
e = obj.Base.Shape.Wires[0].Edges[0]
import DraftGeomUtils
v = DraftGeomUtils.vec(e).normalize()
return e.Vertexes[0].Point,v
return None,None
def execute(self,obj):
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:
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
import DraftGeomUtils
wire = DraftGeomUtils.filletWire(wire,radius)
bpoint, bvec = self.getBaseAndAxis(obj)
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+obj.OffsetEnd) > size:
return
# all tests ok!
pl = obj.Placement
import Part
circle = Part.makeCircle(obj.Diameter/2,bpoint,bvec)
circle = Part.Wire(circle)
try:
bar = wire.makePipeShell([circle],1,0,2)
except:
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:
baseoffset = DraftVecUtils.scaleTo(axis,obj.OffsetStart)
else:
baseoffset = None
interval = size - (obj.OffsetStart + obj.OffsetEnd)
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)
def getIcon(self):
import Arch_rc
return ":/icons/Arch_Rebar_Tree.svg"
FreeCADGui.addCommand('Arch_Rebar',_CommandRebar())