File size: 2,916 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! python
# (c) 2011 Werner Mayer LGPL
#

from __future__ import division  # allows floating point division from integers
import FreeCAD, Part, math
from FreeCAD import Base


class Epitrochoid:
    def __init__(self, obj):
        """Add the properties: Radius1, Radius2, Distance, Segments"""
        obj.addProperty(
            "App::PropertyLength", "Radius1", "Epitrochoid", "Interior radius", locked=True
        ).Radius1 = 60.0
        obj.addProperty(
            "App::PropertyLength", "Radius2", "Epitrochoid", "Exterior radius", locked=True
        ).Radius2 = 20.0
        obj.addProperty(
            "App::PropertyLength", "Distance", "Epitrochoid", "Distance", locked=True
        ).Distance = 10.0
        obj.addProperty(
            "App::PropertyInteger",
            "Segments",
            "Epitrochoid",
            "Number of the line segments",
            locked=True,
        ).Segments = 72
        obj.Proxy = self

    def onChanged(self, fp, prop):
        if (
            prop == "Radius1" or prop == "Radius2" or prop == "Distance" or prop == "Segments"
        ):  # if one of these is changed
            self.execute(fp)

    def execute(self, fp):  # main part of script
        steps = fp.Segments  # get value from property
        dang = math.radians(360 / steps)
        r2 = fp.Radius2
        r1 = fp.Radius1
        f1 = r1 + r2
        f2 = f1 / r2
        d = fp.Distance
        ang = 0
        z = 0

        if r2 == 0:
            raise ValueError("Exterior radius must not be zero")

        for i in range(steps):
            if i == 0:
                x1 = f1 * math.cos(ang) - d * math.cos(f2 * ang)  # coords for line startpoint
                y1 = f1 * math.sin(ang) - d * math.sin(f2 * ang)
                ang = dang
                x2 = f1 * math.cos(ang) - d * math.cos(f2 * ang)  # coords for line endpoint
                y2 = f1 * math.sin(ang) - d * math.sin(f2 * ang)
                seg = Part.makeLine((x1, y1, z), (x2, y2, z))
                wire = Part.Wire([seg])
                x1 = x2
                y1 = y2
            else:
                x2 = f1 * math.cos(ang) - d * math.cos(f2 * ang)
                y2 = f1 * math.sin(ang) - d * math.sin(f2 * ang)
                seg = Part.makeLine((x1, y1, z), (x2, y2, z))
                wire = Part.Wire([wire, seg])
                x1 = x2
                y1 = y2
            ang = ang + dang  # increment angle
        fp.Shape = wire  # result shape


def makeEpitrochoid():
    doc = FreeCAD.activeDocument()
    if doc is None:
        doc = FreeCAD.newDocument()
    epitrochoid = doc.addObject("Part::FeaturePython", "Epitrochoid")  # add object to document
    epitrochoid.Label = "Epitrochoid"
    Epitrochoid(epitrochoid)
    epitrochoid.ViewObject.Proxy = 0
    doc.recompute()


if __name__ == "__main__":  # feature will be generated after macro execution
    makeEpitrochoid()