Manual:Creating and manipulating geometry/pl

W poprzednich rozdziałach dowiedzieliśmy się o różnych środowiskach pracy programu FreeCAD oraz o tym, jak każde z nich stosuje swoje własne narzędzia i typy geometrii. Ta sama koncepcja obowiązuje przy pracy z kodem Pythona.

Zauważyliśmy również, że zdecydowana większość środowisk pracy FreeCAD zależy od bardzo fundamentalnego elementu: Środowiska pracy Part. W rzeczywistości, wiele innych środowisk pracy, takich jak Draft i Arch, wykonuje dokładnie to, co zrobimy w tym rozdziale: używa kodu Python do tworzenia i manipulowania geometrią Części.

So the first thing we need to do to work with Part geometry, is to do the Python equivalent to switching to the Part Workbench: import the Part module:

Take a minute to explore the contents of the Part module, by typing Part. and browsing through the different methods available. The Part module offers several convenience functions such as makeBox, makeCircle, etc... which will instantly build an object for you. Try this, for example:

When you press Enter after typing the line above, nothing will appear in the 3D view, but something like this will be printed on the Python Console:

This is where an important concept takes place. What we created here is a Part Shape. It is not a FreeCAD document object (yet). In FreeCAD, objects and their geometry are independent. Think of a FreeCAD document object as a container, that will host a shape. Parametric objects will also have properties such as Length and Width, and will recalculate their Shape on-the-fly, whenever one of the properties changes. What we did here is calculate a shape manually.

We can now easily create a "generic" document object in the current document (make sure you have at least one new document open), and give it a box shape like the one we just made:

Note how we handled myObj.Shape, notice that it is done exactly like we did it in the previous chapter, when we changed other properties of an object, such as  box.Height = 5. In fact, Shape is also a property, just like Height. Only it takes a Part Shape, not a number. In the next chapter we will have a better look at how these parametric objects are constructed.

For now, let's explore our Part Shapes in more detail. At the end of the chapter about traditional modeling with the Part Workbench we showed a table that explains how Part Shapes are constructed, and their different components (Vertices, edges, faces, etc). The exact same components exist here and can be retrieved from Python. Part Shapes always have the following attributes: Vertexes, Edges, Wires, Faces, Shells and Solids. All of them are lists, that can contain any number of elements or be empty:

For example, let's find the area of each face of our box shape above:

Or, for each edge, its start point and end point:

As you see, if our boxShape has a "Vertexes" attribute, each Edge of the boxShape also has a "Vertexes" attribute. As we can expect, the boxShape will have 8 vertices, while the edge will only have 2, which are both part of the list of 8.

We can always check what is the type of a shape:

So to resume the topic of Part Shapes: Everything starts with Vertices. With one or two vertices, you form an Edge (full circles have only one vertex). With one or more Edges, you form a Wire. With one or more closed Wires, you form a Face (the additional Wires become "holes" in the Face). With one or more Faces, you form a Shell. When a Shell is fully closed (watertight), you can form a Solid from it. And finally, you can join any number of Shapes of any types together, which is then called a Compound.

We can now try creating complex shapes from scratch, by constructing all their components one by one. For example, let's try to create a volume like this:



We will start by creating a planar shape like this:



First, let's create the four base points:

Then we can create the two linear segments:



Note that we didn't need to create Vertices. We could immediately create Part.LineSegments from FreeCAD Vectors. This is because here we haven't created Edges yet. A Part.LineSegment (as well as Part.Circle, Part.Arc, Part.Ellipse or Part.BSpline) does not create an Edge, but rather a base geometry on which an Edge will be created. Edges are always made from such a base geometry, which is stored in its Curve attribute. So if you have an Edge, doing:

will show you what kind of Edge it is, i.e. if it's based on a line, an arc, etc... But let's come back to our exercise, and build the arc segments. For this, we will need a third point, so we can use the convenient Part.Arc, which takes 3 points:



Now we have 2 lines (L1 and L2) and 2 arcs (C1 and C2). We need to turn them into edges:

Alternatively, base geometries also have a toShape function that do exactly the same thing:

Once we have a series of Edges, we can now form a Wire, by giving it a list of Edges. We do need to take care of the order.

And we can check if our Wire was correctly understood, and that it is correctly closed:

Który wydrukuje lub. Aby wykonać powierzchnię, potrzebujemy zamkniętej obwiedni, więc zawsze warto to sprawdzić przed wykonaniem powierzchni. Teraz możemy stworzyć taką powierzchnię, nadając jej pojedynczą obwiednię (lub listę obwiedni, jeśli chcemy uzyskać otwory):

Potem go wyciągamy:

Należy pamiętać, że P jest już bryłą:

To dlatego, że kiedy wyciągamy pojedynczą powierzchnię, zawsze otrzymujemy bryłę. Nie miałoby to miejsca, na przykład, gdybyśmy zamiast tego wyciągnęli obwiednię:

Co oczywiście da nam pustą powłokę, z brakującą górną i dolną powierzchnią.

Teraz, gdy mamy nasz ostateczny kształt, chcemy go zobaczyć na ekranie! Stwórzmy więc ogólny obiekt, i przypiszmy do niego naszą nową bryłę:

Alternatywnie, Środowisko pracy Part dostarcza również skrót, który przyspiesza powyższą operację (ale nie możesz wybrać nazwy obiektu):

Wszystko to, oraz wiele więcej, zostało szczegółowo wyjaśnione na stronie Skrypty danych topologicznych, wraz z przykładami.

Więcej informacji:


 * Środowisko pracy Part
 * Skrypty danych topologicznych