Skrypty w środowisku Część

From FreeCAD Documentation
Revision as of 18:49, 6 July 2023 by Kaktus (talk | contribs) (Created page with "Linia to w rzeczywistości odcinek, stąd punkt początkowy i końcowy.")

Wprowadzenie

Główną strukturą danych używaną w środowisku Część jest typ danych BRep z OpenCASCADE. Prawie cała zawartość i typy obiektów środowiska Część są dostępne w skryptach Python. Obejmuje to geometryczne elementy pierwotne, takie jak Linie, Okręgi i Łuki, a także całą gamę Kształtów Topologicznych, takich jak Wierzchołki, Krawędzie, Polilinie, Ściany, Bryły i Złożenia. Dla każdego z tych obiektów istnieje kilka metod tworzenia, a dla niektórych z nich, zwłaszcza Kształtów Topologicznych, dostępne są również zaawansowane operacje, takie jak operacje logiczne połączenie / różnica / przecięcie. Zapoznaj się z zawartością środowiska Część, jak opisano na stronie Podstawy tworzenia skryptów FreeCAD, aby dowiedzieć się więcej.

Najbardziej podstawowym obiektem, który można utworzyć jest cecha, który posiada prostą właściwość DANEUmiejscowienie, oraz podstawowe cechy określające jego kolor i wygląd.

Innym prostym obiektem używanym w obiektach geometrycznych 2D jest obiekt Część na obiekt 2D, który jest podstawą szkicu, oraz większości rysunków roboczych.

Zobacz również

Skrypt testowy

Przetestuj tworzenie elementów pierwotnych za pomocą skryptu.

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

Skrypt ten znajduje się w katalogu instalacyjnym programu i może być badany w celu sprawdzenia, jak budowane są podstawowe elementy pierwotne.

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

Przykłady

Linia

Aby utworzyć element linii, przełącz się na konsolę Python i wpisz:

import FreeCAD as App
import Part

doc = App.newDocument()

line = Part.LineSegment()
line.StartPoint = (0.0, 0.0, 0.0)
line.EndPoint = (1.0, 1.0, 1.0)
obj = doc.addObject("Part::Feature", "Line")
obj.Shape= line.toShape()

doc.recompute()

Prześledźmy powyższy przykład Pyton krok po kroku:

import FreeCAD as App
import Part
doc = App.newDocument()

Spowoduje to załadowanie modułów FreeCAD i Część oraz utworzenie nowego dokumentu.

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

Linia to w rzeczywistości odcinek, stąd punkt początkowy i końcowy.

obj = doc.addObject("Part::Feature", "Line")
obj.Shape= line.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 use a geometric primitive (the Part.LineSegment) to create a TopoShape out of it (with the toShape() method). Only shapes can be added to the document. In FreeCAD geometric 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 also 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 also be created using:

import FreeCAD as App
import Part

def my_create_line(pt1, pt2, obj_name):
    obj = App.ActiveDocument.addObject("Part::Line", obj_name)
    obj.X1 = pt1[0]
    obj.Y1 = pt1[1]
    obj.Z1 = pt1[2]

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

    App.ActiveDocument.recompute()
    return obj

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

Circle

A circle can be created in a similar way:

import FreeCAD as App
import Part

doc = App.activeDocument()

circle = Part.Circle() 
circle.Radius = 10.0  
obj = doc.addObject("Part::Feature", "Circle")
obj.Shape = circle.toShape()

doc.recompute()

Or using:

import FreeCAD as App
import Part

def my_create_circle(rad, obj_name):
    obj = App.ActiveDocument.addObject("Part::Circle", obj_name)
    obj.Radius = rad

    App.ActiveDocument.recompute()
    return obj

circle = my_create_circle(5.0, "CircleName")

Alternatively we can create a circle by defining its center, axis and radius:

import FreeCAD as App
import Part

doc = App.activeDocument()

center = App.Vector(1, 2, 3)
axis = App.Vector(1, 1, 1)
radius = 10
circle = Part.Circle(center, axis, radius)
obj = doc.addObject("Part::Feature", "Circle")
obj.Shape = circle.toShape()

doc.recompute()

Or by defining three points on its circumference:

import FreeCAD as App
import Part

doc = App.activeDocument()

p1 = App.Vector(10, 0, 0)
p2 = App.Vector(0, 10, 0)
p3 = App.Vector(0, 0, 10)
circle = Part.Circle(p1, p2, p3)
obj = doc.addObject("Part::Feature", "Circle")
obj.Shape = circle.toShape()

doc.recompute()

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

shape = obj.Shape
edge = shape.Edges[0]
curve = edge.Curve

Here we take the Shape of our object obj and then its list of Edges. In this case there will be only one edge because we made the shape out of a single circle. So we take only the first item in the Edges list, and then take its curve. Every edge has a Curve, which is the geometric primitive it is based on.

Arc

An arc can be created like this:

import FreeCAD as App
import Part

doc = App.activeDocument()

p1 = App.Vector(10, 0, 0)
p2 = App.Vector(0, 10, 0)
p3 = App.Vector(-10, 0, 0)
arc = Part.Arc(p1, p2, p3)
obj = doc.addObject("Part::Feature", "Arc")
obj.Shape = arc.toShape()

doc.recompute()

This draws a half circle. The center is at (0, 0, 0). The radius is 10. P1 is the start point on +X axis. P2 is the middle point on +Y axis and P3 is the end point on -X axis.

We can also create an arc from a circle:

import FreeCAD as App
import Part

doc = App.activeDocument()

p1 = App.Vector(10, 0, 0)
p2 = App.Vector(0, 10, 0)
p3 = App.Vector(-10, 0, 0)
circle = Part.Circle(p1, p2, p3)
arc = Part.ArcOfCircle(circle, 0.0, 0.7854)
obj = doc.addObject("Part::Feature", "Arc")
obj.Shape = arc.toShape()

doc.recompute()

It needs a circle, and a start angle and end angle in radians.