chiark / gitweb /
Move SF into its own directory, to seperate SF and Cura. Rename newui to gui.
[cura.git] / Cura / cura_sf / fabmetheus_utilities / geometry / creation / shaft.py
1 """
2 Shaft path.
3
4 """
5
6 from __future__ import absolute_import
7 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
8 import __init__
9
10 from fabmetheus_utilities.geometry.creation import lineation
11 from fabmetheus_utilities.geometry.geometry_tools import path
12 from fabmetheus_utilities.geometry.geometry_utilities import evaluate
13 from fabmetheus_utilities import euclidean
14 import math
15
16
17 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
18 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
19 __date__ = '$Date: 2008/02/05 $'
20 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
21
22
23 def getGeometryOutput(derivation, elementNode):
24         "Get vector3 vertexes from attribute dictionary."
25         if derivation == None:
26                 derivation = ShaftDerivation(elementNode)
27         shaftPath = getShaftPath(derivation.depthBottom, derivation.depthTop, derivation.radius, derivation.sides)
28         return lineation.getGeometryOutputByLoop(elementNode, lineation.SideLoop(shaftPath))
29
30 def getGeometryOutputByArguments(arguments, elementNode):
31         "Get vector3 vertexes from attribute dictionary by arguments."
32         evaluate.setAttributesByArguments(['radius', 'sides'], arguments, elementNode)
33         return getGeometryOutput(None, elementNode)
34
35 def getNewDerivation(elementNode):
36         'Get new derivation.'
37         return ShaftDerivation(elementNode)
38
39 def getShaftPath(depthBottom, depthTop, radius, sides):
40         'Get shaft with the option of a flat on the top and/or bottom.'
41         if radius <= 0.0:
42                 return []
43         sideAngle = 2.0 * math.pi / float(abs(sides))
44         startAngle = 0.5 * sideAngle
45         endAngle = math.pi - 0.1 * sideAngle
46         shaftProfile = []
47         while startAngle < endAngle:
48                 unitPolar = euclidean.getWiddershinsUnitPolar(startAngle)
49                 shaftProfile.append(unitPolar * radius)
50                 startAngle += sideAngle
51         if abs(sides) % 2 == 1:
52                 shaftProfile.append(complex(-radius, 0.0))
53         horizontalBegin = radius - depthTop
54         horizontalEnd = depthBottom - radius
55         shaftProfile = euclidean.getHorizontallyBoundedPath(horizontalBegin, horizontalEnd, shaftProfile)
56         for shaftPointIndex, shaftPoint in enumerate(shaftProfile):
57                 shaftProfile[shaftPointIndex] = complex(shaftPoint.imag, shaftPoint.real)
58         shaftPath = euclidean.getVector3Path(euclidean.getMirrorPath(shaftProfile))
59         if sides > 0:
60                 shaftPath.reverse()
61         return shaftPath
62
63 def processElementNode(elementNode):
64         "Process the xml element."
65         path.convertElementNode(elementNode, getGeometryOutput(None, elementNode))
66
67
68 class ShaftDerivation:
69         "Class to hold shaft variables."
70         def __init__(self, elementNode):
71                 'Set defaults.'
72                 self.depthBottomOverRadius = evaluate.getEvaluatedFloat(0.0, elementNode, 'depthBottomOverRadius')
73                 self.depthTopOverRadius = evaluate.getEvaluatedFloat(0.0, elementNode, 'depthOverRadius')
74                 self.depthTopOverRadius = evaluate.getEvaluatedFloat(
75                         self.depthTopOverRadius, elementNode, 'depthTopOverRadius')
76                 self.radius = evaluate.getEvaluatedFloat(1.0, elementNode, 'radius')
77                 self.sides = evaluate.getEvaluatedInt(4, elementNode, 'sides')
78                 self.depthBottom = self.radius * self.depthBottomOverRadius
79                 self.depthBottom = evaluate.getEvaluatedFloat(self.depthBottom, elementNode, 'depthBottom')
80                 self.depthTop = self.radius * self.depthTopOverRadius
81                 self.depthTop = evaluate.getEvaluatedFloat(self.depthTop, elementNode, 'depth')
82                 self.depthTop = evaluate.getEvaluatedFloat(self.depthTop, elementNode, 'depthTop')