chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / fabmetheus_utilities / geometry / creation / peg.py
1 """
2 Peg.
3
4 """
5
6 from __future__ import absolute_import
7
8 from fabmetheus_utilities.geometry.creation import extrude
9 from fabmetheus_utilities.geometry.creation import lineation
10 from fabmetheus_utilities.geometry.creation import solid
11 from fabmetheus_utilities.geometry.geometry_utilities import evaluate
12 from fabmetheus_utilities.geometry.solids import cylinder
13 from fabmetheus_utilities.vector3 import Vector3
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
24 def addPegOutput(bevel, endZ, outputs, radiusArealized, sides, start, topOverBottom):
25         'Add beveled cylinder to outputs given bevel, endZ, radiusArealized and start.'
26         height = abs(start.z - endZ)
27         bevelStartRatio = max(1.0 - bevel / height, 0.5)
28         oneMinusBevelStartRatio = 1.0 - bevelStartRatio
29         trunkEndZ = bevelStartRatio * endZ + oneMinusBevelStartRatio * start.z
30         trunkTopOverBottom = bevelStartRatio * topOverBottom + oneMinusBevelStartRatio
31         cylinder.addCylinderOutputByEndStart(trunkEndZ, radiusArealized, outputs, sides, start, trunkTopOverBottom)
32         capRadius = radiusArealized * trunkTopOverBottom
33         capStart = bevelStartRatio * Vector3(start.x, start.y, endZ) + oneMinusBevelStartRatio * start
34         radiusMaximum = max(radiusArealized.real, radiusArealized.imag)
35         endRadiusMaximum = radiusMaximum * topOverBottom - bevel
36         trunkRadiusMaximum = radiusMaximum * trunkTopOverBottom
37         capTopOverBottom = endRadiusMaximum / trunkRadiusMaximum
38         cylinder.addCylinderOutputByEndStart(endZ, capRadius, outputs, sides, capStart, capTopOverBottom)
39
40 def getGeometryOutput(derivation, elementNode):
41         'Get vector3 vertexes from attribute dictionary.'
42         if derivation == None:
43                 derivation = PegDerivation(elementNode)
44         positives = []
45         radiusArealized = complex(derivation.radiusArealized, derivation.radiusArealized)
46         addPegOutput(derivation.bevel, derivation.endZ, positives, radiusArealized, derivation.sides, derivation.start, derivation.topOverBottom)
47         return extrude.getGeometryOutputByNegativesPositives(elementNode, [], positives)
48
49 def getGeometryOutputByArguments(arguments, elementNode):
50         'Get vector3 vertexes from attribute dictionary by arguments.'
51         evaluate.setAttributesByArguments(['radius', 'endZ', 'start'], arguments, elementNode)
52         return getGeometryOutput(None, elementNode)
53
54 def getNewDerivation(elementNode):
55         'Get new derivation.'
56         return PegDerivation(elementNode)
57
58 def getTopAddBiconicOutput(bottomRadians, height, outputs, radius, sides, start, tipRadius, topRadians):
59         'Get top and add biconic cylinder to outputs.'
60         radiusMaximum = max(radius.real, radius.imag)
61         topRadiusMaximum = radiusMaximum - height * math.tan(bottomRadians)
62         trunkEndZ = start.z + height
63         trunkTopOverBottom = topRadiusMaximum / radiusMaximum
64         topRadiusComplex = trunkTopOverBottom * radius
65         cylinder.addCylinderOutputByEndStart(trunkEndZ, radius, outputs, sides, start, trunkTopOverBottom)
66         tipOverTop = tipRadius / topRadiusMaximum
67         if tipOverTop >= 1.0:
68                 return trunkEndZ
69         capStart = Vector3(start.x, start.y, trunkEndZ)
70         capEndZ = trunkEndZ + (topRadiusMaximum - tipRadius) / math.tan(topRadians)
71         cylinder.addCylinderOutputByEndStart(capEndZ, topRadiusComplex, outputs, sides, capStart, tipOverTop)
72         return capEndZ
73
74 def processElementNode(elementNode):
75         'Process the xml element.'
76         solid.processElementNodeByGeometry(elementNode, getGeometryOutput(None, elementNode))
77
78 def setTopOverBottomByRadius(derivation, endZ, radius, startZ):
79         'Set the derivation topOverBottom by the angle of the elementNode, the endZ, float radius and startZ.'
80         angleDegrees = evaluate.getEvaluatedFloat(None, derivation.elementNode, 'angle')
81         if angleDegrees != None:
82                 derivation.topOverBottom = cylinder.getTopOverBottom(math.radians(angleDegrees), endZ, complex(radius, radius), startZ)
83
84
85 class PegDerivation(object):
86         'Class to hold peg variables.'
87         def __init__(self, elementNode):
88                 'Set defaults.'
89                 self.bevelOverRadius = evaluate.getEvaluatedFloat(0.25, elementNode, 'bevelOverRadius')
90                 self.clearanceOverRadius = evaluate.getEvaluatedFloat(0.0, elementNode, 'clearanceOverRadius')
91                 self.elementNode = elementNode
92                 self.endZ = evaluate.getEvaluatedFloat(10.0, elementNode, 'endZ')
93                 self.start = evaluate.getVector3ByPrefix(Vector3(), elementNode, 'start')
94                 self.radius = lineation.getFloatByPrefixBeginEnd(elementNode, 'radius', 'diameter', 2.0)
95                 self.sides = evaluate.getSidesMinimumThreeBasedOnPrecision(elementNode, max(self.radius.real, self.radius.imag))
96                 self.radiusArealized = evaluate.getRadiusArealizedBasedOnAreaRadius(elementNode, self.radius, self.sides)
97                 self.topOverBottom = evaluate.getEvaluatedFloat(0.8, elementNode, 'topOverBottom')
98                 setTopOverBottomByRadius(self, self.endZ, self.radiusArealized, self.start.z)
99                 # Set derived variables.
100                 self.bevel = evaluate.getEvaluatedFloat(self.bevelOverRadius * self.radiusArealized, elementNode, 'bevel')
101                 self.clearance = evaluate.getEvaluatedFloat(self.clearanceOverRadius * self.radiusArealized, elementNode, 'clearance')