FreeCAD
81 строка · 3.5 Кб
1# ***************************************************************************
2# * Copyright (c) 2014 Johan Kristensen *
3# * Copyright (c) 2014 Juergen Riegel <FreeCAD@juergen-riegel.net> *
4# * *
5# * This program is free software; you can redistribute it and/or modify *
6# * it under the terms of the GNU Lesser General Public License (LGPL) *
7# * as published by the Free Software Foundation; either version 2 of *
8# * the License, or (at your option) any later version. *
9# * for detail see the LICENCE text file. *
10# * *
11# * This program is distributed in the hope that it will be useful, *
12# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14# * GNU Library General Public License for more details. *
15# * *
16# * You should have received a copy of the GNU Library General Public *
17# * License along with this program; if not, write to the Free Software *
18# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
19# * USA *
20# * *
21# ***************************************************************************
22
23import FreeCAD, FreeCADGui, Sketcher, Part, math
24
25
26__title__ = "Regular polygon profile lib"
27__author__ = "Johan Kristensen"
28__url__ = "https://www.freecad.org"
29
30App = FreeCAD
31Gui = FreeCADGui
32
33
34def makeRegularPolygon(
35sketch,
36sides,
37centerPoint=App.Vector(0, 0, 0),
38firstCornerPoint=App.Vector(-20.00, 34.64, 0),
39construction=False,
40):
41
42if not sketch:
43App.Console.PrintError("No sketch specified in 'makeRegularPolygon'")
44return
45if sides < 3:
46App.Console.PrintError("Number of sides must be at least 3 in 'makeRegularPolygon'")
47return
48
49diffVec = firstCornerPoint - centerPoint
50diffVec.z = 0
51angular_diff = 2 * math.pi / sides
52pointList = []
53for i in range(0, sides):
54cos_v = math.cos(angular_diff * i)
55sin_v = math.sin(angular_diff * i)
56pointList.append(
57centerPoint
58+ App.Vector(
59cos_v * diffVec.x - sin_v * diffVec.y, cos_v * diffVec.y + sin_v * diffVec.x, 0
60)
61)
62
63geoList = []
64for i in range(0, sides - 1):
65geoList.append(Part.LineSegment(pointList[i], pointList[i + 1]))
66geoList.append(Part.LineSegment(pointList[sides - 1], pointList[0]))
67geoList.append(Part.Circle(centerPoint, App.Vector(0, 0, 1), diffVec.Length))
68geoIndices = sketch.addGeometry(geoList, construction)
69
70sketch.setConstruction(geoIndices[-1], True)
71
72conList = []
73for i in range(0, sides - 1):
74conList.append(Sketcher.Constraint("Coincident", geoIndices[i], 2, geoIndices[i + 1], 1))
75conList.append(Sketcher.Constraint("Coincident", geoIndices[sides - 1], 2, geoIndices[0], 1))
76for i in range(0, sides - 1):
77conList.append(Sketcher.Constraint("Equal", geoIndices[0], geoIndices[i + 1]))
78for i in range(0, sides):
79conList.append(Sketcher.Constraint("PointOnObject", geoIndices[i], 2, geoIndices[-1]))
80sketch.addConstraint(conList)
81return
82