File size: 2,264 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
#! python
# (c) 2010 Werner Mayer LGPL

"""
An example for a high-level custom feature object to form a full-parametric distance bolt.
"""

__author__ = "Werner Mayer <wmayer@users.sourceforge.net>"

import FreeCAD, Part, math
from FreeCAD import Base


class DistanceBolt:
    def __init__(self, obj):
        """Add the properties: Length, Edges, Radius, Height"""
        obj.addProperty(
            "App::PropertyInteger", "Edges", "Bolt", "Number of edges of the outline", locked=True
        ).Edges = 6
        obj.addProperty(
            "App::PropertyLength",
            "Length",
            "Bolt",
            "Length of the edges of the outline",
            locked=True,
        ).Length = 10.0
        obj.addProperty(
            "App::PropertyLength", "Radius", "Bolt", "Radius of the inner circle", locked=True
        ).Radius = 4.0
        obj.addProperty(
            "App::PropertyLength", "Height", "Bolt", "Height of the extrusion", locked=True
        ).Height = 20.0
        obj.Proxy = self

    def onChanged(self, fp, prop):
        if prop == "Edges" or prop == "Length" or prop == "Radius" or prop == "Height":
            self.execute(fp)

    def execute(self, fp):
        edges = fp.Edges
        if edges < 3:
            edges = 3
        length = fp.Length
        radius = fp.Radius
        height = fp.Height

        m = Base.Matrix()
        m.rotateZ(math.radians(360.0 / edges))

        # create polygon
        polygon = []
        v = Base.Vector(length, 0, 0)
        for i in range(edges):
            polygon.append(v)
            v = m.multiply(v)
        polygon.append(v)
        wire = Part.makePolygon(polygon)

        # create circle
        circ = Part.makeCircle(radius)

        # Create the face with the polygon as outline and the circle as hole
        face = Part.Face([wire, Part.Wire(circ)])

        # Extrude in z to create the final solid
        extrude = face.extrude(Base.Vector(0, 0, height))
        fp.Shape = extrude


def makeDistanceBolt():
    doc = FreeCAD.activeDocument()
    if doc is None:
        doc = FreeCAD.newDocument()
    bolt = doc.addObject("Part::FeaturePython", "Distance_Bolt")
    bolt.Label = "Distance bolt"
    DistanceBolt(bolt)
    bolt.ViewObject.Proxy = 0