Part scripting/de: Difference between revisions

From FreeCAD Documentation
(Created page with "* Topologisches Daten Skripten * OpenCASCADE")
(Created page with "==Testskript==")
Line 17: Line 17:
* [[OpenCASCADE/de|OpenCASCADE]]
* [[OpenCASCADE/de|OpenCASCADE]]


==Test script==
==Testskript==


Test the creation of [[Part_Primitives|Part Primitives]] with a script. {{Version|0.19}}
Test the creation of [[Part_Primitives|Part Primitives]] with a script. {{Version|0.19}}

Revision as of 22:42, 31 August 2020

Einführung

Die im Modul Part verwendete Hauptdatenstruktur ist der Datentyp BRep von OpenCascade. Fast alle Inhalte und Objekttypen des Part Moduls sind per Python Skripten verfügbar. Dazu gehören geometrische Primitive wie Linie und Kreis (oder Bogen) und die gesamte Palette der TopoFormen, wie Knoten, Kanten, Drähte, Flächen, Festkörper und Verbünde. Für jedes dieser Objekte gibt es mehrere Erstellungsmethoden, und für einige von ihnen, insbesondere die TopoFormen, sind auch fortgeschrittene Operationen wie Bool'sche Vereinigung/Differenz/Überschneidung verfügbar. Entdecke den Inhalt des Part Moduls, wie auf der Seite FreeCAD Grundlagen SKripten beschrieben, um mehr zu erfahren.

Das grundlegendste Objekt, das erstellt werden kann, ist ein Part Formelement, das eine einfache Daten-EigenschaftPlatzierungs Eigenschaft und Grundeigenschaften hat, um seine Farbe und sein Aussehen zu definieren.

Ein weiteres einfaches Objekt, das in geometrischen 2D Objekten verwendet wird, ist Part Part2DObjekt, das die Basis von Skizzierer SkizzeObjekt ist. (Skizzierer), und die meisten Entwurfselemente.

Siehe auch

Testskript

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

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

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

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

Examples

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

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

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

import Part,PartGui
doc=App.newDocument()

loads the Part module and creates a new document

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

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

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

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

doc.recompute()

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

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

A Line can be created also using:

import FreeCAD
import Part
DOC = FreeCAD.newDocument()

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

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

    DOC.recompute()
    return obj

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

A circle can be created in a similar way:

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

or using:

import FreeCAD
import Part
DOC = FreeCAD.newDocument()

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

    DOC.recompute()
    return obj

circle = mycreateCircle(5.0, "CircleName")

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.