FreeCAD

Форк
0
/
RegularPolygon.py 
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

23
import FreeCAD, FreeCADGui, Sketcher, Part, math
24

25

26
__title__ = "Regular polygon profile lib"
27
__author__ = "Johan Kristensen"
28
__url__ = "https://www.freecad.org"
29

30
App = FreeCAD
31
Gui = FreeCADGui
32

33

34
def makeRegularPolygon(
35
    sketch,
36
    sides,
37
    centerPoint=App.Vector(0, 0, 0),
38
    firstCornerPoint=App.Vector(-20.00, 34.64, 0),
39
    construction=False,
40
):
41

42
    if not sketch:
43
        App.Console.PrintError("No sketch specified in 'makeRegularPolygon'")
44
        return
45
    if sides < 3:
46
        App.Console.PrintError("Number of sides must be at least 3 in 'makeRegularPolygon'")
47
        return
48

49
    diffVec = firstCornerPoint - centerPoint
50
    diffVec.z = 0
51
    angular_diff = 2 * math.pi / sides
52
    pointList = []
53
    for i in range(0, sides):
54
        cos_v = math.cos(angular_diff * i)
55
        sin_v = math.sin(angular_diff * i)
56
        pointList.append(
57
            centerPoint
58
            + App.Vector(
59
                cos_v * diffVec.x - sin_v * diffVec.y, cos_v * diffVec.y + sin_v * diffVec.x, 0
60
            )
61
        )
62

63
    geoList = []
64
    for i in range(0, sides - 1):
65
        geoList.append(Part.LineSegment(pointList[i], pointList[i + 1]))
66
    geoList.append(Part.LineSegment(pointList[sides - 1], pointList[0]))
67
    geoList.append(Part.Circle(centerPoint, App.Vector(0, 0, 1), diffVec.Length))
68
    geoIndices = sketch.addGeometry(geoList, construction)
69

70
    sketch.setConstruction(geoIndices[-1], True)
71

72
    conList = []
73
    for i in range(0, sides - 1):
74
        conList.append(Sketcher.Constraint("Coincident", geoIndices[i], 2, geoIndices[i + 1], 1))
75
    conList.append(Sketcher.Constraint("Coincident", geoIndices[sides - 1], 2, geoIndices[0], 1))
76
    for i in range(0, sides - 1):
77
        conList.append(Sketcher.Constraint("Equal", geoIndices[0], geoIndices[i + 1]))
78
    for i in range(0, sides):
79
        conList.append(Sketcher.Constraint("PointOnObject", geoIndices[i], 2, geoIndices[-1]))
80
    sketch.addConstraint(conList)
81
    return
82

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

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

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

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