Rebar Addon for FreeCAD
LShapeRebar.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__ = "LShapeRebar"
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 getpointsOfLShapeRebar(FacePRM, l_cover, r_cover, b_cover, t_cover, orientation):
41  """ getpointsOfLShapeRebar(FacePRM, LeftCover, RightCover, BottomCover, TopCover, Orientation):
42  Return points of the LShape rebar in the form of array for sketch.
43  It takes four different orientations input i.e. 'Bottom Left', 'Bottom Right ', 'Top Left', 'Top Right'.
44  """
45  if orientation == "Bottom Left":
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  elif orientation == "Bottom Right":
53  x1 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
54  y1 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
55  x2 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
56  y2 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
57  x3 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
58  y3 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
59  elif orientation == "Top Left":
60  x1 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
61  y1 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
62  x2 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
63  y2 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
64  x3 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
65  y3 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
66  elif orientation == "Top Right":
67  x1 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
68  y1 = FacePRM[1][1] - FacePRM[0][1] / 2 + b_cover
69  x2 = FacePRM[1][0] - FacePRM[0][0] / 2 + FacePRM[0][0] - r_cover
70  y2 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
71  x3 = FacePRM[1][0] - FacePRM[0][0] / 2 + l_cover
72  y3 = FacePRM[1][1] + FacePRM[0][1] / 2 - t_cover
73  return [FreeCAD.Vector(x1, y1, 0), FreeCAD.Vector(x2, y2, 0),\
74  FreeCAD.Vector(x3, y3, 0)]
75 
77  def __init__(self, Rebar = None):
78  self.CustomSpacing = None
79  if not Rebar:
80  selected_obj = FreeCADGui.Selection.getSelectionEx()[0]
81  self.SelectedObj = selected_obj.Object
82  self.FaceName = selected_obj.SubElementNames[0]
83  else:
84  self.FaceName = Rebar.Base.Support[0][1][0]
85  self.SelectedObj = Rebar.Base.Support[0][0]
86  self.form = FreeCADGui.PySideUic.loadUi(os.path.splitext(__file__)[0] + ".ui")
87  self.form.setWindowTitle(QtGui.QApplication.translate("RebarAddon", "L-Shape Rebar", None))
88  self.form.orientation.addItems(["Bottom Right", "Bottom Left", "Top Right", "Top Left"])
89  self.form.amount_radio.clicked.connect(self.amount_radio_clicked)
90  self.form.spacing_radio.clicked.connect(self.spacing_radio_clicked)
91  self.form.customSpacing.clicked.connect(lambda: runRebarDistribution(self))
92  self.form.removeCustomSpacing.clicked.connect(lambda: removeRebarDistribution(self))
93  self.form.PickSelectedFace.clicked.connect(lambda: getSelectedFace(self))
94  self.form.orientation.currentIndexChanged.connect(self.getOrientation)
95  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/LShapeRebarBR.svg"))
96  self.form.toolButton.setIcon(self.form.toolButton.style().standardIcon(QtGui.QStyle.SP_DialogHelpButton))
97  self.form.toolButton.clicked.connect(lambda: showPopUpImageDialog(os.path.split(os.path.abspath(__file__))[0] + "/icons/LShapeRebarDetailed.svg"))
98  self.Rebar = Rebar
99 
100  def getOrientation(self):
101  orientation = self.form.orientation.currentText()
102  if orientation == "Bottom Right":
103  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/LShapeRebarBR.svg"))
104  elif orientation == "Bottom Left":
105  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/LShapeRebarBL.svg"))
106  elif orientation == "Top Right":
107  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/LShapeRebarTR.svg"))
108  else:
109  self.form.image.setPixmap(QtGui.QPixmap(os.path.split(os.path.abspath(__file__))[0] + "/icons/LShapeRebarTL.svg"))
110 
112  return int(QtGui.QDialogButtonBox.Ok) | int(QtGui.QDialogButtonBox.Apply) | int(QtGui.QDialogButtonBox.Cancel)
113 
114  def clicked(self, button):
115  if button == int(QtGui.QDialogButtonBox.Apply):
116  self.accept(button)
117 
118  def accept(self, signal = None):
119  f_cover = self.form.frontCover.text()
120  f_cover = FreeCAD.Units.Quantity(f_cover).Value
121  b_cover = self.form.bottomCover.text()
122  b_cover = FreeCAD.Units.Quantity(b_cover).Value
123  l_cover = self.form.l_sideCover.text()
124  l_cover = FreeCAD.Units.Quantity(l_cover).Value
125  r_cover = self.form.r_sideCover.text()
126  r_cover = FreeCAD.Units.Quantity(r_cover).Value
127  t_cover = self.form.topCover.text()
128  t_cover = FreeCAD.Units.Quantity(t_cover).Value
129  diameter = self.form.diameter.text()
130  diameter = FreeCAD.Units.Quantity(diameter).Value
131  rounding = self.form.rounding.value()
132  orientation = self.form.orientation.currentText()
133  amount_check = self.form.amount_radio.isChecked()
134  spacing_check = self.form.spacing_radio.isChecked()
135  if not self.Rebar:
136  if amount_check:
137  amount = self.form.amount.value()
138  rebar = makeLShapeRebar(f_cover, b_cover, l_cover, r_cover, diameter, t_cover, rounding, True, amount, orientation, self.SelectedObj, self.FaceName)
139  elif spacing_check:
140  spacing = self.form.spacing.text()
141  spacing = FreeCAD.Units.Quantity(spacing).Value
142  rebar = makeLShapeRebar(f_cover, b_cover, l_cover, r_cover, diameter, t_cover, rounding, False, spacing, orientation, self.SelectedObj, self.FaceName)
143  else:
144  if amount_check:
145  amount = self.form.amount.value()
146  rebar = editLShapeRebar(self.Rebar, f_cover, b_cover, l_cover, r_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 = editLShapeRebar(self.Rebar, f_cover, b_cover, l_cover, r_cover, diameter, t_cover, rounding, False, spacing, orientation, self.SelectedObj, self.FaceName)
151  if self.CustomSpacing:
152  rebar.CustomSpacing = self.CustomSpacing
153  FreeCAD.ActiveDocument.recompute()
154  self.Rebar = rebar
155  if signal == int(QtGui.QDialogButtonBox.Apply):
156  pass
157  else:
158  FreeCADGui.Control.closeDialog(self)
159 
161  self.form.spacing.setEnabled(False)
162  self.form.amount.setEnabled(True)
163 
165  self.form.amount.setEnabled(False)
166  self.form.spacing.setEnabled(True)
167 
168 
169 def makeLShapeRebar(f_cover, b_cover, l_cover, r_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation = "Bottom Left", structure = None, facename = None):
170  """ makeLShapeRebar(FrontCover, BottomCover, LeftCover, RightCover, Diameter, TopCover, Rounding, AmountSpacingCheck, AmountSpacingValue,
171  Orientation, Structure, Facename): Adds the L-Shape reinforcement bar to the selected structural object.
172  It takes four different orientations input i.e. 'Bottom Left', 'Bottom Right ', 'Top Left', 'Top Right'.
173  """
174  if not structure and not facename:
175  selected_obj = FreeCADGui.Selection.getSelectionEx()[0]
176  structure = selected_obj.Object
177  facename = selected_obj.SubElementNames[0]
178  face = structure.Shape.Faces[getFaceNumber(facename) - 1]
179  #StructurePRM = getTrueParametersOfStructure(structure)
180  FacePRM = getParametersOfFace(structure, facename)
181  if not FacePRM:
182  FreeCAD.Console.PrintError("Cannot identified shape or from which base object sturctural element is derived\n")
183  return
184  # Get points of L-Shape rebar
185  points = getpointsOfLShapeRebar(FacePRM, l_cover, r_cover, b_cover, t_cover, orientation)
186  import Part
187  import Arch
188  sketch = FreeCAD.activeDocument().addObject('Sketcher::SketchObject', 'Sketch')
189  sketch.MapMode = "FlatFace"
190  sketch.Support = [(structure, facename)]
191  FreeCAD.ActiveDocument.recompute()
192  sketch.addGeometry(Part.LineSegment(points[0], points[1]), False)
193  sketch.addGeometry(Part.LineSegment(points[1], points[2]), False)
194  import Sketcher
195  if amount_spacing_check:
196  rebar = Arch.makeRebar(structure, sketch, diameter, amount_spacing_value, f_cover)
197  FreeCAD.ActiveDocument.recompute()
198  else:
199  size = (ArchCommands.projectToVector(structure.Shape.copy(), face.normalAt(0, 0))).Length
200  rebar = Arch.makeRebar(structure, sketch, diameter, int((size - diameter) / amount_spacing_value), f_cover)
201  rebar.Rounding = rounding
202  # Adds properties to the rebar object
203  rebar.ViewObject.addProperty("App::PropertyString", "RebarShape", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Shape of rebar")).RebarShape = "LShapeRebar"
204  rebar.ViewObject.setEditorMode("RebarShape", 2)
205  rebar.addProperty("App::PropertyDistance", "FrontCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Front cover of rebar")).FrontCover = f_cover
206  rebar.setEditorMode("FrontCover", 2)
207  rebar.addProperty("App::PropertyDistance", "LeftCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Left Side cover of rebar")).LeftCover = l_cover
208  rebar.setEditorMode("LeftCover", 2)
209  rebar.addProperty("App::PropertyDistance", "RightCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Right Side cover of rebar")).RightCover = r_cover
210  rebar.setEditorMode("RightCover", 2)
211  rebar.addProperty("App::PropertyDistance", "BottomCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Bottom cover of rebar")).BottomCover = b_cover
212  rebar.setEditorMode("BottomCover", 2)
213  rebar.addProperty("App::PropertyBool", "AmountCheck", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Amount radio button is checked")).AmountCheck
214  rebar.setEditorMode("AmountCheck", 2)
215  rebar.addProperty("App::PropertyDistance", "TopCover", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Top cover of rebar")).TopCover = t_cover
216  rebar.setEditorMode("TopCover", 2)
217  rebar.addProperty("App::PropertyDistance", "TrueSpacing", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Spacing between of rebars")).TrueSpacing = amount_spacing_value
218  rebar.addProperty("App::PropertyString", "Orientation", "RebarDialog", QT_TRANSLATE_NOOP("App::Property", "Shape of rebar")).Orientation = orientation
219  rebar.setEditorMode("Orientation", 2)
220  rebar.setEditorMode("TrueSpacing", 2)
221  if amount_spacing_check:
222  rebar.AmountCheck = True
223  else:
224  rebar.AmountCheck = False
225  rebar.TrueSpacing = amount_spacing_value
226  rebar.Label = "LShapeRebar"
227  FreeCAD.ActiveDocument.recompute()
228  return rebar
229 
230 def editLShapeRebar(Rebar, f_cover, b_cover, l_cover, r_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation, structure = None, facename = None):
231  sketch = Rebar.Base
232  if structure and facename:
233  sketch.Support = [(structure, facename)]
234  # Check if sketch support is empty.
235  if not sketch.Support:
236  showWarning("You have checked remove external geometry of base sketchs when needed.\nTo unchecked Edit->Preferences->Arch.")
237  return
238  # Assigned values
239  facename = sketch.Support[0][1][0]
240  structure = sketch.Support[0][0]
241  face = structure.Shape.Faces[getFaceNumber(facename) - 1]
242  #StructurePRM = getTrueParametersOfStructure(structure)
243  # Get parameters of the face where sketch of rebar is drawn
244  FacePRM = getParametersOfFace(structure, facename)
245  # Get points of L-Shape rebar
246  points = getpointsOfLShapeRebar(FacePRM, l_cover, r_cover, b_cover, t_cover, orientation)
247  sketch.movePoint(0, 1, points[0], 0)
248  FreeCAD.ActiveDocument.recompute()
249  sketch.movePoint(0, 2, points[1], 0)
250  FreeCAD.ActiveDocument.recompute()
251  sketch.movePoint(1, 1, points[1], 0)
252  FreeCAD.ActiveDocument.recompute()
253  sketch.movePoint(1, 2, points[2], 0)
254  FreeCAD.ActiveDocument.recompute()
255  Rebar.OffsetStart = f_cover
256  Rebar.OffsetEnd = f_cover
257  if amount_spacing_check:
258  Rebar.Amount = amount_spacing_value
259  FreeCAD.ActiveDocument.recompute()
260  Rebar.AmountCheck = True
261  else:
262  size = (ArchCommands.projectToVector(structure.Shape.copy(), face.normalAt(0, 0))).Length
263  Rebar.Amount = int((size - diameter) / amount_spacing_value)
264  FreeCAD.ActiveDocument.recompute()
265  Rebar.AmountCheck = False
266  Rebar.Diameter = diameter
267  Rebar.FrontCover = f_cover
268  Rebar.LeftCover = l_cover
269  Rebar.RightCover = r_cover
270  Rebar.BottomCover = b_cover
271  Rebar.TopCover = t_cover
272  Rebar.Rounding = rounding
273  Rebar.TrueSpacing = amount_spacing_value
274  Rebar.Orientation = orientation
275  FreeCAD.ActiveDocument.recompute()
276  return Rebar
277 
278 def editDialog(vobj):
279  FreeCADGui.Control.closeDialog()
280  obj = _LShapeRebarTaskPanel(vobj.Object)
281  obj.form.frontCover.setText(str(vobj.Object.FrontCover))
282  obj.form.l_sideCover.setText(str(vobj.Object.LeftCover))
283  obj.form.r_sideCover.setText(str(vobj.Object.RightCover))
284  obj.form.bottomCover.setText(str(vobj.Object.BottomCover))
285  obj.form.diameter.setText(str(vobj.Object.Diameter))
286  obj.form.topCover.setText(str(vobj.Object.TopCover))
287  obj.form.rounding.setValue(vobj.Object.Rounding)
288  obj.form.orientation.setCurrentIndex(obj.form.orientation.findText(str(vobj.Object.Orientation)))
289  if vobj.Object.AmountCheck:
290  obj.form.amount.setValue(vobj.Object.Amount)
291  else:
292  obj.form.amount_radio.setChecked(False)
293  obj.form.spacing_radio.setChecked(True)
294  obj.form.amount.setDisabled(True)
295  obj.form.spacing.setEnabled(True)
296  obj.form.spacing.setText(str(vobj.Object.TrueSpacing))
297  #obj.form.PickSelectedFace.setVisible(False)
298  FreeCADGui.Control.showDialog(obj)
299 
301  selected_obj = check_selected_face()
302  if selected_obj:
303  FreeCADGui.Control.showDialog(_LShapeRebarTaskPanel())
def check_selected_face()
Definition: Rebarfunc.py:255
def accept(self, signal=None)
Definition: LShapeRebar.py:118
def editLShapeRebar(Rebar, f_cover, b_cover, l_cover, r_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation, structure=None, facename=None)
Definition: LShapeRebar.py:230
def CommandLShapeRebar()
Definition: LShapeRebar.py:300
def getSelectedFace(self)
Definition: Rebarfunc.py:278
def getFaceNumber(s)
Definition: Rebarfunc.py:72
def runRebarDistribution(self)
def __init__(self, Rebar=None)
Definition: LShapeRebar.py:77
def getpointsOfLShapeRebar(FacePRM, l_cover, r_cover, b_cover, t_cover, orientation)
Definition: LShapeRebar.py:40
def showPopUpImageDialog(img)
Definition: PopUpImage.py:43
def removeRebarDistribution(self)
def showWarning(message)
Definition: Rebarfunc.py:293
def editDialog(vobj)
Definition: LShapeRebar.py:278
def makeLShapeRebar(f_cover, b_cover, l_cover, r_cover, diameter, t_cover, rounding, amount_spacing_check, amount_spacing_value, orientation="Bottom Left", structure=None, facename=None)
Definition: LShapeRebar.py:169
def getParametersOfFace(structure, facename, sketch=True)
Definition: Rebarfunc.py:126