FreeCAD-macros

Форк
0
90 строк · 2.9 Кб
1
#
2
# Creates a parametric Rectellipse
3
# (c) fcaponi78
4
#
5
#
6

7
from __future__ import division
8

9
__Comment__ = 'Creates a parametric approximation of a rectellipse'
10
__Web__ = 'http://freecadweb.org/wiki/Macro_Rectellipse'
11
__Wiki__ = 'http://freecadweb.org/wiki/Macro_Rectellipse'
12
__Icon__ = ""
13
__Help__ = 'Run, zoom fit, and change parameters'
14
__Author__ = 'fcaponi78, shoogen and other contributors'
15
__Version__ = 0.1
16
__Status__ = 'alpha'
17
__Requires__ = ''
18

19
import FreeCAD
20
from FreeCAD import Part
21
from FreeCAD import Vector
22

23

24
class RectEllipseFeature:
25
    def __init__(self, obj):
26
        """Add the properties: a, b, s, createFace"""
27
        obj.addProperty('App::PropertyLength', 'a', 'Rectellipse',
28
                        'Horizontal radius').a = 16.0
29
        obj.addProperty('App::PropertyLength', 'b', 'Rectellipse',
30
                        'Vertical radius').b = 9.0
31
        obj.addProperty('App::PropertyFloat', 's', 'Rectellipse',
32
                        'Squareness (0=ellipse, 1=rectangle)').s = 0.2
33
        obj.addProperty('App::PropertyBool', 'createFace', 'Rectellipse',
34
                        'true => face; false => wire').createFace = True
35
        obj.Proxy = self
36

37
    def onChanged(self, feature, prop):
38
        if prop in ['a',  'b', 's', 'createFace']:
39
            self.execute(feature)
40

41
    def execute(self, feature):
42
        r1 = feature.a
43
        r2 = feature.b
44
        if feature.s < 0:
45
            feature.s = 0
46
        elif feature.s > 1:
47
            feature.s = 1
48
        z = 0.0
49
        # TODO: tweak p, so that the curve is the exact definition of a
50
        # rectellipse (if possible).
51
        p = 1.0
52
        # NURBS knots weigts.
53
        if feature.s > 0.9999999:
54
            # If larger than 1e7, we obtain a rhombus.
55
            w = 1e7
56
        else:
57
            w = 2 ** 0.5 / 2.0 / (1 - feature.s ** p)
58
        # TODO: use a real rectangle if feature.s close to 1.
59
        curve = Part.BSplineCurve()
60
        curve.setPeriodic()
61
        curve.increaseDegree(2)
62
        # 5 knots, 8 poles
63
        curve.insertKnots([i / 4 for i in (1, 2, 3)], [2] * 3)
64
        curve.setPole(1, Vector(0, -r2, z), 1)
65
        curve.setPole(2, Vector(-r1, -r2, z), w)
66
        curve.setPole(3, Vector(-r1, 0, z), 1)
67
        curve.setPole(4, Vector(-r1, r2, z), w)
68
        curve.setPole(5, Vector(0, r2, z), 1)
69
        curve.setPole(6, Vector(r1, r2, z), w)
70
        curve.setPole(7, Vector(r1, 0, z), 1)
71
        curve.setPole(8, Vector(r1, -r2, z), w)
72
        if feature.createFace:
73
            feature.Shape = Part.Face(Part.Wire(curve.toShape()))
74
        else:
75
            feature.Shape = curve.toShape()
76

77

78
def makeRectellipseFeature():
79
    doc = FreeCAD.activeDocument()
80
    if doc is None:
81
        doc = FreeCAD.newDocument()
82
        if doc is None:
83
            return
84
    obj = doc.addObject('Part::FeaturePython', 'RectEllipseFeature')
85
    obj.Label = 'RectEllipse'
86
    RectEllipseFeature(obj)
87
    obj.ViewObject.Proxy = 0
88

89
if __name__ == "__main__":
90
    makeRectellipseFeature()
91

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

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

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

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