Topological data scripting/ru: Difference between revisions

From FreeCAD Documentation
No edit summary
(Updating to match new version of source page)
 
(40 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
<languages/>

{{Docnav/ru
|[[Part_scripting/ru|Part scripting]]
|[[Scripted_objects/ru|Программируемые объекты]]
}}


{{TOCright}}
{{TOCright}}


<span id="Introduction"></span>
==Введение==
==Введение==


Здесь мы объясним вам, как управлять [[Part_Workbench/ru|верстаком Part]] непосредственно из интерпретатора Python FreeCAD или из любого внешнего сценария. Основы создания сценариев топологических данных описаны в [[Part_Workbench/ru#Explaining_the_concepts|объяснение концепции модуля Part]]. Обязательно просмотрите раздел [[Scripting/ru|Scripting]] и страницы [[FreeCAD_Scripting_Basics/ru|Основы скриптинга FreeCAD]], если вам нужна дополнительная информация о том, как работает python-скриптинг во FreeCAD .
Здесь мы объясним вам, как управлять [[Part_Workbench/ru|верстаком Part]] непосредственно из интерпретатора Python FreeCAD или из любого внешнего сценария. Основы создания сценариев топологических данных описаны в [[Part_Workbench/ru#Explaining_the_concepts|объяснение концепции модуля Part]]. Обязательно просмотрите раздел [[Scripting/ru|Scripting]] и страницы [[FreeCAD_Scripting_Basics/ru|Основы скриптинга FreeCAD]], если вам нужна дополнительная информация о том, как работает python-скриптинг во FreeCAD .


<span id="See_also"></span>
===Смотрите также===
===Смотрите также===


Line 12: Line 19:
* [[OpenCASCADE|OpenCASCADE]]
* [[OpenCASCADE|OpenCASCADE]]


<span id="Class_diagram"></span>
<div class="mw-translate-fuzzy">
=== Диаграмма Классов ===
==Диаграмма классов==

Это обзор наиболее важных классов модуля Part через [http://en.wikipedia.org/wiki/Unified_Modeling_Language Unified Modeling Language (UML)]:
Это обзор наиболее важных классов модуля Part через [http://en.wikipedia.org/wiki/Unified_Modeling_Language Unified Modeling Language (UML)]:
[[Image:Part_Classes.jpg|center|Классы Python, содержащиеся в модуле Part]]
[[Image:Part_Classes.jpg|center|Классы Python, содержащиеся в модуле Part]]
{{Top}}
</div>
<span id="Geometry"></span>
===Геометрия===


This is a [http://en.wikipedia.org/wiki/Unified_Modeling_Language Unified Modeling Language (UML)] overview of the most important classes of the Part module:
[[Image:Part_Classes.jpg|Python classes of the Part module]]
{{Top}}
<div class="mw-translate-fuzzy">
=== Геометрия ===
Геометрические объекты являются строительными блоками для всех топологических объектов:
Геометрические объекты являются строительными блоками для всех топологических объектов:
* '''GEOM''' Базовый класс геометрических объектов
* '''Geom''' Базовый класс геометрических объектов.
* '''LINE''' Прямая линия в 3D, задается начальной и конечной точкой
* '''Line''' Прямая линия в 3D, задается начальной и конечной точкой.
* '''CIRCLE''' Окружность или дуга задается центром, начальной и конечной точкой
* '''Circle''' Окружность или дуга задается центром, начальной и конечной точкой.
* и так далее...
* '''......''' И вскоре еще немного
</div>

The geometric objects are the building blocks of all topological objects:
* '''Geom''' Base class of the geometric objects.
* '''Line''' A straight line in 3D, defined by starting point and end point.
* '''Circle''' Circle or circle segment defined by a center point and start and end point.
* Etc.
{{Top}}
{{Top}}
<span id="Topology"></span>
===Топология===
===Топология===


<div class="mw-translate-fuzzy">
Доступны нижеследующие топологические типы данных:
Доступны нижеследующие топологические типы данных:
* '''COMPOUND''' Группа из топологических объектов любого типа.
* '''COMPOUND''' Группа из топологических объектов любого типа.
Line 48: Line 48:
* '''VERTEX''' Топологический элемент соответствующий точке. Обладает нулевой размерность.
* '''VERTEX''' Топологический элемент соответствующий точке. Обладает нулевой размерность.
* '''SHAPE''' общий термин охватывающий все выше сказанное.
* '''SHAPE''' общий термин охватывающий все выше сказанное.
</div>
{{Top}}
{{Top}}
<span id="Example:_Create_simple_topology"></span>
==Примеры: Создание простейшей топологии==
==Примеры: Создание простейшей топологии==


Line 58: Line 60:
</div>
</div>
{{Top}}
{{Top}}
<span id="Create_geometry"></span>
===Создание геометрии===
===Создание геометрии===


Line 68: Line 71:


{{Code|code=
{{Code|code=
import FreeCAD as App
import Part
import Part
V1 = App.Vector(0, 10, 0)
from FreeCAD import Base
V1 = Base.Vector(0, 10, 0)
V2 = App.Vector(30, 10, 0)
V2 = Base.Vector(30, 10, 0)
V3 = App.Vector(30, -10, 0)
V3 = Base.Vector(30, -10, 0)
V4 = App.Vector(0, -10, 0)
V4 = Base.Vector(0, -10, 0)
}}
}}
{{Top}}
{{Top}}
<span id="Arc"></span>
===Дуга===
===Дуга===


Line 84: Line 88:


{{Code|code=
{{Code|code=
VC1 = Base.Vector(-10, 0, 0)
VC1 = App.Vector(-10, 0, 0)
C1 = Part.Arc(V1, VC1, V4)
C1 = Part.Arc(V1, VC1, V4)
VC2 = Base.Vector(40, 0, 0)
VC2 = App.Vector(40, 0, 0)
C2 = Part.Arc(V2, VC2, V3)
C2 = Part.Arc(V2, VC2, V3)
}}
}}
{{Top}}
{{Top}}
<span id="Line"></span>
===Линия===
===Линия===


Line 102: Line 107:
}}
}}
{{Top}}
{{Top}}
<span id="Put_it_all_together"></span>
===Соединяем все вместе===
===Соединяем все вместе===


Line 112: Line 118:
}}
}}
{{Top}}
{{Top}}
<span id="Make_a_prism"></span>
<div class="mw-translate-fuzzy">
==== Создание призмы ====
===Создание призмы===

Теперь вытягиваем ломанную по направлению и фактически получаем 3D форму:
Теперь вытягиваем ломанную по направлению и фактически получаем 3D форму:
</div>

Now extrude the wire in a direction and make an actual 3D shape:


{{Code|code=
{{Code|code=
W = Part.Wire(S1.Edges)
W = Part.Wire(S1.Edges)
P = W.extrude(Base.Vector(0, 0, 10))
P = W.extrude(App.Vector(0, 0, 10))
}}
}}
{{Top}}
{{Top}}
<span id="Show_it_all"></span>
===Показать всё===
===Показать всё===


Line 130: Line 135:
}}
}}
{{Top}}
{{Top}}
<span id="Create_basic_shapes"></span>
<div class="mw-translate-fuzzy">
== Создание основных фигур ==
==Создание простых фигур==
Вы легко можете создать базовый топологический объект с помощью методов "make...()" содержащихся в модуле Part:
</div>


Вы легко можете создавать простые топологические объекты с помощью методов {{incode|make...()}} содержащихся в модуле Part:
You can easily create basic topological objects with the {{incode|make...()}} methods from the Part module:


{{Code|code=
{{Code|code=
Line 157: Line 160:
</div>
</div>
{{Top}}
{{Top}}
<span id="Import_modules"></span>
===Импорт необходимых модулей===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
В начале нам нужно импортировать модуль Part, чтобы мы могли использовать его содержимое в Python. Также импортируем модуль Base из модуля FreeCAD:
==== Импорт необходимых модулей ====
Сначала нам нужно импортировать модуль Part, чтобы мы могли использовать его содержимое в python.
Также импортируем модуль Base из модуля FreeCAD:
</div>
</div>

First we need to import the Part module so we can use its contents in Python. We'll also import the Base module from inside the FreeCAD module:


{{Code|code=
{{Code|code=
import FreeCAD as App
import Part
import Part
from FreeCAD import Base
}}
}}
{{Top}}
{{Top}}
<span id="Create_a_vector"></span>
===Создание вектора===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
[http://en.wikipedia.org/wiki/Euclidean_vector Векторы] являются одними из самых важных частей информации при построении фигур. Они обычно содержат три числа (но не всегда): декартовы координаты x, y и z. Для
==== Создание вектора ====
[http://en.wikipedia.org/wiki/Euclidean_vector Векторы] являются одними из самых
важных частей информации при построении фигур. Они обычно содержат три числа
(но не всегда): декартовы координаты x, y и z. Для
создания вектора введите:
создания вектора введите:
</div>
</div>

[http://en.wikipedia.org/wiki/Euclidean_vector Vectors] are one of the most important pieces of information when building shapes. They usually contain three numbers (but not necessarily always): the X, Y and Z cartesian coordinates. You create a vector like this:


{{Code|code=
{{Code|code=
myVector = Base.Vector(3, 2, 0)
myVector = App.Vector(3, 2, 0)
}}
}}


Line 194: Line 194:
}}
}}
{{Top}}
{{Top}}
<span id="Create_an_edge"></span>
<div class="mw-translate-fuzzy">
===Создание ребра===
====Как создать Ребро?====
Ребра не что иное как линия с двумя вершинами:
</div>


Ребра это не что иное, как линия с двумя вершинами:
An edge is nothing but a line with two vertices:


{{Code|code=
{{Code|code=
Line 210: Line 208:


{{Code|code=
{{Code|code=
vec1 = Base.Vector(0, 0, 0)
vec1 = App.Vector(0, 0, 0)
vec2 = Base.Vector(10, 0, 0)
vec2 = App.Vector(10, 0, 0)
line = Part.LineSegment(vec1, vec2)
line = Part.LineSegment(vec1, vec2)
edge = line.toShape()
edge = line.toShape()
Line 225: Line 223:
}}
}}
{{Top}}
{{Top}}
<span id="Put_the_shape_on_screen"></span>
<div class="mw-translate-fuzzy">
==== Вывод фигуры на экран ====
===Вывод фигуры на экран===

До сих пор мы создали объект ребро, но не увидели его на экране. Это связано с тем, что 3D-сцена FreeCAD отображает только то, что указано для отображения. Для этого мы используем этот простой метод:
До сих пор мы создали объект ребро, но не увидели его на экране. Это связано с тем, что 3D-сцена FreeCAD отображает только то, что указано для отображения. Для этого мы используем этот простой метод:
</div>

So far we created an edge object, but it doesn't appear anywhere on the screen. This is because the FreeCAD 3D scene only displays what you tell it to display. To do that, we use this simple
method:


{{Code|code=
{{Code|code=
Line 241: Line 236:
</div>
</div>
{{Top}}
{{Top}}
<span id="Create_a_wire"></span>
<div class="mw-translate-fuzzy">
===Создание ломанной кривой===
====Как создать ломанную кривую?====

Ломаная представляет собой многогранную линию и может быть создан из списка ребер или даже из списка ломаных:
Ломаная представляет собой многогранную линию и может быть создан из списка ребер или даже из списка ломаных:
</div>

A wire is a multi-edge line and can be created from a list of edges or even a list of wires:


{{Code|code=
{{Code|code=
Line 276: Line 269:
}}
}}
{{Top}}
{{Top}}
<span id="Create_a_face"></span>
<div class="mw-translate-fuzzy">
===Создание грани===
====Как создать Грань?====
Только грани, созданные из замкнутых ломаных, будут действительными. В этом примере wire3 является замкнутой ломаной, но wire2 не является замкнутым (см. выше)
</div>


Только грани, созданные из замкнутых ломаных, будут действительными. В этом примере wire3 является замкнутой ломаной, но wire2 не является замкнутым (см. выше)
Only faces created from closed wires will be valid. In this example, wire3 is a closed wire but wire2 is not (see above):


{{Code|code=
{{Code|code=
Line 300: Line 291:
Только грани имеют поверхность, а ломанные и ребра нет.
Только грани имеют поверхность, а ломанные и ребра нет.
{{Top}}
{{Top}}
<span id="Create_a_circle"></span>
===Создать окружность===
===Создание окружности===


Окружность может быть создана, например так:
Окружность может быть создана, например так:
Line 313: Line 305:


{{Code|code=
{{Code|code=
ccircle = Part.makeCircle(10, Base.Vector(10, 0, 0), Base.Vector(1, 0, 0))
ccircle = Part.makeCircle(10, App.Vector(10, 0, 0), App.Vector(1, 0, 0))
ccircle.Curve
ccircle.Curve
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0))
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0))
Line 324: Line 316:
{{Code|code=
{{Code|code=
from math import pi
from math import pi
arc1 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)
arc1 = Part.makeCircle(10, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 0, 180)
arc2 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 180, 360)
arc2 = Part.makeCircle(10, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 180, 360)
}}
}}


Line 341: Line 333:
}}
}}
{{Top}}
{{Top}}
<span id="Create_an_arc_along_points"></span>
===Создать дугу по точкам===
===Создать дугу по точкам===


Line 348: Line 341:


{{Code|code=
{{Code|code=
arc = Part.Arc(Base.Vector(0, 0, 0), Base.Vector(0, 5, 0), Base.Vector(5, 5, 0))
arc = Part.Arc(App.Vector(0, 0, 0), App.Vector(0, 5, 0), App.Vector(5, 5, 0))
arc
arc
> <Arc object>
> <Arc object>
Line 361: Line 354:
{{Code|code=
{{Code|code=
from math import pi
from math import pi
circle = Part.Circle(Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 10)
circle = Part.Circle(App.Vector(0, 0, 0), App.Vector(0, 0, 1), 10)
arc = Part.Arc(circle,0,pi)
arc = Part.Arc(circle,0,pi)
}}
}}
Line 367: Line 360:
Дуги являются действительными ребрами, такими как линии, поэтому их можно использовать и в ломаных линиях.
Дуги являются действительными ребрами, такими как линии, поэтому их можно использовать и в ломаных линиях.
{{Top}}
{{Top}}
<span id="Create_a_polygon"></span>
===Создать многоугольник (полигон)===
===Создать многоугольник (полигон)===


Line 375: Line 369:


{{Code|code=
{{Code|code=
lshape_wire = Part.makePolygon([Base.Vector(0, 5, 0), Base.Vector(0, 0, 0), Base.Vector(5, 0, 0)])
lshape_wire = Part.makePolygon([App.Vector(0, 5, 0), App.Vector(0, 0, 0), App.Vector(5, 0, 0)])
}}
}}
{{Top}}
{{Top}}
<span id="Create_a_Bézier_curve"></span>
<div class="mw-translate-fuzzy">
===Создание кривой Безье===
===Создание кривой Безье===
</div>


<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Line 392: Line 389:
}}
}}
{{Top}}
{{Top}}
<span id="Create_a_plane"></span>
===Создание плоскости===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как создать плоскость?====
Плоскость это ровная поверхность, в смысле 2D грань. Метод создания её это
Плоскость это ровная поверхность, в смысле 2D грань. Метод создания её это
'''makePlane(length,width,[start_pnt,dir_normal])'''. По умолчанию
'''makePlane(length,width,[start_pnt,dir_normal])'''. По умолчанию
Line 400: Line 399:
плоскость обращённую к положительному направлению оси х:
плоскость обращённую к положительному направлению оси х:
</div>
</div>

A Plane is a flat rectangular surface. The method used to create one is {{incode|makePlane(length, width, [start_pnt, dir_normal])}}. By default start_pnt = Vector(0, 0, 0) and dir_normal = Vector(0, 0, 1). Using dir_normal = Vector(0, 0, 1) will create the plane facing in the positive Z axis direction, while dir_normal = Vector(1, 0, 0) will create the plane facing in the positive X axis direction:


{{Code|code=
{{Code|code=
Line 407: Line 404:
plane
plane
> <Face object at 028AF990>
> <Face object at 028AF990>
plane = Part.makePlane(2, 2, Base.Vector(3, 0, 0), Base.Vector(0, 1, 0))
plane = Part.makePlane(2, 2, App.Vector(3, 0, 0), App.Vector(0, 1, 0))
plane.BoundBox
plane.BoundBox
> BoundBox (3, 0, 0, 5, 0, 2)
> BoundBox (3, 0, 0, 5, 0, 2)
Line 421: Line 418:
</div>
</div>
{{Top}}
{{Top}}
<span id="Create_an_ellipse"></span>
<div class="mw-translate-fuzzy">
===Создание эллипса===
====Как создать эллипс?====
Создать эллипс можно несколькими путями:
</div>


Эллипс можно создать несколькими способами:
There are several ways to create an ellipse:


{{Code|code=
{{Code|code=
Line 463: Line 458:


{{Code|code=
{{Code|code=
eli = Part.Ellipse(Base.Vector(10, 0, 0), Base.Vector(0, 5, 0), Base.Vector(0, 0, 0))
eli = Part.Ellipse(App.Vector(10, 0, 0), App.Vector(0, 5, 0), App.Vector(0, 0, 0))
Part.show(eli.toShape())
Part.show(eli.toShape())
}}
}}
Line 476: Line 471:


{{Code|code=
{{Code|code=
eli = Part.Ellipse(Base.Vector(0, 0, 0), 10, 5)
eli = Part.Ellipse(App.Vector(0, 0, 0), 10, 5)
Part.show(eli.toShape())
Part.show(eli.toShape())
}}
}}
Line 484: Line 479:
</div>
</div>
{{Top}}
{{Top}}
<span id="Create_a_torus"></span>
===Создание тора===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как создать Тор?====
Используя '''makeTorus(radius1,radius2,[pnt,dir,angle1,angle2,angle])'''.
Используя '''makeTorus(radius1,radius2,[pnt,dir,angle1,angle2,angle])'''.
По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=0,angle2=360 и angle=360
По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=0,angle2=360 и angle=360
Line 492: Line 489:
angle1 и angle2 углы в радианах для малого круга, последний параметр angle для создания секцию (части) тора:
angle1 и angle2 углы в радианах для малого круга, последний параметр angle для создания секцию (части) тора:
</div>
</div>

Using {{incode|makeTorus(radius1, radius2, [pnt, dir, angle1, angle2, angle])}}.
By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = 0, angle2 = 360 and angle = 360.
Consider a torus as small circle sweeping along a big circle. Radius1 is the radius of the big circle, radius2 is the radius of the small circle, pnt is the center of the torus and dir is the normal direction. angle1 and angle2 are angles in degrees for the small circle; the last angle parameter is to make a section of the torus:


{{Code|code=
{{Code|code=
Line 506: Line 499:


{{Code|code=
{{Code|code=
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)
tor=Part.makeTorus(10, 5, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 0, 180)
}}
}}


Line 512: Line 505:


{{Code|code=
{{Code|code=
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 360, 180)
tor=Part.makeTorus(10, 5, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 0, 360, 180)
}}
}}


Line 519: Line 512:
</div>
</div>
{{Top}}
{{Top}}
<span id="Create_a_box_or_cuboid"></span>
===Создание параллелепипеда или кубоида===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как создать блок или параллелепипед?====

Используя '''makeBox(length,width,height,[pnt,dir])''', создаем блок расположенный в pnt с размерами (length,width,height). По умолчанию pnt=Vector(0,0,0) и dir=Vector(0,0,1).
Используя '''makeBox(length,width,height,[pnt,dir])''', создаем блок расположенный в pnt с размерами (length,width,height). По умолчанию pnt=Vector(0,0,0) и dir=Vector(0,0,1).
</div>
</div>

Using {{incode|makeBox(length, width, height, [pnt, dir])}}.
By default pnt = Vector(0, 0, 0) and dir = Vector(0, 0, 1).


{{Code|code=
{{Code|code=
Line 534: Line 525:
}}
}}
{{Top}}
{{Top}}
<span id="Create_a_sphere"></span>
===Создание сферы===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как создать Сферу?====
Используя '''makeSphere(radius,[pnt, dir, angle1,angle2,angle3])'''.
Используя '''makeSphere(radius,[pnt, dir, angle1,angle2,angle3])'''.
По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=-90, angle2=90 и angle3=360.
По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=-90, angle2=90 и angle3=360.
Line 541: Line 534:
angle3 is the sphere diameter (определяет замкнутое ли это тело вращения или его секция).
angle3 is the sphere diameter (определяет замкнутое ли это тело вращения или его секция).
</div>
</div>

Using {{incode|makeSphere(radius, [pnt, dir, angle1, angle2, angle3])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = -90, angle2 = 90 and angle3 = 360.
Angle1 and angle2 are the vertical minimum and maximum of the sphere, angle3 is the sphere diameter.


{{Code|code=
{{Code|code=
sphere = Part.makeSphere(10)
sphere = Part.makeSphere(10)
hemisphere = Part.makeSphere(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), -90, 90, 180)
hemisphere = Part.makeSphere(10, App.Vector(0, 0, 0), App.Vector(0, 0, 1), -90, 90, 180)
}}
}}
{{Top}}
{{Top}}
<span id="Create_a_cylinder"></span>
===Создание цилиндра===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Используя '''makeCylinder(radius,height,[pnt,dir,angle])''', создается цилиндр с указанным радиусом и высотой. По умолчанию pnt=Vector(0,0,0),dir=Vector(0,0,1) и angle=360.
====Как создать Цилиндр?====
Используя '''makeCylinder(radius,height,[pnt,dir,angle])''', создается цилиндр с указанным радиусом и высотой. По умолчанию pnt=Vector(0,0,0),dir=Vector(0,0,1) и angle=360.
</div>
</div>


Using {{incode|makeCylinder(radius, height, [pnt, dir, angle])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
{{Code|code=
{{Code|code=
cylinder = Part.makeCylinder(5, 20)
cylinder = Part.makeCylinder(5, 20)
partCylinder = Part.makeCylinder(5, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)
partCylinder = Part.makeCylinder(5, 20, App.Vector(20, 0, 0), App.Vector(0, 0, 1), 180)
}}{{Top}}
}}
{{Top}}
<span id="Create_a_cone"></span>
===Cоздание конуса===

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как создать Конус?====
Используя '''makeCone(radius1,radius2,height,[pnt,dir,angle])''', создаем конус с указанными радиусами и высотой. По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1) и angle=360.
Используя '''makeCone(radius1,radius2,height,[pnt,dir,angle])''', создаем конус с указанными радиусами и высотой. По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1) и angle=360.
</div>
</div>


Using {{incode|makeCone(radius1, radius2, height, [pnt, dir, angle])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
{{Code|code=
{{Code|code=
cone = Part.makeCone(10, 0, 20)
cone = Part.makeCone(10, 0, 20)
semicone = Part.makeCone(10, 0, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)
semicone = Part.makeCone(10, 0, 20, App.Vector(20, 0, 0), App.Vector(0, 0, 1), 180)
}}{{Top}}
}}
{{Top}}
==Modify shapes==
==Modify shapes==


Line 579: Line 573:


Translating is the act of moving a shape from one place to another. Any shape (edge, face, cube, etc...) can be translated the same way:
Translating is the act of moving a shape from one place to another. Any shape (edge, face, cube, etc...) can be translated the same way:

{{Code|code=
{{Code|code=
myShape = Part.makeBox(2, 2, 2)
myShape = Part.makeBox(2, 2, 2)
myShape.translate(Base.Vector(2, 0, 0))
myShape.translate(App.Vector(2, 0, 0))
}}
}}

This will move our shape "myShape" 2 units in the X direction.
This will move our shape "myShape" 2 units in the X direction.
{{Top}}
{{Top}}
Line 588: Line 584:


To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle:
To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle:

{{Code|code=
{{Code|code=
myShape.rotate(Base.Vector(0, 0, 0),Base.Vector(0, 0, 1), 180)
myShape.rotate(App.Vector(0, 0, 0),App.Vector(0, 0, 1), 180)
}}
}}

The above code will rotate the shape 180 degrees around the Z Axis.
The above code will rotate the shape 180 degrees around the Z Axis.
{{Top}}
{{Top}}
Line 596: Line 594:


A matrix is a very convenient way to store transformations in the 3D world. In a single matrix, you can set translation, rotation and scaling values to be applied to an object. For example:
A matrix is a very convenient way to store transformations in the 3D world. In a single matrix, you can set translation, rotation and scaling values to be applied to an object. For example:

{{Code|code=
{{Code|code=
myMat = Base.Matrix()
myMat = App.Matrix()
myMat.move(Base.Vector(2, 0, 0))
myMat.move(App.Vector(2, 0, 0))
myMat.rotateZ(math.pi/2)
myMat.rotateZ(math.pi/2)
}}
}}

Note: FreeCAD matrixes work in radians. Also, almost all matrix operations that take a vector can also take three numbers, so these two lines do the same thing:
Note: FreeCAD matrixes work in radians. Also, almost all matrix operations that take a vector can also take three numbers, so these two lines do the same thing:

{{Code|code=
{{Code|code=
myMat.move(2, 0, 0)
myMat.move(2, 0, 0)
myMat.move(Base.Vector(2, 0, 0))
myMat.move(App.Vector(2, 0, 0))
}}
}}

Once our matrix is set, we can apply it to our shape. FreeCAD provides two methods for doing that: {{incode|transformShape()}} and {{incode|transformGeometry()}}. The difference
is that with the first one, you are sure that no deformations will occur (see [[#Scaling a shape|Scaling a shape]] below). We can apply our transformation like this:
Once our matrix is set, we can apply it to our shape. FreeCAD provides two methods for doing that: {{incode|transformShape()}} and {{incode|transformGeometry()}}. The difference is that with the first one, you are sure that no deformations will occur (see [[#Scaling a shape|Scaling a shape]] below). We can apply our transformation like this:

{{Code|code=
{{Code|code=
myShape.transformShape(myMat)
myShape.transformShape(myMat)
}}
}}

или
или

{{Code|code=
{{Code|code=
myShape.transformGeometry(myMat)
myShape.transformGeometry(myMat)
}}{{Top}}
}}
{{Top}}
===Scale a shape===
===Scale a shape===


Scaling a shape is a more dangerous operation because, unlike translation or rotation, scaling non-uniformly (with different values for X, Y and Z) can modify the structure of the shape. For example, scaling a circle with a higher value horizontally than vertically will transform it into an ellipse, which behaves mathematically very differently. For scaling, we cannot use the {{incode|transformShape()}}, we must use {{incode|transformGeometry()}}:
Scaling a shape is a more dangerous operation because, unlike translation or rotation, scaling non-uniformly (with different values for X, Y and Z) can modify the structure of the shape. For example, scaling a circle with a higher value horizontally than vertically will transform it into an ellipse, which behaves mathematically very differently. For scaling, we cannot use the {{incode|transformShape()}}, we must use {{incode|transformGeometry()}}:

{{Code|code=
{{Code|code=
myMat = Base.Matrix()
myMat = App.Matrix()
myMat.scale(2, 1, 1)
myMat.scale(2, 1, 1)
myShape=myShape.transformGeometry(myMat)
myShape=myShape.transformGeometry(myMat)
}}{{Top}}
}}
{{Top}}
<span id="Boolean_operations"></span>
==Булевы Операции==
==Булевы Операции==


<span id="Subtraction"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как вырезать одну форму из других?====
====Как вырезать одну форму из других?====
Line 632: Line 641:


Subtracting a shape from another one is called "cut" in FreeCAD and is done like this:
Subtracting a shape from another one is called "cut" in FreeCAD and is done like this:

{{Code|code=
{{Code|code=
cylinder = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
sphere = Part.makeSphere(5, Base.Vector(5, 0, 0))
sphere = Part.makeSphere(5, App.Vector(5, 0, 0))
diff = cylinder.cut(sphere)
diff = cylinder.cut(sphere)
}}{{Top}}
}}
{{Top}}
<span id="Intersection"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как получить пересечение двух форм?====
====Как получить пересечение двух форм?====
Line 643: Line 655:


The same way, the intersection between two shapes is called "common" and is done this way:
The same way, the intersection between two shapes is called "common" and is done this way:

{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder1 = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
cylinder2 = Part.makeCylinder(3, 10, App.Vector(5, 0, -5), App.Vector(0, 0, 1))
common = cylinder1.common(cylinder2)
common = cylinder1.common(cylinder2)
}}{{Top}}
}}
{{Top}}
<span id="Union"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как объединить две формы?====
====Как объединить две формы?====
Line 655: Line 670:


Union is called "fuse" and works the same way:
Union is called "fuse" and works the same way:

{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder1 = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
cylinder2 = Part.makeCylinder(3, 10, App.Vector(5, 0, -5), App.Vector(0, 0, 1))
fuse = cylinder1.fuse(cylinder2)
fuse = cylinder1.fuse(cylinder2)
}}{{Top}}
}}
{{Top}}
<span id="Section"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
====Как получить сечение тела и заданной формы?====
====Как получить сечение тела и заданной формы?====
Line 667: Line 685:


A "section" is the intersection between a solid shape and a plane shape. It will return an intersection curve, a compound curve composed of edges.
A "section" is the intersection between a solid shape and a plane shape. It will return an intersection curve, a compound curve composed of edges.

{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder1 = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
cylinder2 = Part.makeCylinder(3, 10, App.Vector(5, 0, -5), App.Vector(0, 0, 1))
section = cylinder1.section(cylinder2)
section = cylinder1.section(cylinder2)
section.Wires
section.Wires
Line 677: Line 696:
<Edge object at 0D86DE18>, <Edge object at 0D9B8E80>, <Edge object at 012A3640>,
<Edge object at 0D86DE18>, <Edge object at 0D9B8E80>, <Edge object at 012A3640>,
<Edge object at 0D8F4BB0>]
<Edge object at 0D8F4BB0>]
}}{{Top}}
}}
{{Top}}
<span id="Extrusion"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
==== Выдавливание ====
==== Выдавливание ====
Line 684: Line 705:


Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in a solid body. Think of a circle becoming a tube by "pushing it out":
Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in a solid body. Think of a circle becoming a tube by "pushing it out":

{{Code|code=
{{Code|code=
circle = Part.makeCircle(10)
circle = Part.makeCircle(10)
tube = circle.extrude(Base.Vector(0, 0, 2))
tube = circle.extrude(App.Vector(0, 0, 2))
}}
}}

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Если ваш круг полый, вы получите полую трубу. Если ваш круг это диск с заполненной поверхностью, вы получите сплошной цилиндр:
Если ваш круг полый, вы получите полую трубу. Если ваш круг это диск с заполненной поверхностью, вы получите сплошной цилиндр:
</div>
</div>

{{Code|code=
{{Code|code=
wire = Part.Wire(circle)
wire = Part.Wire(circle)
disc = Part.Face(wire)
disc = Part.Face(wire)
cylinder = disc.extrude(Base.Vector(0, 0, 2))
cylinder = disc.extrude(App.Vector(0, 0, 2))
}}{{Top}}
}}
{{Top}}
<span id="Explore_shapes"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
== Исследование Форм ==
== Исследование Форм ==
Line 702: Line 728:


You can easily explore the topological data structure:
You can easily explore the topological data structure:

{{Code|code=
{{Code|code=
import Part
import Part
Line 717: Line 744:
v.Point
v.Point
}}
}}

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Если ввести строчку выше в интерпретатор python , вы получите хорошее представление об устройстве объектов Part. Здесь наша команда makeBox() создает твердое тело. Это тело, как и все тела Part, содержит грани. Грани всегда содержат ломанные, которые являются набором ребер ограничивающих грань. Каждая грань обладает минимум одной замкнутой ломаной (может больше, если есть отверстие). В ломанной мы можем посмотреть на каждое ребро отдельно, и по краям каждого ребра мы можем увидеть вершины. Прямые ребра обладают только двумя вершинами, разумеется. Вершины модуля Part являются формами OCC(OpenCascade), но они обладают атрибутом Point, который возвращает вектор FreeCAD.
Если ввести строчку выше в интерпретатор python , вы получите хорошее представление об устройстве объектов Part. Здесь наша команда makeBox() создает твердое тело. Это тело, как и все тела Part, содержит грани. Грани всегда содержат ломанные, которые являются набором ребер ограничивающих грань. Каждая грань обладает минимум одной замкнутой ломаной (может больше, если есть отверстие). В ломанной мы можем посмотреть на каждое ребро отдельно, и по краям каждого ребра мы можем увидеть вершины. Прямые ребра обладают только двумя вершинами, разумеется. Вершины модуля Part являются формами OCC(OpenCascade), но они обладают атрибутом Point, который возвращает вектор FreeCAD.
</div>
</div>
{{Top}}
{{Top}}
<span id="Edge_analysis"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
=== Исследование Рёбер ===
=== Исследование Рёбер ===
Line 727: Line 756:


In case of an edge, which is an arbitrary curve, it's most likely you want to do a discretization. In FreeCAD the edges are parametrized by their lengths. That means you can walk an edge/curve by its length:
In case of an edge, which is an arbitrary curve, it's most likely you want to do a discretization. In FreeCAD the edges are parametrized by their lengths. That means you can walk an edge/curve by its length:

{{Code|code=
{{Code|code=
import Part
import Part
Line 733: Line 763:
print(anEdge.Length)
print(anEdge.Length)
}}
}}

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Теперь вы получить доступ ко всем свойствам ребра, с помощью длинны или позиции. Это означает, что у ребра
Теперь вы получить доступ ко всем свойствам ребра, с помощью длинны или позиции. Это означает, что у ребра
в 100mm длинной, начальная позиция это 0 а конечная это 100.
в 100mm длинной, начальная позиция это 0 а конечная это 100.
</div>
</div>

{{Code|code=
{{Code|code=
anEdge.tangentAt(0.0) # tangent direction at the beginning
anEdge.tangentAt(0.0) # tangent direction at the beginning
Line 747: Line 779:
anEdge.curvatureAt(50.0) # the curvature
anEdge.curvatureAt(50.0) # the curvature
anEdge.normalAt(50) # normal vector at that position (if defined)
anEdge.normalAt(50) # normal vector at that position (if defined)
}}{{Top}}
}}
{{Top}}
<span id="Use_a_selection"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
=== Использование выделения(выбора) ===
=== Использование выделения(выбора) ===
Line 754: Line 788:
</div>
</div>


Here we see now how we can use a selection the user did in the viewer.
Here we see now how we can use a selection the user did in the viewer. First of all we create a box and show it in the viewer.

First of all we create a box and show it in the viewer.
{{Code|code=
{{Code|code=
import Part
import Part
Line 761: Line 795:
Gui.SendMsgToActiveView("ViewFit")
Gui.SendMsgToActiveView("ViewFit")
}}
}}

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Теперь выберем грани или ребра. С помощью этого сценария вы можете повторить по всем выделенным объектам и их субэлементам:
Теперь выберем грани или ребра. С помощью этого сценария вы можете повторить по всем выделенным объектам и их субэлементам:
</div>
</div>

{{Code|code=
{{Code|code=
for o in Gui.Selection.getSelectionEx():
for o in Gui.Selection.getSelectionEx():
Line 772: Line 808:
print("object: ", s)
print("object: ", s)
}}
}}

Выделим несколько ребер и этот сценарий подсчитает их сумарную длину:
Выделим несколько ребер и этот сценарий подсчитает их сумарную длину:

{{Code|code=
{{Code|code=
length = 0.0
length = 0.0
Line 780: Line 818:


print("Length of the selected edges: ", length)
print("Length of the selected edges: ", length)
}}{{Top}}
}}
{{Top}}
<span id="Example:_The_OCC_bottle"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
== Полный пример: бутыль OCC ==
== Полный пример: бутыль OCC ==
Line 787: Line 827:


A typical example found on the [https://www.opencascade.com/doc/occt-6.9.0/overview/html/occt__tutorial.html OpenCasCade Technology website] is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the {{FileName|Mod/Part}} folder) and can be called from the Python interpreter by typing:
A typical example found on the [https://www.opencascade.com/doc/occt-6.9.0/overview/html/occt__tutorial.html OpenCasCade Technology website] is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the {{FileName|Mod/Part}} folder) and can be called from the Python interpreter by typing:

{{Code|code=
{{Code|code=
import Part
import Part
Line 792: Line 833:
bottle = MakeBottle.makeBottle()
bottle = MakeBottle.makeBottle()
Part.show(bottle)
Part.show(bottle)
}}{{Top}}
}}
{{Top}}
<span id="The_script"></span>
<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
=== Готовый сценарий ===
=== Готовый сценарий ===
Line 800: Line 843:


For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded.
For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded.

{{Code|code=
{{Code|code=
import FreeCAD as App
import Part, math
import Part, math
from FreeCAD import Base


def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
aPnt1=Base.Vector(-myWidth / 2., 0, 0)
aPnt1=App.Vector(-myWidth / 2., 0, 0)
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt2=App.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt3=Base.Vector(0, -myThickness / 2., 0)
aPnt3=App.Vector(0, -myThickness / 2., 0)
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt4=App.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt5=Base.Vector(myWidth / 2., 0, 0)
aPnt5=App.Vector(myWidth / 2., 0, 0)


aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
Line 820: Line 864:
aWire=Part.Wire([aEdge1, aEdge2, aEdge3])
aWire=Part.Wire([aEdge1, aEdge2, aEdge3])


aTrsf=Base.Matrix()
aTrsf=App.Matrix()
aTrsf.rotateZ(math.pi) # rotate around the z-axis
aTrsf.rotateZ(math.pi) # rotate around the z-axis


Line 828: Line 872:


myFaceProfile=Part.Face(myWireProfile)
myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0, 0, myHeight)
aPrismVec=App.Vector(0, 0, myHeight)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myFaceProfile.extrude(aPrismVec)


myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)
myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)


neckLocation=Base.Vector(0, 0, myHeight)
neckLocation=App.Vector(0, 0, myHeight)
neckNormal=Base.Vector(0, 0, 1)
neckNormal=App.Vector(0, 0, 1)


myNeckRadius = myThickness / 4.
myNeckRadius = myThickness / 4.
Line 845: Line 889:
el = makeBottleTut()
el = makeBottleTut()
Part.show(el)
Part.show(el)
}}{{Top}}
}}
{{Top}}
<span id="Detailed_explanation"></span>
===Подробные объяснения===
===Подробные объяснения===

{{Code|code=
{{Code|code=
import FreeCAD as App
import Part, math
import Part, math
from FreeCAD import Base
}}
}}

<div class="mw-translate-fuzzy">
Нам, конечно, необходимы модуль {{incode|Part}}, а также модуль {{incode|FreeCAD.Base}}, который содержит основные структуры FreeCAD, такие как векторы и матрицы.
Нам, конечно, необходимы модуль {{incode|Part}}, а также модуль {{incode|FreeCAD.Base}}, который содержит основные структуры FreeCAD, такие как векторы и матрицы.
</div>


{{Code|code=
{{Code|code=
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
aPnt1=Base.Vector(-myWidth / 2., 0, 0)
aPnt1=App.Vector(-myWidth / 2., 0, 0)
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt2=App.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt3=Base.Vector(0, -myThickness / 2., 0)
aPnt3=App.Vector(0, -myThickness / 2., 0)
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt4=App.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt5=Base.Vector(myWidth / 2., 0, 0)
aPnt5=App.Vector(myWidth / 2., 0, 0)
}}
}}


Line 889: Line 939:
{{Code|code=
{{Code|code=
...
...
aTrsf=Base.Matrix()
aTrsf=App.Matrix()
aTrsf.rotateZ(math.pi) # rotate around the z-axis
aTrsf.rotateZ(math.pi) # rotate around the z-axis


Line 904: Line 954:
...
...
myFaceProfile=Part.Face(myWireProfile)
myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0, 0, myHeight)
aPrismVec=App.Vector(0, 0, myHeight)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myFaceProfile.extrude(aPrismVec)


Line 913: Line 963:
Теперь мы получили замкнутую ломаную, которую можно обратить в грань. Когда мы имеем грань, мы можем вытянуть её. Сделав это, мы получим твердое тело. Теперь мы добавим небольшое скругление к нашему объекту, потому что мы заботимся о качественном дизайне, не так ли?
Теперь мы получили замкнутую ломаную, которую можно обратить в грань. Когда мы имеем грань, мы можем вытянуть её. Сделав это, мы получим твердое тело. Теперь мы добавим небольшое скругление к нашему объекту, потому что мы заботимся о качественном дизайне, не так ли?
</div>
</div>

{{Code|code=
{{Code|code=
...
...
neckLocation=Base.Vector(0, 0, myHeight)
neckLocation=App.Vector(0, 0, myHeight)
neckNormal=Base.Vector(0, 0, 1)
neckNormal=App.Vector(0, 0, 1)


myNeckRadius = myThickness / 4.
myNeckRadius = myThickness / 4.
Line 922: Line 973:
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
}}
}}

<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Теперь тело нашей бутыли создано, но нам нужно создать горлышко. Так что мы создаем новое твердое тело, с цилиндром.
Теперь тело нашей бутыли создано, но нам нужно создать горлышко. Так что мы создаем новое твердое тело, с цилиндром.
</div>
</div>

{{Code|code=
{{Code|code=
...
...
myBody = myBody.fuse(myNeck)
myBody = myBody.fuse(myNeck)
}}
}}

Операция слияния очень мощная. Она заботится о склеивании, о том, что должно быть приклеено и удаляет части, которые следует удалить.
Операция слияния очень мощная. Она заботится о склеивании, о том, что должно быть приклеено и удаляет части, которые следует удалить.

{{Code|code=
{{Code|code=
...
...
return myBody
return myBody
}}
}}

Теперь мы получаем нашу твёрдое тело модуля Part как результат нашей функции.
Теперь мы получаем нашу твёрдое тело модуля Part как результат нашей функции.

{{Code|code=
{{Code|code=
el = makeBottleTut()
el = makeBottleTut()
Part.show(el)
Part.show(el)
}}
}}

В итоге мы вызываем функцию для фактического создания детали, а потом делаем её видимой.
В итоге мы вызываем функцию для фактического создания детали, а потом делаем её видимой.
{{Top}}
{{Top}}
Line 946: Line 1,004:


Конструкция делается по одной стороне за раз. Когда куб закончен, он выдалбливается вырезанием цилиндра через него.
Конструкция делается по одной стороне за раз. Когда куб закончен, он выдалбливается вырезанием цилиндра через него.

{{Code|code=
{{Code|code=
import FreeCAD as App
import Part, math
import Part, math
from FreeCAD import Base


size = 10
size = 10
Line 960: Line 1,019:
face6 = Part.Face(poly)
face6 = Part.Face(poly)
myMat = Base.Matrix()
myMat = App.Matrix()


myMat.rotateZ(math.pi / 2)
myMat.rotateZ(math.pi / 2)
face2.transformShape(myMat)
face2.transformShape(myMat)
face2.translate(Base.Vector(size, 0, 0))
face2.translate(App.Vector(size, 0, 0))


myMat.rotateZ(math.pi / 2)
myMat.rotateZ(math.pi / 2)
face3.transformShape(myMat)
face3.transformShape(myMat)
face3.translate(Base.Vector(size, size, 0))
face3.translate(App.Vector(size, size, 0))


myMat.rotateZ(math.pi / 2)
myMat.rotateZ(math.pi / 2)
face4.transformShape(myMat)
face4.transformShape(myMat)
face4.translate(Base.Vector(0, size, 0))
face4.translate(App.Vector(0, size, 0))


myMat = Base.Matrix()
myMat = App.Matrix()


myMat.rotateX(-math.pi / 2)
myMat.rotateX(-math.pi / 2)
Line 980: Line 1,039:


face6.transformShape(myMat)
face6.transformShape(myMat)
face6.translate(Base.Vector(0, 0, size))
face6.translate(App.Vector(0, 0, size))


myShell = Part.makeShell([face1, face2, face3, face4, face5, face6])
myShell = Part.makeShell([face1, face2, face3, face4, face5, face6])
Line 986: Line 1,045:


myCyl = Part.makeCylinder(2, 20)
myCyl = Part.makeCylinder(2, 20)
myCyl.translate(Base.Vector(size / 2, size / 2, 0))
myCyl.translate(App.Vector(size / 2, size / 2, 0))


cut_part = mySolid.cut(myCyl)
cut_part = mySolid.cut(myCyl)


Part.show(cut_part)
Part.show(cut_part)
}}{{Top}}
}}
{{Top}}
<span id="Loading_and_saving"></span>
== Загрузка и Сохранение ==
== Загрузка и Сохранение ==


Line 998: Line 1,059:
</div>
</div>


<div class="mw-translate-fuzzy">
Сохранить форму в файл легко. Есть доступные для всех форм методы {{incode|exportBrep()}}, {{incode|exportIges()}}, {{incode|exportStep()}} и {{incode|exportStl()}}. Таким образом:
Сохранить форму в файл легко. Есть доступные для всех форм методы {{incode|exportBrep()}}, {{incode|exportIges()}}, {{incode|exportStep()}} и {{incode|exportStl()}}. Таким образом:
</div>

{{Code|code=
{{Code|code=
import Part
import Part
Line 1,004: Line 1,068:
s.exportStep("test.stp")
s.exportStep("test.stp")
}}
}}

это сохранит наш блок в файл формата STEP. Для загрузки BREP, IGES или STEP файлов:
это сохранит наш блок в файл формата STEP. Для загрузки BREP, IGES или STEP файлов:

{{Code|code=
{{Code|code=
import Part
import Part
Line 1,010: Line 1,076:
s.read("test.stp")
s.read("test.stp")
}}
}}

Для преобразования файла STEP в файл IGS:
Для преобразования файла STEP в файл IGS:

{{Code|code=
{{Code|code=
import Part
import Part
Line 1,016: Line 1,084:
s.read("file.stp") # incoming file igs, stp, stl, brep
s.read("file.stp") # incoming file igs, stp, stl, brep
s.exportIges("file.igs") # outbound file igs
s.exportIges("file.igs") # outbound file igs
}}{{Top}}
}}
{{Top}}

{{Docnav/ru
|[[FreeCAD_Scripting_Basics/ru|FreeCAD Scripting Basics]]
|[[Mesh_Scripting/ru|Mesh Scripting]]
}}

{{Powerdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}

Latest revision as of 18:55, 12 October 2023

Введение

Здесь мы объясним вам, как управлять верстаком Part непосредственно из интерпретатора Python FreeCAD или из любого внешнего сценария. Основы создания сценариев топологических данных описаны в объяснение концепции модуля Part. Обязательно просмотрите раздел Scripting и страницы Основы скриптинга FreeCAD, если вам нужна дополнительная информация о том, как работает python-скриптинг во FreeCAD .

Смотрите также

Диаграмма классов

Это обзор наиболее важных классов модуля Part через Unified Modeling Language (UML):

Классы Python, содержащиеся в модуле Part
Классы Python, содержащиеся в модуле Part

наверх

Геометрия

Геометрические объекты являются строительными блоками для всех топологических объектов:

  • Geom Базовый класс геометрических объектов.
  • Line Прямая линия в 3D, задается начальной и конечной точкой.
  • Circle Окружность или дуга задается центром, начальной и конечной точкой.
  • и так далее...

наверх

Топология

Доступны нижеследующие топологические типы данных:

  • COMPOUND Группа из топологических объектов любого типа.
  • COMPSOLID Составное твердое тело, как набор твердых тел соединенными гранями. Он расширяет понятие Ломаной кривой(WIRE) и оболочки(SHELL) для твердых тел.
  • SOLID Часть пространства ограниченная оболочкой. Она трехмерная.
  • SHELL Набор граней соединенных между собой через ребра. Оболочки могут быть открытыми или закрытыми.
  • FACE В 2D это часть плоскости; в 3D это часть поверхности. Это геометрия ограничена (обрезана) по контурам. Она двухмерная.
  • WIRE Набор ребер соединенных через вершины. Он может быть как открытым, так и закрытым в зависимости от того связаны ли крайние ребра или нет.
  • EDGE Топологический элемент соответствующий ограниченной кривой. Ребро как правило ограничивается вершинами. Оно одномерное.
  • VERTEX Топологический элемент соответствующий точке. Обладает нулевой размерность.
  • SHAPE общий термин охватывающий все выше сказанное.

наверх

Примеры: Создание простейшей топологии

Wire

Теперь мы создадим топологию из геометрических примитивов. Для изучения мы используем деталь(part), как показано на картинке состоящую из четырех вершин, двух окружностей и двух линий.

наверх

Создание геометрии

В начале мы должны создать отдельную деталь из данной ломаной. И мы должны убедиться что вершины геометрических частей расположены на тех же позициях. В противном случае позже мы не смогли бы соединить геометрические части в топологию!

Итак, сначала мы создаем точки:

import FreeCAD as App
import Part
V1 = App.Vector(0, 10, 0)
V2 = App.Vector(30, 10, 0)
V3 = App.Vector(30, -10, 0)
V4 = App.Vector(0, -10, 0)

наверх

Дуга

Circle


Для каждой дуги нам нужно создать вспомогательную точку и провести дугу через три точки:

VC1 = App.Vector(-10, 0, 0)
C1 = Part.Arc(V1, VC1, V4)
VC2 = App.Vector(40, 0, 0)
C2 = Part.Arc(V2, VC2, V3)

наверх

Линия

Line


Сегменты линии могут быть созданы из двух точек:

L1 = Part.LineSegment(V1, V2)
L2 = Part.LineSegment(V3, V4)

наверх

Соединяем все вместе

Последний шаг - собираем все основные геометрические элементы вместе и получаем форму:

S1 = Part.Shape([C1, L1, C2, L2])

наверх

Создание призмы

Теперь вытягиваем ломанную по направлению и фактически получаем 3D форму:

W = Part.Wire(S1.Edges)
P = W.extrude(App.Vector(0, 0, 10))

наверх

Показать всё

Part.show(P)

наверх

Создание простых фигур

Вы легко можете создавать простые топологические объекты с помощью методов make...() содержащихся в модуле Part:

b = Part.makeBox(100, 100, 100)
Part.show(b)

Доступные make...() методы:

  • makeBox(l,w,h,[p,d]) : создает прямоугольник, с началом в точке p и вытянутый в направлении d с размерами (l,w,h) По умолчанию p установлен как Vector(0,0,0) и d установлен как Vector(0,0,1)
  • makeCircle(radius,[p,d,angle1,angle2]) -- Создает окружность с заданным радиусом. По умолчанию p=Vector(0,0,0), d=Vector(0,0,1), angle1=0 и angle2=360
  • makeCone(radius1,radius2,height,[p,d,angle]) -- Создает конус с заданным радиусами и высотой. По умолчанию p=Vector(0,0,0), d=Vector(0,0,1) и angle=360
  • makeCylinder(radius,height,[p,d,angle]) -- Создает цилиндр с заданным радиусом и высотой. По умолчанию p=Vector(0,0,0), d=Vector(0,0,1) и angle=360
  • makeLine((x1,y1,z1),(x2,y2,z2)) -- Создает линию проходящую через две точки
  • makePlane(length,width,[p,d]) -- Создает плоскость с заданной длинной и шириной. По умолчанию p=Vector(0,0,0) и d=Vector(0,0,1)
  • makePolygon(list) -- Создает многоугольник из списка точек
  • makeSphere(radius,[p,d,angle1,angle2,angle3]) -- Создает сферу с заданным радиусом. По умолчанию p=Vector(0,0,0), d=Vector(0,0,1), angle1=0, angle2=90 и angle3=360
  • makeTorus(radius1,radius2,[p,d,angle1,angle2,angle3]) -- Создает тор по заданными радиусам.По умолчанию p=Vector(0,0,0), d=Vector(0,0,1), angle1=0, angle2=360 и angle3=360

На странице Part API приведен полный список доступных методов модуля Part.

наверх

Импорт необходимых модулей

В начале нам нужно импортировать модуль Part, чтобы мы могли использовать его содержимое в Python. Также импортируем модуль Base из модуля FreeCAD:

import FreeCAD as App
import Part

наверх

Создание вектора

Векторы являются одними из самых важных частей информации при построении фигур. Они обычно содержат три числа (но не всегда): декартовы координаты x, y и z. Для создания вектора введите:

myVector = App.Vector(3, 2, 0)

Мы только что создали вектор с координатами x = 3, y = 2, z = 0. В модуле Part векторы используются повсеместно. Формы детали также используют другой тип представления точек, называемый Vertex, который является просто контейнером для вектора. Вы можете получить доступ к вектору вершины следующим образом:

myVertex = myShape.Vertexes[0]
print(myVertex.Point)
> Vector (3, 2, 0)

наверх

Создание ребра

Ребра это не что иное, как линия с двумя вершинами:

edge = Part.makeLine((0, 0, 0), (10, 0, 0))
edge.Vertexes
> [<Vertex object at 01877430>, <Vertex object at 014888E0>]

Примечание: Вы можете создать ребро передав два вектора.

vec1 = App.Vector(0, 0, 0)
vec2 = App.Vector(10, 0, 0)
line = Part.LineSegment(vec1, vec2)
edge = line.toShape()

Вы можете узнать длину и центр ребра, вот так:

edge.Length
> 10.0
edge.CenterOfMass
> Vector (5, 0, 0)

наверх

Вывод фигуры на экран

До сих пор мы создали объект ребро, но не увидели его на экране. Это связано с тем, что 3D-сцена FreeCAD отображает только то, что указано для отображения. Для этого мы используем этот простой метод:

Part.show(edge)

Функция show создает объект "shape" в нашем FreeCAD документе. Используйте это всякий раз, когда пришло время показать свое творение на экране.

наверх

Создание ломанной кривой

Ломаная представляет собой многогранную линию и может быть создан из списка ребер или даже из списка ломаных:

edge1 = Part.makeLine((0, 0, 0), (10, 0, 0))
edge2 = Part.makeLine((10, 0, 0), (10, 10, 0))
wire1 = Part.Wire([edge1, edge2]) 
edge3 = Part.makeLine((10, 10, 0), (0, 10, 0))
edge4 = Part.makeLine((0, 10, 0), (0, 0, 0))
wire2 = Part.Wire([edge3, edge4])
wire3 = Part.Wire([wire1, wire2])
wire3.Edges
> [<Edge object at 016695F8>, <Edge object at 0197AED8>, <Edge object at 01828B20>, <Edge object at 0190A788>]
Part.show(wire3)

Part.show(wire3) пакажет 4 ребра, из которых состоит наша ломаная линяи. Другая полезная информация может быть легко найдена:

wire3.Length
> 40.0
wire3.CenterOfMass
> Vector (5, 5, 0)
wire3.isClosed()
> True
wire2.isClosed()
> False

наверх

Создание грани

Только грани, созданные из замкнутых ломаных, будут действительными. В этом примере wire3 является замкнутой ломаной, но wire2 не является замкнутым (см. выше)

face = Part.Face(wire3)
face.Area
> 99.99999999999999
face.CenterOfMass
> Vector (5, 5, 0)
face.Length
> 40.0
face.isValid()
> True
sface = Part.Face(wire2)
sface.isValid()
> False

Только грани имеют поверхность, а ломанные и ребра нет.

наверх

Создание окружности

Окружность может быть создана, например так:

circle = Part.makeCircle(10)
circle.Curve
> Circle (Radius : 10, Position : (0, 0, 0), Direction : (0, 0, 1))

Если вы хотите создать её с определенным положением и в определенном направлении

ccircle = Part.makeCircle(10, App.Vector(10, 0, 0), App.Vector(1, 0, 0))
ccircle.Curve
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0))

ccircle будет создана на расстоянии 10 от начала координат x и будет направлена вдоль оси x. Примечание: makeCircle принимает только тип Base.Vector() в качестве позиции и нормали. Вы также можете создать часть окружности, задав начальный и конечный угол:

from math import pi
arc1 = Part.makeCircle(10, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 0, 180)
arc2 = Part.makeCircle(10, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 180, 360)

Обе arc1 и arc2 вместе составляют окружность. Углы задаются в градусах, если вы хотите задать радианами, просто преобразуйте используя формулу: degrees = radians * 180/PI или используя math модуль python-а (прежде, конечно, выполнив import math): degrees = math.degrees(radians)

import math
degrees = math.degrees(radians)

наверх

Создать дугу по точкам

К сожалению нет функции makeArc, но у нас есть функция Part.Arc для создания дуги через три точки. Она создает объект дуги, соединяющий начальную точку с конечной точкой через среднюю точку. Функция .toShape() объекта дуги должна вызываться для получения объекта ребра, так же, как при использовании Part.LineSegment вместо Part.makeLine.

arc = Part.Arc(App.Vector(0, 0, 0), App.Vector(0, 5, 0), App.Vector(5, 5, 0))
arc
> <Arc object>
arc_edge = arc.toShape()
Part.show(arc_edge)

Arc принимает только Base.Vector() для точек. arc_edge - это то, что нам нужно, и мы можем отобразить его с помощью Part.show(arc_edge). Вы также можете получить дугу, используя часть круга:

from math import pi
circle = Part.Circle(App.Vector(0, 0, 0), App.Vector(0, 0, 1), 10)
arc = Part.Arc(circle,0,pi)

Дуги являются действительными ребрами, такими как линии, поэтому их можно использовать и в ломаных линиях.

наверх

Создать многоугольник (полигон)

Линия по нескольким точкам, не что иное как создание ломаной с множеством ребер. функция makePolygon берет список точек и создает ломанную по этим точкам:

lshape_wire = Part.makePolygon([App.Vector(0, 5, 0), App.Vector(0, 0, 0), App.Vector(5, 0, 0)])

наверх

Создание кривой Безье

Кривые Безье используются для моделирования гладких кривых с использованием ряда полюсов (точек) и необязательных весов. Функция ниже делает Part.BezierCurve из ряда точек FreeCAD.Vector. (Примечание: при «получении» и «установке» одного полюса или веса индексы начинаются с 1, а не с 0.)

def makeBCurveEdge(Points):
   geomCurve = Part.BezierCurve()
   geomCurve.setPoles(Points)
   edge = Part.Edge(geomCurve)
   return(edge)

наверх

Создание плоскости

Плоскость это ровная поверхность, в смысле 2D грань. Метод создания её это makePlane(length,width,[start_pnt,dir_normal]). По умолчанию start_pnt=Vector(0,0,0) и dir_normal=Vector(0,0,1). Используя dir_normal = Vector(0,0,1) создаёт плоскость, обращённую к положительному направлению оси z, в то время как dir_normal=Vector(1,0,0) создаёт плоскость обращённую к положительному направлению оси х:

plane = Part.makePlane(2, 2)
plane
> <Face object at 028AF990>
plane = Part.makePlane(2, 2, App.Vector(3, 0, 0), App.Vector(0, 1, 0))
plane.BoundBox
> BoundBox (3, 0, 0, 5, 0, 2)

BoundBox является параллелепипед вмещающих плоскость с диагональю, начиная с (3,0,0) и концом в (5,0,2). Здесь толщина BoundBoxпо оси y равна нулю, поскольку его форма полностью плоская.

Примечание: makePlane доступны только Base.Vector() для задания start_pnt и dir_normal а не кортежи

наверх

Создание эллипса

Эллипс можно создать несколькими способами:

Part.Ellipse()

Создает эллипс с большой полуосью 2 и малой полуосью 1 с центром в (0,0,0)

Part.Ellipse(Ellipse)

Создает копию данного эллипса.

Part.Ellipse(S1, S2, Center)

Создаст эллипс с центров точке Center, где плоскость эллипса определяет Center, S1 и S2, это большая ось ззаданная Center и S1, это больший радиус расстояние между Center и S1, и меньший радиус это расстояние между S2 и юольшей осью.

Part.Ellipse(Center, MajorRadius, MinorRadius)

Создает эллипс с большим и меньшим радиусом MajorRadius и MinorRadius, расположенными в плоскости заданной точкой Center и нормалью (0,0,1)

eli = Part.Ellipse(App.Vector(10, 0, 0), App.Vector(0, 5, 0), App.Vector(0, 0, 0))
Part.show(eli.toShape())

В приведенном выше коде мы ввели S1, S2 и center. Аналогично Дуге, Эллипс также создает объект, а не ребро, так что мы должны превратить его в ребро используя toShape() для отображения

Примечание: Дуга допускает только Base.Vector() для задания точек, а не кортеж.

eli = Part.Ellipse(App.Vector(0, 0, 0), 10, 5)
Part.show(eli.toShape())

для вышеуказанного конструктора Ellipse мы передали center, MajorRadius и MinorRadius.

наверх

Создание тора

Используя makeTorus(radius1,radius2,[pnt,dir,angle1,angle2,angle]). По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=0,angle2=360 и angle=360

Рассмотрим тор как маленький круг, вытянутый вдоль большого круга. Radius1 это радиус большого круга, radius2 это радиус малого круга, pnt это центр тора и dir это направление нормали. angle1 и angle2 углы в радианах для малого круга, последний параметр angle для создания секцию (части) тора:

torus = Part.makeTorus(10, 2)

В коде выше, был создан тор с диаметром 20 (радиус 10) и толщиной 4 (малая окружность радиусом 2)

tor=Part.makeTorus(10, 5, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 0, 180)

В приведенном выше коде, создан кусочек тора.

tor=Part.makeTorus(10, 5, App.Vector(0, 0, 0), App.Vector(0, 0, 1), 0, 360, 180)

В приведенном выше коде, создан полу-тор, изменен только последний параметр. Т.е. angle а остальные углы установлены по умолчанию. Подстановка угла 180 создаст тор от 0 до 180, т.е. половину тора.

наверх

Создание параллелепипеда или кубоида

Используя makeBox(length,width,height,[pnt,dir]), создаем блок расположенный в pnt с размерами (length,width,height). По умолчанию pnt=Vector(0,0,0) и dir=Vector(0,0,1).

box = Part.makeBox(10, 10, 10)
len(box.Vertexes)
> 8

наверх

Создание сферы

Используя makeSphere(radius,[pnt, dir, angle1,angle2,angle3]). По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=-90, angle2=90 и angle3=360. angle1 и angle2 это вертикальный минимум и максимум сферы (срезает часть сферы снизу или сверху), angle3 is the sphere diameter (определяет замкнутое ли это тело вращения или его секция).

sphere = Part.makeSphere(10)
hemisphere = Part.makeSphere(10, App.Vector(0, 0, 0), App.Vector(0, 0, 1), -90, 90, 180)

наверх

Создание цилиндра

Используя makeCylinder(radius,height,[pnt,dir,angle]), создается цилиндр с указанным радиусом и высотой. По умолчанию pnt=Vector(0,0,0),dir=Vector(0,0,1) и angle=360.
cylinder = Part.makeCylinder(5, 20)
partCylinder = Part.makeCylinder(5, 20, App.Vector(20, 0, 0), App.Vector(0, 0, 1), 180)

наверх

Cоздание конуса

Используя makeCone(radius1,radius2,height,[pnt,dir,angle]), создаем конус с указанными радиусами и высотой. По умолчанию pnt=Vector(0,0,0), dir=Vector(0,0,1) и angle=360.

cone = Part.makeCone(10, 0, 20)
semicone = Part.makeCone(10, 0, 20, App.Vector(20, 0, 0), App.Vector(0, 0, 1), 180)

наверх

Modify shapes

There are several ways to modify shapes. Some are simple transformation operations such as moving or rotating shapes, others are more complex, such as unioning and subtracting one shape from another.

наверх

Transform operations

Translate a shape

Translating is the act of moving a shape from one place to another. Any shape (edge, face, cube, etc...) can be translated the same way:

myShape = Part.makeBox(2, 2, 2)
myShape.translate(App.Vector(2, 0, 0))

This will move our shape "myShape" 2 units in the X direction.

наверх

Rotate a shape

To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle:

myShape.rotate(App.Vector(0, 0, 0),App.Vector(0, 0, 1), 180)

The above code will rotate the shape 180 degrees around the Z Axis.

наверх

Matrix transformations

A matrix is a very convenient way to store transformations in the 3D world. In a single matrix, you can set translation, rotation and scaling values to be applied to an object. For example:

myMat = App.Matrix()
myMat.move(App.Vector(2, 0, 0))
myMat.rotateZ(math.pi/2)

Note: FreeCAD matrixes work in radians. Also, almost all matrix operations that take a vector can also take three numbers, so these two lines do the same thing:

myMat.move(2, 0, 0)
myMat.move(App.Vector(2, 0, 0))

Once our matrix is set, we can apply it to our shape. FreeCAD provides two methods for doing that: transformShape() and transformGeometry(). The difference is that with the first one, you are sure that no deformations will occur (see Scaling a shape below). We can apply our transformation like this:

myShape.transformShape(myMat)

или

myShape.transformGeometry(myMat)

наверх

Scale a shape

Scaling a shape is a more dangerous operation because, unlike translation or rotation, scaling non-uniformly (with different values for X, Y and Z) can modify the structure of the shape. For example, scaling a circle with a higher value horizontally than vertically will transform it into an ellipse, which behaves mathematically very differently. For scaling, we cannot use the transformShape(), we must use transformGeometry():

myMat = App.Matrix()
myMat.scale(2, 1, 1)
myShape=myShape.transformGeometry(myMat)

наверх

Булевы Операции

Как вырезать одну форму из других?

cut(...) - Вычисление различий задано в топологическом классе shape.

Subtracting a shape from another one is called "cut" in FreeCAD and is done like this:

cylinder = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
sphere = Part.makeSphere(5, App.Vector(5, 0, 0))
diff = cylinder.cut(sphere)

наверх

Как получить пересечение двух форм?

Тем же способом, пересечение между двумя фигурами называется "common(...)" (пересечение задано в топологическом классе shape) и делается так:

The same way, the intersection between two shapes is called "common" and is done this way:

cylinder1 = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, App.Vector(5, 0, -5), App.Vector(0, 0, 1))
common = cylinder1.common(cylinder2)

наверх

Как объединить две формы?

fuse(...) - Объединение задано в топологическом классе shape

Union is called "fuse" and works the same way:

cylinder1 = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, App.Vector(5, 0, -5), App.Vector(0, 0, 1))
fuse = cylinder1.fuse(cylinder2)

наверх

Как получить сечение тела и заданной формы?

Section это пересечение твердого тела и плоской фигуры (сечение задано в топологическом классе shape). Вернет секущую кривую, составную кривую, состоящую из ребер.

A "section" is the intersection between a solid shape and a plane shape. It will return an intersection curve, a compound curve composed of edges.

cylinder1 = Part.makeCylinder(3, 10, App.Vector(0, 0, 0), App.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, App.Vector(5, 0, -5), App.Vector(0, 0, 1))
section = cylinder1.section(cylinder2)
section.Wires
> []
section.Edges
> [<Edge object at 0D87CFE8>, <Edge object at 019564F8>, <Edge object at 0D998458>, 
 <Edge  object at 0D86DE18>, <Edge object at 0D9B8E80>, <Edge object at 012A3640>, 
 <Edge object at 0D8F4BB0>]

наверх

Выдавливание

Выдавливание - это процесс «выпячивания» плоской фигуры в определенном направлении, становящейся твердым телом. Представьте, как «выпячивание» круга сделало его трубой:

Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in a solid body. Think of a circle becoming a tube by "pushing it out":

circle = Part.makeCircle(10)
tube = circle.extrude(App.Vector(0, 0, 2))

Если ваш круг полый, вы получите полую трубу. Если ваш круг это диск с заполненной поверхностью, вы получите сплошной цилиндр:

wire = Part.Wire(circle)
disc = Part.Face(wire)
cylinder = disc.extrude(App.Vector(0, 0, 2))

наверх

Исследование Форм

Вы легко можете исследовать структуру топологических данных:

You can easily explore the topological data structure:

import Part
b = Part.makeBox(100, 100, 100)
b.Wires
w = b.Wires[0]
w
w.Wires
w.Vertexes
Part.show(w)
w.Edges
e = w.Edges[0]
e.Vertexes
v = e.Vertexes[0]
v.Point

Если ввести строчку выше в интерпретатор python , вы получите хорошее представление об устройстве объектов Part. Здесь наша команда makeBox() создает твердое тело. Это тело, как и все тела Part, содержит грани. Грани всегда содержат ломанные, которые являются набором ребер ограничивающих грань. Каждая грань обладает минимум одной замкнутой ломаной (может больше, если есть отверстие). В ломанной мы можем посмотреть на каждое ребро отдельно, и по краям каждого ребра мы можем увидеть вершины. Прямые ребра обладают только двумя вершинами, разумеется. Вершины модуля Part являются формами OCC(OpenCascade), но они обладают атрибутом Point, который возвращает вектор FreeCAD.

наверх

Исследование Рёбер

В случае ребра, которое является произвольной кривой, вы наверняка захотите произвести дискретизицию. В FreeCAD ребра задаются с помощью параметра длинны. Это означает что вы можете перемещатся вдоль ребра/кривой задавая длинну:

In case of an edge, which is an arbitrary curve, it's most likely you want to do a discretization. In FreeCAD the edges are parametrized by their lengths. That means you can walk an edge/curve by its length:

import Part
box = Part.makeBox(100, 100, 100)
anEdge = box.Edges[0]
print(anEdge.Length)

Теперь вы получить доступ ко всем свойствам ребра, с помощью длинны или позиции. Это означает, что у ребра в 100mm длинной, начальная позиция это 0 а конечная это 100.

anEdge.tangentAt(0.0)          # tangent direction at the beginning
anEdge.valueAt(0.0)            # Point at the beginning
anEdge.valueAt(100.0)          # Point at the end of the edge
anEdge.derivative1At(50.0)     # first derivative of the curve in the middle
anEdge.derivative2At(50.0)     # second derivative of the curve in the middle
anEdge.derivative3At(50.0)     # third derivative of the curve in the middle
anEdge.centerOfCurvatureAt(50) # center of the curvature for that position
anEdge.curvatureAt(50.0)       # the curvature
anEdge.normalAt(50)            # normal vector at that position (if defined)

наверх

Использование выделения(выбора)

Здесь мы увидим как можно использовать "выделение", которое пользователь сделал в программе просмотра. прежде всего мы создадим блок и отобразим его в окне просмотра.

Here we see now how we can use a selection the user did in the viewer. First of all we create a box and show it in the viewer.

import Part
Part.show(Part.makeBox(100, 100, 100))
Gui.SendMsgToActiveView("ViewFit")

Теперь выберем грани или ребра. С помощью этого сценария вы можете повторить по всем выделенным объектам и их субэлементам:

for o in Gui.Selection.getSelectionEx():
    print(o.ObjectName)
    for s in o.SubElementNames:
        print("name: ", s)
        for s in o.SubObjects:
            print("object: ", s)

Выделим несколько ребер и этот сценарий подсчитает их сумарную длину:

length = 0.0
for o in Gui.Selection.getSelectionEx():
    for s in o.SubObjects:
        length += s.Length

print("Length of the selected edges: ", length)

наверх

Полный пример: бутыль OCC

Типовой пример, взятый на OpenCasCade Technology Tutorial - это как построить бутыль. Это отличный пример и для FreeCAD. В самом деле, если последуете нашему примеру изложенному ниже и странице OCC одновременно, вы лучше поймете как структуры OCC реализованы в FreeCAD. Готовый сценарий описанный ниже, также включен в установленный FreeCAD (в папке Mod/Part) и может быть вызван интерпретатором python, вводом:

A typical example found on the OpenCasCade Technology website is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the Mod/Part folder) and can be called from the Python interpreter by typing:

import Part
import MakeBottle
bottle = MakeBottle.makeBottle()
Part.show(bottle)

наверх

Готовый сценарий

Здесь представлен готовый сценарий MakeBottle:

For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded.

import FreeCAD as App
import Part, math

def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
    aPnt1=App.Vector(-myWidth / 2., 0, 0)
    aPnt2=App.Vector(-myWidth / 2., -myThickness / 4., 0)
    aPnt3=App.Vector(0, -myThickness / 2., 0)
    aPnt4=App.Vector(myWidth / 2., -myThickness / 4., 0)
    aPnt5=App.Vector(myWidth / 2., 0, 0)

    aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
    aSegment1=Part.LineSegment(aPnt1, aPnt2)
    aSegment2=Part.LineSegment(aPnt4, aPnt5)

    aEdge1=aSegment1.toShape()
    aEdge2=aArcOfCircle.toShape()
    aEdge3=aSegment2.toShape()
    aWire=Part.Wire([aEdge1, aEdge2, aEdge3])

    aTrsf=App.Matrix()
    aTrsf.rotateZ(math.pi) # rotate around the z-axis

    aMirroredWire=aWire.copy()
    aMirroredWire.transformShape(aTrsf)
    myWireProfile=Part.Wire([aWire, aMirroredWire])

    myFaceProfile=Part.Face(myWireProfile)
    aPrismVec=App.Vector(0, 0, myHeight)
    myBody=myFaceProfile.extrude(aPrismVec)

    myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)

    neckLocation=App.Vector(0, 0, myHeight)
    neckNormal=App.Vector(0, 0, 1)

    myNeckRadius = myThickness / 4.
    myNeckHeight = myHeight / 10.
    myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
    myBody = myBody.fuse(myNeck)

    return myBody

el = makeBottleTut()
Part.show(el)

наверх

Подробные объяснения

import FreeCAD as App
import Part, math

Нам, конечно, необходимы модуль Part, а также модуль FreeCAD.Base, который содержит основные структуры FreeCAD, такие как векторы и матрицы.

def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
    aPnt1=App.Vector(-myWidth / 2., 0, 0)
    aPnt2=App.Vector(-myWidth / 2., -myThickness / 4., 0)
    aPnt3=App.Vector(0, -myThickness / 2., 0)
    aPnt4=App.Vector(myWidth / 2., -myThickness / 4., 0)
    aPnt5=App.Vector(myWidth / 2., 0, 0)

Здесь мы задаем нашу функцию makeBottleTut. Эта функция может быть вызвана без аргументов, как мы делали выше, в этом случае будут использоваться значения по умолчанию для ширины, высоты и толщины. Затем мы определили несколько точек которые будут использоваться для построения базового сечения.

...
    aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
    aSegment1=Part.LineSegment(aPnt1, aPnt2)
    aSegment2=Part.LineSegment(aPnt4, aPnt5)

Здесь мы задаём геометрию: дугу, созданую по 3 точкам, и два линейных сегмента, созданные по 2 точкам.

...
    aEdge1=aSegment1.toShape()
    aEdge2=aArcOfCircle.toShape()
    aEdge3=aSegment2.toShape()
    aWire=Part.Wire([aEdge1, aEdge2, aEdge3])

Запомнили различие между геометрией и формой? Здесь мы создаем форму из нашей строительной геометрии. Три рёбра (ребра могут быть прямыми или кривыми), затем из этих трёх рёбер создается ломанная.

...
    aTrsf=App.Matrix()
    aTrsf.rotateZ(math.pi) # rotate around the z-axis

    aMirroredWire=aWire.copy()
    aMirroredWire.transformShape(aTrsf)
    myWireProfile=Part.Wire([aWire, aMirroredWire])

Пока мы построили только половину сечения. Вместо построения таким же образом целого профиля, мы можем просто отразить то, что мы сделали, и склеить две половинки. Сначала создадим матрицу. Матрица является распространенным способом произвести изменения над объектом в трёхмерном пространстве, поскольку она может содержать в одной структуре все базовые преобразования, которым могут подвергаться трёхмерные объекты (перемещение, вращение и масштабирование). После создания матрицы, мы отражаем её, затем создаем копию нашей ломанной и применяем к ней трансформационную матрицу. Теперь мы получили две ломанные и мы можем создать из них третью ломаную, так как ломанные это всего лишь список ребер.

...
    myFaceProfile=Part.Face(myWireProfile)
    aPrismVec=App.Vector(0, 0, myHeight)
    myBody=myFaceProfile.extrude(aPrismVec)

    myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)

Теперь мы получили замкнутую ломаную, которую можно обратить в грань. Когда мы имеем грань, мы можем вытянуть её. Сделав это, мы получим твердое тело. Теперь мы добавим небольшое скругление к нашему объекту, потому что мы заботимся о качественном дизайне, не так ли?

...
    neckLocation=App.Vector(0, 0, myHeight)
    neckNormal=App.Vector(0, 0, 1)

    myNeckRadius = myThickness / 4.
    myNeckHeight = myHeight / 10.
    myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)

Теперь тело нашей бутыли создано, но нам нужно создать горлышко. Так что мы создаем новое твердое тело, с цилиндром.

...
    myBody = myBody.fuse(myNeck)

Операция слияния очень мощная. Она заботится о склеивании, о том, что должно быть приклеено и удаляет части, которые следует удалить.

...
    return myBody

Теперь мы получаем нашу твёрдое тело модуля Part как результат нашей функции.

el = makeBottleTut()
Part.show(el)

В итоге мы вызываем функцию для фактического создания детали, а потом делаем её видимой.

наверх

Example: Pierced box

Here is a complete example of building a pierced box.

Конструкция делается по одной стороне за раз. Когда куб закончен, он выдалбливается вырезанием цилиндра через него.

import FreeCAD as App
import Part, math

size = 10
poly = Part.makePolygon([(0, 0, 0), (size, 0, 0), (size, 0, size), (0, 0, size), (0, 0, 0)])

face1 = Part.Face(poly)
face2 = Part.Face(poly)
face3 = Part.Face(poly)
face4 = Part.Face(poly)
face5 = Part.Face(poly)
face6 = Part.Face(poly)
     
myMat = App.Matrix()

myMat.rotateZ(math.pi / 2)
face2.transformShape(myMat)
face2.translate(App.Vector(size, 0, 0))

myMat.rotateZ(math.pi / 2)
face3.transformShape(myMat)
face3.translate(App.Vector(size, size, 0))

myMat.rotateZ(math.pi / 2)
face4.transformShape(myMat)
face4.translate(App.Vector(0, size, 0))

myMat = App.Matrix()

myMat.rotateX(-math.pi / 2)
face5.transformShape(myMat)

face6.transformShape(myMat)               
face6.translate(App.Vector(0, 0, size))

myShell = Part.makeShell([face1, face2, face3, face4, face5, face6])   
mySolid = Part.makeSolid(myShell)

myCyl = Part.makeCylinder(2, 20)
myCyl.translate(App.Vector(size / 2, size / 2, 0))

cut_part = mySolid.cut(myCyl)

Part.show(cut_part)

наверх

Загрузка и Сохранение

Существует несколько способов сохранить вашу работу. Вы конечно можете сохранить ваш FreeCAD документ, а также вы можете сохранить Part(Деталь) объект напрямую в обычные CAD форматы, такие как BREP, IGS, STEP и STL.

Сохранить форму в файл легко. Есть доступные для всех форм методы exportBrep(), exportIges(), exportStep() и exportStl(). Таким образом:

import Part
s = Part.makeBox(10, 10, 10)
s.exportStep("test.stp")

это сохранит наш блок в файл формата STEP. Для загрузки BREP, IGES или STEP файлов:

import Part
s = Part.Shape()
s.read("test.stp")

Для преобразования файла STEP в файл IGS:

import Part
 s = Part.Shape()
 s.read("file.stp")       # incoming file igs, stp, stl, brep
 s.exportIges("file.igs") # outbound file igs

наверх