Freecad Python Scripting
FreeCAD
scripting
object manipulation
Python
circles
bounds
dimensions
This article provides an explanation of FreeCAD scripting, specifically focusing on how to manipulate objects and create circles within specified bounds using Python. The article includes examples of defining dimensions and the number of circles needed, as well as calculating side lengths and margins to generate squares with given parameters.
Reference:
https://wiki.freecad.org/FreeCAD_Scripting_Basics
import Part
= FreeCAD.ActiveDocument
doc # list all objects
= doc.Objects
all_objects # list all names
= [it.Name for it in all_objects]
all_object_names # get object by name
= doc.getObject("myObjectName")
obj # get vertex point
= obj.Shape.Vertexes[0].Point
vertex_point # create new line
= Part.makeLine((-200, -200, 0), (200, 200, 0))
new_line # insert the line
Part.show(new_line)# recompute the document
doc.recompute()
Draw squares within specific bounds:
= 15
margin # Define the dimensions of the square area
= -210.134 + margin
x_min = -84.134 - margin
x_max = -140.0997 + margin
y_min = -14.0997 - margin
y_max = 0
z # Define the number of squares in each row and column
= 10
num_squares # Calculate the side length of each square
= (x_max - x_min) / num_squares
x_length = (y_max - y_min) / num_squares
y_length = 0.17
margin_portion = 1 - 2 * margin_portion
hole_portion # Create the squares
for i in range(num_squares):
for j in range(num_squares):
= x_min + i * x_length + x_length * margin_portion
x_start = y_min + j * y_length + y_length * margin_portion
y_start = [
square_points
(x_start, y_start, z),+ x_length * hole_portion, y_start, z),
(x_start + x_length * hole_portion, y_start + y_length * hole_portion, z),
(x_start + y_length * hole_portion, z),
(x_start, y_start # to make it closed
(x_start, y_start, z),
]= Part.makePolygon(square_points)
square Part.show(square)
Draw circles with specific bounds:
= 15
margin # Define the dimensions of the square area
= -210.134 + margin
x_min = -84.134 - margin
x_max = -140.0997 + margin
y_min = -14.0997 - margin
y_max = 0
z # Define the number of circles in each row and column
= 10
num_circles # Calculate the side length of each circle
= (x_max - x_min) / num_circles
x_length = (y_max - y_min) / num_circles
y_length = 0.17
margin_portion = 0.5 - margin_portion
radius_portion = x_length * radius_portion
radius = App.Vector(0, 0, 1)
direction # Create the squares
for i in range(num_circles):
for j in range(num_circles):
= x_min + i * x_length + x_length * 0.5
x_center = y_min + j * y_length + y_length * 0.5
y_center = Part.makeCircle(radius, App.Vector(x_center, y_center, 0), direction)
circle Part.show(circle)