ThreeMFWriter.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. # Copyright (c) 2015-2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import json
  4. import re
  5. from typing import Optional, cast, List, Dict, Pattern, Set
  6. from UM.Mesh.MeshWriter import MeshWriter
  7. from UM.Math.Vector import Vector
  8. from UM.Logger import Logger
  9. from UM.Math.Matrix import Matrix
  10. from UM.Application import Application
  11. from UM.OutputDevice import OutputDeviceError
  12. from UM.Message import Message
  13. from UM.Resources import Resources
  14. from UM.Scene.SceneNode import SceneNode
  15. from UM.Settings.ContainerRegistry import ContainerRegistry
  16. from cura.CuraApplication import CuraApplication
  17. from cura.CuraPackageManager import CuraPackageManager
  18. from cura.Settings import CuraContainerStack
  19. from cura.Utils.Threading import call_on_qt_thread
  20. from cura.Scene.CuraSceneNode import CuraSceneNode
  21. from cura.Snapshot import Snapshot
  22. from PyQt6.QtCore import Qt, QBuffer
  23. from PyQt6.QtGui import QImage, QPainter
  24. import pySavitar as Savitar
  25. from .UCPDialog import UCPDialog
  26. import numpy
  27. import datetime
  28. MYPY = False
  29. try:
  30. if not MYPY:
  31. import xml.etree.cElementTree as ET
  32. except ImportError:
  33. Logger.log("w", "Unable to load cElementTree, switching to slower version")
  34. import xml.etree.ElementTree as ET
  35. import zipfile
  36. import UM.Application
  37. from .SettingsExportModel import SettingsExportModel
  38. from .SettingsExportGroup import SettingsExportGroup
  39. from UM.i18n import i18nCatalog
  40. catalog = i18nCatalog("cura")
  41. THUMBNAIL_PATH = "Metadata/thumbnail.png"
  42. MODEL_PATH = "3D/3dmodel.model"
  43. PACKAGE_METADATA_PATH = "Cura/packages.json"
  44. class ThreeMFWriter(MeshWriter):
  45. def __init__(self):
  46. super().__init__()
  47. self._namespaces = {
  48. "3mf": "http://schemas.microsoft.com/3dmanufacturing/core/2015/02",
  49. "content-types": "http://schemas.openxmlformats.org/package/2006/content-types",
  50. "relationships": "http://schemas.openxmlformats.org/package/2006/relationships",
  51. "cura": "http://software.ultimaker.com/xml/cura/3mf/2015/10"
  52. }
  53. self._unit_matrix_string = ThreeMFWriter._convertMatrixToString(Matrix())
  54. self._archive: Optional[zipfile.ZipFile] = None
  55. self._store_archive = False
  56. @staticmethod
  57. def _convertMatrixToString(matrix):
  58. result = ""
  59. result += str(matrix._data[0, 0]) + " "
  60. result += str(matrix._data[1, 0]) + " "
  61. result += str(matrix._data[2, 0]) + " "
  62. result += str(matrix._data[0, 1]) + " "
  63. result += str(matrix._data[1, 1]) + " "
  64. result += str(matrix._data[2, 1]) + " "
  65. result += str(matrix._data[0, 2]) + " "
  66. result += str(matrix._data[1, 2]) + " "
  67. result += str(matrix._data[2, 2]) + " "
  68. result += str(matrix._data[0, 3]) + " "
  69. result += str(matrix._data[1, 3]) + " "
  70. result += str(matrix._data[2, 3])
  71. return result
  72. def setStoreArchive(self, store_archive):
  73. """Should we store the archive
  74. Note that if this is true, the archive will not be closed.
  75. The object that set this parameter is then responsible for closing it correctly!
  76. """
  77. self._store_archive = store_archive
  78. @staticmethod
  79. def _convertUMNodeToSavitarNode(um_node,
  80. transformation = Matrix(),
  81. exported_settings: Optional[Dict[str, Set[str]]] = None):
  82. """Convenience function that converts an Uranium SceneNode object to a SavitarSceneNode
  83. :returns: Uranium Scene node.
  84. """
  85. if not isinstance(um_node, SceneNode):
  86. return None
  87. active_build_plate_nr = CuraApplication.getInstance().getMultiBuildPlateModel().activeBuildPlate
  88. if um_node.callDecoration("getBuildPlateNumber") != active_build_plate_nr:
  89. return
  90. savitar_node = Savitar.SceneNode()
  91. savitar_node.setName(um_node.getName())
  92. node_matrix = Matrix()
  93. mesh_data = um_node.getMeshData()
  94. # compensate for original center position, if object(s) is/are not around its zero position
  95. if mesh_data is not None:
  96. extents = mesh_data.getExtents()
  97. if extents is not None:
  98. # We use a different coordinate space while writing, so flip Z and Y
  99. center_vector = Vector(extents.center.x, extents.center.z, extents.center.y)
  100. node_matrix.setByTranslation(center_vector)
  101. node_matrix.multiply(um_node.getLocalTransformation())
  102. matrix_string = ThreeMFWriter._convertMatrixToString(node_matrix.preMultiply(transformation))
  103. savitar_node.setTransformation(matrix_string)
  104. if mesh_data is not None:
  105. savitar_node.getMeshData().setVerticesFromBytes(mesh_data.getVerticesAsByteArray())
  106. indices_array = mesh_data.getIndicesAsByteArray()
  107. if indices_array is not None:
  108. savitar_node.getMeshData().setFacesFromBytes(indices_array)
  109. else:
  110. savitar_node.getMeshData().setFacesFromBytes(numpy.arange(mesh_data.getVertices().size / 3, dtype=numpy.int32).tostring())
  111. # Handle per object settings (if any)
  112. stack = um_node.callDecoration("getStack")
  113. if stack is not None:
  114. changed_setting_keys = stack.getTop().getAllKeys()
  115. if exported_settings is None:
  116. # Ensure that we save the extruder used for this object in a multi-extrusion setup
  117. if stack.getProperty("machine_extruder_count", "value") > 1:
  118. changed_setting_keys.add("extruder_nr")
  119. # Get values for all changed settings & save them.
  120. for key in changed_setting_keys:
  121. savitar_node.setSetting("cura:" + key, str(stack.getProperty(key, "value")))
  122. else:
  123. # We want to export only the specified settings
  124. if um_node.getName() in exported_settings:
  125. model_exported_settings = exported_settings[um_node.getName()]
  126. # Get values for all exported settings & save them.
  127. for key in model_exported_settings:
  128. savitar_node.setSetting("cura:" + key, str(stack.getProperty(key, "value")))
  129. if isinstance(um_node, CuraSceneNode):
  130. savitar_node.setSetting("cura:print_order", str(um_node.printOrder))
  131. # Store the metadata.
  132. for key, value in um_node.metadata.items():
  133. savitar_node.setSetting(key, value)
  134. for child_node in um_node.getChildren():
  135. # only save the nodes on the active build plate
  136. if child_node.callDecoration("getBuildPlateNumber") != active_build_plate_nr:
  137. continue
  138. savitar_child_node = ThreeMFWriter._convertUMNodeToSavitarNode(child_node,
  139. exported_settings = exported_settings)
  140. if savitar_child_node is not None:
  141. savitar_node.addChild(savitar_child_node)
  142. return savitar_node
  143. def getArchive(self):
  144. return self._archive
  145. def _addShareLogoToThumbnail(self, primary_image):
  146. # Load the icon png image
  147. icon_image = QImage(Resources.getPath(Resources.Images, "cura-share.png"))
  148. # Resize icon_image to be 1/4 of primary_image size
  149. new_width = int(primary_image.width() / 4)
  150. new_height = int(primary_image.height() / 4)
  151. icon_image = icon_image.scaled(new_width, new_height, Qt.AspectRatioMode.KeepAspectRatio)
  152. # Create a QPainter to draw on the image
  153. painter = QPainter(primary_image)
  154. # Draw the icon in the top-left corner (adjust coordinates as needed)
  155. icon_position = (10, 10)
  156. painter.drawImage(icon_position[0], icon_position[1], icon_image)
  157. painter.end()
  158. def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode, export_settings_model = None) -> bool:
  159. self._archive = None # Reset archive
  160. archive = zipfile.ZipFile(stream, "w", compression = zipfile.ZIP_DEFLATED)
  161. try:
  162. model_file = zipfile.ZipInfo(MODEL_PATH)
  163. # Because zipfile is stupid and ignores archive-level compression settings when writing with ZipInfo.
  164. model_file.compress_type = zipfile.ZIP_DEFLATED
  165. # Create content types file
  166. content_types_file = zipfile.ZipInfo("[Content_Types].xml")
  167. content_types_file.compress_type = zipfile.ZIP_DEFLATED
  168. content_types = ET.Element("Types", xmlns = self._namespaces["content-types"])
  169. rels_type = ET.SubElement(content_types, "Default", Extension = "rels", ContentType = "application/vnd.openxmlformats-package.relationships+xml")
  170. model_type = ET.SubElement(content_types, "Default", Extension = "model", ContentType = "application/vnd.ms-package.3dmanufacturing-3dmodel+xml")
  171. # Create _rels/.rels file
  172. relations_file = zipfile.ZipInfo("_rels/.rels")
  173. relations_file.compress_type = zipfile.ZIP_DEFLATED
  174. relations_element = ET.Element("Relationships", xmlns = self._namespaces["relationships"])
  175. model_relation_element = ET.SubElement(relations_element, "Relationship", Target = "/" + MODEL_PATH, Id = "rel0", Type = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel")
  176. # Attempt to add a thumbnail
  177. snapshot = self._createSnapshot()
  178. if snapshot:
  179. if export_settings_model != None:
  180. self._addShareLogoToThumbnail(snapshot)
  181. thumbnail_buffer = QBuffer()
  182. thumbnail_buffer.open(QBuffer.OpenModeFlag.ReadWrite)
  183. snapshot.save(thumbnail_buffer, "PNG")
  184. thumbnail_file = zipfile.ZipInfo(THUMBNAIL_PATH)
  185. # Don't try to compress snapshot file, because the PNG is pretty much as compact as it will get
  186. archive.writestr(thumbnail_file, thumbnail_buffer.data())
  187. # Add PNG to content types file
  188. thumbnail_type = ET.SubElement(content_types, "Default", Extension="png", ContentType="image/png")
  189. # Add thumbnail relation to _rels/.rels file
  190. thumbnail_relation_element = ET.SubElement(relations_element, "Relationship",
  191. Target="/" + THUMBNAIL_PATH, Id="rel1",
  192. Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail")
  193. # Write material metadata
  194. packages_metadata = self._getMaterialPackageMetadata() + self._getPluginPackageMetadata()
  195. self._storeMetadataJson({"packages": packages_metadata}, archive, PACKAGE_METADATA_PATH)
  196. savitar_scene = Savitar.Scene()
  197. scene_metadata = CuraApplication.getInstance().getController().getScene().getMetaData()
  198. for key, value in scene_metadata.items():
  199. savitar_scene.setMetaDataEntry(key, value)
  200. current_time_string = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  201. if "Application" not in scene_metadata:
  202. # This might sound a bit strange, but this field should store the original application that created
  203. # the 3mf. So if it was already set, leave it to whatever it was.
  204. savitar_scene.setMetaDataEntry("Application", CuraApplication.getInstance().getApplicationDisplayName())
  205. if "CreationDate" not in scene_metadata:
  206. savitar_scene.setMetaDataEntry("CreationDate", current_time_string)
  207. savitar_scene.setMetaDataEntry("ModificationDate", current_time_string)
  208. transformation_matrix = Matrix()
  209. transformation_matrix._data[1, 1] = 0
  210. transformation_matrix._data[1, 2] = -1
  211. transformation_matrix._data[2, 1] = 1
  212. transformation_matrix._data[2, 2] = 0
  213. global_container_stack = Application.getInstance().getGlobalContainerStack()
  214. # Second step: 3MF defines the left corner of the machine as center, whereas cura uses the center of the
  215. # build volume.
  216. if global_container_stack:
  217. translation_vector = Vector(x=global_container_stack.getProperty("machine_width", "value") / 2,
  218. y=global_container_stack.getProperty("machine_depth", "value") / 2,
  219. z=0)
  220. translation_matrix = Matrix()
  221. translation_matrix.setByTranslation(translation_vector)
  222. transformation_matrix.preMultiply(translation_matrix)
  223. root_node = UM.Application.Application.getInstance().getController().getScene().getRoot()
  224. exported_model_settings = ThreeMFWriter._extractModelExportedSettings(export_settings_model)
  225. for node in nodes:
  226. if node == root_node:
  227. for root_child in node.getChildren():
  228. savitar_node = ThreeMFWriter._convertUMNodeToSavitarNode(root_child,
  229. transformation_matrix,
  230. exported_model_settings)
  231. if savitar_node:
  232. savitar_scene.addSceneNode(savitar_node)
  233. else:
  234. savitar_node = self._convertUMNodeToSavitarNode(node,
  235. transformation_matrix,
  236. exported_model_settings)
  237. if savitar_node:
  238. savitar_scene.addSceneNode(savitar_node)
  239. parser = Savitar.ThreeMFParser()
  240. scene_string = parser.sceneToString(savitar_scene)
  241. archive.writestr(model_file, scene_string)
  242. archive.writestr(content_types_file, b'<?xml version="1.0" encoding="UTF-8"?> \n' + ET.tostring(content_types))
  243. archive.writestr(relations_file, b'<?xml version="1.0" encoding="UTF-8"?> \n' + ET.tostring(relations_element))
  244. except Exception as error:
  245. Logger.logException("e", "Error writing zip file")
  246. self.setInformation(str(error))
  247. return False
  248. finally:
  249. if not self._store_archive:
  250. archive.close()
  251. else:
  252. self._archive = archive
  253. return True
  254. @staticmethod
  255. def _storeMetadataJson(metadata: Dict[str, List[Dict[str, str]]], archive: zipfile.ZipFile, path: str) -> None:
  256. """Stores metadata inside archive path as json file"""
  257. metadata_file = zipfile.ZipInfo(path)
  258. # We have to set the compress type of each file as well (it doesn't keep the type of the entire archive)
  259. metadata_file.compress_type = zipfile.ZIP_DEFLATED
  260. archive.writestr(metadata_file,
  261. json.dumps(metadata, separators=(", ", ": "), indent=4, skipkeys=True, ensure_ascii=False))
  262. @staticmethod
  263. def _getPluginPackageMetadata() -> List[Dict[str, str]]:
  264. """Get metadata for all backend plugins that are used in the project.
  265. :return: List of material metadata dictionaries.
  266. """
  267. backend_plugin_enum_value_regex = re.compile(
  268. r"PLUGIN::(?P<plugin_id>\w+)@(?P<version>\d+.\d+.\d+)::(?P<value>\w+)")
  269. # This regex parses enum values to find if they contain custom
  270. # backend engine values. These custom enum values are in the format
  271. # PLUGIN::<plugin_id>@<version>::<value>
  272. # where
  273. # - plugin_id is the id of the plugin
  274. # - version is in the semver format
  275. # - value is the value of the enum
  276. plugin_ids = set()
  277. def addPluginIdsInStack(stack: CuraContainerStack) -> None:
  278. for key in stack.getAllKeys():
  279. value = str(stack.getProperty(key, "value"))
  280. for plugin_id, _version, _value in backend_plugin_enum_value_regex.findall(value):
  281. plugin_ids.add(plugin_id)
  282. # Go through all stacks and find all the plugin id contained in the project
  283. global_stack = CuraApplication.getInstance().getMachineManager().activeMachine
  284. addPluginIdsInStack(global_stack)
  285. for container in global_stack.getContainers():
  286. addPluginIdsInStack(container)
  287. for extruder_stack in global_stack.extruderList:
  288. addPluginIdsInStack(extruder_stack)
  289. for container in extruder_stack.getContainers():
  290. addPluginIdsInStack(container)
  291. metadata = {}
  292. package_manager = cast(CuraPackageManager, CuraApplication.getInstance().getPackageManager())
  293. for plugin_id in plugin_ids:
  294. package_data = package_manager.getInstalledPackageInfo(plugin_id)
  295. metadata[plugin_id] = {
  296. "id": plugin_id,
  297. "display_name": package_data.get("display_name") if package_data.get("display_name") else "",
  298. "package_version": package_data.get("package_version") if package_data.get("package_version") else "",
  299. "sdk_version_semver": package_data.get("sdk_version_semver") if package_data.get(
  300. "sdk_version_semver") else "",
  301. "type": "plugin",
  302. }
  303. # Storing in a dict and fetching values to avoid duplicates
  304. return list(metadata.values())
  305. @staticmethod
  306. def _getMaterialPackageMetadata() -> List[Dict[str, str]]:
  307. """Get metadata for installed materials in active extruder stack, this does not include bundled materials.
  308. :return: List of material metadata dictionaries.
  309. """
  310. metadata = {}
  311. package_manager = cast(CuraPackageManager, CuraApplication.getInstance().getPackageManager())
  312. for extruder in CuraApplication.getInstance().getExtruderManager().getActiveExtruderStacks():
  313. if not extruder.isEnabled:
  314. # Don't export materials not in use
  315. continue
  316. if isinstance(extruder.material, type(ContainerRegistry.getInstance().getEmptyInstanceContainer())):
  317. # This is an empty material container, no material to export
  318. continue
  319. if package_manager.isMaterialBundled(extruder.material.getFileName(), extruder.material.getMetaDataEntry("GUID")):
  320. # Don't export bundled materials
  321. continue
  322. package_id = package_manager.getMaterialFilePackageId(extruder.material.getFileName(),
  323. extruder.material.getMetaDataEntry("GUID"))
  324. package_data = package_manager.getInstalledPackageInfo(package_id)
  325. # We failed to find the package for this material
  326. if not package_data:
  327. Logger.info(f"Could not find package for material in extruder {extruder.id}, skipping.")
  328. continue
  329. material_metadata = {
  330. "id": package_id,
  331. "display_name": package_data.get("display_name") if package_data.get("display_name") else "",
  332. "package_version": package_data.get("package_version") if package_data.get("package_version") else "",
  333. "sdk_version_semver": package_data.get("sdk_version_semver") if package_data.get(
  334. "sdk_version_semver") else "",
  335. "type": "material",
  336. }
  337. metadata[package_id] = material_metadata
  338. # Storing in a dict and fetching values to avoid duplicates
  339. return list(metadata.values())
  340. @call_on_qt_thread # must be called from the main thread because of OpenGL
  341. def _createSnapshot(self):
  342. Logger.log("d", "Creating thumbnail image...")
  343. if not CuraApplication.getInstance().isVisible:
  344. Logger.log("w", "Can't create snapshot when renderer not initialized.")
  345. return None
  346. try:
  347. snapshot = Snapshot.snapshot(width=300, height=300)
  348. except:
  349. Logger.logException("w", "Failed to create snapshot image")
  350. return None
  351. return snapshot
  352. @staticmethod
  353. def sceneNodesToString(scene_nodes: [SceneNode]) -> str:
  354. savitar_scene = Savitar.Scene()
  355. for scene_node in scene_nodes:
  356. savitar_node = ThreeMFWriter._convertUMNodeToSavitarNode(scene_node)
  357. savitar_scene.addSceneNode(savitar_node)
  358. parser = Savitar.ThreeMFParser()
  359. scene_string = parser.sceneToString(savitar_scene)
  360. return scene_string
  361. @staticmethod
  362. def _extractModelExportedSettings(model: Optional[SettingsExportModel]) -> Dict[str, Set[str]]:
  363. extra_settings = {}
  364. if model is not None:
  365. for group in model.settingsGroups:
  366. if group.category == SettingsExportGroup.Category.Model:
  367. exported_model_settings = set()
  368. for exported_setting in group.settings:
  369. if exported_setting.selected:
  370. exported_model_settings.add(exported_setting.id)
  371. extra_settings[group.category_details] = exported_model_settings
  372. return extra_settings
  373. def exportUcp(self):
  374. preferences = CuraApplication.getInstance().getPreferences()
  375. if preferences.getValue("cura/dialog_on_ucp_project_save"):
  376. self._config_dialog = UCPDialog()
  377. self._config_dialog.show()
  378. else:
  379. application = CuraApplication.getInstance()
  380. workspace_handler = application.getInstance().getWorkspaceFileHandler()
  381. # Set the model to the workspace writer
  382. mesh_writer = workspace_handler.getWriter("3MFWriter")
  383. mesh_writer.setExportModel(SettingsExportModel())
  384. # Open file dialog and write the file
  385. device = application.getOutputDeviceManager().getOutputDevice("local_file")
  386. nodes = [application.getController().getScene().getRoot()]
  387. file_name = CuraApplication.getInstance().getPrintInformation().baseName
  388. try:
  389. device.requestWrite(
  390. nodes,
  391. file_name,
  392. ["application/vnd.ms-package.3dmanufacturing-3dmodel+xml"],
  393. workspace_handler,
  394. preferred_mimetype_list="application/vnd.ms-package.3dmanufacturing-3dmodel+xml"
  395. )
  396. except OutputDeviceError.UserCanceledError:
  397. self._onRejected()
  398. except Exception as e:
  399. message = Message(
  400. catalog.i18nc("@info:error", "Unable to write to file: {0}", file_name),
  401. title=catalog.i18nc("@info:title", "Error"),
  402. message_type=Message.MessageType.ERROR
  403. )
  404. message.show()
  405. Logger.logException("e", "Unable to write to file %s: %s", file_name, e)
  406. self._onRejected()