chiark / gitweb /
Add very basic AMF support.
authordaid303 <daid303@gmail.com>
Mon, 28 Jan 2013 12:29:12 +0000 (13:29 +0100)
committerdaid303 <daid303@gmail.com>
Mon, 28 Jan 2013 12:29:12 +0000 (13:29 +0100)
Cura/gui/projectPlanner.py
Cura/gui/tools/minecraftImport.py
Cura/util/meshLoader.py
Cura/util/meshLoaders/__init__.py [new file with mode: 0644]
Cura/util/meshLoaders/amf.py [new file with mode: 0644]
Cura/util/meshLoaders/dae.py [moved from Cura/util/dae.py with 100% similarity]
Cura/util/meshLoaders/obj.py [moved from Cura/util/obj.py with 100% similarity]
Cura/util/meshLoaders/stl.py [moved from Cura/util/stl.py with 100% similarity]
Cura/util/version.py

index 134123fa65d8e3dab3877b41f5057cdcd3590427..377bd9972242f04d46bc7a6a093dac841bd8d827 100644 (file)
@@ -26,7 +26,7 @@ from Cura.util import validators
 from Cura.util import profile
 from Cura.util import util3d
 from Cura.util import meshLoader
-from Cura.util import stl
+from Cura.util.meshLoaders import stl
 from Cura.util import mesh
 from Cura.util import sliceRun
 from Cura.util import gcodeInterpreter
index 9765c2b09e2131466a9a850b14a6774e6307447a..35eda8c1c6533145d5d585a876c551be4f47f064 100644 (file)
@@ -6,7 +6,7 @@ import os
 import numpy
 
 from Cura.util import mesh
-from Cura.util import stl
+from Cura.util.meshLoaders import stl
 from Cura.util.pymclevel import mclevel
 
 def hasMinecraft():
index db0f3dbfcf7bcb454eb98cf3f77ecbc43bbba89a..6bf2767dd8c8521277828bee1337295733db9b2a 100644 (file)
@@ -1,11 +1,12 @@
 from __future__ import absolute_import
 
-from Cura.util import stl
-from Cura.util import obj
-from Cura.util import dae
+from Cura.util.meshLoaders import stl
+from Cura.util.meshLoaders import obj
+from Cura.util.meshLoaders import dae
+from Cura.util.meshLoaders import amf
 
 def supportedExtensions():
-       return ['.stl', '.obj', '.dae']
+       return ['.stl', '.obj', '.dae', '.amf']
 
 def wildcardFilter():
        wildcardList = ';'.join(map(lambda s: '*' + s, supportedExtensions()))
@@ -19,6 +20,8 @@ def loadMesh(filename):
                return obj.objModel().load(filename)
        if ext == '.dae':
                return dae.daeModel().load(filename)
+       if ext == '.amf':
+               return amf.amfModel().load(filename)
        print 'Error: Unknown model extension: %s' % (ext)
        return None
 
diff --git a/Cura/util/meshLoaders/__init__.py b/Cura/util/meshLoaders/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Cura/util/meshLoaders/amf.py b/Cura/util/meshLoaders/amf.py
new file mode 100644 (file)
index 0000000..30a950c
--- /dev/null
@@ -0,0 +1,75 @@
+from __future__ import absolute_import
+
+import zipfile
+try:
+       from xml.etree import cElementTree as ElementTree
+except:
+       from xml.etree import ElementTree
+
+from Cura.util import mesh
+
+class amfModel(mesh.mesh):
+       def __init__(self):
+               super(amfModel, self).__init__()
+
+       def load(self, filename):
+               try:
+                       zfile = zipfile.ZipFile(filename)
+                       xml = zfile.read(zfile.namelist()[0])
+                       zfile.close()
+               except zipfile.BadZipfile:
+                       f = open(filename, "r")
+                       xml = f.read()
+                       f.close()
+               amf = ElementTree.fromstring(xml)
+               if 'unit' in amf.attrib:
+                       unit = amf.attrib['unit'].lower()
+               else:
+                       unit = 'millimeter'
+               if unit == 'millimeter':
+                       scale = 1.0
+               elif unit == 'meter':
+                       scale = 1000.0
+               elif unit == 'inch':
+                       scale = 25.4
+               elif unit == 'feet':
+                       scale = 304.8
+               elif unit == 'micron':
+                       scale = 0.001
+               else:
+                       print "Unknown unit in amf: %s" % (unit)
+                       scale = 1.0
+
+               count = 0
+               for obj in amf.iter('object'):
+                       for mesh in obj.iter('mesh'):
+                               for volume in mesh.iter('volume'):
+                                       for triangle in volume.iter('triangle'):
+                                               count += 3
+
+               self._prepareVertexCount(count)
+
+               for obj in amf.iter('object'):
+                       for mesh in obj.iter('mesh'):
+                               vertexList = []
+                               for vertices in mesh.iter('vertices'):
+                                       for vertex in vertices.iter('vertex'):
+                                               for coordinates in vertex.iter('coordinates'):
+                                                       v = [0.0,0.0,0.0]
+                                                       for t in coordinates:
+                                                               if t.tag == 'x':
+                                                                       v[0] = float(t.text)
+                                                               elif t.tag == 'y':
+                                                                       v[1] = float(t.text)
+                                                               elif t.tag == 'z':
+                                                                       v[2] = float(t.text)
+                                                       vertexList.append(v)
+                               for volume in mesh.iter('volume'):
+                                       for triangle in volume.iter('triangle'):
+                                               for t in triangle:
+                                                       v = vertexList[int(t.text)]
+                                                       self.addVertex(v[0], v[1], v[2])
+
+               self.vertexes *= scale
+               self._postProcessAfterLoad()
+               return self
index 8ffceb4e30eb0e0168149ad4401a162482e9b74b..f153c345769a6804fc0597069b7b3798d309c6e4 100644 (file)
@@ -4,7 +4,10 @@ import os
 import sys
 import urllib2
 import platform
-from xml.etree import ElementTree
+try:
+       from xml.etree import cElementTree as ElementTree
+except:
+       from xml.etree import ElementTree
 
 from Cura.util import resources