Post : Python for FreeCAD
URL :
https://cnirbhay.wordpress.com/2018/12/22/python-for-freecad/
Posted : December 22, 2018 at 8:53 pm
Author : Nirbhay Chauhan
Categories : 3D Modeling in FreeCAD, BIM, BIM Modeling in FreeCAD, Parametric Modeling, Python
Disclaimer: All views expressed in this post are my own and not the views of BuroHappold. (For Educational purposes only)
https://cnirbhay.files.wordpress.com/2018/12/mainWindow.jpg?w=840
Hi Everyone. How are you all doing! Hope you all are great.
It has been a year that the blog has been inactive. Especially, I've not posted anything regarding my favourite BIM tool FreeCAD. Though, it has been very tough to spare some personal time but still I tried this weekend to spend some time experimenting on FreeCAD's Python interface and it was a great feeling. Thanks to my friend Kirandeep Singh (
https://www.linkedin.com/in/kiranbirdi/ ) for accompanying me for this task.
In my opinion, so far what I've experienced, FreeCAD has got the most friendly ever (and the most powerful too) Python console that even WRITES THE WHOLE CODE FOR YOU. Yes, you heard that right. Its macro recorder gives you all the commands that you've executed so far to build your objects through GUI. Then by some slight manual interpretations, the code can be refined as per the requirement and reused whenever needed as a separate portable program.
In the below Python code mentioned, we tried to parametrically model a sample house.
#importing some important modules
import FreeCAD
import DraftTools
import Draft
import ArchWall
import Arch
import ArchStructure
#creating parameters
lengthOfWall = 5000
widthOfWall = 3000
heightOfWall = 3000
thicknessOfSlab = 200
offset1 = 1000
offset2 = 1500
#creating walls
pl = FreeCAD.Placement()
pl.Rotation.Q = (0.0,0.0,0.0,1.0)
pl.Base = FreeCAD.Vector(0.0,0.0,0.0)
rec = Draft.makeRectangle(lengthOfWall,widthOfWall,pl,False,None)
Draft.autogroup(rec)
obj = Arch.makeWall(FreeCAD.ActiveDocument.Rectangle)
App.ActiveDocument.Wall.Align = 'Right'
Draft.autogroup(obj)
#creating floor slab
rec = Draft.makeRectangle(lengthOfWall + offset1,widthOfWall + offset1,placement=pl,face=False,support=None)
Draft.autogroup(rec)
Draft.move([FreeCAD.ActiveDocument.Rectangle001],FreeCAD.Vector(-offset1/2,-offset1/2.0,0.0),copy=False)
obj = Arch.makeStructure(FreeCAD.ActiveDocument.Rectangle001)
Draft.autogroup(obj)
App.ActiveDocument.Structure.Height = thicknessOfSlab
App.ActiveDocument.Structure.Normal.z = -1
#creating roof slab
pl = FreeCAD.Placement()
pl.Rotation.Q = (0.0,0.0,0,1.0)
pl.Base = FreeCAD.Vector(0,0,heightOfWall)
rec = Draft.makeRectangle(lengthOfWall + offset2,widthOfWall + offset2,placement=pl,face=False,support=FreeCAD.ActiveDocument.getObject("Wall"))
Draft.autogroup(rec)
Draft.move([FreeCAD.ActiveDocument.Rectangle002],FreeCAD.Vector(-offset2/2,-offset2/2.0,0.0),copy=False)
obj = Arch.makeStructure(FreeCAD.ActiveDocument.Rectangle002)
Draft.autogroup(obj)
App.ActiveDocument.Structure001.Height = thicknessOfSlab
App.ActiveDocument.Structure001.Normal.z = 1
App.ActiveDocument.recompute()
To test the above code, simply copy it and paste in the Python console of FreeCAD and hit enter. Another way, you can also copy the code in Macro editor and create your own custom executable macro with your custom parameters.
The above code is also available on GitHub (
https://github.com/cnirbhay/FreeCAD-Python-Codes.git ) .
Thank you and have a nice day!
Add a comment to this post:
https://cnirbhay.wordpress.com/2018/12/22/python-for-freecad/#respond
--
H. S. Rai
:
What is business:
http://www.youtube.com/watch?v=D1uob7Rh0EE
: