first attempt to write FreeCAD code (for Arch module)

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
lambda
Posts: 4
Joined: Sat Feb 25, 2017 3:10 pm

first attempt to write FreeCAD code (for Arch module)

Postby lambda » Sat Mar 18, 2017 7:26 pm

Hi,

not sure if this belongs here or into the Arch-subforum. Since my questions isn't really Arch related, I try here.

I'm playing with the following trivial patch, which allows me to create ladder style staircases often built from wood:

Code: Select all

diff --git a/src/Mod/Arch/ArchStairs.py b/src/Mod/Arch/ArchStairs.py
index 15f2196..30a831d 100644
--- a/src/Mod/Arch/ArchStairs.py
+++ b/src/Mod/Arch/ArchStairs.py
@@ -128,6 +128,7 @@ class _Stairs(ArchComponent.Component):
         obj.addProperty("App::PropertyLength","StructureThickness","Structure",QT_TRANSLATE_NOOP("App::Property","The thickness of the massive structure or of the stringers"))
         obj.addProperty("App::PropertyLength","StringerWidth","Structure",QT_TRANSLATE_NOOP("App::Property","The width of the stringers"))
         obj.addProperty("App::PropertyLength","StructureOffset","Structure",QT_TRANSLATE_NOOP("App::Property","The offset between the border of the stairs and the structure"))
+        obj.addProperty("App::PropertyLength","StringerOverlap","Structure",QT_TRANSLATE_NOOP("App::Property","The overlap of the stringers above the bottom of the treads"))
 
         obj.Align = ['Left','Right','Center']
         obj.Landings = ["None","At center","At each corner"]
@@ -417,7 +418,7 @@ class _Stairs(ArchComponent.Component):
             if obj.StringerWidth.Value and obj.StructureThickness.Value:
                 hyp = math.sqrt(vHeight.Length**2 + vLength.Length**2)
                 l1 = Vector(vLength).multiply(numberofsteps-1)
-                h1 = Vector(vHeight).multiply(numberofsteps-1).add(Vector(0,0,-abs(obj.TreadThickness.Value)))
+                h1 = Vector(vHeight).multiply(numberofsteps-1).add(Vector(0,0,-abs(obj.TreadThickness.Value)+obj.StringerOverlap.Value))
                 p1 = vBase.add(l1).add(h1)
                 p1 = self.align(p1,obj.Align,vWidth)
                 lProfile.append(p1)


This mostly works, but when loading an old file with a staircase into my patched version of FreeCAD, I get an exception because the StringerOverlap property is missing. What is the FreeCAD way to ensure that objects are extended with new properties in such situations?
User avatar
yorik
Site Admin
Posts: 8556
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: first attempt to write FreeCAD code (for Arch module)

Postby yorik » Mon Mar 20, 2017 11:57 pm

Hi,
basically you must check that the property exists, everywhere you are using it. Unfortunately there is not much other way. You can do that like this:

Code: Select all

if hasattr(myObj,"MyProperty"):

or

Code: Select all

if "MyProperty" in myObj.PropertiesList:
User avatar
yorik
Site Admin
Posts: 8556
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: first attempt to write FreeCAD code (for Arch module)

Postby yorik » Mon Mar 20, 2017 11:58 pm

But indeed one day we should think of an uniform way to upgrade missing properties in python objects...
User avatar
Kunda1
Posts: 631
Joined: Thu Jan 05, 2017 9:03 pm

Re: first attempt to write FreeCAD code (for Arch module)

Postby Kunda1 » Tue Mar 21, 2017 1:08 am

yorik wrote:But indeed one day we should think of an uniform way to upgrade missing properties in python objects...

Created issue #2977 to keep track of this :)
lambda
Posts: 4
Joined: Sat Feb 25, 2017 3:10 pm

Re: first attempt to write FreeCAD code (for Arch module)

Postby lambda » Tue Mar 21, 2017 10:08 am

Thanks for the answers. I will upgrade my code as suggested and open a PR.

I note there are two possible semantics for StringerOverlap - vertical as I chose or normal to the stringer. Vertical seems more natural to me, because it then relates to the thickness of the treads easily, but let me know if you prefer the other.

I will ask my local python guru, how the problem might be solved in a general way.