Part scripting: Difference between revisions

From FreeCAD Documentation
No edit summary
(Marked this version for translation)
(13 intermediate revisions by 5 users not shown)
Line 2: Line 2:
<translate>
<translate>


== Scripting == <!--T:1-->
<!--T:78-->
{{Docnav
|[[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]]
|[[Topological_data_scripting|Topological data scripting]]
}}


</translate>
<!--T:2-->
{{TOCright}}
<translate>

==Introduction== <!--T:18-->

<!--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.
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.
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.


=== Examples === <!--T:3-->
<!--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.


<!--T:4-->
<!--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]].

===See also=== <!--T:74-->

<!--T:75-->
* [[Topological_data_scripting|Topological data scripting]]
* [[OpenCASCADE|OpenCASCADE]]

==Test script== <!--T:69-->

<!--T:70-->
Test the creation of [[Part_Primitives|Part Primitives]] with a script. {{Version|0.19}}

</translate>
{{Code|code=
import parttests.part_test_objects as pto
pto.create_test_file("example_file")
}}
<translate>

<!--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.
</translate>
{{Code|code=
$INSTALL_DIR/Mod/Part/parttests/part_test_objects.py
}}
<translate>

==Examples== <!--T:20-->

<!--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 and type in:

</translate>
</translate>
{{Code|code=
{{Code|code=
Line 24: Line 67:
<translate>
<translate>


<!--T:5-->
<!--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=
Line 33: Line 77:
<translate>
<translate>


<!--T:6-->
<!--T:23-->
loads the Part module and creates a new document
loads the Part module and creates a new document

</translate>
</translate>
{{Code|code=
{{Code|code=
Line 43: Line 88:
<translate>
<translate>


<!--T:7-->
<!--T:24-->
Line is actually a line segment, hence the start and endpoint.
Line is actually a line segment, hence the start and endpoint.

</translate>
</translate>
{{Code|code=
{{Code|code=
Line 51: Line 97:
<translate>
<translate>


<!--T:8-->
<!--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 '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.

</translate>
</translate>
{{Code|code=
{{Code|code=
Line 59: Line 106:
<translate>
<translate>


<!--T:9-->
<!--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:10-->
<!--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 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.


<!--T:11-->
<!--T:72-->
A Line can be created also using:

</translate>
{{Code|code=
import FreeCAD
import Part
DOC = FreeCAD.newDocument()

def mycreateLine(pt1, pt2, objName):
obj = DOC.addObject("Part::Line", objName)
obj.X1 = pt1[0]
obj.Y1 = pt1[1]
obj.Z1 = pt1[2]

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

DOC.recompute()
return obj

line = mycreateLine((0,0,0), (0,10,0), "LineName")
}}
<translate>

<!--T:28-->
A circle can be created in a similar way:
A circle can be created in a similar way:

</translate>
</translate>
{{Code|code=
{{Code|code=
Line 79: Line 153:
<translate>
<translate>


<!--T:12-->
<!--T:73-->
or using:

</translate>
{{Code|code=
import FreeCAD
import Part
DOC = FreeCAD.newDocument()

def mycreateCircle(rad, objName):
obj = DOC.addObject("Part::Circle", objName)
obj.Radius = rad

DOC.recompute()
return obj

circle = mycreateCircle(5.0, "CircleName")
}}
<translate>

<!--T:76-->
or create a circle defined by it's center, axis and radius using:

</translate>
{{Code|code=
import Part
doc = App.activeDocument()
center = App.Vector(1,2,3)
axis = App.Vector(1,1,1)
radius = 10
c=Part.Circle(center,axis,radius)
f = doc.addObject("Part::Feature", "Circle")
f.Shape = c.toShape()
doc.recompute()
}}
<translate>

<!--T:77-->
or create a circle defined by three points using:

</translate>
{{Code|code=
import Part
doc = App.activeDocument()
p1 = App.Vector(10,0,0)
p2 = App.Vector(0,10,0)
p3 = App.Vector(0,0,10)
c = Part.Circle(p1,p2,p3)
f = doc.addObject("Part::Feature", "Circle")
f.Shape = c.toShape()
doc.recompute()
}}
<translate>

<!--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 (geometry primitive) to construct a shape out of it. We can of course still access our construction geometry afterwards, by doing:

</translate>
</translate>
{{Code|code=
{{Code|code=
Line 89: Line 218:
<translate>
<translate>


<!--T:13-->
<!--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 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.


<!--T:31-->

<!--T:14-->
Head to the [[Topological data scripting|Topological data scripting]] page if you would like to know more.
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>
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{Part Tools navi{{#translation:}}}}
{{Part Tools navi{{#translation:}}}}
{{Userdocnavi{{#translation:}}}}
{{clear}}

Revision as of 13:51, 22 October 2021

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 by 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 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 Part Part2DObject, which is the base of Sketcher SketchObject (Sketcher), 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

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

import Part,PartGui 
doc=App.newDocument()  
l=Part.LineSegment()
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()

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

import Part,PartGui
doc=App.newDocument()

loads the Part module and creates a new document

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

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

doc.addObject("Part::Feature","Line").Shape=l.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 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.

doc.recompute()

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

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.

A Line can be created also using:

import FreeCAD
import Part
DOC = FreeCAD.newDocument()

def mycreateLine(pt1, pt2, objName):
    obj = DOC.addObject("Part::Line", objName)
    obj.X1 = pt1[0]
    obj.Y1 = pt1[1]
    obj.Z1 = pt1[2]

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

    DOC.recompute()
    return obj

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

A circle can be created in a similar way:

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

or using:

import FreeCAD
import Part
DOC = FreeCAD.newDocument()

def mycreateCircle(rad, objName):
    obj = DOC.addObject("Part::Circle", objName)
    obj.Radius = rad

    DOC.recompute()
    return obj

circle = mycreateCircle(5.0, "CircleName")

or create a circle defined by it's center, axis and radius using:

import Part
doc = App.activeDocument()
center = App.Vector(1,2,3)
axis = App.Vector(1,1,1)
radius = 10
c=Part.Circle(center,axis,radius)
f = doc.addObject("Part::Feature", "Circle")
f.Shape = c.toShape()
doc.recompute()

or create a circle defined by three points using:

import Part
doc = App.activeDocument()
p1 = App.Vector(10,0,0)
p2 = App.Vector(0,10,0)
p3 = App.Vector(0,0,10)
c = Part.Circle(p1,p2,p3)
f = doc.addObject("Part::Feature", "Circle")
f.Shape = c.toShape()
doc.recompute()

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:

s = f.Shape
e = s.Edges[0]
c = e.Curve

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.

Head to the Topological data scripting page if you would like to know more.