Rebar Addon for FreeCAD
UShapeRebar.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # ***************************************************************************
3 # * *
4 # * Copyright (c) 2017 - Amritpal Singh <amrit3701@gmail.com> *
5 # * *
6 # * This program is free software; you can redistribute it and/or modify *
7 # * it under the terms of the GNU Lesser General Public License (LGPL) *
8 # * as published by the Free Software Foundation; either version 2 of *
9 # * the License, or (at your option) any later version. *
10 # * for detail see the LICENCE text file. *
11 # * *
12 # * This program is distributed in the hope that it will be useful, *
13 # * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 # * GNU Library General Public License for more details. *
16 # * *
17 # * You should have received a copy of the GNU Library General Public *
18 # * License along with this program; if not, write to the Free Software *
19 # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
20 # * USA *
21 # * *
22 # ***************************************************************************
23 
24 __title__ = "UShapeRebar"
25 __author__ = "Amritpal Singh"
26 __url__ = "https://www.freecadweb.org"
27 
28 from PySide import QtCore, QtGui
29 from Rebarfunc import *
30 from PySide.QtCore import QT_TRANSLATE_NOOP
31 from RebarDistribution import runRebarDistribution, removeRebarDistribution
32 from PopUpImage import showPopUpImageDialog
33 import FreeCAD
34 import FreeCADGui
35 import ArchCommands
36 import os
37 import sys
38 import math
39 
40 def getpointsOfUShapeRebar(FacePRM, r_cover, l_cover, b_cover, t_cover, orientation):
41  """ getpointsOfUShapeRebar(FacePRM, RightCover, LeftCover, BottomCover, TopCover, Orientation):
42  Return points of the UShape rebar in the form of array for sketch.
43  It takes four different orientations input i.e. 'Bottom', 'Top', 'Left', 'Right'.
44  """
45  if orientation == "Bottom":
46  x1 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
47  y1 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
48  x2 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
49  y2 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
50  x3 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
51  y3 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
52  x4 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
53  y4 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
54  elif orientation == "Top":
55  x1 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
56  y1 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
57  x2 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
58  y2 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
59  x3 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
60  y3 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
61  x4 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
62  y4 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
63  elif orientation == "Left":
64  x1 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
65  y1 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
66  x2 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
67  y2 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
68  x3 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
69  y3 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
70  x4 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
71  y4 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
72  elif orientation == "Right":
73  x1 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
74  y1 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
75  x2 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
76  y2 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
77  x3 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
78  y3 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
79  x4 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
80  y4 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
81  return [FreeCAD.Vector(x1, y1, 0), FreeCAD.Vector(x2, y2, 0),\
82  FreeCAD.Vector(x3, y3, 0), FreeCAD.Vector(x4, y4, 0)]
83 
85  def __init__(self, Rebar = None):
86  self.CustomSpacing = None
87  if not Rebar:
88  selected_obj = FreeCADGui.Selection.getSelectionEx()[0]
89  self.SelectedObj = selected_obj.Object
90  self.FaceName = selected_obj.SubElementNames[0]
91  else:
92  self.FaceName = Rebar.Base.Support[0][1][0]
93  self.SelectedObj = Rebar.Base.Support[0][0]
94  self.form = FreeCADGui.PySideUic.loadUi(os.path.splitext(__file__)[0] + ".ui")
95  self.form.setWindowTitle(QtGui.QApplication.translate("RebarAddon", "U-Shape Rebar", None))
96  self.form.orientation.addItems(["Bottom", "Top", "Right", "Left"])
97  self.form.amount_radio.clicked.connect(self.amount_radio_clicked)
98  self.form.spacing_radio.clicked.connect(self.spacing_radio_clicked)
99  self.form.customSpacing.clicked.connect(lambda: runRebarDistribution(self))
100  self.form.removeCustomSpacing.clicked.connect(lambda: removeRebarDistribution(self))
101  self.form.PickSelectedFace.clicked.connect(lambda: getSelectedFace(self))
102  self.form.orientation.currentIndexChanged.connect(self.getOrientation)
103  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/UShapeRebarBottom.svg"))
104  self.form.toolButton.setIcon(self.form.toolButton.style().standardIcon(QtGui.QStyle.SP_DialogHelpButton))
105  self.form.toolButton.clicked.connect(lambda: showPopUpImageDialog(os.path.split(os.path.abspath(__file__))[0] + "/icons/UShapeRebarDetailed.svg"))
106  self.Rebar = Rebar
107 
108  def getOrientation(self):
109  orientation = self.form.orientation.currentText()
110  if orientation == "Bottom":
111  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/UShapeRebarBottom.svg"))
112  elif orientation == "Top":
113  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/UShapeRebarTop.svg"))
114  elif orientation == "Right":
115  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/UShapeRebarRight.svg"))
116  else:
117  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/UShapeRebarLeft.svg"))
118 
120  return int(QtGui.QDialogButtonBox.Ok) | int(QtGui.QDialogButtonBox.Apply) | int(QtGui.QDialogButtonBox.Cancel)
121 
122  def clicked(self, button):
123  if button == int(QtGui.QDialogButtonBox.Apply):
124  self.accept(button)
125 
126  def accept(self, signal = None):
127  f_cover = self.form.frontCover.text()
128  f_cover = FreeCAD.Units.Quantity(f_cover).Value
129  b_cover = self.form.bottomCover.text()
130  b_cover = FreeCAD.Units.Quantity(b_cover).Value
131  r_cover = self.form.r_sideCover.text()
132  r_cover = FreeCAD.Units.Quantity(r_cover).Value
133  l_cover = self.form.l_sideCover.text()
134  l_cover = FreeCAD.Units.Quantity(l_cover).Value
135  t_cover = self.form.topCover.text()
136  t_cover = FreeCAD.Units.Quantity(t_cover).Value
137  diameter = self.form.diameter.text()
138  diameter = FreeCAD.Units.Quantity(diameter).Value
139  rounding = self.form.rounding.value()
140  orientation = self.form.orientation.currentText()
141  amount_check = self.form.amount_radio.isChecked()
142  spacing_check = self.form.spacing_radio.isChecked()
143  if not self.Rebar:
144  if amount_check:
145  amount = self.form.amount.value()
146  rebar = makeUShapeRebar(f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, True, amount, orientation, self.SelectedObj, self.FaceName)
147  elif spacing_check:
148  spacing = self.form.spacing.text()
149  spacing = FreeCAD.Units.Quantity(spacing).Value
150  rebar = makeUShapeRebar(f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, False, spacing, orientation, self.SelectedObj, self.FaceName)
151  else:
152  if amount_check:
153  amount = self.form.amount.value()
154  rebar = editUShapeRebar(self.Rebar, f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, True, amount, orientation, self.SelectedObj, self.FaceName)
155  elif spacing_check:
156  spacing = self.form.spacing.text()
157  spacing = FreeCAD.Units.Quantity(spacing).Value
158  rebar = editUShapeRebar(self.Rebar, f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, False, spacing, orientation, self.SelectedObj, self.FaceName)
159  if self.CustomSpacing:
160  rebar.CustomSpacing = self.CustomSpacing
161  FreeCAD.ActiveDocument.recompute()
162  self.Rebar = rebar
163  if signal == int(QtGui.QDialogButtonBox.Apply):
164  pass
165  else:
166  FreeCADGui.Control.closeDialog(self)
167 
169  self.form.spacing.setEnabled(False)
170  self.form.amount.setEnabled(True)
171 
173  self.form.amount.setEnabled(False)
174  self.form.spacing.setEnabled(True)
175 
176 
177 def makeUShapeRebar(f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation = "Bottom", structure = None, facename = None):
178  """ makeUShapeRebar(FrontCover, BottomCover, RightCover, LeftCover, Diameter, Topcover, Rounding, AmountSpacingCheck, AmountSpacingValue,
179  Orientation, Structure, Facename): Adds the U-Shape reinforcement bar to the selected structural object.
180  It takes four different types of orientations as input i.e 'Bottom', 'Top', 'Right', 'Left'.
181  """
182  if not structure and not facename:
183  selected_obj = FreeCADGui.Selection.getSelectionEx()[0]
184  structure = selected_obj.Object
185  facename = selected_obj.SubElementNames[0]
186  face = structure.Shape.Faces[getFaceNumber(facename) - 1]
187  #StructurePRM = getTrueParametersOfStructure(structure)
188  FacePRM = getParametersOfFace(structure, facename)
189  if not FacePRM:
190  FreeCAD.Console.PrintError("Cannot identified shape or from which base object sturctural element is derived\n")
191  return
192  # Get points of U-Shape rebar
193  points = getpointsOfUShapeRebar(FacePRM, r_cover, l_cover, b_cover, t_cover, orientation)
194  import Part
195  import Arch
196  sketch = FreeCAD.activeDocument().addObject('Sketcher::SketchObject', 'Sketch')
197  sketch.MapMode = "FlatFace"
198  sketch.Support = [(structure, facename)]
199  FreeCAD.ActiveDocument.recompute()
200  sketch.addGeometry(Part.LineSegment(points[0], points[1]), False)
201  sketch.addGeometry(Part.LineSegment(points[1], points[2]), False)
202  import Sketcher
203  sketch.addGeometry(Part.LineSegment(points[2], points[3]), False)
204  if amount_spacing_check:
205  rebar = Arch.makeRebar(structure, sketch, diameter, amount_spacing_value, f_cover)
206  FreeCAD.ActiveDocument.recompute()
207  else:
208  size = (ArchCommands.projectToVector(structure.Shape.copy(), face.normalAt(0, 0))).Length
209  rebar = Arch.makeRebar(structure, sketch, diameter, int((size - diameter) / amount_spacing_value), f_cover)
210  rebar.Rounding = rounding
211  # Adds properties to the rebar object
212  rebar.ViewObject.addProperty("App::PropertyString", "RebarShape", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Shape of rebar")).RebarShape = "UShapeRebar"
213  rebar.ViewObject.setEditorMode("RebarShape", 2)
214  rebar.addProperty("App::PropertyDistance", "FrontCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Front cover of rebar")).FrontCover = f_cover
215  rebar.setEditorMode("FrontCover", 2)
216  rebar.addProperty("App::PropertyDistance", "RightCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Right Side cover of rebar")).RightCover = r_cover
217  rebar.setEditorMode("RightCover", 2)
218  rebar.addProperty("App::PropertyDistance", "LeftCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Left Side cover of rebar")).LeftCover = l_cover
219  rebar.setEditorMode("LeftCover", 2)
220  rebar.addProperty("App::PropertyDistance", "BottomCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Bottom cover of rebar")).BottomCover = b_cover
221  rebar.setEditorMode("BottomCover", 2)
222  rebar.addProperty("App::PropertyBool", "AmountCheck", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Amount radio button is checked")).AmountCheck
223  rebar.setEditorMode("AmountCheck", 2)
224  rebar.addProperty("App::PropertyDistance", "TopCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Top cover of rebar")).TopCover = t_cover
225  rebar.setEditorMode("TopCover", 2)
226  rebar.addProperty("App::PropertyDistance", "TrueSpacing", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Spacing between of rebars")).TrueSpacing = amount_spacing_value
227  rebar.setEditorMode("TrueSpacing", 2)
228  rebar.addProperty("App::PropertyString", "Orientation", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Shape of rebar")).Orientation = orientation
229  rebar.setEditorMode("Orientation", 2)
230  if amount_spacing_check:
231  rebar.AmountCheck = True
232  else:
233  rebar.AmountCheck = False
234  rebar.TrueSpacing = amount_spacing_value
235  rebar.Label = "UShapeRebar"
236  FreeCAD.ActiveDocument.recompute()
237  return rebar
238 
239 def editUShapeRebar(Rebar, f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation, structure = None, facename = None):
240  sketch = Rebar.Base
241  if structure and facename:
242  sketch.Support = [(structure, facename)]
243  # Check if sketch support is empty.
244  if not sketch.Support:
245  showWarning("You have checked remove external geometry of base sketchs when needed.\nTo unchecked Edit->Preferences->Arch.")
246  return
247  # Assigned values
248  facename = sketch.Support[0][1][0]
249  structure = sketch.Support[0][0]
250  face = structure.Shape.Faces[getFaceNumber(facename) - 1]
251  #StructurePRM = getTrueParametersOfStructure(structure)
252  # Get parameters of the face where sketch of rebar is drawn
253  FacePRM = getParametersOfFace(structure, facename)
254  # Get points of U-Shape rebar
255  points = getpointsOfUShapeRebar(FacePRM, r_cover, l_cover, b_cover, t_cover, orientation)
256  sketch.movePoint(0, 1, points[0], 0)
257  FreeCAD.ActiveDocument.recompute()
258  sketch.movePoint(0, 2, points[1], 0)
259  FreeCAD.ActiveDocument.recompute()
260  sketch.movePoint(1, 1, points[1], 0)
261  FreeCAD.ActiveDocument.recompute()
262  sketch.movePoint(1, 2, points[2], 0)
263  FreeCAD.ActiveDocument.recompute()
264  sketch.movePoint(2, 1, points[2], 0)
265  FreeCAD.ActiveDocument.recompute()
266  sketch.movePoint(2, 2, points[3], 0)
267  FreeCAD.ActiveDocument.recompute()
268  Rebar.OffsetStart = f_cover
269  Rebar.OffsetEnd = f_cover
270  if amount_spacing_check:
271  Rebar.Amount = amount_spacing_value
272  FreeCAD.ActiveDocument.recompute()
273  Rebar.AmountCheck = True
274  else:
275  size = (ArchCommands.projectToVector(structure.Shape.copy(), face.normalAt(0, 0))).Length
276  Rebar.Amount = int((size - diameter) / amount_spacing_value)
277  FreeCAD.ActiveDocument.recompute()
278  Rebar.AmountCheck = False
279  Rebar.Diameter = diameter
280  Rebar.FrontCover = f_cover
281  Rebar.RightCover = r_cover
282  Rebar.LeftCover = l_cover
283  Rebar.BottomCover = b_cover
284  Rebar.TopCover = t_cover
285  Rebar.Rounding = rounding
286  Rebar.TrueSpacing = amount_spacing_value
287  Rebar.Orientation = orientation
288  FreeCAD.ActiveDocument.recompute()
289  return Rebar
290 
291 def editDialog(vobj):
292  FreeCADGui.Control.closeDialog()
293  obj = _UShapeRebarTaskPanel(vobj.Object)
294  obj.form.frontCover.setText(str(vobj.Object.FrontCover))
295  obj.form.r_sideCover.setText(str(vobj.Object.RightCover))
296  obj.form.l_sideCover.setText(str(vobj.Object.LeftCover))
297  obj.form.bottomCover.setText(str(vobj.Object.BottomCover))
298  obj.form.diameter.setText(str(vobj.Object.Diameter))
299  obj.form.topCover.setText(str(vobj.Object.TopCover))
300  obj.form.rounding.setValue(vobj.Object.Rounding)
301  obj.form.orientation.setCurrentIndex(obj.form.orientation.findText(str(vobj.Object.Orientation)))
302  if vobj.Object.AmountCheck:
303  obj.form.amount.setValue(vobj.Object.Amount)
304  else:
305  obj.form.amount_radio.setChecked(False)
306  obj.form.spacing_radio.setChecked(True)
307  obj.form.amount.setDisabled(True)
308  obj.form.spacing.setEnabled(True)
309  obj.form.spacing.setText(str(vobj.Object.TrueSpacing))
310  #obj.form.PickSelectedFace.setVisible(False)
311  FreeCADGui.Control.showDialog(obj)
312 
314  selected_obj = check_selected_face()
315  if selected_obj:
316  FreeCADGui.Control.showDialog(_UShapeRebarTaskPanel())
def check_selected_face()
Definition: Rebarfunc.py:255
def __init__(self, Rebar=None)
Definition: UShapeRebar.py:85
def makeUShapeRebar(f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation="Bottom", structure=None, facename=None)
Definition: UShapeRebar.py:177
def getSelectedFace(self)
Definition: Rebarfunc.py:278
def getFaceNumber(s)
Definition: Rebarfunc.py:72
def runRebarDistribution(self)
def getpointsOfUShapeRebar(FacePRM, r_cover, l_cover, b_cover, t_cover, orientation)
Definition: UShapeRebar.py:40
def accept(self, signal=None)
Definition: UShapeRebar.py:126
def showPopUpImageDialog(img)
Definition: PopUpImage.py:43
def removeRebarDistribution(self)
def showWarning(message)
Definition: Rebarfunc.py:293
def CommandUShapeRebar()
Definition: UShapeRebar.py:313
def editUShapeRebar(Rebar, f_cover, b_cover, r_cover, l_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation, structure=None, facename=None)
Definition: UShapeRebar.py:239
def getParametersOfFace(structure, facename, sketch=True)
Definition: Rebarfunc.py:126
def editDialog(vobj)
Definition: UShapeRebar.py:291