Topological data scripting/de: Difference between revisions

From FreeCAD Documentation
(Created page with "Du kannst die Länge und den Mittelpunkt einer Kante wie diese finden:")
(Updating to match new version of source page)
(30 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
<languages/>

<div class="mw-translate-fuzzy">
{{docnav/de|Mesh Scripting/de|Mesh to Part/de}}
{{docnav/de|Mesh Scripting/de|Mesh to Part/de}}
</div>


{{TOCright}}
{{TutorialInfo/de
|Topic=Programming
|Level=Intermediate
|Time=
|Author=
|FCVersion=
|Files=
}}


Diese Seite beschreibt mehrere Methoden zum Erstellen und Ändern von [[Part Module/de|Teilformen]] aus Python. Bevor Du diese Seite liest, wenn Du neu in Python bist, ist es eine gute Idee, über [[Introduction to Python/de|Python Skripten]] und [[FreeCAD Scripting Basics/de|Wie Python Skripten funktioniert in FreeCAD]] zu lesen.


<div class="mw-translate-fuzzy">
Hier erklären wir Dir, wie Du das [[Part Module/de]] direkt aus dem FreeCAD Python Interpreter oder von einem externen Skript aus steuern kannst. Die Grundlagen des Topologischen Datenskripts sind unter [[Part_Module#Explaining_the_concepts/de|Part Arbeitsbereich Erläuterung des Konzepts]] beschrieben. Achte darauf, den Abschnitt [[Scripting/de]] und die Seiten [[FreeCAD Scripting Basics/de]] zu durchsuchen, wenn Du weitere Informationen darüber benötigst, wie Python Skripten in FreeCAD funktioniert.
Hier erklären wir Dir, wie Du das [[Part Module/de]] direkt aus dem FreeCAD Python Interpreter oder von einem externen Skript aus steuern kannst. Die Grundlagen des Topologischen Datenskripts sind unter [[Part_Module#Explaining_the_concepts/de|Part Arbeitsbereich Erläuterung des Konzepts]] beschrieben. Achte darauf, den Abschnitt [[Scripting/de]] und die Seiten [[FreeCAD Scripting Basics/de]] zu durchsuchen, wenn Du weitere Informationen darüber benötigst, wie Python Skripten in FreeCAD funktioniert.
</div>


Here we will explain to you how to control the [[Part_Module|Part Module]] directly from the FreeCAD Python interpreter, or from any external script. The basics about Topological data scripting are described in [[Part_Module#Explaining_the_concepts|Part Module Explaining the concepts]]. Be sure to browse the [[Scripting]] section and the [[FreeCAD_Scripting_Basics]] pages if you need more information about how Python scripting works in FreeCAD. If you are new to Python, it is a good idea to first read the [[Introduction_to_Python|Introduction to Python]].

<div class="mw-translate-fuzzy">
=== Klassen Diagramm ===
=== Klassen Diagramm ===
Dies ist ein [http://de.wikipedia.org/wiki/Unified_Modeling_Language Unified Modeling Language (UML)] Überblick über die wesentlichen Klassen des Part Arbweitsbereichs:
Dies ist ein [http://de.wikipedia.org/wiki/Unified_Modeling_Language Unified Modeling Language (UML)] Überblick über die wesentlichen Klassen des Part Arbweitsbereichs:
[[Image:Part_Classes.jpg|center|Python Klassen des Part Arbeitsbereichs]]
[[Image:Part_Classes.jpg|center|Python Klassen des Part Arbeitsbereichs]]
</div>


This is a [http://en.wikipedia.org/wiki/Unified_Modeling_Language Unified Modeling Language (UML)] overview of the most important classes of the Part module:
[[Image:Part_Classes.jpg|Python classes of the Part module]]

[[#top|top]]

<div class="mw-translate-fuzzy">
=== Geometrie ===
=== Geometrie ===
Die geometrischen Objekte sind die Bausteine aller topologischen Objekte:
Die geometrischen Objekte sind die Bausteine aller topologischen Objekte:
Line 26: Line 31:
* '''Circle''' Kreis oder Kreissegment definiert durch einen Mittelpunkt und einen Start- und Endpunkt
* '''Circle''' Kreis oder Kreissegment definiert durch einen Mittelpunkt und einen Start- und Endpunkt
* '''......''' Und demnächst mehr davon
* '''......''' Und demnächst mehr davon
</div>


The geometric objects are the building blocks of all topological objects:
* '''Geom''' Base class of the geometric objects.
* '''Line''' A straight line in 3D, defined by starting point and end point.
* '''Circle''' Circle or circle segment defined by a center point and start and end point.
* '''......''' Etc.

[[#top|top]]

<div class="mw-translate-fuzzy">
=== Topologie ===
=== Topologie ===
The folgenden topologischen Datentypen stehen zur Verfügung:
The folgenden topologischen Datentypen stehen zur Verfügung:
Line 38: Line 53:
* '''Vertex''' Ein topologisches Element das mit einem Punkt korrespondiert. Es ist nulldimensional.
* '''Vertex''' Ein topologisches Element das mit einem Punkt korrespondiert. Es ist nulldimensional.
* '''Shape''' Ein generischer Term für all die zuvor aufgezählten Elemente.
* '''Shape''' Ein generischer Term für all die zuvor aufgezählten Elemente.
</div>


The following topological data types are available:
* '''Compound''' A group of any type of topological objects.
* '''Compsolid''' A composite solid is a set of solids connected by their faces. It expands the notions of WIRE and SHELL to solids.
* '''Solid''' A part of space limited by shells. It is three dimensional.
* '''Shell''' A set of faces connected by their edges. A shell can be open or closed.
* '''Face''' In 2D it is part of a plane; in 3D it is part of a surface. Its geometry is constrained (trimmed) by contours. It is two dimensional.
* '''Wire''' A set of edges connected by their vertices. It can be an open or closed contour depending on whether the edges are linked or not.
* '''Edge''' A topological element corresponding to a restrained curve. An edge is generally limited by vertices. It has one dimension.
* '''Vertex''' A topological element corresponding to a point. It has zero dimension.
* '''Shape''' A generic term covering all of the above.

[[#top|top]]

<div class="mw-translate-fuzzy">
=== Kurzes Beispiel: Erstellung einer einfachen Topologie ===
=== Kurzes Beispiel: Erstellung einer einfachen Topologie ===
</div>


[[Image:Wire.png|Wire]]
[[Image:Wire.png|Wire]]


<div class="mw-translate-fuzzy">

Wir werden nun eine Topologie erstellen, indem wir sie aus einer einfacheren Geometrie konstruieren.
Wir werden nun eine Topologie erstellen, indem wir sie aus einer einfacheren Geometrie konstruieren.
Als Fallstudie verwenden wir ein Teil, wie im Bild zu sehen, das aus
Als Fallstudie verwenden wir ein Teil, wie im Bild zu sehen, das aus
vier Knoten, zwei Kreise und zwei Linien besteht.
vier Knoten, zwei Kreise und zwei Linien besteht.
</div>


[[#top|top]]

<div class="mw-translate-fuzzy">
==== Erstellen der Geometrie ====
==== Erstellen der Geometrie ====
Zuerst müssen wir die verschiedenen geometrischen Teile dieses Drahtes erstellen. Und wir müssen darauf achten, dass die Scheitelpunkte der geometrischen Teile
Zuerst müssen wir die verschiedenen geometrischen Teile dieses Drahtes erstellen. Und wir müssen darauf achten, dass die Scheitelpunkte der geometrischen Teile
an der '''gleichen''' Stelle sind . Sonst wären wir später vielleicht nicht mehr in der Lage, die geometrischen Teile zu einer Topologie zu verbinden!
an der '''gleichen''' Stelle sind . Sonst wären wir später vielleicht nicht mehr in der Lage, die geometrischen Teile zu einer Topologie zu verbinden!
</div>


First we create the distinct geometric parts of this wire. Making sure that parts that have to be connected later
share the same vertices.

<div class="mw-translate-fuzzy">
Also erstellen wir zuerst die Punkte:
Also erstellen wir zuerst die Punkte:
</div>


{{Code|code=
{{Code|code=
import Part
from FreeCAD import Base
from FreeCAD import Base
V1 = Base.Vector(0,10,0)
V1 = Base.Vector(0, 10, 0)
V2 = Base.Vector(30,10,0)
V2 = Base.Vector(30, 10, 0)
V3 = Base.Vector(30,-10,0)
V3 = Base.Vector(30, -10, 0)
V4 = Base.Vector(0,-10,0)
V4 = Base.Vector(0, -10, 0)
}}
}}


[[#top|top]]
==== Kreisbogen ====

<div class="mw-translate-fuzzy">
==== Bogen ====
</div>


[[Image:Circel.png|Circle]]
[[Image:Circel.png|Circle]]




<div class="mw-translate-fuzzy">
Um einen Kreisbogen zu erzeugen, machen wir einen Hilfspunkt und erzeugen den Kreisbogen durch drei Punkte:
Um einen Kreisbogen zu erzeugen, machen wir einen Hilfspunkt und erzeugen den Kreisbogen durch drei Punkte:
</div>


{{Code|code=
{{Code|code=
VC1 = Base.Vector(-10,0,0)
VC1 = Base.Vector(-10, 0, 0)
C1 = Part.Arc(V1,VC1,V4)
C1 = Part.Arc(V1, VC1, V4)
VC2 = Base.Vector(40, 0, 0)
# and the second one
VC2 = Base.Vector(40,0,0)
C2 = Part.Arc(V2, VC2, V3)
C2 = Part.Arc(V2,VC2,V3)
}}
}}


[[#top|top]]

<div class="mw-translate-fuzzy">
==== Linie ====
==== Linie ====
</div>


[[Image:Line.png|Line]]
[[Image:Line.png|Line]]




<div class="mw-translate-fuzzy">
Das Liniensegment kann sehr einfach aus den Punkten erstellt werden:
Das Liniensegment kann sehr einfach aus den Punkten erstellt werden:
</div>


{{Code|code=
{{Code|code=
L1 = Part.LineSegment(V1,V2)
L1 = Part.LineSegment(V1, V2)
L2 = Part.LineSegment(V3, V4)
# and the second one
L2 = Part.LineSegment(V3,V4)
}}
}}


[[#top|top]]
Hinweis: in FreeCAD wurde 0.16 Part.Line verwendet, für FreeCAD 0.17 muss Part.LineSegment verwendet werden'''


<div class="mw-translate-fuzzy">
==== Alles zusammensetzen ====
==== Alles zusammensetzen ====
Der letzte Schritt besteht darin, die geometrischen Basiselemente zusammenzusetzen und eine topologische Form backen:
Der letzte Schritt besteht darin, die geometrischen Basiselemente zusammenzusetzen und eine topologische Form backen:
</div>

The last step is to put the geometric base elements together
and bake a topological shape:


{{Code|code=
{{Code|code=
S1 = Part.Shape([C1,L1,C2,L2])
S1 = Part.Shape([C1, L1, C2, L2])
}}
}}


[[#top|top]]

<div class="mw-translate-fuzzy">
==== Ein Prisma herstellen ====
==== Ein Prisma herstellen ====
Extrudiere nun den Draht in eine Richtung und erstelle eine echte 3D Form:
Extrudiere nun den Draht in eine Richtung und erstelle eine echte 3D Form:
</div>

Now extrude the wire in a direction and make an actual 3D shape:


{{Code|code=
{{Code|code=
W = Part.Wire(S1.Edges)
W = Part.Wire(S1.Edges)
P = W.extrude(Base.Vector(0,0,10))
P = W.extrude(Base.Vector(0, 0, 10))
}}
}}


[[#top|top]]

<div class="mw-translate-fuzzy">
==== Alles anzeigen ====
==== Alles anzeigen ====
</div>


{{Code|code=
{{Code|code=
Line 113: Line 180:
}}
}}


[[#top|top]]

<div class="mw-translate-fuzzy">
== Erstellen von Grundformen ==
== Erstellen von Grundformen ==
Du kannst ganz einfach topologische Grundobjekte
Du kannst ganz einfach topologische Grundobjekte
mit den "make....()" Methoden aus dem Arbeitsbereich Part erstellen:
mit den "make....()" Methoden aus dem Arbeitsbereich Part erstellen:
</div>

You can easily create basic topological objects with the {{incode|make...()}}
methods from the Part Module:


{{Code|code=
{{Code|code=
b = Part.makeBox(100,100,100)
b = Part.makeBox(100, 100, 100)
Part.show(b)
Part.show(b)
}}
}}
Line 136: Line 210:
</div>
</div>


[[#top|top]]

<div class="mw-translate-fuzzy">
==== Import der notwendigen Module ====
==== Import der notwendigen Module ====
Zuerst müssen wir den Part Arbeitsbereich importieren, damit wir seinen Inhalt in Python verwenden können.
Zuerst müssen wir den Part Arbeitsbereich importieren, damit wir seinen Inhalt in Python verwenden können.
Wir werden auch das Basismodul aus dem FreeCAD Modul importieren:
Wir werden auch das Basismodul aus dem FreeCAD Modul importieren:
</div>

First we need to import the Part module so we can use its contents in Python.
We'll also import the Base module from inside the FreeCAD module:


{{Code|code=
{{Code|code=
Line 145: Line 226:
}}
}}


[[#top|top]]

<div class="mw-translate-fuzzy">
==== Erstellen eines Vektors ====
==== Erstellen eines Vektors ====
[http://en.wikipedia.org/wiki/Euclidean_vector Vektoren] sind einer der am häufigsten verwendeten wichtigen Informationsteile
[http://en.wikipedia.org/wiki/Euclidean_vector Vektoren] sind einer der am häufigsten verwendeten wichtigen Informationsteile
beim Bau von Formen. Sie enthalten in der Regel drei Zahlen
beim Bau von Formen. Sie enthalten in der Regel drei Zahlen
(aber nicht notwendigerweise immer): die kartesischen Koordinaten x, y und z. Du erstelltst einen Vektor wie diesen:
(aber nicht notwendigerweise immer): die kartesischen Koordinaten x, y und z. Du erstelltst einen Vektor wie diesen:
</div>

[http://en.wikipedia.org/wiki/Euclidean_vector Vectors] are one of the most
important pieces of information when building shapes. They usually contain three numbers
(but not necessarily always): the X, Y and Z cartesian coordinates. You
create a vector like this:


{{Code|code=
{{Code|code=
myVector = Base.Vector(3,2,0)
myVector = Base.Vector(3, 2, 0)
}}
}}


<div class="mw-translate-fuzzy">
Wir haben gerade einen Vektor mit den Koordinaten x=3, y=2, z=0 erstellt. Im Part Arbeitsbereich , werden Vektoren überall verwendet. Teileformen verwenden auch eine andere Art von Punkt
Wir haben gerade einen Vektor mit den Koordinaten x=3, y=2, z=0 erstellt. Im Part Arbeitsbereich , werden Vektoren überall verwendet. Teileformen verwenden auch eine andere Art von Punkt
Darstellung namens Vertex, die lediglich ein Behälter für einen Vektor ist. Du greifst auf den Vektor eines Knoten wie folgt zu:
Darstellung namens Vertex, die lediglich ein Behälter für einen Vektor ist. Du greifst auf den Vektor eines Knoten wie folgt zu:
</div>


{{Code|code=
{{Code|code=
Line 163: Line 255:
}}
}}


[[#top|top]]

<div class="mw-translate-fuzzy">
==== Erstellen einer Kante ====
==== Erstellen einer Kante ====
Eine Kante ist nichts anderes als eine Linie mit zwei Knoten:
Eine Kante ist nichts anderes als eine Linie mit zwei Knoten:
</div>

An edge is nothing but a line with two vertices:


{{Code|code=
{{Code|code=
edge = Part.makeLine((0,0,0), (10,0,0))
edge = Part.makeLine((0, 0, 0), (10, 0, 0))
edge.Vertexes
edge.Vertexes
> [<Vertex object at 01877430>, <Vertex object at 014888E0>]
> [<Vertex object at 01877430>, <Vertex object at 014888E0>]
Line 175: Line 273:


{{Code|code=
{{Code|code=
vec1 = Base.Vector(0,0,0)
vec1 = Base.Vector(0, 0, 0)
vec2 = Base.Vector(10,0,0)
vec2 = Base.Vector(10, 0, 0)
line = Part.LineSegment(vec1,vec2)
line = Part.LineSegment(vec1, vec2)
edge = line.toShape()
edge = line.toShape()
}}
}}
Line 190: Line 288:
}}
}}


[[#top|top]]
==== Putting the shape on screen ====

<div class="mw-translate-fuzzy">
==== Die Form auf den Bildschirm bringen ====
Bisher haben wir ein Kantenobjekt erstellt, das aber nirgendwo auf dem Bildschirm erscheint.
Das liegt daran, dass die FreeCAD 3D Szene
nur das anzeigt, was du ihm sagst, dass er anzeigen soll. Um das zu tun, verwenden wir folgende einfache Methode:
</div>

So far we created an edge object, but it doesn't appear anywhere on the screen.
So far we created an edge object, but it doesn't appear anywhere on the screen.
This is because the FreeCAD 3D scene
This is because the FreeCAD 3D scene
Line 200: Line 306:
}}
}}


Die Anzeigefunktion erzeugt ein Objekt in unserem FreeCAD Dokument und weist unsere "Kantenform" ihm zu. Verwende dies, wenn es an der Zeit ist, deine Erstellung auf dem Bildschirm anzuzeigen.
The show function creates an object in our FreeCAD document and assigns our "edge" shape

to it. Use this whenever it is time to display your creation on screen.
[[#top|top]]

<div class="mw-translate-fuzzy">
==== Erstellen eines Drahts ====
Ein Draht ist eine Mehrkantenlinie und kann aus einer Liste von Kanten oder sogar einer Liste von Drähten erstellt werden:
</div>


==== Creating a Wire ====
A wire is a multi-edge line and can be created from a list of edges
A wire is a multi-edge line and can be created from a list of edges
or even a list of wires:
or even a list of wires:


{{Code|code=
{{Code|code=
edge1 = Part.makeLine((0,0,0), (10,0,0))
edge1 = Part.makeLine((0, 0, 0), (10, 0, 0))
edge2 = Part.makeLine((10,0,0), (10,10,0))
edge2 = Part.makeLine((10, 0, 0), (10, 10, 0))
wire1 = Part.Wire([edge1,edge2])
wire1 = Part.Wire([edge1, edge2])
edge3 = Part.makeLine((10,10,0), (0,10,0))
edge3 = Part.makeLine((10, 10, 0), (0, 10, 0))
edge4 = Part.makeLine((0,10,0), (0,0,0))
edge4 = Part.makeLine((0, 10, 0), (0, 0, 0))
wire2 = Part.Wire([edge3,edge4])
wire2 = Part.Wire([edge3, edge4])
wire3 = Part.Wire([wire1,wire2])
wire3 = Part.Wire([wire1, wire2])
wire3.Edges
wire3.Edges
> [<Edge object at 016695F8>, <Edge object at 0197AED8>, <Edge object at 01828B20>, <Edge object at 0190A788>]
> [<Edge object at 016695F8>, <Edge object at 0197AED8>, <Edge object at 01828B20>, <Edge object at 0190A788>]
Line 220: Line 331:
}}
}}


<div class="mw-translate-fuzzy">
Part.show(wire3) will display the 4 edges that compose our wire. Other
Part.show(Draht3) zeigt die 4 Kanten, die unseren Draht bilden. Sonstige nützliche Informationen können leicht abgerufen werden:
useful information can be easily retrieved:
</div>


{{Code|code=
{{Code|code=
Line 234: Line 346:
}}
}}


[[#top|top]]
==== Creating a Face ====

<div class="mw-translate-fuzzy">
==== Erstellen einer Fläche ====
Nur Flächen, die aus geschlossenen Drähten erstellt wurden, sind gültig. In diesem Beispiel ist Draht3 ein geschlossener Draht, aber Draht2 ist kein geschlossener Draht (siehe oben).
</div>

Only faces created from closed wires will be valid. In this example, wire3
Only faces created from closed wires will be valid. In this example, wire3
is a closed wire but wire2 is not a closed wire (see above)
is a closed wire but wire2 is not (see above):


{{Code|code=
{{Code|code=
face = Part.Face(wire3)
face = Part.Face(wire3)
face.Area
face.Area
> 99.999999999999972
> 99.99999999999999
face.CenterOfMass
face.CenterOfMass
> Vector (5, 5, 0)
> Vector (5, 5, 0)
Line 253: Line 371:
}}
}}


<div class="mw-translate-fuzzy">
Only faces will have an area, not wires nor edges.
Nur Flächen haben eine Grundfläche, keine Drähte oder Kanten.
</div>

[[#top|top]]

<div class="mw-translate-fuzzy">
==== Erstellen eines Kreises ====
So einfach kann ein Kreis erstellt werden:
</div>


A circle can be created like this:
==== Creating a Circle ====
A circle can be created as simply as this:


{{Code|code=
{{Code|code=
Line 264: Line 390:
}}
}}


Wenn Du ihn an einer bestimmten Stelle und mit einer bestimmten Richtung erzeugen möchtest:
If you want to create it at a certain position and with a certain direction:


{{Code|code=
{{Code|code=
ccircle = Part.makeCircle(10, Base.Vector(10,0,0), Base.Vector(1,0,0))
ccircle = Part.makeCircle(10, Base.Vector(10, 0, 0), Base.Vector(1, 0, 0))
ccircle.Curve
ccircle.Curve
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0))
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0))
}}
}}


<div class="mw-translate-fuzzy">
ccircle will be created at distance 10 from the x origin and will be facing
Der Kreis wird im Abstand 10 vom x Ursprung erstellt und ist nach außen entlang der x Achse gerichtet. Hinweis: erzeugeKreis akzeptiert nur Base.Vector() für die Position und normale Parameter, nicht Tupel. Du kannst auch einen Teil des Kreises durch Angabe
outwards along the x axis. Note: makeCircle only accepts Base.Vector() for the position
eines Anfangs- und eines Endwinkels erstellen:
and normal parameters, not tuples. You can also create part of the circle by giving
</div>
a start and an end angle:


{{Code|code=
{{Code|code=
from math import pi
from math import pi
arc1 = Part.makeCircle(10, Base.Vector(0,0,0), Base.Vector(0,0,1), 0, 180)
arc1 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)
arc2 = Part.makeCircle(10, Base.Vector(0,0,0), Base.Vector(0,0,1), 180, 360)
arc2 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 180, 360)
}}
}}


<div class="mw-translate-fuzzy">
Both arc1 and arc2 jointly will make a circle. Angles should be provided in
Sowohl arc1 als auch arc2 bilden gemeinsam einen Kreis. Winkel sollten angegeben werden in
degrees; if you have radians simply convert them using the formula:
Grad; wenn Du Bogenmaß hast, wandelst Du sie einfach mit der Formel um:
degrees = radians * 180/PI or using python's math module (after doing import
Grad = Bogenmaß * 180/PI oder mit dem Mathematikmodul von python (nach dem ausführen von import
math, of course):
math, natürlich):
</div>


{{Code|code=
{{Code|code=
import math
degrees = math.degrees(radians)
degrees = math.degrees(radians)
}}
}}


[[#top|top]]
==== Creating an Arc along points ====

Unfortunately there is no makeArc function, but we have the Part.Arc function to
<div class="mw-translate-fuzzy">
==== Erstellen eines Bogens entlang von Punkten ====
Leider gibt es keine makeArc-Funktion, aber wir haben die Part.Arc Funktion um einen Bogen durch drei Punkte zu erzeugen. Es erzeugt ein Bogenobjekt das den Startpunktes mit dem Endpunkt durch den Mittelpunkt verbindet.
Die .toShape() Funktion des Bogenobjekts muss aufgerufen werden, um ein Kantenobjekt zu erhalten,
wie bei der Verwendung von Part.LineSegment anstelle von Part.makeLine.
</div>

Unfortunately there is no {{incode|makeArc()}} function, but we have the {{incode|Part.Arc()}} function to
create an arc through three points. It creates an arc object
create an arc through three points. It creates an arc object
joining the start point to the end point through the middle point.
joining the start point to the end point through the middle point.
The arc object's .toShape() function must be called to get an edge object,
The arc object's {{incode|toShape()}} function must be called to get an edge object,
the same as when using Part.LineSegment instead of Part.makeLine.
the same as when using {{incode|Part.LineSegment}} instead of {{incode|Part.makeLine}}.


{{Code|code=
{{Code|code=
arc = Part.Arc(Base.Vector(0,0,0),Base.Vector(0,5,0),Base.Vector(5,5,0))
arc = Part.Arc(Base.Vector(0, 0, 0), Base.Vector(0, 5, 0), Base.Vector(5, 5, 0))
arc
arc
> <Arc object>
> <Arc object>
arc_edge = arc.toShape()
arc_edge = arc.toShape()
Part.show(arc_edge)
}}
}}


<div class="mw-translate-fuzzy">
Arc only accepts Base.Vector() for points but not tuples. arc_edge is what
Arc akzeptiert nur Base.Vector() für Punkte, aber nicht für Tupel. arc_edge ist das, was wir wollen, was wir durch Verwendung von Part.show(arc_edge) anzeigen können.
we want which we can display using Part.show(arc_edge). You can also obtain
Du kannst einen Bogen auch unter Verwendung eines Kreisabschnitts erhalten:
an arc by using a portion of a circle:
</div>


{{Code|code=
{{Code|code=
from math import pi
from math import pi
circle = Part.Circle(Base.Vector(0,0,0),Base.Vector(0,0,1),10)
circle = Part.Circle(Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 10)
arc = Part.Arc(circle,0,pi)
arc = Part.Arc(circle,0,pi)
}}
}}


Bögen sind gültige Kanten wie Linien, so dass sie auch in Drähten verwendet werden können.
Arcs are valid edges like lines, so they can be used in wires also.


[[#top|top]]
==== Creating a polygon ====

A polygon is simply a wire with multiple straight edges. The makePolygon
<div class="mw-translate-fuzzy">
==== Erstellen eines Polygons ====
Ein Polygon ist einfach ein Draht mit mehreren geraden Kanten. Die makePolygon
Funktion nimmt eine Liste von Punkten und erstellt einen Draht durch diese Punkte:
</div>

A polygon is simply a wire with multiple straight edges. The {{incode|makePolygon()}}
function takes a list of points and creates a wire through those points:
function takes a list of points and creates a wire through those points:


{{Code|code=
{{Code|code=
lshape_wire = Part.makePolygon([Base.Vector(0,5,0),Base.Vector(0,0,0),Base.Vector(5,0,0)])
lshape_wire = Part.makePolygon([Base.Vector(0, 5, 0), Base.Vector(0, 0, 0), Base.Vector(5, 0, 0)])
}}
}}


[[#top|top]]
==== Creating a Bézier curve ====

Bézier curves are used to model smooth curves using a series of poles (points) and optional weights. The function below makes a Part.BezierCurve from a series of FreeCAD.Vector points. (Note: when "getting" and "setting" a single pole or weight, indices start at 1, not 0.)
<div class="mw-translate-fuzzy">
==== Erstellen einer Bézier Kurve ====
Bézier Kurven werden verwendet, um sanfte Kurven mit einer Reihe von Polen (Punkten) und optionalen Gewichten zu modellieren. Die folgende Funktion erstellt eine Part.BezierCurve aus einer Reihe von FreeCAD.Vector Punkten. (Hinweis: Wenn du einen einzelnen Pol oder ein Gewicht "erhältst" und "einstellst", beginnen die Indizes bei 1, nicht bei 0.)
</div>

Bézier curves are used to model smooth curves using a series of poles (points) and optional weights. The function below makes a {{incode|Part.BezierCurve()}} from a series of {{incode|FreeCAD.Vector()}} points. Note: when "getting" and "setting" a single pole or weight, indices start at 1, not 0.


{{Code|code=
{{Code|code=
Line 337: Line 489:
}}
}}


[[#top|top]]
==== Creating a Plane ====

A Plane is simply a flat rectangular surface. The method used to create one is '''makePlane(length,width,[start_pnt,dir_normal])'''. By default
<div class="mw-translate-fuzzy">
start_pnt = Vector(0,0,0) and dir_normal = Vector(0,0,1). Using dir_normal = Vector(0,0,1)
==== Erstellen einer Ebene ====
will create the plane facing in the positive z axis direction, while dir_normal = Vector(1,0,0) will create the
Eine Ebene ist einfach eine flache rechteckige Fläche. Die Methode, mit der eine solche erstellt wird, ist '''makePlane(length,width,[start_pnt,dir_normal])''''. Standardmäßig
plane facing in the positive x axis direction:
start_pnt = Vektor(0,0,0,0) und dir_normal = Vektor(0,0,1). Verwendung von dir_normal = Vector(0,0,0,1)
erzeugt die Ebene, die in Richtung der positiven z-Achse zeigt, während dir_normal = Vector(1,0,0,0) die Ebene erzeugt.
Ebene, die in Richtung der positiven x-Achse zeigt:
</div>

A Plane is a flat rectangular surface. The method used to create one is {{incode|makePlane(length, width, [start_pnt, dir_normal])}}. By default start_pnt = Vector(0, 0, 0) and dir_normal = Vector(0, 0, 1). Using dir_normal = Vector(0, 0, 1)
will create the plane facing in the positive Z axis direction, while dir_normal = Vector(1, 0, 0) will create the
plane facing in the positive X axis direction:


{{Code|code=
{{Code|code=
plane = Part.makePlane(2,2)
plane = Part.makePlane(2, 2)
plane
plane
><Face object at 028AF990>
> <Face object at 028AF990>
plane = Part.makePlane(2, 2, Base.Vector(3,0,0), Base.Vector(0,1,0))
plane = Part.makePlane(2, 2, Base.Vector(3, 0, 0), Base.Vector(0, 1, 0))
plane.BoundBox
plane.BoundBox
> BoundBox (3, 0, 0, 5, 0, 2)
> BoundBox (3, 0, 0, 5, 0, 2)
}}
}}


<div class="mw-translate-fuzzy">
BoundBox is a cuboid enclosing the plane with a diagonal starting at
BoundBox ist ein Quader, der die Ebene mit einer Diagonale beginnend bei
(3,0,0) and ending at (5,0,2). Here the BoundBox thickness along the y axis is zero,
(3,0,0,0) und endet bei (5,0,2) einschliesst. Hier ist die BoundBox Dicke entlang der y-Achse Null,
since our shape is totally flat.
zumal unsere Form völlig flach ist.
</div>


<div class="mw-translate-fuzzy">
Note: makePlane only accepts Base.Vector() for start_pnt and dir_normal but not tuples.
Hinweis: makePlane akzeptiert nur Base.Vector() für start_pnt und dir_normal, nicht aber Tupel.
</div>

[[#top|top]]

<div class="mw-translate-fuzzy">
==== Erstellen einer Ellipse ====
Es gibt mehrere Möglichkeiten, eine Ellipse zu erstellen:
</div>


==== Creating an ellipse ====
There are several ways to create an ellipse:
There are several ways to create an ellipse:


Line 365: Line 535:
}}
}}


<div class="mw-translate-fuzzy">
Creates an ellipse with major radius 2 and minor radius 1 with the center at (0,0,0).
Erzeugt eine Ellipse mit Hauptradius 2 und Nebenradius 1 mit dem Mittelpunkt bei (0,0,0).
</div>


{{Code|code=
{{Code|code=
Line 371: Line 543:
}}
}}


Erstellt eine Kopie der angegebenen Ellipse.
Creates a copy of the given ellipse.


{{Code|code=
{{Code|code=
Part.Ellipse(S1,S2,Center)
Part.Ellipse(S1, S2, Center)
}}
}}


Erzeugt eine Ellipse, die auf den Punkt Mitte zentriert ist, an dem die Ebene der Ellipse definiert ist durch Mittelpunkt, S1 und S2, ihre Hauptachse definiert ist durch Mittelpunkt und S1, sein Hauptradius ist der Abstand zwischen Mittelpunkt und S1,
Creates an ellipse centered on the point Center, where the plane of the
und sein kleiner Radius ist der Abstand zwischen S2 und der Hauptachse.
ellipse is defined by Center, S1 and S2, its major axis is defined by
Center and S1, its major radius is the distance between Center and S1,
and its minor radius is the distance between S2 and the major axis.


{{Code|code=
{{Code|code=
Part.Ellipse(Center,MajorRadius,MinorRadius)
Part.Ellipse(Center, MajorRadius, MinorRadius)
}}
}}


<div class="mw-translate-fuzzy">
Creates an ellipse with major and minor radii MajorRadius and MinorRadius,
Erstellt eine Ellipse mit Haupt- und Nebenradien Hauptradius und Nebenradius,
located in the plane defined by Center and the normal (0,0,1)
die sich in der durch den Mittelpunkt definierten Ebene und der Normalen (0,0,1) befinden.
</div>


{{Code|code=
{{Code|code=
eli = Part.Ellipse(Base.Vector(10,0,0),Base.Vector(0,5,0),Base.Vector(0,0,0))
eli = Part.Ellipse(Base.Vector(10, 0, 0), Base.Vector(0, 5, 0), Base.Vector(0, 0, 0))
Part.show(eli.toShape())
Part.show(eli.toShape())
}}
}}


<div class="mw-translate-fuzzy">
In the above code we have passed S1, S2 and center. Similar to Arc,
Im obigen Code haben wir S1, S2 und Mitte überschritten. Ähnlich wie Arc,
Ellipse creates an ellipse object but not edge, so we need to
erzeugt Ellipse ein Ellipsenobjekt, aber keine Kante, also müssen wir es in eine Kante mit toShape() zur Anzeige konvertieren.
convert it into an edge using toShape() for display.
</div>


<div class="mw-translate-fuzzy">
Note: Arc only accepts Base.Vector() for points but not tuples.
Hinweis: Arc akzeptiert nur Base.Vector() für Punkte, nicht aber für Tupel.
</div>


{{Code|code=
{{Code|code=
eli = Part.Ellipse(Base.Vector(0,0,0),10,5)
eli = Part.Ellipse(Base.Vector(0, 0, 0), 10, 5)
Part.show(eli.toShape())
Part.show(eli.toShape())
}}
}}


<div class="mw-translate-fuzzy">
for the above Ellipse constructor we have passed center, MajorRadius and MinorRadius.
für den obigen Ellipsenkonstruktor haben wir Mitte, MajorRadius und MinorRadius überschritten.
</div>

[[#top|top]]

===Create a torus===


Using {{incode|makeTorus(radius1, radius2, [pnt, dir, angle1, angle2, angle])}}.
==== Creating a Torus ====
Using '''makeTorus(radius1,radius2,[pnt,dir,angle1,angle2,angle])'''.
By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = 0, angle2 = 360 and angle = 360.
By default pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=0, angle2=360 and angle=360.
Consider a torus as small circle sweeping along a big circle. Radius1 is the
Consider a torus as small circle sweeping along a big circle. Radius1 is the
radius of the big cirlce, radius2 is the radius of the small circle, pnt is the center
radius of the big circle, radius2 is the radius of the small circle, pnt is the center
of the torus and dir is the normal direction. angle1 and angle2 are angles in
of the torus and dir is the normal direction. angle1 and angle2 are angles in
radians for the small circle; the last parameter angle is to make a section of
degrees for the small circle; the last angle parameter is to make a section of
the torus:
the torus:


Line 424: Line 604:


{{Code|code=
{{Code|code=
tor=Part.makeTorus(10, 5, Base.Vector(0,0,0), Base.Vector(0,0,1), 0, 180)
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)
}}
}}


Line 430: Line 610:


{{Code|code=
{{Code|code=
tor=Part.makeTorus(10, 5, Base.Vector(0,0,0), Base.Vector(0,0,1), 0, 360, 180)
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 360, 180)
}}
}}


The above code will create a semi torus; only the last parameter is changed.
The above code will create a semi torus; only the last parameter is changed.
i.e the angle and remaining angles are defaults. Giving the angle 180 will
i.e the remaining angles are defaults. Giving the angle 180 will
create the torus from 0 to 180, that is, a half torus.
create the torus from 0 to 180, that is, a half torus.


[[#top|top]]
==== Creating a box or cuboid ====

Using '''makeBox(length,width,height,[pnt,dir])'''.
===Create a box or cuboid===
By default pnt=Vector(0,0,0) and dir=Vector(0,0,1).

Using {{incode|makeBox(length, width, height, [pnt, dir])}}.
By default pnt = Vector(0, 0, 0) and dir = Vector(0, 0, 1).


{{Code|code=
{{Code|code=
box = Part.makeBox(10,10,10)
box = Part.makeBox(10, 10, 10)
len(box.Vertexes)
len(box.Vertexes)
> 8
> 8
}}
}}


[[#top|top]]
==== Creating a Sphere ====

Using '''makeSphere(radius,[pnt, dir, angle1,angle2,angle3])'''. By default
===Create a sphere===
pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=-90, angle2=90 and angle3=360.

Using {{incode|makeSphere(radius, [pnt, dir, angle1, angle2, angle3])}}. By default
pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = -90, angle2 = 90 and angle3 = 360.
angle1 and angle2 are the vertical minimum and maximum of the sphere, angle3
angle1 and angle2 are the vertical minimum and maximum of the sphere, angle3
is the sphere diameter.
is the sphere diameter.
Line 455: Line 641:
{{Code|code=
{{Code|code=
sphere = Part.makeSphere(10)
sphere = Part.makeSphere(10)
hemisphere = Part.makeSphere(10,Base.Vector(0,0,0),Base.Vector(0,0,1),-90,90,180)
hemisphere = Part.makeSphere(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), -90, 90, 180)
}}
}}


[[#top|top]]
==== Creating a Cylinder ====

Using '''makeCylinder(radius,height,[pnt,dir,angle])'''. By default
===Create a cylinder===
pnt=Vector(0,0,0),dir=Vector(0,0,1) and angle=360.

Using {{incode|makeCylinder(radius, height, [pnt, dir, angle])}}. By default
pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
{{Code|code=
{{Code|code=
cylinder = Part.makeCylinder(5,20)
cylinder = Part.makeCylinder(5, 20)
partCylinder = Part.makeCylinder(5,20,Base.Vector(20,0,0),Base.Vector(0,0,1),180)
partCylinder = Part.makeCylinder(5, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)
}}
}}
[[#top|top]]
==== Creating a Cone ====

Using '''makeCone(radius1,radius2,height,[pnt,dir,angle])'''. By default
===Create a cone===
pnt=Vector(0,0,0), dir=Vector(0,0,1) and angle=360.

Using {{incode|makeCone(radius1, radius2, height, [pnt, dir, angle])}}. By default
pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
{{Code|code=
{{Code|code=
cone = Part.makeCone(10,0,20)
cone = Part.makeCone(10, 0, 20)
semicone = Part.makeCone(10,0,20,Base.Vector(20,0,0),Base.Vector(0,0,1),180)
semicone = Part.makeCone(10, 0, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)
}}
}}
[[#top|top]]
== Modifying shapes ==

==Modify shapes==

There are several ways to modify shapes. Some are simple transformation operations
There are several ways to modify shapes. Some are simple transformation operations
such as moving or rotating shapes, others are more complex, such as unioning and
such as moving or rotating shapes, others are more complex, such as unioning and
subtracting one shape from another.
subtracting one shape from another.


[[#top|top]]
=== Transform operations ===

==Transform operations==

===Translate a shape===


==== Translating a shape ====
Translating is the act of moving a shape from one place to another.
Translating is the act of moving a shape from one place to another.
Any shape (edge, face, cube, etc...) can be translated the same way:
Any shape (edge, face, cube, etc...) can be translated the same way:
{{Code|code=
{{Code|code=
myShape = Part.makeBox(2,2,2)
myShape = Part.makeBox(2, 2, 2)
myShape.translate(Base.Vector(2,0,0))
myShape.translate(Base.Vector(2, 0, 0))
}}
}}
This will move our shape "myShape" 2 units in the x direction.
This will move our shape "myShape" 2 units in the X direction.

[[#top|top]]

===Rotate a shape===


==== Rotating a shape ====
To rotate a shape, you need to specify the rotation center, the axis,
To rotate a shape, you need to specify the rotation center, the axis,
and the rotation angle:
and the rotation angle:
{{Code|code=
{{Code|code=
myShape.rotate(Vector(0,0,0),Vector(0,0,1),180)
myShape.rotate(Base.Vector(0, 0, 0),Base.Vector(0, 0, 1), 180)
}}
}}
The above code will rotate the shape 180 degrees around the Z Axis.
The above code will rotate the shape 180 degrees around the Z Axis.


[[#top|top]]
==== Generic transformations with matrixes ====

===Matrix transformations===

A matrix is a very convenient way to store transformations in the 3D
A matrix is a very convenient way to store transformations in the 3D
world. In a single matrix, you can set translation, rotation and scaling
world. In a single matrix, you can set translation, rotation and scaling
Line 502: Line 706:
{{Code|code=
{{Code|code=
myMat = Base.Matrix()
myMat = Base.Matrix()
myMat.move(Base.Vector(2,0,0))
myMat.move(Base.Vector(2, 0, 0))
myMat.rotateZ(math.pi/2)
myMat.rotateZ(math.pi/2)
}}
}}
Line 508: Line 712:
that take a vector can also take three numbers, so these two lines do the same thing:
that take a vector can also take three numbers, so these two lines do the same thing:
{{Code|code=
{{Code|code=
myMat.move(2,0,0)
myMat.move(2, 0, 0)
myMat.move(Base.Vector(2,0,0))
myMat.move(Base.Vector(2, 0, 0))
}}
}}
Once our matrix is set, we can apply it to our shape. FreeCAD provides two
Once our matrix is set, we can apply it to our shape. FreeCAD provides two
methods for doing that: transformShape() and transformGeometry(). The difference
methods for doing that: {{incode|transformShape()}} and {{incode|transformGeometry()}}. The difference
is that with the first one, you are sure that no deformations will occur (see
is that with the first one, you are sure that no deformations will occur (see
"scaling a shape" below). We can apply our transformation like this:
[[#Scaling a shape|Scaling a shape]] below). We can apply our transformation like this:
{{Code|code=
{{Code|code=
myShape.transformShape(myMat)
myShape.transformShape(myMat)
Line 522: Line 726:
myShape.transformGeometry(myMat)
myShape.transformGeometry(myMat)
}}
}}
[[#top|top]]
==== Scaling a shape ====

===Scale a shape===

Scaling a shape is a more dangerous operation because, unlike translation
Scaling a shape is a more dangerous operation because, unlike translation
or rotation, scaling non-uniformly (with different values for x, y and z)
or rotation, scaling non-uniformly (with different values for X, Y and Z)
can modify the structure of the shape. For example, scaling a circle with
can modify the structure of the shape. For example, scaling a circle with
a higher value horizontally than vertically will transform it into an
a higher value horizontally than vertically will transform it into an
ellipse, which behaves mathematically very differently. For scaling, we
ellipse, which behaves mathematically very differently. For scaling, we
can't use the transformShape, we must use transformGeometry():
cannot use the {{incode|transformShape()}}, we must use {{incode|transformGeometry()}}:
{{Code|code=
{{Code|code=
myMat = Base.Matrix()
myMat = Base.Matrix()
myMat.scale(2,1,1)
myMat.scale(2, 1, 1)
myShape=myShape.transformGeometry(myMat)
myShape=myShape.transformGeometry(myMat)
}}
}}
[[#top|top]]
=== Boolean Operations ===


==Boolean operations==
==== Subtraction ====

Subtracting a shape from another one is called "cut" in OCC/FreeCAD jargon
===Subtraction===

Subtracting a shape from another one is called "cut" in FreeCAD
and is done like this:
and is done like this:
{{Code|code=
{{Code|code=
cylinder = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0))
cylinder = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
sphere = Part.makeSphere(5,Base.Vector(5,0,0))
sphere = Part.makeSphere(5, Base.Vector(5, 0, 0))
diff = cylinder.cut(sphere)
diff = cylinder.cut(sphere)
}}
}}
[[#top|top]]
==== Intersection ====

===Intersection===

The same way, the intersection between two shapes is called "common" and is done
The same way, the intersection between two shapes is called "common" and is done
this way:
this way:
{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0))
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3,10,Base.Vector(5,0,-5),Base.Vector(0,0,1))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
common = cylinder1.common(cylinder2)
common = cylinder1.common(cylinder2)
}}
}}
[[#top|top]]
==== Union ====

===Union===

Union is called "fuse" and works the same way:
Union is called "fuse" and works the same way:
{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0))
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3,10,Base.Vector(5,0,-5),Base.Vector(0,0,1))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
fuse = cylinder1.fuse(cylinder2)
fuse = cylinder1.fuse(cylinder2)
}}
}}
[[#top|top]]
==== Section ====

A Section is the intersection between a solid shape and a plane shape.
===Section===

A "section" is the intersection between a solid shape and a plane shape.
It will return an intersection curve, a compound curve composed of edges.
It will return an intersection curve, a compound curve composed of edges.
{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0))
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3,10,Base.Vector(5,0,-5),Base.Vector(0,0,1))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
section = cylinder1.section(cylinder2)
section = cylinder1.section(cylinder2)
section.Wires
section.Wires
Line 573: Line 792:
<Edge object at 0D8F4BB0>]
<Edge object at 0D8F4BB0>]
}}
}}
[[#top|top]]
==== Extrusion ====

===Extrusion===

Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in
Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in
a solid body. Think of a circle becoming a tube by "pushing it out":
a solid body. Think of a circle becoming a tube by "pushing it out":
{{Code|code=
{{Code|code=
circle = Part.makeCircle(10)
circle = Part.makeCircle(10)
tube = circle.extrude(Base.Vector(0,0,2))
tube = circle.extrude(Base.Vector(0, 0, 2))
}}
}}
If your circle is hollow, you will obtain a hollow tube. If your circle is actually
If your circle is hollow, you will obtain a hollow tube. If your circle is actually
Line 585: Line 807:
wire = Part.Wire(circle)
wire = Part.Wire(circle)
disc = Part.Face(wire)
disc = Part.Face(wire)
cylinder = disc.extrude(Base.Vector(0,0,2))
cylinder = disc.extrude(Base.Vector(0, 0, 2))
}}
}}
[[#top|top]]
== Exploring shapes ==

==Explore shapes==

You can easily explore the topological data structure:
You can easily explore the topological data structure:
{{Code|code=
{{Code|code=
import Part
import Part
b = Part.makeBox(100,100,100)
b = Part.makeBox(100, 100, 100)
b.Wires
b.Wires
w = b.Wires[0]
w = b.Wires[0]
Line 604: Line 829:
v.Point
v.Point
}}
}}
By typing the lines above in the python interpreter, you will gain a good
By typing the lines above in the Python interpreter, you will gain a good
understanding of the structure of Part objects. Here, our makeBox() command
understanding of the structure of Part objects. Here, our {{incode|makeBox()}} command
created a solid shape. This solid, like all Part solids, contains faces.
created a solid shape. This solid, like all Part solids, contains faces.
Faces always contain wires, which are lists of edges that border the face.
Faces always contain wires, which are lists of edges that border the face.
Each face has at least one closed wire (it can have more if the face has a hole).
Each face has at least one closed wire (it can have more if the face has a hole).
In the wire, we can look at each edge separately, and inside each edge, we can
In the wire, we can look at each edge separately, and inside each edge, we can
see the vertexes. Straight edges have only two vertexes, obviously.
see the vertices. Straight edges have only two vertices, obviously.

[[#top|top]]

===Edge analysis===


=== Edge analysis ===
In case of an edge, which is an arbitrary curve, it's most likely you want to
In case of an edge, which is an arbitrary curve, it's most likely you want to
do a discretization. In FreeCAD the edges are parametrized by their lengths.
do a discretization. In FreeCAD the edges are parametrized by their lengths.
Line 618: Line 846:
{{Code|code=
{{Code|code=
import Part
import Part
box = Part.makeBox(100,100,100)
box = Part.makeBox(100, 100, 100)
anEdge = box.Edges[0]
anEdge = box.Edges[0]
print anEdge.Length
print(anEdge.Length)
}}
}}
Now you can access a lot of properties of the edge by using the length as a
Now you can access a lot of properties of the edge by using the length as a
Line 626: Line 854:
the end position 100.
the end position 100.
{{Code|code=
{{Code|code=
anEdge.tangentAt(0.0) # tangent direction at the beginning
anEdge.tangentAt(0.0) # tangent direction at the beginning
anEdge.valueAt(0.0) # Point at the beginning
anEdge.valueAt(0.0) # Point at the beginning
anEdge.valueAt(100.0) # Point at the end of the edge
anEdge.valueAt(100.0) # Point at the end of the edge
anEdge.derivative1At(50.0) # first derivative of the curve in the middle
anEdge.derivative1At(50.0) # first derivative of the curve in the middle
anEdge.derivative2At(50.0) # second derivative of the curve in the middle
anEdge.derivative2At(50.0) # second derivative of the curve in the middle
anEdge.derivative3At(50.0) # third derivative of the curve in the middle
anEdge.derivative3At(50.0) # third derivative of the curve in the middle
anEdge.centerOfCurvatureAt(50) # center of the curvature for that position
anEdge.centerOfCurvatureAt(50) # center of the curvature for that position
anEdge.curvatureAt(50.0) # the curvature
anEdge.curvatureAt(50.0) # the curvature
anEdge.normalAt(50) # normal vector at that position (if defined)
anEdge.normalAt(50) # normal vector at that position (if defined)
}}
}}
[[#top|top]]
=== Using the selection ===

Here we see now how we can use the selection the user did in the viewer.
===Use a selection===

Here we see now how we can use a selection the user did in the viewer.
First of all we create a box and show it in the viewer.
First of all we create a box and show it in the viewer.
{{Code|code=
{{Code|code=
import Part
import Part
Part.show(Part.makeBox(100,100,100))
Part.show(Part.makeBox(100, 100, 100))
Gui.SendMsgToActiveView("ViewFit")
Gui.SendMsgToActiveView("ViewFit")
}}
}}
Line 648: Line 879:
{{Code|code=
{{Code|code=
for o in Gui.Selection.getSelectionEx():
for o in Gui.Selection.getSelectionEx():
print o.ObjectName
print(o.ObjectName)
for s in o.SubElementNames:
for s in o.SubElementNames:
print "name: ",s
print("name: ", s)
for s in o.SubObjects:
for s in o.SubObjects:
print "object: ",s
print("object: ", s)
}}
}}
Select some edges and this script will calculate the length:
Select some edges and this script will calculate the length:
Line 658: Line 889:
length = 0.0
length = 0.0
for o in Gui.Selection.getSelectionEx():
for o in Gui.Selection.getSelectionEx():
for s in o.SubObjects:
for s in o.SubObjects:
length += s.Length
length += s.Length

print "Length of the selected edges:" ,length
print("Length of the selected edges: ", length)
}}
}}
[[#top|top]]
== Complete example: The OCC bottle ==

A typical example found in the
==Example: The OCC bottle==
[http://www.opencascade.com/doc/occt-6.9.0/overview/html/occt__tutorial.html#sec1 OpenCasCade Technology Tutorial]

is how to build a bottle. This is a good exercise for FreeCAD too. In fact,
A typical example found on the [https://www.opencascade.com/doc/occt-6.9.0/overview/html/occt__tutorial.html OpenCasCade Technology website] is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the {{FileName|Mod/Part}} folder) and can be called from the Python interpreter by typing:
if you follow our example below and the OCC page simultaneously, you will
see how well OCC structures are implemented in FreeCAD. The complete script
below is also included in the FreeCAD installation (inside the Mod/Part folder) and
can be called from the python interpreter by typing:
{{Code|code=
{{Code|code=
import Part
import Part
Line 676: Line 905:
Part.show(bottle)
Part.show(bottle)
}}
}}
[[#top|top]]
=== The complete script ===

Here is the complete MakeBottle script:
===The script===

For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded.
{{Code|code=
{{Code|code=
import Part, FreeCAD, math
import Part, math
from FreeCAD import Base
from FreeCAD import Base


def makeBottle(myWidth=50.0, myHeight=70.0, myThickness=30.0):
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
aPnt1=Base.Vector(-myWidth/2.,0,0)
aPnt1=Base.Vector(-myWidth / 2., 0, 0)
aPnt2=Base.Vector(-myWidth/2.,-myThickness/4.,0)
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt3=Base.Vector(0,-myThickness/2.,0)
aPnt3=Base.Vector(0, -myThickness / 2., 0)
aPnt4=Base.Vector(myWidth/2.,-myThickness/4.,0)
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt5=Base.Vector(myWidth/2.,0,0)
aPnt5=Base.Vector(myWidth / 2., 0, 0)
aArcOfCircle = Part.Arc(aPnt2,aPnt3,aPnt4)
aSegment1=Part.LineSegment(aPnt1,aPnt2)
aSegment2=Part.LineSegment(aPnt4,aPnt5)
aEdge1=aSegment1.toShape()
aEdge2=aArcOfCircle.toShape()
aEdge3=aSegment2.toShape()
aWire=Part.Wire([aEdge1,aEdge2,aEdge3])
aTrsf=Base.Matrix()
aTrsf.rotateZ(math.pi) # rotate around the z-axis
aMirroredWire=aWire.transformGeometry(aTrsf)
myWireProfile=Part.Wire([aWire,aMirroredWire])
myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0,0,myHeight)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myBody.makeFillet(myThickness/12.0,myBody.Edges)
neckLocation=Base.Vector(0,0,myHeight)
neckNormal=Base.Vector(0,0,1)
myNeckRadius = myThickness / 4.
myNeckHeight = myHeight / 10
myNeck = Part.makeCylinder(myNeckRadius,myNeckHeight,neckLocation,neckNormal)
myBody = myBody.fuse(myNeck)
faceToRemove = 0
zMax = -1.0
for xp in myBody.Faces:
try:
surf = xp.Surface
if type(surf) == Part.Plane:
z = surf.Position.z
if z > zMax:
zMax = z
faceToRemove = xp
except:
continue
myBody = myBody.makeFillet(myThickness/12.0,myBody.Edges)
return myBody


aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
el = makeBottle()
aSegment1=Part.LineSegment(aPnt1, aPnt2)
aSegment2=Part.LineSegment(aPnt4, aPnt5)

aEdge1=aSegment1.toShape()
aEdge2=aArcOfCircle.toShape()
aEdge3=aSegment2.toShape()
aWire=Part.Wire([aEdge1, aEdge2, aEdge3])

aTrsf=Base.Matrix()
aTrsf.rotateZ(math.pi) # rotate around the z-axis

aMirroredWire=aWire.copy()
aMirroredWire.transformShape(aTrsf)
myWireProfile=Part.Wire([aWire, aMirroredWire])

myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0, 0, myHeight)
myBody=myFaceProfile.extrude(aPrismVec)

myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)

neckLocation=Base.Vector(0, 0, myHeight)
neckNormal=Base.Vector(0, 0, 1)

myNeckRadius = myThickness / 4.
myNeckHeight = myHeight / 10.
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
myBody = myBody.fuse(myNeck)

return myBody

el = makeBottleTut()
Part.show(el)
Part.show(el)
}}
}}
[[#top|top]]
=== Detailed explanation ===

===Detailed explanation===
{{Code|code=
{{Code|code=
import Part, FreeCAD, math
import Part, math
from FreeCAD import Base
from FreeCAD import Base
}}
}}
We will need, of course, the Part module, but also the FreeCAD.Base module,
We will need, of course, the {{incode|Part}} module, but also the {{incode|FreeCAD.Base}} module, which contains basic FreeCAD structures like vectors and matrices.

which contains basic FreeCAD structures like vectors and matrixes.
{{Code|code=
{{Code|code=
def makeBottle(myWidth=50.0, myHeight=70.0, myThickness=30.0):
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
aPnt1=Base.Vector(-myWidth/2.,0,0)
aPnt1=Base.Vector(-myWidth / 2., 0, 0)
aPnt2=Base.Vector(-myWidth/2.,-myThickness/4.,0)
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt3=Base.Vector(0,-myThickness/2.,0)
aPnt3=Base.Vector(0, -myThickness / 2., 0)
aPnt4=Base.Vector(myWidth/2.,-myThickness/4.,0)
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt5=Base.Vector(myWidth/2.,0,0)
aPnt5=Base.Vector(myWidth / 2., 0, 0)
}}
}}

Here we define our makeBottle function. This function can be called without
Here we define our {{incode|makeBottleTut}} function. This function can be called without
arguments, like we did above, in which case default values for width, height,
arguments, like we did above, in which case default values for width, height,
and thickness will be used. Then, we define a couple of points that will be used
and thickness will be used. Then, we define a couple of points that will be used
for building our base profile.
for building our base profile.

{{Code|code=
{{Code|code=
...
aArcOfCircle = Part.Arc(aPnt2,aPnt3,aPnt4)
aSegment1=Part.LineSegment(aPnt1,aPnt2)
aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
aSegment2=Part.LineSegment(aPnt4,aPnt5)
aSegment1=Part.LineSegment(aPnt1, aPnt2)
aSegment2=Part.LineSegment(aPnt4, aPnt5)
}}
}}

Here we actually define the geometry: an arc, made of three points, and two
line segments, made of two points.
Here we define the geometry: an arc, made of three points, and two line segments, made of two points.

{{Code|code=
{{Code|code=
...
aEdge1=aSegment1.toShape()
aEdge2=aArcOfCircle.toShape()
aEdge1=aSegment1.toShape()
aEdge3=aSegment2.toShape()
aEdge2=aArcOfCircle.toShape()
aEdge3=aSegment2.toShape()
aWire=Part.Wire([aEdge1,aEdge2,aEdge3])
aWire=Part.Wire([aEdge1, aEdge2, aEdge3])
}}
}}

Remember the difference between geometry and shapes? Here we build
Remember the difference between geometry and shapes? Here we build
shapes out of our construction geometry. Three edges (edges can be straight
shapes out of our construction geometry. Three edges (edges can be straight
or curved), then a wire made of those three edges.
or curved), then a wire made of those three edges.

{{Code|code=
{{Code|code=
...
aTrsf=Base.Matrix()
aTrsf=Base.Matrix()
aTrsf.rotateZ(math.pi) # rotate around the z-axis
aTrsf.rotateZ(math.pi) # rotate around the z-axis
aMirroredWire=aWire.transformGeometry(aTrsf)

myWireProfile=Part.Wire([aWire,aMirroredWire])
aMirroredWire=aWire.copy()
aMirroredWire.transformShape(aTrsf)
myWireProfile=Part.Wire([aWire, aMirroredWire])
}}
}}

So far we have built only a half profile. Instead of building the whole profile
So far we have built only a half profile. Instead of building the whole profile
the same way, we can just mirror what we did and glue both halves together.
the same way, we can just mirror what we did and glue both halves together.
Line 781: Line 1,016:
transformations that 3D objects can undergo (move, rotate and scale).
transformations that 3D objects can undergo (move, rotate and scale).
After we create the matrix we mirror it, then we create a copy of our wire
After we create the matrix we mirror it, then we create a copy of our wire
with that transformation matrix applied to it. We now have two wires, and
and apply the transformation matrix to it. We now have two wires, and
we can make a third wire out of them, since wires are actually lists of edges.
we can make a third wire out of them, since wires are actually lists of edges.

{{Code|code=
{{Code|code=
...
myFaceProfile=Part.Face(myWireProfile)
myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0,0,myHeight)
aPrismVec=Base.Vector(0, 0, myHeight)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myBody.makeFillet(myThickness/12.0,myBody.Edges)

myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)
}}
}}

Now that we have a closed wire, it can be turned into a face. Once we have a face,
Now that we have a closed wire, it can be turned into a face. Once we have a face,
we can extrude it. In doing so, we make a solid. Then we apply a nice little
we can extrude it. In doing so, we make a solid. Then we apply a nice little
fillet to our object because we care about good design, don't we?
fillet to our object because we care about good design, don't we?
{{Code|code=
{{Code|code=
...
neckLocation=Base.Vector(0,0,myHeight)
neckNormal=Base.Vector(0,0,1)
neckLocation=Base.Vector(0, 0, myHeight)
neckNormal=Base.Vector(0, 0, 1)
myNeckRadius = myThickness / 4.

myNeckHeight = myHeight / 10
myNeckRadius = myThickness / 4.
myNeck = Part.makeCylinder(myNeckRadius,myNeckHeight,neckLocation,neckNormal)
myNeckHeight = myHeight / 10.
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
}}
}}
At this point, the body of our bottle is made, but we still need to create a neck. So we
At this point, the body of our bottle is made, but we still need to create a neck. So we
make a new solid, with a cylinder.
make a new solid, with a cylinder.
{{Code|code=
{{Code|code=
...
myBody = myBody.fuse(myNeck)
myBody = myBody.fuse(myNeck)
}}
}}
The fuse operation is very powerful. It will take care of gluing what needs to be glued and remove parts that need to be removed.
The fuse operation, which in other applications is sometimes called a union, is very
powerful. It will take care of gluing what needs to be glued and remove parts that
need to be removed.
{{Code|code=
{{Code|code=
return myBody
...
return myBody
}}
}}
Then, we return our Part solid as the result of our function.
Then, we return our Part solid as the result of our function.
{{Code|code=
{{Code|code=
el = makeBottle()
el = makeBottleTut()
Part.show(el)
Part.show(el)
}}
}}
Finally, we call the function to actually create the part, then make it visible.
Finally, we call the function to actually create the part, then make it visible.


[[#top|top]]
==Box pierced==

==Example: Pierced box==

Here is a complete example of building a pierced box.
Here is a complete example of building a pierced box.


The construction is done one side at a time; when the cube is finished, it is hollowed out by cutting a cylinder through it.
The construction is done one side at a time. When the cube is finished, it is hollowed out by cutting a cylinder through it.
{{Code|code=
{{Code|code=
import Draft, Part, FreeCAD, math, PartGui, FreeCADGui, PyQt4
import Part, math
from math import sqrt, pi, sin, cos, asin
from FreeCAD import Base
from FreeCAD import Base


size = 10
size = 10
poly = Part.makePolygon( [ (0,0,0), (size, 0, 0), (size, 0, size), (0, 0, size), (0, 0, 0)])
poly = Part.makePolygon([(0, 0, 0), (size, 0, 0), (size, 0, size), (0, 0, size), (0, 0, 0)])


face1 = Part.Face(poly)
face1 = Part.Face(poly)
Line 836: Line 1,079:
face6 = Part.Face(poly)
face6 = Part.Face(poly)
myMat = FreeCAD.Matrix()
myMat = Base.Matrix()

myMat.rotateZ(math.pi/2)
myMat.rotateZ(math.pi / 2)
face2.transformShape(myMat)
face2.transformShape(myMat)
face2.translate(FreeCAD.Vector(size, 0, 0))
face2.translate(Base.Vector(size, 0, 0))


myMat.rotateZ(math.pi/2)
myMat.rotateZ(math.pi / 2)
face3.transformShape(myMat)
face3.transformShape(myMat)
face3.translate(FreeCAD.Vector(size, size, 0))
face3.translate(Base.Vector(size, size, 0))


myMat.rotateZ(math.pi/2)
myMat.rotateZ(math.pi / 2)
face4.transformShape(myMat)
face4.transformShape(myMat)
face4.translate(FreeCAD.Vector(0, size, 0))
face4.translate(Base.Vector(0, size, 0))


myMat = FreeCAD.Matrix()
myMat = Base.Matrix()

myMat.rotateX(-math.pi/2)
myMat.rotateX(-math.pi / 2)
face5.transformShape(myMat)
face5.transformShape(myMat)


face6.transformShape(myMat)
face6.transformShape(myMat)
face6.translate(FreeCAD.Vector(0,0,size))
face6.translate(Base.Vector(0, 0, size))

myShell = Part.makeShell([face1,face2,face3,face4,face5,face6])


myShell = Part.makeShell([face1, face2, face3, face4, face5, face6])
mySolid = Part.makeSolid(myShell)
mySolid = Part.makeSolid(myShell)
mySolidRev = mySolid.copy()
mySolidRev.reverse()


myCyl = Part.makeCylinder(2,20)
myCyl = Part.makeCylinder(2, 20)
myCyl.translate(FreeCAD.Vector(size/2, size/2, 0))
myCyl.translate(Base.Vector(size / 2, size / 2, 0))


cut_part = mySolidRev.cut(myCyl)
cut_part = mySolid.cut(myCyl)


Part.show(cut_part)
Part.show(cut_part)
}}
}}
[[#top|top]]
== Loading and Saving ==
There are several ways to save your work in the Part module. You can
of course save your FreeCAD document, but you can also save Part
objects directly to common CAD formats, such as BREP, IGS, STEP and STL.


==Loading and saving==
Saving a shape to a file is easy. There are exportBrep(), exportIges(),

exportStl() and exportStep() methods available for all shape objects.
There are several ways to save your work. You can of course save your FreeCAD document, but you can also save [[Part_workbench|Part]] objects directly to common CAD formats, such as BREP, IGS, STEP and STL.

Saving a shape to a file is easy. There are {{incode|exportBrep()}}, {{incode|exportIges()}}, {{incode|exportStep()}} and {{incode|exportStl()}} methods available for all shape objects.
So, doing:
So, doing:
{{Code|code=
{{Code|code=
import Part
import Part
s = Part.makeBox(0,0,0,10,10,10)
s = Part.makeBox(10, 10, 10)
s.exportStep("test.stp")
s.exportStep("test.stp")
}}
}}
will save our box into a STEP file. To load a BREP,
will save our box into a STEP file. To load a BREP, IGES or STEP file:
IGES or STEP file:
{{Code|code=
{{Code|code=
import Part
import Part
Line 889: Line 1,130:
s.read("test.stp")
s.read("test.stp")
}}
}}
To convert an '''.stp''' file to an '''.igs''' file:
To convert a STEP file to an IGS file:
{{Code|code=
{{Code|code=
import Part
import Part
Line 896: Line 1,137:
s.exportIges("file.igs") # outbound file igs
s.exportIges("file.igs") # outbound file igs
}}
}}
[[#top|top]]
Note that importing or opening BREP, IGES or STEP files can also be done
directly from the File → Open or File → Import menu, while exporting
can be done with File → Export.

{{docnav|Mesh Scripting|Mesh to Part}}

{{Userdocnavi}}

[[Category:Poweruser Documentation/de]]

[[Category:Python Code]]


{{Docnav
|[[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]]
|[[Mesh_Scripting|Mesh Scripting]]
}}


{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}
{{clear}}

Revision as of 20:58, 22 June 2020

Mesh Scripting/de
Mesh to Part/de

Hier erklären wir Dir, wie Du das Part Module/de direkt aus dem FreeCAD Python Interpreter oder von einem externen Skript aus steuern kannst. Die Grundlagen des Topologischen Datenskripts sind unter Part Arbeitsbereich Erläuterung des Konzepts beschrieben. Achte darauf, den Abschnitt Scripting/de und die Seiten FreeCAD Scripting Basics/de zu durchsuchen, wenn Du weitere Informationen darüber benötigst, wie Python Skripten in FreeCAD funktioniert.

Here we will explain to you how to control the Part Module directly from the FreeCAD Python interpreter, or from any external script. The basics about Topological data scripting are described in Part Module Explaining the concepts. Be sure to browse the Scripting section and the FreeCAD_Scripting_Basics pages if you need more information about how Python scripting works in FreeCAD. If you are new to Python, it is a good idea to first read the Introduction to Python.

Klassen Diagramm

Dies ist ein Unified Modeling Language (UML) Überblick über die wesentlichen Klassen des Part Arbweitsbereichs:

Python Klassen des Part Arbeitsbereichs
Python Klassen des Part Arbeitsbereichs

This is a Unified Modeling Language (UML) overview of the most important classes of the Part module: Python classes of the Part module

top

Geometrie

Die geometrischen Objekte sind die Bausteine aller topologischen Objekte:

  • Geom Basisklasse der geometrischen Objekte
  • Line Eine gerade Linie im Raum, definiert durch den Start- und Endpunkt
  • Circle Kreis oder Kreissegment definiert durch einen Mittelpunkt und einen Start- und Endpunkt
  • ...... Und demnächst mehr davon

The geometric objects are the building blocks of all topological objects:

  • Geom Base class of the geometric objects.
  • Line A straight line in 3D, defined by starting point and end point.
  • Circle Circle or circle segment defined by a center point and start and end point.
  • ...... Etc.

top

Topologie

The folgenden topologischen Datentypen stehen zur Verfügung:

  • Compound Eine Gruppe von beliebigen topologischen Objekten.
  • Compsolid Ein zusammengesetzter Körper (solid) ist ein Set von Körpern, die durch ihre Seiten verbunden sind. Dies erweitert das Konzept von WIRE and SHELL auf Körpern (solids).
  • Solid Ein Teil des Raumes, der durch eine geschlossene dreidimensionale Hülle begrenzt ist.
  • Shell Hülle = Ein Satz von über ihre Kanten verbundenen Flächen. Eine Hülle kann offen oder geschlossen sein.
  • Face Im zweidimensionalen ist es ein Teil einer Ebene; im dreidimensionalen ist es ein Teil einer Oberfläche. Die Form ist durch Konturen begrenzt (getrimmt). Auch im 3D gekrümmte Flächen haben sind Inneren zweidimensional parametriert.
  • Wire Ein Satz von über ihre Endpunkten verknüpften Kanten. Ein "Wire" kann eine offene oder geschlossene Form haben, je nach dem ob nicht verknüpfte Endpunkte vorhanden sind oder nicht.
  • Edge Ein topologisches Element (Kante) das mit einer beschränkten Kurve korrespondiert. Eine Kante ist generell durch Vertexe begrenzt. Eine Kante ist eindimensional.
  • Vertex Ein topologisches Element das mit einem Punkt korrespondiert. Es ist nulldimensional.
  • Shape Ein generischer Term für all die zuvor aufgezählten Elemente.

The following topological data types are available:

  • Compound A group of any type of topological objects.
  • Compsolid A composite solid is a set of solids connected by their faces. It expands the notions of WIRE and SHELL to solids.
  • Solid A part of space limited by shells. It is three dimensional.
  • Shell A set of faces connected by their edges. A shell can be open or closed.
  • Face In 2D it is part of a plane; in 3D it is part of a surface. Its geometry is constrained (trimmed) by contours. It is two dimensional.
  • Wire A set of edges connected by their vertices. It can be an open or closed contour depending on whether the edges are linked or not.
  • Edge A topological element corresponding to a restrained curve. An edge is generally limited by vertices. It has one dimension.
  • Vertex A topological element corresponding to a point. It has zero dimension.
  • Shape A generic term covering all of the above.

top

Kurzes Beispiel: Erstellung einer einfachen Topologie

Wire

Wir werden nun eine Topologie erstellen, indem wir sie aus einer einfacheren Geometrie konstruieren. Als Fallstudie verwenden wir ein Teil, wie im Bild zu sehen, das aus vier Knoten, zwei Kreise und zwei Linien besteht.

top

Erstellen der Geometrie

Zuerst müssen wir die verschiedenen geometrischen Teile dieses Drahtes erstellen. Und wir müssen darauf achten, dass die Scheitelpunkte der geometrischen Teile an der gleichen Stelle sind . Sonst wären wir später vielleicht nicht mehr in der Lage, die geometrischen Teile zu einer Topologie zu verbinden!

First we create the distinct geometric parts of this wire. Making sure that parts that have to be connected later share the same vertices.

Also erstellen wir zuerst die Punkte:

import Part
from FreeCAD import Base
V1 = Base.Vector(0, 10, 0)
V2 = Base.Vector(30, 10, 0)
V3 = Base.Vector(30, -10, 0)
V4 = Base.Vector(0, -10, 0)

top

Bogen

Circle


Um einen Kreisbogen zu erzeugen, machen wir einen Hilfspunkt und erzeugen den Kreisbogen durch drei Punkte:

VC1 = Base.Vector(-10, 0, 0)
C1 = Part.Arc(V1, VC1, V4)
VC2 = Base.Vector(40, 0, 0)
C2 = Part.Arc(V2, VC2, V3)

top

Linie

Line


Das Liniensegment kann sehr einfach aus den Punkten erstellt werden:

L1 = Part.LineSegment(V1, V2)
L2 = Part.LineSegment(V3, V4)

top

Alles zusammensetzen

Der letzte Schritt besteht darin, die geometrischen Basiselemente zusammenzusetzen und eine topologische Form backen:

The last step is to put the geometric base elements together and bake a topological shape:

S1 = Part.Shape([C1, L1, C2, L2])

top

Ein Prisma herstellen

Extrudiere nun den Draht in eine Richtung und erstelle eine echte 3D Form:

Now extrude the wire in a direction and make an actual 3D shape:

W = Part.Wire(S1.Edges)
P = W.extrude(Base.Vector(0, 0, 10))

top

Alles anzeigen

Part.show(P)

top

Erstellen von Grundformen

Du kannst ganz einfach topologische Grundobjekte mit den "make....()" Methoden aus dem Arbeitsbereich Part erstellen:

You can easily create basic topological objects with the make...() methods from the Part Module:

b = Part.makeBox(100, 100, 100)
Part.show(b)

Andere verfügbare make....() Methoden:

  • makeBox(l,w,h): Erstellt einen Quader, der sich in p befindet und in die Richtung d mit den Abmessungen (l,w,h) zeigt.
  • makeCircle(radius): Erstellt einen Kreis mit einem gegebenen Radius.
  • makeCone(radius1,radius2,height): Erstellt einen Kegel mit den gegebenen Radien und Höhen.
  • makeCylinder(radius,height): Erstellt einen Zylinder mit einem gegebenen Radius und Höhe.
  • makeLine((x1,y1,z1),(x2,y2,z2))): Erstellt eine Linie aus zwei Punkten.
  • makePlane(length,width): Erstellt eine Ebene mit Länge und Breite.
  • makePolygon(list): Erstellt ein Polygon aus einer Liste von Punkten.
  • makeSphere(radius): Erstellt eine Kugel mit einem bestimmten Radius.
  • makeTorus(radius1,radius2): Erstellt einen Torus mit den gegebenen Radien.

Siehe die Part API Seite für eine vollständige Liste der verfügbaren Methoden des Part Arbeitsbereichs.

top

Import der notwendigen Module

Zuerst müssen wir den Part Arbeitsbereich importieren, damit wir seinen Inhalt in Python verwenden können. Wir werden auch das Basismodul aus dem FreeCAD Modul importieren:

First we need to import the Part module so we can use its contents in Python. We'll also import the Base module from inside the FreeCAD module:

import Part
from FreeCAD import Base

top

Erstellen eines Vektors

Vektoren sind einer der am häufigsten verwendeten wichtigen Informationsteile beim Bau von Formen. Sie enthalten in der Regel drei Zahlen (aber nicht notwendigerweise immer): die kartesischen Koordinaten x, y und z. Du erstelltst einen Vektor wie diesen:

Vectors are one of the most important pieces of information when building shapes. They usually contain three numbers (but not necessarily always): the X, Y and Z cartesian coordinates. You create a vector like this:

myVector = Base.Vector(3, 2, 0)

Wir haben gerade einen Vektor mit den Koordinaten x=3, y=2, z=0 erstellt. Im Part Arbeitsbereich , werden Vektoren überall verwendet. Teileformen verwenden auch eine andere Art von Punkt Darstellung namens Vertex, die lediglich ein Behälter für einen Vektor ist. Du greifst auf den Vektor eines Knoten wie folgt zu:

myVertex = myShape.Vertexes[0]
print myVertex.Point
> Vector (3, 2, 0)

top

Erstellen einer Kante

Eine Kante ist nichts anderes als eine Linie mit zwei Knoten:

An edge is nothing but a line with two vertices:

edge = Part.makeLine((0, 0, 0), (10, 0, 0))
edge.Vertexes
> [<Vertex object at 01877430>, <Vertex object at 014888E0>]

Hinweis: Du kannst auch eine Kante erzeugen, indem du zwei Vektoren übergibst:

vec1 = Base.Vector(0, 0, 0)
vec2 = Base.Vector(10, 0, 0)
line = Part.LineSegment(vec1, vec2)
edge = line.toShape()

Du kannst die Länge und den Mittelpunkt einer Kante wie diese finden:

edge.Length
> 10.0
edge.CenterOfMass
> Vector (5, 0, 0)

top

Die Form auf den Bildschirm bringen

Bisher haben wir ein Kantenobjekt erstellt, das aber nirgendwo auf dem Bildschirm erscheint. Das liegt daran, dass die FreeCAD 3D Szene nur das anzeigt, was du ihm sagst, dass er anzeigen soll. Um das zu tun, verwenden wir folgende einfache Methode:

So far we created an edge object, but it doesn't appear anywhere on the screen. This is because the FreeCAD 3D scene only displays what you tell it to display. To do that, we use this simple method:

Part.show(edge)

Die Anzeigefunktion erzeugt ein Objekt in unserem FreeCAD Dokument und weist unsere "Kantenform" ihm zu. Verwende dies, wenn es an der Zeit ist, deine Erstellung auf dem Bildschirm anzuzeigen.

top

Erstellen eines Drahts

Ein Draht ist eine Mehrkantenlinie und kann aus einer Liste von Kanten oder sogar einer Liste von Drähten erstellt werden:

A wire is a multi-edge line and can be created from a list of edges or even a list of wires:

edge1 = Part.makeLine((0, 0, 0), (10, 0, 0))
edge2 = Part.makeLine((10, 0, 0), (10, 10, 0))
wire1 = Part.Wire([edge1, edge2]) 
edge3 = Part.makeLine((10, 10, 0), (0, 10, 0))
edge4 = Part.makeLine((0, 10, 0), (0, 0, 0))
wire2 = Part.Wire([edge3, edge4])
wire3 = Part.Wire([wire1, wire2])
wire3.Edges
> [<Edge object at 016695F8>, <Edge object at 0197AED8>, <Edge object at 01828B20>, <Edge object at 0190A788>]
Part.show(wire3)

Part.show(Draht3) zeigt die 4 Kanten, die unseren Draht bilden. Sonstige nützliche Informationen können leicht abgerufen werden:

wire3.Length
> 40.0
wire3.CenterOfMass
> Vector (5, 5, 0)
wire3.isClosed()
> True
wire2.isClosed()
> False

top

Erstellen einer Fläche

Nur Flächen, die aus geschlossenen Drähten erstellt wurden, sind gültig. In diesem Beispiel ist Draht3 ein geschlossener Draht, aber Draht2 ist kein geschlossener Draht (siehe oben).

Only faces created from closed wires will be valid. In this example, wire3 is a closed wire but wire2 is not (see above):

face = Part.Face(wire3)
face.Area
> 99.99999999999999
face.CenterOfMass
> Vector (5, 5, 0)
face.Length
> 40.0
face.isValid()
> True
sface = Part.Face(wire2)
face.isValid()
> False

Nur Flächen haben eine Grundfläche, keine Drähte oder Kanten.

top

Erstellen eines Kreises

So einfach kann ein Kreis erstellt werden:

A circle can be created like this:

circle = Part.makeCircle(10)
circle.Curve
> Circle (Radius : 10, Position : (0, 0, 0), Direction : (0, 0, 1))

Wenn Du ihn an einer bestimmten Stelle und mit einer bestimmten Richtung erzeugen möchtest:

ccircle = Part.makeCircle(10, Base.Vector(10, 0, 0), Base.Vector(1, 0, 0))
ccircle.Curve
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0))

Der Kreis wird im Abstand 10 vom x Ursprung erstellt und ist nach außen entlang der x Achse gerichtet. Hinweis: erzeugeKreis akzeptiert nur Base.Vector() für die Position und normale Parameter, nicht Tupel. Du kannst auch einen Teil des Kreises durch Angabe eines Anfangs- und eines Endwinkels erstellen:

from math import pi
arc1 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)
arc2 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 180, 360)

Sowohl arc1 als auch arc2 bilden gemeinsam einen Kreis. Winkel sollten angegeben werden in Grad; wenn Du Bogenmaß hast, wandelst Du sie einfach mit der Formel um: Grad = Bogenmaß * 180/PI oder mit dem Mathematikmodul von python (nach dem ausführen von import math, natürlich):

import math
degrees = math.degrees(radians)

top

Erstellen eines Bogens entlang von Punkten

Leider gibt es keine makeArc-Funktion, aber wir haben die Part.Arc Funktion um einen Bogen durch drei Punkte zu erzeugen. Es erzeugt ein Bogenobjekt das den Startpunktes mit dem Endpunkt durch den Mittelpunkt verbindet. Die .toShape() Funktion des Bogenobjekts muss aufgerufen werden, um ein Kantenobjekt zu erhalten, wie bei der Verwendung von Part.LineSegment anstelle von Part.makeLine.

Unfortunately there is no makeArc() function, but we have the Part.Arc() function to create an arc through three points. It creates an arc object joining the start point to the end point through the middle point. The arc object's toShape() function must be called to get an edge object, the same as when using Part.LineSegment instead of Part.makeLine.

arc = Part.Arc(Base.Vector(0, 0, 0), Base.Vector(0, 5, 0), Base.Vector(5, 5, 0))
arc
> <Arc object>
arc_edge = arc.toShape()
Part.show(arc_edge)

Arc akzeptiert nur Base.Vector() für Punkte, aber nicht für Tupel. arc_edge ist das, was wir wollen, was wir durch Verwendung von Part.show(arc_edge) anzeigen können. Du kannst einen Bogen auch unter Verwendung eines Kreisabschnitts erhalten:

from math import pi
circle = Part.Circle(Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 10)
arc = Part.Arc(circle,0,pi)

Bögen sind gültige Kanten wie Linien, so dass sie auch in Drähten verwendet werden können.

top

Erstellen eines Polygons

Ein Polygon ist einfach ein Draht mit mehreren geraden Kanten. Die makePolygon Funktion nimmt eine Liste von Punkten und erstellt einen Draht durch diese Punkte:

A polygon is simply a wire with multiple straight edges. The makePolygon() function takes a list of points and creates a wire through those points:

lshape_wire = Part.makePolygon([Base.Vector(0, 5, 0), Base.Vector(0, 0, 0), Base.Vector(5, 0, 0)])

top

Erstellen einer Bézier Kurve

Bézier Kurven werden verwendet, um sanfte Kurven mit einer Reihe von Polen (Punkten) und optionalen Gewichten zu modellieren. Die folgende Funktion erstellt eine Part.BezierCurve aus einer Reihe von FreeCAD.Vector Punkten. (Hinweis: Wenn du einen einzelnen Pol oder ein Gewicht "erhältst" und "einstellst", beginnen die Indizes bei 1, nicht bei 0.)

Bézier curves are used to model smooth curves using a series of poles (points) and optional weights. The function below makes a Part.BezierCurve() from a series of FreeCAD.Vector() points. Note: when "getting" and "setting" a single pole or weight, indices start at 1, not 0.

def makeBCurveEdge(Points):
   geomCurve = Part.BezierCurve()
   geomCurve.setPoles(Points)
   edge = Part.Edge(geomCurve)
   return(edge)

top

Erstellen einer Ebene

Eine Ebene ist einfach eine flache rechteckige Fläche. Die Methode, mit der eine solche erstellt wird, ist makePlane(length,width,[start_pnt,dir_normal])'. Standardmäßig start_pnt = Vektor(0,0,0,0) und dir_normal = Vektor(0,0,1). Verwendung von dir_normal = Vector(0,0,0,1) erzeugt die Ebene, die in Richtung der positiven z-Achse zeigt, während dir_normal = Vector(1,0,0,0) die Ebene erzeugt. Ebene, die in Richtung der positiven x-Achse zeigt:

A Plane is a flat rectangular surface. The method used to create one is makePlane(length, width, [start_pnt, dir_normal]). By default start_pnt = Vector(0, 0, 0) and dir_normal = Vector(0, 0, 1). Using dir_normal = Vector(0, 0, 1) will create the plane facing in the positive Z axis direction, while dir_normal = Vector(1, 0, 0) will create the plane facing in the positive X axis direction:

plane = Part.makePlane(2, 2)
plane
> <Face object at 028AF990>
plane = Part.makePlane(2, 2, Base.Vector(3, 0, 0), Base.Vector(0, 1, 0))
plane.BoundBox
> BoundBox (3, 0, 0, 5, 0, 2)

BoundBox ist ein Quader, der die Ebene mit einer Diagonale beginnend bei (3,0,0,0) und endet bei (5,0,2) einschliesst. Hier ist die BoundBox Dicke entlang der y-Achse Null, zumal unsere Form völlig flach ist.

Hinweis: makePlane akzeptiert nur Base.Vector() für start_pnt und dir_normal, nicht aber Tupel.

top

Erstellen einer Ellipse

Es gibt mehrere Möglichkeiten, eine Ellipse zu erstellen:

There are several ways to create an ellipse:

Part.Ellipse()

Erzeugt eine Ellipse mit Hauptradius 2 und Nebenradius 1 mit dem Mittelpunkt bei (0,0,0).

Part.Ellipse(Ellipse)

Erstellt eine Kopie der angegebenen Ellipse.

Part.Ellipse(S1, S2, Center)

Erzeugt eine Ellipse, die auf den Punkt Mitte zentriert ist, an dem die Ebene der Ellipse definiert ist durch Mittelpunkt, S1 und S2, ihre Hauptachse definiert ist durch Mittelpunkt und S1, sein Hauptradius ist der Abstand zwischen Mittelpunkt und S1, und sein kleiner Radius ist der Abstand zwischen S2 und der Hauptachse.

Part.Ellipse(Center, MajorRadius, MinorRadius)

Erstellt eine Ellipse mit Haupt- und Nebenradien Hauptradius und Nebenradius, die sich in der durch den Mittelpunkt definierten Ebene und der Normalen (0,0,1) befinden.

eli = Part.Ellipse(Base.Vector(10, 0, 0), Base.Vector(0, 5, 0), Base.Vector(0, 0, 0))
Part.show(eli.toShape())

Im obigen Code haben wir S1, S2 und Mitte überschritten. Ähnlich wie Arc, erzeugt Ellipse ein Ellipsenobjekt, aber keine Kante, also müssen wir es in eine Kante mit toShape() zur Anzeige konvertieren.

Hinweis: Arc akzeptiert nur Base.Vector() für Punkte, nicht aber für Tupel.

eli = Part.Ellipse(Base.Vector(0, 0, 0), 10, 5)
Part.show(eli.toShape())

für den obigen Ellipsenkonstruktor haben wir Mitte, MajorRadius und MinorRadius überschritten.

top

Create a torus

Using makeTorus(radius1, radius2, [pnt, dir, angle1, angle2, angle]). By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = 0, angle2 = 360 and angle = 360. Consider a torus as small circle sweeping along a big circle. Radius1 is the radius of the big circle, radius2 is the radius of the small circle, pnt is the center of the torus and dir is the normal direction. angle1 and angle2 are angles in degrees for the small circle; the last angle parameter is to make a section of the torus:

torus = Part.makeTorus(10, 2)

The above code will create a torus with diameter 20 (radius 10) and thickness 4 (small circle radius 2)

tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)

The above code will create a slice of the torus.

tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 360, 180)

The above code will create a semi torus; only the last parameter is changed. i.e the remaining angles are defaults. Giving the angle 180 will create the torus from 0 to 180, that is, a half torus.

top

Create a box or cuboid

Using makeBox(length, width, height, [pnt, dir]). By default pnt = Vector(0, 0, 0) and dir = Vector(0, 0, 1).

box = Part.makeBox(10, 10, 10)
len(box.Vertexes)
> 8

top

Create a sphere

Using makeSphere(radius, [pnt, dir, angle1, angle2, angle3]). By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = -90, angle2 = 90 and angle3 = 360. angle1 and angle2 are the vertical minimum and maximum of the sphere, angle3 is the sphere diameter.

sphere = Part.makeSphere(10)
hemisphere = Part.makeSphere(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), -90, 90, 180)

top

Create a cylinder

Using makeCylinder(radius, height, [pnt, dir, angle]). By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.

cylinder = Part.makeCylinder(5, 20)
partCylinder = Part.makeCylinder(5, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)

top

Create a cone

Using makeCone(radius1, radius2, height, [pnt, dir, angle]). By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.

cone = Part.makeCone(10, 0, 20)
semicone = Part.makeCone(10, 0, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)

top

Modify shapes

There are several ways to modify shapes. Some are simple transformation operations such as moving or rotating shapes, others are more complex, such as unioning and subtracting one shape from another.

top

Transform operations

Translate a shape

Translating is the act of moving a shape from one place to another. Any shape (edge, face, cube, etc...) can be translated the same way:

myShape = Part.makeBox(2, 2, 2)
myShape.translate(Base.Vector(2, 0, 0))

This will move our shape "myShape" 2 units in the X direction.

top

Rotate a shape

To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle:

myShape.rotate(Base.Vector(0, 0, 0),Base.Vector(0, 0, 1), 180)

The above code will rotate the shape 180 degrees around the Z Axis.

top

Matrix transformations

A matrix is a very convenient way to store transformations in the 3D world. In a single matrix, you can set translation, rotation and scaling values to be applied to an object. For example:

myMat = Base.Matrix()
myMat.move(Base.Vector(2, 0, 0))
myMat.rotateZ(math.pi/2)

Note: FreeCAD matrixes work in radians. Also, almost all matrix operations that take a vector can also take three numbers, so these two lines do the same thing:

myMat.move(2, 0, 0)
myMat.move(Base.Vector(2, 0, 0))

Once our matrix is set, we can apply it to our shape. FreeCAD provides two methods for doing that: transformShape() and transformGeometry(). The difference is that with the first one, you are sure that no deformations will occur (see Scaling a shape below). We can apply our transformation like this:

myShape.transformShape(myMat)

or

myShape.transformGeometry(myMat)

top

Scale a shape

Scaling a shape is a more dangerous operation because, unlike translation or rotation, scaling non-uniformly (with different values for X, Y and Z) can modify the structure of the shape. For example, scaling a circle with a higher value horizontally than vertically will transform it into an ellipse, which behaves mathematically very differently. For scaling, we cannot use the transformShape(), we must use transformGeometry():

myMat = Base.Matrix()
myMat.scale(2, 1, 1)
myShape=myShape.transformGeometry(myMat)

top

Boolean operations

Subtraction

Subtracting a shape from another one is called "cut" in FreeCAD and is done like this:

cylinder = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
sphere = Part.makeSphere(5, Base.Vector(5, 0, 0))
diff = cylinder.cut(sphere)

top

Intersection

The same way, the intersection between two shapes is called "common" and is done this way:

cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
common = cylinder1.common(cylinder2)

top

Union

Union is called "fuse" and works the same way:

cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
fuse = cylinder1.fuse(cylinder2)

top

Section

A "section" is the intersection between a solid shape and a plane shape. It will return an intersection curve, a compound curve composed of edges.

cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
section = cylinder1.section(cylinder2)
section.Wires
> []
section.Edges
> [<Edge object at 0D87CFE8>, <Edge object at 019564F8>, <Edge object at 0D998458>, 
 <Edge  object at 0D86DE18>, <Edge object at 0D9B8E80>, <Edge object at 012A3640>, 
 <Edge object at 0D8F4BB0>]

top

Extrusion

Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in a solid body. Think of a circle becoming a tube by "pushing it out":

circle = Part.makeCircle(10)
tube = circle.extrude(Base.Vector(0, 0, 2))

If your circle is hollow, you will obtain a hollow tube. If your circle is actually a disc with a filled face, you will obtain a solid cylinder:

wire = Part.Wire(circle)
disc = Part.Face(wire)
cylinder = disc.extrude(Base.Vector(0, 0, 2))

top

Explore shapes

You can easily explore the topological data structure:

import Part
b = Part.makeBox(100, 100, 100)
b.Wires
w = b.Wires[0]
w
w.Wires
w.Vertexes
Part.show(w)
w.Edges
e = w.Edges[0]
e.Vertexes
v = e.Vertexes[0]
v.Point

By typing the lines above in the Python interpreter, you will gain a good understanding of the structure of Part objects. Here, our makeBox() command created a solid shape. This solid, like all Part solids, contains faces. Faces always contain wires, which are lists of edges that border the face. Each face has at least one closed wire (it can have more if the face has a hole). In the wire, we can look at each edge separately, and inside each edge, we can see the vertices. Straight edges have only two vertices, obviously.

top

Edge analysis

In case of an edge, which is an arbitrary curve, it's most likely you want to do a discretization. In FreeCAD the edges are parametrized by their lengths. That means you can walk an edge/curve by its length:

import Part
box = Part.makeBox(100, 100, 100)
anEdge = box.Edges[0]
print(anEdge.Length)

Now you can access a lot of properties of the edge by using the length as a position. That means if the edge is 100mm long the start position is 0 and the end position 100.

anEdge.tangentAt(0.0)          # tangent direction at the beginning
anEdge.valueAt(0.0)            # Point at the beginning
anEdge.valueAt(100.0)          # Point at the end of the edge
anEdge.derivative1At(50.0)     # first derivative of the curve in the middle
anEdge.derivative2At(50.0)     # second derivative of the curve in the middle
anEdge.derivative3At(50.0)     # third derivative of the curve in the middle
anEdge.centerOfCurvatureAt(50) # center of the curvature for that position
anEdge.curvatureAt(50.0)       # the curvature
anEdge.normalAt(50)            # normal vector at that position (if defined)

top

Use a selection

Here we see now how we can use a selection the user did in the viewer. First of all we create a box and show it in the viewer.

import Part
Part.show(Part.makeBox(100, 100, 100))
Gui.SendMsgToActiveView("ViewFit")

Now select some faces or edges. With this script you can iterate over all selected objects and their sub elements:

for o in Gui.Selection.getSelectionEx():
    print(o.ObjectName)
    for s in o.SubElementNames:
        print("name: ", s)
        for s in o.SubObjects:
            print("object: ", s)

Select some edges and this script will calculate the length:

length = 0.0
for o in Gui.Selection.getSelectionEx():
    for s in o.SubObjects:
        length += s.Length

print("Length of the selected edges: ", length)

top

Example: The OCC bottle

A typical example found on the OpenCasCade Technology website is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the Mod/Part folder) and can be called from the Python interpreter by typing:

import Part
import MakeBottle
bottle = MakeBottle.makeBottle()
Part.show(bottle)

top

The script

For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded.

import Part, math
from FreeCAD import Base

def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
    aPnt1=Base.Vector(-myWidth / 2., 0, 0)
    aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
    aPnt3=Base.Vector(0, -myThickness / 2., 0)
    aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
    aPnt5=Base.Vector(myWidth / 2., 0, 0)

    aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
    aSegment1=Part.LineSegment(aPnt1, aPnt2)
    aSegment2=Part.LineSegment(aPnt4, aPnt5)

    aEdge1=aSegment1.toShape()
    aEdge2=aArcOfCircle.toShape()
    aEdge3=aSegment2.toShape()
    aWire=Part.Wire([aEdge1, aEdge2, aEdge3])

    aTrsf=Base.Matrix()
    aTrsf.rotateZ(math.pi) # rotate around the z-axis

    aMirroredWire=aWire.copy()
    aMirroredWire.transformShape(aTrsf)
    myWireProfile=Part.Wire([aWire, aMirroredWire])

    myFaceProfile=Part.Face(myWireProfile)
    aPrismVec=Base.Vector(0, 0, myHeight)
    myBody=myFaceProfile.extrude(aPrismVec)

    myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)

    neckLocation=Base.Vector(0, 0, myHeight)
    neckNormal=Base.Vector(0, 0, 1)

    myNeckRadius = myThickness / 4.
    myNeckHeight = myHeight / 10.
    myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
    myBody = myBody.fuse(myNeck)

    return myBody

el = makeBottleTut()
Part.show(el)

top

Detailed explanation

import Part, math
from FreeCAD import Base

We will need, of course, the Part module, but also the FreeCAD.Base module, which contains basic FreeCAD structures like vectors and matrices.

def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
    aPnt1=Base.Vector(-myWidth / 2., 0, 0)
    aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
    aPnt3=Base.Vector(0, -myThickness / 2., 0)
    aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
    aPnt5=Base.Vector(myWidth / 2., 0, 0)

Here we define our makeBottleTut function. This function can be called without arguments, like we did above, in which case default values for width, height, and thickness will be used. Then, we define a couple of points that will be used for building our base profile.

...
    aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
    aSegment1=Part.LineSegment(aPnt1, aPnt2)
    aSegment2=Part.LineSegment(aPnt4, aPnt5)

Here we define the geometry: an arc, made of three points, and two line segments, made of two points.

...
    aEdge1=aSegment1.toShape()
    aEdge2=aArcOfCircle.toShape()
    aEdge3=aSegment2.toShape()
    aWire=Part.Wire([aEdge1, aEdge2, aEdge3])

Remember the difference between geometry and shapes? Here we build shapes out of our construction geometry. Three edges (edges can be straight or curved), then a wire made of those three edges.

...
    aTrsf=Base.Matrix()
    aTrsf.rotateZ(math.pi) # rotate around the z-axis

    aMirroredWire=aWire.copy()
    aMirroredWire.transformShape(aTrsf)
    myWireProfile=Part.Wire([aWire, aMirroredWire])

So far we have built only a half profile. Instead of building the whole profile the same way, we can just mirror what we did and glue both halves together. We first create a matrix. A matrix is a very common way to apply transformations to objects in the 3D world, since it can contain in one structure all basic transformations that 3D objects can undergo (move, rotate and scale). After we create the matrix we mirror it, then we create a copy of our wire and apply the transformation matrix to it. We now have two wires, and we can make a third wire out of them, since wires are actually lists of edges.

...
    myFaceProfile=Part.Face(myWireProfile)
    aPrismVec=Base.Vector(0, 0, myHeight)
    myBody=myFaceProfile.extrude(aPrismVec)

    myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)

Now that we have a closed wire, it can be turned into a face. Once we have a face, we can extrude it. In doing so, we make a solid. Then we apply a nice little fillet to our object because we care about good design, don't we?

...
    neckLocation=Base.Vector(0, 0, myHeight)
    neckNormal=Base.Vector(0, 0, 1)

    myNeckRadius = myThickness / 4.
    myNeckHeight = myHeight / 10.
    myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)

At this point, the body of our bottle is made, but we still need to create a neck. So we make a new solid, with a cylinder.

...
    myBody = myBody.fuse(myNeck)

The fuse operation is very powerful. It will take care of gluing what needs to be glued and remove parts that need to be removed.

...
    return myBody

Then, we return our Part solid as the result of our function.

el = makeBottleTut()
Part.show(el)

Finally, we call the function to actually create the part, then make it visible.

top

Example: Pierced box

Here is a complete example of building a pierced box.

The construction is done one side at a time. When the cube is finished, it is hollowed out by cutting a cylinder through it.

import Part, math
from FreeCAD import Base

size = 10
poly = Part.makePolygon([(0, 0, 0), (size, 0, 0), (size, 0, size), (0, 0, size), (0, 0, 0)])

face1 = Part.Face(poly)
face2 = Part.Face(poly)
face3 = Part.Face(poly)
face4 = Part.Face(poly)
face5 = Part.Face(poly)
face6 = Part.Face(poly)
     
myMat = Base.Matrix()

myMat.rotateZ(math.pi / 2)
face2.transformShape(myMat)
face2.translate(Base.Vector(size, 0, 0))

myMat.rotateZ(math.pi / 2)
face3.transformShape(myMat)
face3.translate(Base.Vector(size, size, 0))

myMat.rotateZ(math.pi / 2)
face4.transformShape(myMat)
face4.translate(Base.Vector(0, size, 0))

myMat = Base.Matrix()

myMat.rotateX(-math.pi / 2)
face5.transformShape(myMat)

face6.transformShape(myMat)               
face6.translate(Base.Vector(0, 0, size))

myShell = Part.makeShell([face1, face2, face3, face4, face5, face6])   
mySolid = Part.makeSolid(myShell)

myCyl = Part.makeCylinder(2, 20)
myCyl.translate(Base.Vector(size / 2, size / 2, 0))

cut_part = mySolid.cut(myCyl)

Part.show(cut_part)

top

Loading and saving

There are several ways to save your work. You can of course save your FreeCAD document, but you can also save Part objects directly to common CAD formats, such as BREP, IGS, STEP and STL.

Saving a shape to a file is easy. There are exportBrep(), exportIges(), exportStep() and exportStl() methods available for all shape objects. So, doing:

import Part
s = Part.makeBox(10, 10, 10)
s.exportStep("test.stp")

will save our box into a STEP file. To load a BREP, IGES or STEP file:

import Part
s = Part.Shape()
s.read("test.stp")

To convert a STEP file to an IGS file:

import Part
 s = Part.Shape()
 s.read("file.stp")       # incoming file igs, stp, stl, brep
 s.exportIges("file.igs") # outbound file igs

top