Create a Line with Python Scripting in FreeCAD

Python FreeCAD

Hello friends welcome to FreeCAD tutorial in this tutorial we will learn basic python scripting in FreeCAD. This tutorial is for beginner user to get familiar with python scripting in FreeCAD we will create a simple line with the help of python console. All geometric primitives and shape of part design workbench can be easily used with python console and it is very easy to write Python script using python console.

To create a line element switch to the Python console and type in:

import Part,PartGui
doc=App.newDocument()
l=Part.Line()
l.StartPoint=(0.0,0.0,0.0)
l.EndPoint=(1.0,1.0,1.0)
doc.addObject("Part::Feature","Line").Shape=l.toShape()
doc.recompute()

The above code will create a line in FreeCAD. Now we will understand each line of code.

import Part,PartGui
doc=App.newDocument()
above two line of code loads the Part module and creates a new document
l=Part.Line()
l.StartPoint=(0.0,0.0,0.0)
l.EndPoint=(1.0,1.0,1.0)

First line is a line segment, hence the start and endpoint start point is (0.0,0.0,0.0) and end point is (1.0,1.0,1.0).

doc.addObject("Part::Feature","Line").Shape=l.toShape()

above line of code adds a Part object type to the FreeCAD document and assigns the shape representation of the line segment to the ‘Shape’ property of the added object. It is important to understand here that we used a geometric primitive.

doc.recompute()

The above command use to Updates the document. This also prepares the visual representation of the new part object.

This how to create to create a line with the help of Python Console. With the help of this tutorial you can create other geometric primitives of part design workbench like line, circle, square, rectangle etc.

Code for create a circle is follow.

import Part
doc = App.activeDocument()
c = Part.Circle()
c.Radius=10.0
f = doc. addObject("Part::Feature", "Circle")
Shape = c.toShape()
doc.recompute()

“Thank you for reading! If you found this article insightful and valuable, consider sharing it with your friends and followers on social media. Your share can help others discover this content too. Let’s spread knowledge together. Your support is greatly appreciated!”

Amar Patel
About Amar Patel 145 Articles
Hi, I am Amar Patel from India. Founder, Author and Administrator of mechnexus.com. I am a Simple Average Man who Loves life and Love living life. Professionally I am a Mechanical Engineer with Solid command over CAD software like FreeCAD, SolidWorks, Autodesk Inventor and AutoCAD Mechanical. I’m here to share my knowledge to help you accomplish your design and engineering tasks quicker and easier. I am Passionate about learning new things especially about Open-Source Software. I love teaching therefore I started my YouTube Channel on FreeCAD and I believe FreeCAD have lots of potential than traditional 3D software. contact me - [email protected]