ThreeMFWriter.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Uranium is released under the terms of the AGPLv3 or higher.
  3. from UM.Mesh.MeshWriter import MeshWriter
  4. from UM.Math.Vector import Vector
  5. from UM.Logger import Logger
  6. from UM.Math.Matrix import Matrix
  7. from UM.Settings.SettingRelation import RelationType
  8. try:
  9. import xml.etree.cElementTree as ET
  10. except ImportError:
  11. Logger.log("w", "Unable to load cElementTree, switching to slower version")
  12. import xml.etree.ElementTree as ET
  13. import zipfile
  14. import UM.Application
  15. class ThreeMFWriter(MeshWriter):
  16. def __init__(self):
  17. super().__init__()
  18. self._namespaces = {
  19. "3mf": "http://schemas.microsoft.com/3dmanufacturing/core/2015/02",
  20. "content-types": "http://schemas.openxmlformats.org/package/2006/content-types",
  21. "relationships": "http://schemas.openxmlformats.org/package/2006/relationships",
  22. "cura": "http://software.ultimaker.com/xml/cura/3mf/2015/10"
  23. }
  24. self._unit_matrix_string = self._convertMatrixToString(Matrix())
  25. self._archive = None
  26. self._store_archive = False
  27. def _convertMatrixToString(self, matrix):
  28. result = ""
  29. result += str(matrix._data[0,0]) + " "
  30. result += str(matrix._data[1,0]) + " "
  31. result += str(matrix._data[2,0]) + " "
  32. result += str(matrix._data[0,1]) + " "
  33. result += str(matrix._data[1,1]) + " "
  34. result += str(matrix._data[2,1]) + " "
  35. result += str(matrix._data[0,2]) + " "
  36. result += str(matrix._data[1,2]) + " "
  37. result += str(matrix._data[2,2]) + " "
  38. result += str(matrix._data[0,3]) + " "
  39. result += str(matrix._data[1,3]) + " "
  40. result += str(matrix._data[2,3]) + " "
  41. return result
  42. ## Should we store the archive
  43. # Note that if this is true, the archive will not be closed.
  44. # The object that set this parameter is then responsible for closing it correctly!
  45. def setStoreArchive(self, store_archive):
  46. self._store_archive = store_archive
  47. def getArchive(self):
  48. return self._archive
  49. def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
  50. try:
  51. MeshWriter._meshNodes(nodes).__next__()
  52. except StopIteration:
  53. return False #Don't write anything if there is no mesh data.
  54. self._archive = None # Reset archive
  55. archive = zipfile.ZipFile(stream, "w", compression = zipfile.ZIP_DEFLATED)
  56. try:
  57. model_file = zipfile.ZipInfo("3D/3dmodel.model")
  58. # Because zipfile is stupid and ignores archive-level compression settings when writing with ZipInfo.
  59. model_file.compress_type = zipfile.ZIP_DEFLATED
  60. # Create content types file
  61. content_types_file = zipfile.ZipInfo("[Content_Types].xml")
  62. content_types_file.compress_type = zipfile.ZIP_DEFLATED
  63. content_types = ET.Element("Types", xmlns = self._namespaces["content-types"])
  64. rels_type = ET.SubElement(content_types, "Default", Extension = "rels", ContentType = "application/vnd.openxmlformats-package.relationships+xml")
  65. model_type = ET.SubElement(content_types, "Default", Extension = "model", ContentType = "application/vnd.ms-package.3dmanufacturing-3dmodel+xml")
  66. # Create _rels/.rels file
  67. relations_file = zipfile.ZipInfo("_rels/.rels")
  68. relations_file.compress_type = zipfile.ZIP_DEFLATED
  69. relations_element = ET.Element("Relationships", xmlns = self._namespaces["relationships"])
  70. model_relation_element = ET.SubElement(relations_element, "Relationship", Target = "/3D/3dmodel.model", Id = "rel0", Type = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel")
  71. model = ET.Element("model", unit = "millimeter", xmlns = self._namespaces["3mf"])
  72. resources = ET.SubElement(model, "resources")
  73. build = ET.SubElement(model, "build")
  74. added_nodes = []
  75. # Write all nodes with meshData to the file as objects inside the resource tag
  76. for index, n in enumerate(MeshWriter._meshNodes(nodes)):
  77. added_nodes.append(n) # Save the nodes that have mesh data
  78. object = ET.SubElement(resources, "object", id = str(index+1), type = "model")
  79. mesh = ET.SubElement(object, "mesh")
  80. mesh_data = n.getMeshData()
  81. vertices = ET.SubElement(mesh, "vertices")
  82. verts = mesh_data.getVertices()
  83. if verts is None:
  84. Logger.log("d", "3mf writer can't write nodes without mesh data. Skipping this node.")
  85. continue # No mesh data, nothing to do.
  86. if mesh_data.hasIndices():
  87. for face in mesh_data.getIndices():
  88. v1 = verts[face[0]]
  89. v2 = verts[face[1]]
  90. v3 = verts[face[2]]
  91. xml_vertex1 = ET.SubElement(vertices, "vertex", x = str(v1[0]), y = str(v1[1]), z = str(v1[2]))
  92. xml_vertex2 = ET.SubElement(vertices, "vertex", x = str(v2[0]), y = str(v2[1]), z = str(v2[2]))
  93. xml_vertex3 = ET.SubElement(vertices, "vertex", x = str(v3[0]), y = str(v3[1]), z = str(v3[2]))
  94. triangles = ET.SubElement(mesh, "triangles")
  95. for face in mesh_data.getIndices():
  96. triangle = ET.SubElement(triangles, "triangle", v1 = str(face[0]) , v2 = str(face[1]), v3 = str(face[2]))
  97. else:
  98. triangles = ET.SubElement(mesh, "triangles")
  99. for idx, vert in enumerate(verts):
  100. xml_vertex = ET.SubElement(vertices, "vertex", x = str(vert[0]), y = str(vert[1]), z = str(vert[2]))
  101. # If we have no faces defined, assume that every three subsequent vertices form a face.
  102. if idx % 3 == 0:
  103. triangle = ET.SubElement(triangles, "triangle", v1 = str(idx), v2 = str(idx + 1), v3 = str(idx + 2))
  104. # Handle per object settings
  105. stack = n.callDecoration("getStack")
  106. if stack is not None:
  107. changed_setting_keys = set(stack.getTop().getAllKeys())
  108. # Ensure that we save the extruder used for this object.
  109. if stack.getProperty("machine_extruder_count", "value") > 1:
  110. changed_setting_keys.add("extruder_nr")
  111. settings_xml = ET.SubElement(object, "settings", xmlns=self._namespaces["cura"])
  112. # Get values for all changed settings & save them.
  113. for key in changed_setting_keys:
  114. setting_xml = ET.SubElement(settings_xml, "setting", key = key)
  115. setting_xml.text = str(stack.getProperty(key, "value"))
  116. # Add one to the index as we haven't incremented the last iteration.
  117. index += 1
  118. nodes_to_add = set()
  119. for node in added_nodes:
  120. # Check the parents of the nodes with mesh_data and ensure that they are also added.
  121. parent_node = node.getParent()
  122. while parent_node is not None:
  123. if parent_node.callDecoration("isGroup"):
  124. nodes_to_add.add(parent_node)
  125. parent_node = parent_node.getParent()
  126. else:
  127. parent_node = None
  128. # Sort all the nodes by depth (so nodes with the highest depth are done first)
  129. sorted_nodes_to_add = sorted(nodes_to_add, key=lambda node: node.getDepth(), reverse = True)
  130. # We have already saved the nodes with mesh data, but now we also want to save nodes required for the scene
  131. for node in sorted_nodes_to_add:
  132. object = ET.SubElement(resources, "object", id=str(index + 1), type="model")
  133. components = ET.SubElement(object, "components")
  134. for child in node.getChildren():
  135. if child in added_nodes:
  136. component = ET.SubElement(components, "component", objectid = str(added_nodes.index(child) + 1), transform = self._convertMatrixToString(child.getLocalTransformation()))
  137. index += 1
  138. added_nodes.append(node)
  139. # Create a transformation Matrix to convert from our worldspace into 3MF.
  140. # First step: flip the y and z axis.
  141. transformation_matrix = Matrix()
  142. transformation_matrix._data[1, 1] = 0
  143. transformation_matrix._data[1, 2] = -1
  144. transformation_matrix._data[2, 1] = 1
  145. transformation_matrix._data[2, 2] = 0
  146. global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
  147. # Second step: 3MF defines the left corner of the machine as center, whereas cura uses the center of the
  148. # build volume.
  149. if global_container_stack:
  150. translation_vector = Vector(x=global_container_stack.getProperty("machine_width", "value") / 2,
  151. y=global_container_stack.getProperty("machine_depth", "value") / 2,
  152. z=0)
  153. translation_matrix = Matrix()
  154. translation_matrix.setByTranslation(translation_vector)
  155. transformation_matrix.preMultiply(translation_matrix)
  156. # Find out what the final build items are and add them.
  157. for node in added_nodes:
  158. if node.getParent().callDecoration("isGroup") is None:
  159. node_matrix = node.getLocalTransformation()
  160. ET.SubElement(build, "item", objectid = str(added_nodes.index(node) + 1), transform = self._convertMatrixToString(node_matrix.preMultiply(transformation_matrix)))
  161. archive.writestr(model_file, b'<?xml version="1.0" encoding="UTF-8"?> \n' + ET.tostring(model))
  162. archive.writestr(content_types_file, b'<?xml version="1.0" encoding="UTF-8"?> \n' + ET.tostring(content_types))
  163. archive.writestr(relations_file, b'<?xml version="1.0" encoding="UTF-8"?> \n' + ET.tostring(relations_element))
  164. except Exception as e:
  165. Logger.logException("e", "Error writing zip file")
  166. return False
  167. finally:
  168. if not self._store_archive:
  169. archive.close()
  170. else:
  171. self._archive = archive
  172. return True