FreeCAD-macros
90 строк · 2.9 Кб
1#
2# Creates a parametric Rectellipse
3# (c) fcaponi78
4#
5#
6
7from __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
19import FreeCAD
20from FreeCAD import Part
21from FreeCAD import Vector
22
23
24class RectEllipseFeature:
25def __init__(self, obj):
26"""Add the properties: a, b, s, createFace"""
27obj.addProperty('App::PropertyLength', 'a', 'Rectellipse',
28'Horizontal radius').a = 16.0
29obj.addProperty('App::PropertyLength', 'b', 'Rectellipse',
30'Vertical radius').b = 9.0
31obj.addProperty('App::PropertyFloat', 's', 'Rectellipse',
32'Squareness (0=ellipse, 1=rectangle)').s = 0.2
33obj.addProperty('App::PropertyBool', 'createFace', 'Rectellipse',
34'true => face; false => wire').createFace = True
35obj.Proxy = self
36
37def onChanged(self, feature, prop):
38if prop in ['a', 'b', 's', 'createFace']:
39self.execute(feature)
40
41def execute(self, feature):
42r1 = feature.a
43r2 = feature.b
44if feature.s < 0:
45feature.s = 0
46elif feature.s > 1:
47feature.s = 1
48z = 0.0
49# TODO: tweak p, so that the curve is the exact definition of a
50# rectellipse (if possible).
51p = 1.0
52# NURBS knots weigts.
53if feature.s > 0.9999999:
54# If larger than 1e7, we obtain a rhombus.
55w = 1e7
56else:
57w = 2 ** 0.5 / 2.0 / (1 - feature.s ** p)
58# TODO: use a real rectangle if feature.s close to 1.
59curve = Part.BSplineCurve()
60curve.setPeriodic()
61curve.increaseDegree(2)
62# 5 knots, 8 poles
63curve.insertKnots([i / 4 for i in (1, 2, 3)], [2] * 3)
64curve.setPole(1, Vector(0, -r2, z), 1)
65curve.setPole(2, Vector(-r1, -r2, z), w)
66curve.setPole(3, Vector(-r1, 0, z), 1)
67curve.setPole(4, Vector(-r1, r2, z), w)
68curve.setPole(5, Vector(0, r2, z), 1)
69curve.setPole(6, Vector(r1, r2, z), w)
70curve.setPole(7, Vector(r1, 0, z), 1)
71curve.setPole(8, Vector(r1, -r2, z), w)
72if feature.createFace:
73feature.Shape = Part.Face(Part.Wire(curve.toShape()))
74else:
75feature.Shape = curve.toShape()
76
77
78def makeRectellipseFeature():
79doc = FreeCAD.activeDocument()
80if doc is None:
81doc = FreeCAD.newDocument()
82if doc is None:
83return
84obj = doc.addObject('Part::FeaturePython', 'RectEllipseFeature')
85obj.Label = 'RectEllipse'
86RectEllipseFeature(obj)
87obj.ViewObject.Proxy = 0
88
89if __name__ == "__main__":
90makeRectellipseFeature()
91