Difference between revisions of "Part scripting"

From FreeCAD Documentation
Jump to navigation Jump to search
(Marked this version for translation)
 
(32 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
<languages/>
 
<languages/>
 
<translate>
 
<translate>
 +
 +
<!--T:78-->
 +
{{Docnav
 +
|[[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]]
 +
|[[Topological_data_scripting|Topological data scripting]]
 +
}}
  
 
</translate>
 
</translate>
Line 9: Line 15:
  
 
<!--T:19-->
 
<!--T:19-->
The main data structure used in the Part module is the [http://en.wikipedia.org/wiki/Boundary_representation BRep] data type from OpenCascade.
+
The main data structure used in the Part module is the [http://en.wikipedia.org/wiki/Boundary_representation BRep] data type from [[OpenCASCADE|OpenCASCADE]]. Almost all contents and object types of the Part module are available in [[Python|Python]] scripting. This includes geometric primitives, such as Lines, Circles and Arcs, and the whole range of TopoShapes, like Vertexes, Edges, Wires, Faces, Solids and Compounds. For each of those objects, several creation methods exist, and for some of them, especially the TopoShapes, advanced operations like boolean union/difference/intersection are also available. Explore the contents of the Part module, as described in the [[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]] page, to know more.
Almost all contents and object types of the Part module are available by [[Python|Python]] scripting. This includes geometric primitives, such as Line and Circle (or Arc), and the whole range of TopoShapes, like Vertexes, Edges, Wires, Faces, Solids and Compounds. For each of those objects, several creation methods exist, and for some of them, especially the TopoShapes, advanced operations like boolean union/difference/intersection are also available. Explore the contents of the Part module, as described in the [[FreeCAD Scripting Basics|FreeCAD Scripting Basics]] page, to know more.
 
  
 
<!--T:63-->
 
<!--T:63-->
The most basic object that can be created is a [[Part Feature|Part Feature]], which has a simple {{PropertyData|Placement}} property, and basic properties to define its color and appearance.  
+
The most basic object that can be created is a [[Part_Feature|Part Feature]], which has a simple {{PropertyData|Placement}} property, and basic properties to define its color and appearance.  
  
 
<!--T:64-->
 
<!--T:64-->
Another simple object used in 2D geometrical objects is [[Part Part2DObject|Part Part2DObject]], which is the base of [[Sketcher_SketchObject|Sketcher SketchObject]] ([[Sketcher Workbench|Sketcher]]), and most [[Draft Workbench|Draft elements]].
+
Another simple object used in 2D geometrical objects is the [[Part_Part2DObject|Part Part2DObject]], which is the base of the [[Sketcher_SketchObject|Sketcher SketchObject]] and most [[Draft_Workbench|Draft elements]].
  
 
===See also=== <!--T:74-->
 
===See also=== <!--T:74-->
Line 38: Line 43:
 
<!--T:71-->
 
<!--T:71-->
 
This script is located in the installation directory of the program, and can be examined to see how the basic primitives are built.
 
This script is located in the installation directory of the program, and can be examined to see how the basic primitives are built.
 +
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
Line 45: Line 51:
  
 
==Examples== <!--T:20-->
 
==Examples== <!--T:20-->
 +
 +
===Line=== <!--T:80-->
  
 
<!--T:21-->
 
<!--T:21-->
To create a line element switch to the Python console and type in:
+
To create a line element switch to the [[Python_console|Python console]] and enter:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
import Part,PartGui
+
import FreeCAD as App
doc=App.newDocument()
+
import Part
l=Part.LineSegment()
+
 
l.StartPoint=(0.0,0.0,0.0)
+
doc = App.newDocument()
l.EndPoint=(1.0,1.0,1.0)
+
 
doc.addObject("Part::Feature","Line").Shape=l.toShape()  
+
line = Part.LineSegment()
 +
line.StartPoint = (0.0, 0.0, 0.0)
 +
line.EndPoint = (1.0, 1.0, 1.0)
 +
obj = doc.addObject("Part::Feature", "Line")
 +
obj.Shape= line.toShape()
 +
 
 
doc.recompute()
 
doc.recompute()
 
}}
 
}}
Line 62: Line 75:
  
 
<!--T:22-->
 
<!--T:22-->
Let's go through the above python example step by step:
+
Let's go through the above Python example step by step:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
import Part,PartGui
+
import FreeCAD as App
doc=App.newDocument()
+
import Part
 +
doc = App.newDocument()
 
}}
 
}}
 
<translate>
 
<translate>
  
 
<!--T:23-->
 
<!--T:23-->
loads the Part module and creates a new document
+
This loads the FreeCAD and Part modules and creates a new document.
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
l=Part.LineSegment()
+
line = Part.LineSegment()
l.StartPoint=(0.0,0.0,0.0)
+
line.StartPoint = (0.0, 0.0, 0.0)
l.EndPoint=(1.0,1.0,1.0)
+
line.EndPoint = (1.0, 1.0, 1.0)
 
}}
 
}}
 
<translate>
 
<translate>
Line 87: Line 101:
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
doc.addObject("Part::Feature","Line").Shape=l.toShape()
+
obj = doc.addObject("Part::Feature", "Line")
 +
obj.Shape= line.toShape()
 
}}
 
}}
 
<translate>
 
<translate>
  
 
<!--T:25-->
 
<!--T:25-->
This adds a Part object type to the 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 (the Part.LineSegment) to create a TopoShape out of it (the toShape() method). Only Shapes can be added to the document. In FreeCAD, geometry primitives are used as "building structures" for Shapes.
+
This adds a Part object type to the document and assigns the shape representation of the line segment to the {{Incode|Shape}} property of the added object. It is important to understand here that we use a geometric primitive (the {{Incode|Part.LineSegment}}) to create a TopoShape out of it (with the {{Incode|toShape()}} method). Only shapes can be added to the document. In FreeCAD geometric primitives are used as "building structures" for shapes.
  
 
</translate>
 
</translate>
Line 101: Line 116:
  
 
<!--T:26-->
 
<!--T:26-->
Updates the document. This also prepares the visual representation of the new part object.
+
Updates the document. This also prepares the visual representation of the new Part object.
  
 
<!--T:27-->
 
<!--T:27-->
Note that a Line Segment can be created by specifying its start and endpoint directly in the constructor, for example Part.LineSegment(point1,point2), or we can create a default line and set its properties afterwards, as we did here.
+
Note that a line segment can also be created by specifying its start and endpoint directly in the constructor, for example {{Incode|Part.LineSegment(point1, point2)}}, or we can create a default line and set its properties afterwards, as we did here.
  
 
<!--T:72-->
 
<!--T:72-->
A Line can be created also using:
+
A Line can also be created using:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
import FreeCAD
+
import FreeCAD as App
 
import Part
 
import Part
DOC = FreeCAD.newDocument()
 
  
def mycreateLine(pt1, pt2, objName):
+
def my_create_line(pt1, pt2, obj_name):
     obj = DOC.addObject("Part::Line", objName)
+
     obj = App.ActiveDocument.addObject("Part::Line", obj_name)
 
     obj.X1 = pt1[0]
 
     obj.X1 = pt1[0]
     obj.Y1 = pt1[2]
+
     obj.Y1 = pt1[1]
 
     obj.Z1 = pt1[2]
 
     obj.Z1 = pt1[2]
  
Line 125: Line 139:
 
     obj.Z2 = pt2[2]
 
     obj.Z2 = pt2[2]
  
     DOC.recompute()
+
     App.ActiveDocument.recompute()
 
     return obj
 
     return obj
  
line = mycreateLine((0,0,0), (0,10,0), "LineName")
+
line = my_create_line((0, 0, 0), (0, 10, 0), "LineName")
 
}}
 
}}
 
<translate>
 
<translate>
 +
 +
===Circle=== <!--T:81-->
  
 
<!--T:28-->
 
<!--T:28-->
Line 137: Line 153:
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 +
import FreeCAD as App
 
import Part
 
import Part
 +
 
doc = App.activeDocument()
 
doc = App.activeDocument()
c = Part.Circle()  
+
 
c.Radius=10.0   
+
circle = Part.Circle()  
f = doc.addObject("Part::Feature", "Circle")
+
circle.Radius = 10.0   
f.Shape = c.toShape()
+
obj = doc.addObject("Part::Feature", "Circle")
 +
obj.Shape = circle.toShape()
 +
 
 
doc.recompute()
 
doc.recompute()
 
}}
 
}}
Line 148: Line 168:
  
 
<!--T:73-->
 
<!--T:73-->
or using:
+
Or using:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
import FreeCAD
+
import FreeCAD as App
 
import Part
 
import Part
DOC = FreeCAD.newDocument()
 
  
def mycreateCircle(rad, objName):
+
def my_create_circle(rad, obj_name):
     obj = DOC.addObject("Part::Circle", objName)
+
     obj = App.ActiveDocument.addObject("Part::Circle", obj_name)
 
     obj.Radius = rad
 
     obj.Radius = rad
  
     DOC.recompute()
+
     App.ActiveDocument.recompute()
 
     return obj
 
     return obj
  
circle = mycreateCircle(5.0, "CircleName")
+
circle = my_create_circle(5.0, "CircleName")
 +
}}
 +
<translate>
 +
 
 +
<!--T:76-->
 +
Alternatively we can create a circle by defining its center, axis and radius:
 +
 
 +
</translate>
 +
{{Code|code=
 +
import FreeCAD as App
 +
import Part
 +
 
 +
doc = App.activeDocument()
 +
 
 +
center = App.Vector(1, 2, 3)
 +
axis = App.Vector(1, 1, 1)
 +
radius = 10
 +
circle = Part.Circle(center, axis, radius)
 +
obj = doc.addObject("Part::Feature", "Circle")
 +
obj.Shape = circle.toShape()
 +
 
 +
doc.recompute()
 +
}}
 +
<translate>
 +
 
 +
<!--T:77-->
 +
Or by defining three points on its circumference:
 +
 
 +
</translate>
 +
{{Code|code=
 +
import FreeCAD as App
 +
import Part
 +
 
 +
doc = App.activeDocument()
 +
 
 +
p1 = App.Vector(10, 0, 0)
 +
p2 = App.Vector(0, 10, 0)
 +
p3 = App.Vector(0, 0, 10)
 +
circle = Part.Circle(p1, p2, p3)
 +
obj = doc.addObject("Part::Feature", "Circle")
 +
obj.Shape = circle.toShape()
 +
 
 +
doc.recompute()
 
}}
 
}}
 
<translate>
 
<translate>
  
 
<!--T:29-->
 
<!--T:29-->
Note again, we used the circle (geometry primitive) to construct a shape out of it. We can of course still access our construction geometry afterwards, by doing:
+
Note again, we used the circle (geometric primitive) to construct a shape. We can of course still access our construction geometry afterwards, by doing:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
s = f.Shape
+
shape = obj.Shape
e = s.Edges[0]
+
edge = shape.Edges[0]
c = e.Curve
+
curve = edge.Curve
 
}}
 
}}
 
<translate>
 
<translate>
  
 
<!--T:30-->
 
<!--T:30-->
Here we take the shape of our object f, then we take its list of edges. In this case there will be only one because we made the whole shape out of a single circle, so we take only the first item of the Edges list, and we takes its curve. Every Edge has a Curve, which is the geometry primitive it is based on.
+
Here we take the {{Incode|Shape}} of our object {{Incode|obj}} and then its list of {{Incode|Edges}}. In this case there will be only one edge because we made the shape out of a single circle. So we take only the first item in the {{Incode|Edges}} list, and then take its curve. Every edge has a {{Incode|Curve}}, which is the geometric primitive it is based on.
 +
 
 +
===Arc=== <!--T:82-->
 +
 
 +
<!--T:83-->
 +
An arc can be created like this:
 +
 
 +
</translate>
 +
{{Code|code=
 +
import FreeCAD as App
 +
import Part
 +
 
 +
doc = App.activeDocument()
 +
 
 +
p1 = App.Vector(10, 0, 0)
 +
p2 = App.Vector(0, 10, 0)
 +
p3 = App.Vector(-10, 0, 0)
 +
arc = Part.Arc(p1, p2, p3)
 +
obj = doc.addObject("Part::Feature", "Arc")
 +
obj.Shape = arc.toShape()
 +
 
 +
doc.recompute()
 +
}}
 +
<translate>
 +
 
 +
<!--T:84-->
 +
This draws a half circle. The center is at (0, 0, 0). The radius is 10. P1 is the start point on +X axis. P2 is the middle point on +Y axis and P3 is the end point on -X axis.
 +
 
 +
<!--T:85-->
 +
We can also create an arc from a circle:
 +
 
 +
</translate>
 +
{{Code|code=
 +
import FreeCAD as App
 +
import Part
 +
 
 +
doc = App.activeDocument()
 +
 
 +
p1 = App.Vector(10, 0, 0)
 +
p2 = App.Vector(0, 10, 0)
 +
p3 = App.Vector(-10, 0, 0)
 +
circle = Part.Circle(p1, p2, p3)
 +
arc = Part.ArcOfCircle(circle, 0.0, 0.7854)
 +
obj = doc.addObject("Part::Feature", "Arc")
 +
obj.Shape = arc.toShape()
 +
 
 +
doc.recompute()
 +
}}
 +
<translate>
 +
 
 +
<!--T:86-->
 +
It needs a circle, and a start angle and end angle in radians.
  
<!--T:31-->
 
Head to the [[Topological data scripting|Topological data scripting]] page if you would like to know more.
 
  
 +
<!--T:79-->
 +
{{Docnav
 +
|[[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]]
 +
|[[Topological_data_scripting|Topological data scripting]]
 +
}}
  
 
</translate>
 
</translate>
Line 190: Line 305:
 
[[Category:Python Code{{#translation:}}]]
 
[[Category:Python Code{{#translation:}}]]
 
{{Part Tools navi{{#translation:}}}}
 
{{Part Tools navi{{#translation:}}}}
{{clear}}
 

Latest revision as of 20:40, 13 February 2022

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎polski • ‎português do Brasil • ‎русский

Introduction

The main data structure used in the Part module is the BRep data type from OpenCASCADE. Almost all contents and object types of the Part module are available in Python scripting. This includes geometric primitives, such as Lines, Circles and Arcs, and the whole range of TopoShapes, like Vertexes, Edges, Wires, Faces, Solids and Compounds. For each of those objects, several creation methods exist, and for some of them, especially the TopoShapes, advanced operations like boolean union/difference/intersection are also available. Explore the contents of the Part module, as described in the FreeCAD Scripting Basics page, to know more.

The most basic object that can be created is a Part Feature, which has a simple DataPlacement property, and basic properties to define its color and appearance.

Another simple object used in 2D geometrical objects is the Part Part2DObject, which is the base of the Sketcher SketchObject and most Draft elements.

See also

Test script

Test the creation of Part Primitives with a script. introduced in version 0.19

import parttests.part_test_objects as pto
pto.create_test_file("example_file")

This script is located in the installation directory of the program, and can be examined to see how the basic primitives are built.

$INSTALL_DIR/Mod/Part/parttests/part_test_objects.py

Examples

Line

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

import FreeCAD as App
import Part

doc = App.newDocument()

line = Part.LineSegment()
line.StartPoint = (0.0, 0.0, 0.0)
line.EndPoint = (1.0, 1.0, 1.0)
obj = doc.addObject("Part::Feature", "Line")
obj.Shape= line.toShape()

doc.recompute()

Let's go through the above Python example step by step:

import FreeCAD as App
import Part
doc = App.newDocument()

This loads the FreeCAD and Part modules and creates a new document.

line = Part.LineSegment()
line.StartPoint = (0.0, 0.0, 0.0)
line.EndPoint = (1.0, 1.0, 1.0)

Line is actually a line segment, hence the start and endpoint.

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

This adds a Part object type to the 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 use a geometric primitive (the Part.LineSegment) to create a TopoShape out of it (with the toShape() method). Only shapes can be added to the document. In FreeCAD geometric primitives are used as "building structures" for shapes.

doc.recompute()

Updates the document. This also prepares the visual representation of the new Part object.

Note that a line segment can also be created by specifying its start and endpoint directly in the constructor, for example Part.LineSegment(point1, point2), or we can create a default line and set its properties afterwards, as we did here.

A Line can also be created using:

import FreeCAD as App
import Part

def my_create_line(pt1, pt2, obj_name):
    obj = App.ActiveDocument.addObject("Part::Line", obj_name)
    obj.X1 = pt1[0]
    obj.Y1 = pt1[1]
    obj.Z1 = pt1[2]

    obj.X2 = pt2[0]
    obj.Y2 = pt2[1]
    obj.Z2 = pt2[2]

    App.ActiveDocument.recompute()
    return obj

line = my_create_line((0, 0, 0), (0, 10, 0), "LineName")

Circle

A circle can be created in a similar way:

import FreeCAD as App
import Part

doc = App.activeDocument()

circle = Part.Circle() 
circle.Radius = 10.0  
obj = doc.addObject("Part::Feature", "Circle")
obj.Shape = circle.toShape()

doc.recompute()

Or using:

import FreeCAD as App
import Part

def my_create_circle(rad, obj_name):
    obj = App.ActiveDocument.addObject("Part::Circle", obj_name)
    obj.Radius = rad

    App.ActiveDocument.recompute()
    return obj

circle = my_create_circle(5.0, "CircleName")

Alternatively we can create a circle by defining its center, axis and radius:

import FreeCAD as App
import Part

doc = App.activeDocument()

center = App.Vector(1, 2, 3)
axis = App.Vector(1, 1, 1)
radius = 10
circle = Part.Circle(center, axis, radius)
obj = doc.addObject("Part::Feature", "Circle")
obj.Shape = circle.toShape()

doc.recompute()

Or by defining three points on its circumference:

import FreeCAD as App
import Part

doc = App.activeDocument()

p1 = App.Vector(10, 0, 0)
p2 = App.Vector(0, 10, 0)
p3 = App.Vector(0, 0, 10)
circle = Part.Circle(p1, p2, p3)
obj = doc.addObject("Part::Feature", "Circle")
obj.Shape = circle.toShape()

doc.recompute()

Note again, we used the circle (geometric primitive) to construct a shape. We can of course still access our construction geometry afterwards, by doing:

shape = obj.Shape
edge = shape.Edges[0]
curve = edge.Curve

Here we take the Shape of our object obj and then its list of Edges. In this case there will be only one edge because we made the shape out of a single circle. So we take only the first item in the Edges list, and then take its curve. Every edge has a Curve, which is the geometric primitive it is based on.

Arc

An arc can be created like this:

import FreeCAD as App
import Part

doc = App.activeDocument()

p1 = App.Vector(10, 0, 0)
p2 = App.Vector(0, 10, 0)
p3 = App.Vector(-10, 0, 0)
arc = Part.Arc(p1, p2, p3)
obj = doc.addObject("Part::Feature", "Arc")
obj.Shape = arc.toShape()

doc.recompute()

This draws a half circle. The center is at (0, 0, 0). The radius is 10. P1 is the start point on +X axis. P2 is the middle point on +Y axis and P3 is the end point on -X axis.

We can also create an arc from a circle:

import FreeCAD as App
import Part

doc = App.activeDocument()

p1 = App.Vector(10, 0, 0)
p2 = App.Vector(0, 10, 0)
p3 = App.Vector(-10, 0, 0)
circle = Part.Circle(p1, p2, p3)
arc = Part.ArcOfCircle(circle, 0.0, 0.7854)
obj = doc.addObject("Part::Feature", "Arc")
obj.Shape = arc.toShape()

doc.recompute()

It needs a circle, and a start angle and end angle in radians.