FreeCAD
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# @{
30import math31
32import FreeCAD as App33import Part34import DraftGeomUtils35import draftutils.utils as utils36import draftutils.gui_utils as gui_utils37
38from draftobjects.circle import Circle39
40if App.GuiUp:41from draftviewproviders.view_base import ViewProviderDraft42
43
44def make_circle(radius, placement=None, face=None, startangle=None, endangle=None, support=None):45"""make_circle(radius, [placement, face, startangle, endangle])46or make_circle(edge,[face]):
47
48Creates a circle object with given parameters.
49
50If startangle and endangle are provided and not equal, the object will show
51an arc instead of a full circle.
52
53Parameters
54----------
55radius : the radius of the circle.
56
57placement :
58If placement is given, it is used.
59
60face : Bool
61If face is False, the circle is shown as a wireframe,
62otherwise as a face.
63
64startangle : start angle of the circle (in degrees)
65Recalculated if not in the -360 to 360 range.
66
67endangle : end angle of the circle (in degrees)
68Recalculated if not in the -360 to 360 range.
69
70edge : edge.Curve must be a 'Part.Circle'
71The circle is created from the given edge.
72
73support :
74TODO: Describe
75"""
76
77if not App.ActiveDocument:78App.Console.PrintError("No active document. Aborting\n")79return80
81if placement:82utils.type_check([(placement,App.Placement)], "make_circle")83
84if startangle != endangle:85_name = "Arc"86else:87_name = "Circle"88
89obj = App.ActiveDocument.addObject("Part::Part2DObjectPython", _name)90
91Circle(obj)92
93if face is not None:94obj.MakeFace = face95
96if isinstance(radius,Part.Edge):97edge = radius98if DraftGeomUtils.geomType(edge) == "Circle":99obj.Radius = edge.Curve.Radius100placement = App.Placement(edge.Placement)101delta = edge.Curve.Center.sub(placement.Base)102placement.move(delta)103# Rotation of the edge104rotOk = App.Rotation(edge.Curve.XAxis, edge.Curve.YAxis, edge.Curve.Axis, "ZXY")105placement.Rotation = rotOk106if len(edge.Vertexes) > 1:107v0 = edge.Curve.XAxis108v1 = (edge.Vertexes[0].Point).sub(edge.Curve.Center)109v2 = (edge.Vertexes[-1].Point).sub(edge.Curve.Center)110# Angle between edge.Curve.XAxis and the vector from center to start of arc111a0 = math.degrees(App.Vector.getAngle(v0, v1))112# Angle between edge.Curve.XAxis and the vector from center to end of arc113a1 = math.degrees(App.Vector.getAngle(v0, v2))114obj.FirstAngle = a0115obj.LastAngle = a1116else:117obj.Radius = radius118if (startangle is not None) and (endangle is not None):119obj.FirstAngle = math.copysign(abs(startangle) % 360, startangle)120obj.LastAngle = math.copysign(abs(endangle) % 360, endangle)121
122obj.AttachmentSupport = support123
124if placement:125obj.Placement = placement126
127if App.GuiUp:128ViewProviderDraft(obj.ViewObject)129gui_utils.format_object(obj)130gui_utils.select(obj)131
132return obj133
134
135makeCircle = make_circle136
137## @}
138