Difference between revisions of "Part Module"

From FreeCAD Documentation
Jump to: navigation, search
(included Werner's excellent explanations from the forum)
Line 1: Line 1:
The Part module deals with the CAD data structures.
+
== Introduction ==
  
At the moment the CAD capabilites of FreeCAD are very limited but it  already supports a few basic CAD primitives like plane, box, cylinder, cone, sphere and torus. For all these primitives a user interface exists to create such elements. Moreover there are several additional primitives for which no user interface exists but that can be created over their Python interface. These primitives are circle and line. More primitives will follow.
+
the CAD capabilities of FreeCAD are based on the [http://en.wikipedia.org/wiki/Open_CASCADE OpenCasCade] kernel. The Part module distinguishes between geometric primitives and (topological) shapes. A geometric primitive can be a point, a line, a circle, a plane, etc. or even some more complex types like a B-Spline curve or surface. In OpenCasCADE (OCC) dialect, a shape can be a vertex, an edge, a wire, a face, solid, a compound, etc.
  
== Basic data structures ==
+
To get a complete list of all of them refer to the [http://www.opencascade.org/org/doc/ OCC documentation] and search for Geom_Geometry and TopoDS_Shape. There you can also read what are the differences between geometric objects and shapes. Please note that unfortunately the OCC documentation is not available online (you must download an archive) and is mostly aimed at programmers, not at end-users. But hopefully you'll find enough information to get started here.
The main data structure in FreeCAD is the [http://en.wikipedia.org/wiki/Boundary_representation BRep] data type based on OpenCascade.  
+
 
To get the basics see: [[Topological data scripting]]
+
The geometric types actually can be divided into two major groups: curves and surfaces. Out of the curves (line, circle, ...) you can directly build an edge, out of the surfaces (plane, cylinder, ...) a face can be built. For example, the geometric primitive line is unlimited, i.e. it is defined by a base vector and a direction vector while its shape representation must be something limited by a start and end point. And a box -- a solid -- can be created by six limited planes.
 +
 
 +
From an edge or face you can also go back to its geometric primitive counter part.
 +
 
 +
Thus, out of shapes you can build very complex parts or, the other way round, extract all sub-shape a more complex shape is made of.
  
 
== User interface ==
 
== User interface ==
  
To create primitives over the user interface switch to the Part design workbench and click on 'Create primitives...' menu item under the Part menu. This opens a dialog where your are able to define the type of primitive and define further properties depending on the type you have chosen. Then click on the 'Create' button to create such an element.
+
To create primitives from the user interface switch to the Part design workbench and click on 'Create primitives...' menu item under the Part menu. This opens a dialog where you are able to define the type of primitive and further properties depending on the type you have chosen. Then click on the 'Create' button to create such an element.
  
== Command line ==
+
The Part module is at the moment intended primarily to make its objects available to other parts of FreeCAD, so it has very few user interface elements on its own.
  
There are two further primitives that can only be created over the command line. So far there does not exists a user interface for them.
+
== Scripting ==
  
To create a line element switch to the Python console and type in
+
The main data structure used in the Part module is the [http://en.wikipedia.org/wiki/Boundary_representation BRep] data type from OpenCascade.
 +
About all contents and object types of the Part module are now available to python scripting. This includes geometric primitives, such as Line and Circle (or Arc), and the whole range of TopoShapes, like Vertexes, Edges, Wires, Faces, Solids and Compounds. For each of those objects, several creations methods exist, and for some of them, especially the TopoShapes, advanced operations like booleans union/difference/intersection are also available. Explore the contents of the Part module, as described in the [[FreeCAD Scripting Basics]] page, to know more.
 +
 
 +
== Examples ==
 +
 
 +
To create a line element switch to the Python console and type in:
  
 
<code python>
 
<code python>
Line 28: Line 37:
 
</code>
 
</code>
  
A circle can be created in a similar way
+
Let's go through the above python example step by step:
 +
 
 +
<code python>
 +
import Part,PartGui
 +
doc=App.newDocument()
 +
</code>
 +
 
 +
loads the Part module and creates a new document
 +
 
 +
<code python>
 +
l=Part.Line()
 +
l.StartPoint=(0.0,0.0,0.0)
 +
l.EndPoint=(1.0,1.0,1.0)
 +
</code>
 +
 
 +
Line is actually a line segment, hence the start and endpoint.
 +
 
 +
<code python>
 +
doc.addObject("Part::Feature","Line").Shape=l.toShape()
 +
</code>
 +
 
 +
This adds a Part object type to the document and assigns the shape representation of the line segment to the 'Shape' property of the added object. It is important to understand here that we used a geometric primitive (the Part.line) to create a TopoShape out of it (the toShape() method). Only Shapes can be added to the document. In FreeCAD, geometry primitives are used as "building structures" for Shapes.
 +
 
 +
<code python>
 +
doc.recompute()
 +
</code>
 +
 
 +
Updates the document. This also prepare the visual representation of the new part object.
 +
 
 +
Note that a Line can be created by specifiying its start and endpoint directly in the constructor, for ex. Part.Line(point1,point2) or we can create a default line and set its properties afterwards, like we did here.
 +
 
 +
A circle can be created in a similar way:
  
 
<code python>
 
<code python>
Line 36: Line 76:
 
c.Radius=10.0   
 
c.Radius=10.0   
 
f = doc.addObject("Part::Feature", "Circle")
 
f = doc.addObject("Part::Feature", "Circle")
f.Shape = c.toShape() # create a shape out of the geometry and assign it the shape property of the feature
+
f.Shape = c.toShape()
 
doc.recompute()
 
doc.recompute()
 
</code>
 
</code>
 +
 +
Note again, we used the circle (geometry primitive) to construct a shape out of it. We can of course still access our construction geometry afterwards, by doing:
 +
 +
<code python>
 +
s = f.Shape
 +
e = s.Edges[0]
 +
c = e.Curve
 +
</code>
 +
 +
Here we take the shape of our object f, then we take its list of edges, in this case there will be only one because we made the whole shape out of a single circle, so we take only the first item of the Edges list, and we takes its curve. Every Edge has a Curve, which is the geometry primitive it is based on.
 +
 +
Head to the [[Topological data scripting]] page if you would like to know more.

Revision as of 22:53, 29 December 2008

Introduction

the CAD capabilities of FreeCAD are based on the OpenCasCade kernel. The Part module distinguishes between geometric primitives and (topological) shapes. A geometric primitive can be a point, a line, a circle, a plane, etc. or even some more complex types like a B-Spline curve or surface. In OpenCasCADE (OCC) dialect, a shape can be a vertex, an edge, a wire, a face, solid, a compound, etc.

To get a complete list of all of them refer to the OCC documentation and search for Geom_Geometry and TopoDS_Shape. There you can also read what are the differences between geometric objects and shapes. Please note that unfortunately the OCC documentation is not available online (you must download an archive) and is mostly aimed at programmers, not at end-users. But hopefully you'll find enough information to get started here.

The geometric types actually can be divided into two major groups: curves and surfaces. Out of the curves (line, circle, ...) you can directly build an edge, out of the surfaces (plane, cylinder, ...) a face can be built. For example, the geometric primitive line is unlimited, i.e. it is defined by a base vector and a direction vector while its shape representation must be something limited by a start and end point. And a box -- a solid -- can be created by six limited planes.

From an edge or face you can also go back to its geometric primitive counter part.

Thus, out of shapes you can build very complex parts or, the other way round, extract all sub-shape a more complex shape is made of.

User interface

To create primitives from the user interface switch to the Part design workbench and click on 'Create primitives...' menu item under the Part menu. This opens a dialog where you are able to define the type of primitive and further properties depending on the type you have chosen. Then click on the 'Create' button to create such an element.

The Part module is at the moment intended primarily to make its objects available to other parts of FreeCAD, so it has very few user interface elements on its own.

Scripting

The main data structure used in the Part module is the BRep data type from OpenCascade. About all contents and object types of the Part module are now available to python scripting. This includes geometric primitives, such as Line and Circle (or Arc), and the whole range of TopoShapes, like Vertexes, Edges, Wires, Faces, Solids and Compounds. For each of those objects, several creations methods exist, and for some of them, especially the TopoShapes, advanced operations like booleans union/difference/intersection are also available. Explore the contents of the Part module, as described in the FreeCAD Scripting Basics page, to know more.

Examples

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

import Part,PartGui doc=App.newDocument()

  1. add a line element to the document and set its points

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

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

import Part,PartGui doc=App.newDocument()

loads the Part module and creates a new document

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

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

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

This adds a Part object type to the document and assigns the shape representation of the line segment to the 'Shape' property of the added object. It is important to understand here that we used a geometric primitive (the Part.line) to create a TopoShape out of it (the toShape() method). Only Shapes can be added to the document. In FreeCAD, geometry primitives are used as "building structures" for Shapes.

doc.recompute()

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

Note that a Line can be created by specifiying its start and endpoint directly in the constructor, for ex. Part.Line(point1,point2) or we can create a default line and set its properties afterwards, like we did here.

A circle can be created in a similar way:

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

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

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

Here we take the shape of our object f, then we take its list of edges, in this case there will be only one because we made the whole shape out of a single circle, so we take only the first item of the Edges list, and we takes its curve. Every Edge has a Curve, which is the geometry primitive it is based on.

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