FreeCAD

Форк
0
/
make_circle.py 
137 строк · 5.0 Кб
1
# ***************************************************************************
2
# *   Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net>        *
3
# *   Copyright (c) 2009, 2010 Ken Cline <cline@frii.com>                   *
4
# *   Copyright (c) 2020 FreeCAD Developers                                 *
5
# *                                                                         *
6
# *   This program is free software; you can redistribute it and/or modify  *
7
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
8
# *   as published by the Free Software Foundation; either version 2 of     *
9
# *   the License, or (at your option) any later version.                   *
10
# *   for detail see the LICENCE text file.                                 *
11
# *                                                                         *
12
# *   This program is distributed in the hope that it will be useful,       *
13
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
# *   GNU Library General Public License for more details.                  *
16
# *                                                                         *
17
# *   You should have received a copy of the GNU Library General Public     *
18
# *   License along with this program; if not, write to the Free Software   *
19
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
20
# *   USA                                                                   *
21
# *                                                                         *
22
# ***************************************************************************
23
"""Provides functions to create Circle objects."""
24
## @package make_circle
25
# \ingroup draftmake
26
# \brief Provides functions to create Circle objects.
27

28
## \addtogroup draftmake
29
# @{
30
import math
31

32
import FreeCAD as App
33
import Part
34
import DraftGeomUtils
35
import draftutils.utils as utils
36
import draftutils.gui_utils as gui_utils
37

38
from draftobjects.circle import Circle
39

40
if App.GuiUp:
41
    from draftviewproviders.view_base import ViewProviderDraft
42

43

44
def make_circle(radius, placement=None, face=None, startangle=None, endangle=None, support=None):
45
    """make_circle(radius, [placement, face, startangle, endangle])
46
    or make_circle(edge,[face]):
47

48
    Creates a circle object with given parameters.
49

50
    If startangle and endangle are provided and not equal, the object will show
51
    an arc instead of a full circle.
52

53
    Parameters
54
    ----------
55
    radius : the radius of the circle.
56

57
    placement :
58
        If placement is given, it is used.
59

60
    face : Bool
61
        If face is False, the circle is shown as a wireframe,
62
        otherwise as a face.
63

64
    startangle : start angle of the circle (in degrees)
65
        Recalculated if not in the -360 to 360 range.
66

67
    endangle : end angle of the circle (in degrees)
68
        Recalculated if not in the -360 to 360 range.
69

70
    edge : edge.Curve must be a 'Part.Circle'
71
        The circle is created from the given edge.
72

73
    support :
74
        TODO: Describe
75
    """
76

77
    if not App.ActiveDocument:
78
        App.Console.PrintError("No active document. Aborting\n")
79
        return
80

81
    if placement:
82
        utils.type_check([(placement,App.Placement)], "make_circle")
83

84
    if startangle != endangle:
85
        _name = "Arc"
86
    else:
87
        _name = "Circle"
88

89
    obj = App.ActiveDocument.addObject("Part::Part2DObjectPython", _name)
90

91
    Circle(obj)
92

93
    if face is not None:
94
        obj.MakeFace = face
95

96
    if isinstance(radius,Part.Edge):
97
        edge = radius
98
        if DraftGeomUtils.geomType(edge) == "Circle":
99
            obj.Radius = edge.Curve.Radius
100
            placement = App.Placement(edge.Placement)
101
            delta = edge.Curve.Center.sub(placement.Base)
102
            placement.move(delta)
103
            # Rotation of the edge
104
            rotOk = App.Rotation(edge.Curve.XAxis, edge.Curve.YAxis, edge.Curve.Axis, "ZXY")
105
            placement.Rotation = rotOk
106
            if len(edge.Vertexes) > 1:
107
                v0 = edge.Curve.XAxis
108
                v1 = (edge.Vertexes[0].Point).sub(edge.Curve.Center)
109
                v2 = (edge.Vertexes[-1].Point).sub(edge.Curve.Center)
110
                # Angle between edge.Curve.XAxis and the vector from center to start of arc
111
                a0 = math.degrees(App.Vector.getAngle(v0, v1))
112
                # Angle between edge.Curve.XAxis and the vector from center to end of arc
113
                a1 = math.degrees(App.Vector.getAngle(v0, v2))
114
                obj.FirstAngle = a0
115
                obj.LastAngle = a1
116
    else:
117
        obj.Radius = radius
118
        if (startangle is not None) and (endangle is not None):
119
            obj.FirstAngle = math.copysign(abs(startangle) % 360, startangle)
120
            obj.LastAngle = math.copysign(abs(endangle) % 360, endangle)
121

122
    obj.AttachmentSupport = support
123

124
    if placement:
125
        obj.Placement = placement
126

127
    if App.GuiUp:
128
        ViewProviderDraft(obj.ViewObject)
129
        gui_utils.format_object(obj)
130
        gui_utils.select(obj)
131

132
    return obj
133

134

135
makeCircle = make_circle
136

137
## @}
138

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.