Script Part

From FreeCAD Documentation
Revision as of 12:01, 10 August 2019 by David69 (talk | contribs) (Created page with "charge l'atelier Part (Pièce) et crée un nouveau document")
Jump to navigation Jump to search
Other languages:
English • ‎français • ‎italiano

Script

La structure de données principale utilisée dans le module Part est le type de données [1] de OpenCascade. Presque tous les contenus et types d'objet du module Part sont disponibles à l'aide de scripts Python. Cela inclut les primitives géométriques, telles que Line et Circle (ou Arc), et toute la gamme de TopoShapes, telles que les vertex, les arêtes, les fils, les faces, les solides et les composés. Plusieurs méthodes de création existent pour chacun de ces objets. Pour certaines d’entre elles, en particulier TopoShapes, des opérations avancées telles que union booléenne/différence/intersection booléenne sont également disponibles. Explorez le contenu du module Part comme décrit dans la page Débuter avec les scripts pour en savoir plus.

Exemples

Pour créer un élément ligne, passer à la console Python et taper :

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

Passons en revue l'exemple Python ci-dessus étape par étape:

import Part,PartGui
doc=App.newDocument()

charge l'atelier Part (Pièce) et crée un nouveau document

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

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

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

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

doc.recompute()

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

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

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