chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / fabmetheus_utilities / geometry / creation / square.py
1 """
2 Square 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.vector3 import Vector3
14 from fabmetheus_utilities import euclidean
15 import math
16
17
18 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
19 __credits__ = 'Art of Illusion <http://www.artofillusion.org/>'
20 __date__ = '$Date: 2008/02/05 $'
21 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
22
23
24 def getGeometryOutput(derivation, elementNode):
25         "Get vector3 vertexes from attribute dictionary."
26         if derivation == None:
27                 derivation = SquareDerivation(elementNode)
28         topRight = complex(derivation.topDemiwidth, derivation.demiheight)
29         topLeft = complex(-derivation.topDemiwidth, derivation.demiheight)
30         bottomLeft = complex(-derivation.bottomDemiwidth, -derivation.demiheight)
31         bottomRight = complex(derivation.bottomDemiwidth, -derivation.demiheight)
32         if derivation.interiorAngle != 90.0:
33                 interiorPlaneAngle = euclidean.getWiddershinsUnitPolar(math.radians(derivation.interiorAngle - 90.0))
34                 topRight = (topRight - bottomRight) * interiorPlaneAngle + bottomRight
35                 topLeft = (topLeft - bottomLeft) * interiorPlaneAngle + bottomLeft
36         lineation.setClosedAttribute(elementNode, derivation.revolutions)
37         complexLoop = [topRight, topLeft, bottomLeft, bottomRight]
38         originalLoop = complexLoop[:]
39         for revolution in xrange(1, derivation.revolutions):
40                 complexLoop += originalLoop
41         spiral = lineation.Spiral(derivation.spiral, 0.25)
42         loop = []
43         loopCentroid = euclidean.getLoopCentroid(originalLoop)
44         for point in complexLoop:
45                 unitPolar = euclidean.getNormalized(point - loopCentroid)
46                 loop.append(spiral.getSpiralPoint(unitPolar, Vector3(point.real, point.imag)))
47         return lineation.getGeometryOutputByLoop(elementNode, lineation.SideLoop(loop, 0.5 * math.pi))
48
49 def getGeometryOutputByArguments(arguments, elementNode):
50         "Get vector3 vertexes from attribute dictionary by arguments."
51         if len(arguments) < 1:
52                 return getGeometryOutput(None, elementNode)
53         inradius = 0.5 * euclidean.getFloatFromValue(arguments[0])
54         elementNode.attributes['inradius.x'] = str(inradius)
55         if len(arguments) > 1:
56                 inradius = 0.5 * euclidean.getFloatFromValue(arguments[1])
57         elementNode.attributes['inradius.y'] = str(inradius)
58         return getGeometryOutput(None, elementNode)
59
60 def getNewDerivation(elementNode):
61         'Get new derivation.'
62         return SquareDerivation(elementNode)
63
64 def processElementNode(elementNode):
65         "Process the xml element."
66         path.convertElementNode(elementNode, getGeometryOutput(None, elementNode))
67
68
69 class SquareDerivation:
70         "Class to hold square variables."
71         def __init__(self, elementNode):
72                 'Set defaults.'
73                 self.inradius = lineation.getInradius(complex(1.0, 1.0), elementNode)
74                 self.demiwidth = lineation.getFloatByPrefixBeginEnd(elementNode, 'demiwidth', 'width', self.inradius.real)
75                 self.demiheight = lineation.getFloatByPrefixBeginEnd(elementNode, 'demiheight', 'height', self.inradius.imag)
76                 self.bottomDemiwidth = lineation.getFloatByPrefixBeginEnd(elementNode, 'bottomdemiwidth', 'bottomwidth', self.demiwidth)
77                 self.topDemiwidth = lineation.getFloatByPrefixBeginEnd(elementNode, 'topdemiwidth', 'topwidth', self.demiwidth)
78                 self.interiorAngle = evaluate.getEvaluatedFloat(90.0, elementNode, 'interiorangle')
79                 self.revolutions = evaluate.getEvaluatedInt(1, elementNode, 'revolutions')
80                 self.spiral = evaluate.getVector3ByPrefix(None, elementNode, 'spiral')