CuraApplication.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Qt.QtApplication import QtApplication
  4. from UM.Scene.SceneNode import SceneNode
  5. from UM.Scene.Camera import Camera
  6. from UM.Scene.Platform import Platform
  7. from UM.Math.Vector import Vector
  8. from UM.Math.Matrix import Matrix
  9. from UM.Math.Quaternion import Quaternion
  10. from UM.Resources import Resources
  11. from UM.Scene.ToolHandle import ToolHandle
  12. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  13. from UM.Mesh.WriteMeshJob import WriteMeshJob
  14. from UM.Mesh.ReadMeshJob import ReadMeshJob
  15. from UM.Logger import Logger
  16. from UM.Preferences import Preferences
  17. from UM.Message import Message
  18. from UM.PluginRegistry import PluginRegistry
  19. from UM.JobQueue import JobQueue
  20. from UM.Scene.BoxRenderer import BoxRenderer
  21. from UM.Scene.Selection import Selection
  22. from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
  23. from UM.Operations.RemoveSceneNodeOperation import RemoveSceneNodeOperation
  24. from UM.Operations.GroupedOperation import GroupedOperation
  25. from UM.Operations.SetTransformOperation import SetTransformOperation
  26. from UM.i18n import i18nCatalog
  27. from . import PlatformPhysics
  28. from . import BuildVolume
  29. from . import CameraAnimation
  30. from . import PrintInformation
  31. from . import CuraActions
  32. from PyQt5.QtCore import pyqtSlot, QUrl, Qt, pyqtSignal, pyqtProperty
  33. from PyQt5.QtGui import QColor, QIcon
  34. import platform
  35. import sys
  36. import os.path
  37. import numpy
  38. numpy.seterr(all="ignore")
  39. class CuraApplication(QtApplication):
  40. def __init__(self):
  41. Resources.addResourcePath(os.path.join(QtApplication.getInstallPrefix(), "share", "cura"))
  42. if not hasattr(sys, "frozen"):
  43. Resources.addResourcePath(os.path.join(os.path.abspath(os.path.dirname(__file__)), ".."))
  44. super().__init__(name = "cura", version = "15.05.96")
  45. self.setWindowIcon(QIcon(Resources.getPath(Resources.ImagesLocation, "cura-icon.png")))
  46. self.setRequiredPlugins([
  47. "CuraEngineBackend",
  48. "MeshView",
  49. "LayerView",
  50. "STLReader",
  51. "SelectionTool",
  52. "CameraTool",
  53. "GCodeWriter",
  54. "LocalFileStorage"
  55. ])
  56. self._physics = None
  57. self._volume = None
  58. self._platform = None
  59. self._output_devices = {}
  60. self._print_information = None
  61. self._i18n_catalog = None
  62. self._previous_active_tool = None
  63. self.activeMachineChanged.connect(self._onActiveMachineChanged)
  64. Preferences.getInstance().addPreference("cura/active_machine", "")
  65. Preferences.getInstance().addPreference("cura/active_mode", "simple")
  66. Preferences.getInstance().addPreference("cura/recent_files", "")
  67. Preferences.getInstance().addPreference("cura/categories_expanded", "")
  68. JobQueue.getInstance().jobFinished.connect(self._onJobFinished)
  69. self._recent_files = []
  70. files = Preferences.getInstance().getValue("cura/recent_files").split(";")
  71. for f in files:
  72. if not os.path.isfile(f):
  73. continue
  74. self._recent_files.append(QUrl.fromLocalFile(f))
  75. ## Handle loading of all plugin types (and the backend explicitly)
  76. # \sa PluginRegistery
  77. def _loadPlugins(self):
  78. self._plugin_registry.addPluginLocation(os.path.join(QtApplication.getInstallPrefix(), "lib", "cura"))
  79. if not hasattr(sys, "frozen"):
  80. self._plugin_registry.addPluginLocation(os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", "plugins"))
  81. self._plugin_registry.loadPlugins({ "type": "logger"})
  82. self._plugin_registry.loadPlugins({ "type": "storage_device" })
  83. self._plugin_registry.loadPlugins({ "type": "view" })
  84. self._plugin_registry.loadPlugins({ "type": "mesh_reader" })
  85. self._plugin_registry.loadPlugins({ "type": "mesh_writer" })
  86. self._plugin_registry.loadPlugins({ "type": "tool" })
  87. self._plugin_registry.loadPlugins({ "type": "extension" })
  88. self._plugin_registry.loadPlugin("CuraEngineBackend")
  89. def addCommandLineOptions(self, parser):
  90. parser.add_argument("file", nargs="*", help="Files to load after starting the application.")
  91. def run(self):
  92. self._i18n_catalog = i18nCatalog("cura");
  93. self.addOutputDevice("local_file", {
  94. "id": "local_file",
  95. "function": self._writeToLocalFile,
  96. "description": self._i18n_catalog.i18nc("Save button tooltip", "Save to Disk"),
  97. "icon": "save",
  98. "priority": 0
  99. })
  100. self.showSplashMessage(self._i18n_catalog.i18nc("Splash screen message", "Setting up scene..."))
  101. controller = self.getController()
  102. controller.setActiveView("MeshView")
  103. controller.setCameraTool("CameraTool")
  104. controller.setSelectionTool("SelectionTool")
  105. t = controller.getTool("TranslateTool")
  106. if t:
  107. t.setEnabledAxis([ToolHandle.XAxis, ToolHandle.ZAxis])
  108. Selection.selectionChanged.connect(self.onSelectionChanged)
  109. root = controller.getScene().getRoot()
  110. self._platform = Platform(root)
  111. self._volume = BuildVolume.BuildVolume(root)
  112. self.getRenderer().setLightPosition(Vector(0, 150, 0))
  113. self.getRenderer().setBackgroundColor(QColor(245, 245, 245))
  114. self._physics = PlatformPhysics.PlatformPhysics(controller, self._volume)
  115. camera = Camera("3d", root)
  116. camera.setPosition(Vector(-150, 150, 300))
  117. camera.setPerspective(True)
  118. camera.lookAt(Vector(0, 0, 0))
  119. self._camera_animation = CameraAnimation.CameraAnimation()
  120. self._camera_animation.setCameraTool(self.getController().getTool("CameraTool"))
  121. controller.getScene().setActiveCamera("3d")
  122. self.showSplashMessage(self._i18n_catalog.i18nc("Splash screen message", "Loading interface..."))
  123. self.setMainQml(Resources.getPath(Resources.QmlFilesLocation, "Cura.qml"))
  124. self.initializeEngine()
  125. self.getStorageDevice("LocalFileStorage").removableDrivesChanged.connect(self._removableDrivesChanged)
  126. if self.getMachines():
  127. active_machine_pref = Preferences.getInstance().getValue("cura/active_machine")
  128. if active_machine_pref:
  129. for machine in self.getMachines():
  130. if machine.getName() == active_machine_pref:
  131. self.setActiveMachine(machine)
  132. if not self.getActiveMachine():
  133. self.setActiveMachine(self.getMachines()[0])
  134. else:
  135. self.requestAddPrinter.emit()
  136. self._removableDrivesChanged()
  137. if self._engine.rootObjects:
  138. self.closeSplash()
  139. for file in self.getCommandLineOption("file", []):
  140. job = ReadMeshJob(os.path.abspath(file))
  141. job.finished.connect(self._onFileLoaded)
  142. job.start()
  143. self.exec_()
  144. def registerObjects(self, engine):
  145. engine.rootContext().setContextProperty("Printer", self)
  146. self._print_information = PrintInformation.PrintInformation()
  147. engine.rootContext().setContextProperty("PrintInformation", self._print_information)
  148. self._cura_actions = CuraActions.CuraActions(self)
  149. engine.rootContext().setContextProperty("CuraActions", self._cura_actions)
  150. def onSelectionChanged(self):
  151. if Selection.hasSelection():
  152. if not self.getController().getActiveTool():
  153. if self._previous_active_tool:
  154. self.getController().setActiveTool(self._previous_active_tool)
  155. self._previous_active_tool = None
  156. else:
  157. self.getController().setActiveTool("TranslateTool")
  158. self._camera_animation.setStart(self.getController().getTool("CameraTool").getOrigin())
  159. self._camera_animation.setTarget(Selection.getSelectedObject(0).getWorldPosition())
  160. self._camera_animation.start()
  161. else:
  162. if self.getController().getActiveTool():
  163. self._previous_active_tool = self.getController().getActiveTool().getPluginId()
  164. self.getController().setActiveTool(None)
  165. else:
  166. self._previous_active_tool = None
  167. requestAddPrinter = pyqtSignal()
  168. ## Remove an object from the scene
  169. @pyqtSlot("quint64")
  170. def deleteObject(self, object_id):
  171. object = self.getController().getScene().findObject(object_id)
  172. if not object and object_id != 0: #Workaround for tool handles overlapping the selected object
  173. object = Selection.getSelectedObject(0)
  174. if object:
  175. op = RemoveSceneNodeOperation(object)
  176. op.push()
  177. ## Create a number of copies of existing object.
  178. @pyqtSlot("quint64", int)
  179. def multiplyObject(self, object_id, count):
  180. node = self.getController().getScene().findObject(object_id)
  181. if not node and object_id != 0: #Workaround for tool handles overlapping the selected object
  182. node = Selection.getSelectedObject(0)
  183. if node:
  184. op = GroupedOperation()
  185. for i in range(count):
  186. new_node = SceneNode()
  187. new_node.setMeshData(node.getMeshData())
  188. new_node.setScale(node.getScale())
  189. new_node.translate(Vector((i + 1) * node.getBoundingBox().width, 0, 0))
  190. new_node.setSelectable(True)
  191. op.addOperation(AddSceneNodeOperation(new_node, node.getParent()))
  192. op.push()
  193. ## Center object on platform.
  194. @pyqtSlot("quint64")
  195. def centerObject(self, object_id):
  196. node = self.getController().getScene().findObject(object_id)
  197. if not node and object_id != 0: #Workaround for tool handles overlapping the selected object
  198. node = Selection.getSelectedObject(0)
  199. if node:
  200. op = SetTransformOperation(node, Vector())
  201. op.push()
  202. ## Delete all mesh data on the scene.
  203. @pyqtSlot()
  204. def deleteAll(self):
  205. nodes = []
  206. for node in DepthFirstIterator(self.getController().getScene().getRoot()):
  207. if type(node) is not SceneNode or not node.getMeshData():
  208. continue
  209. nodes.append(node)
  210. if nodes:
  211. op = GroupedOperation()
  212. for node in nodes:
  213. op.addOperation(RemoveSceneNodeOperation(node))
  214. op.push()
  215. ## Reset all translation on nodes with mesh data.
  216. @pyqtSlot()
  217. def resetAllTranslation(self):
  218. nodes = []
  219. for node in DepthFirstIterator(self.getController().getScene().getRoot()):
  220. if type(node) is not SceneNode or not node.getMeshData():
  221. continue
  222. nodes.append(node)
  223. if nodes:
  224. op = GroupedOperation()
  225. for node in nodes:
  226. op.addOperation(SetTransformOperation(node, Vector()))
  227. op.push()
  228. ## Reset all transformations on nodes with mesh data.
  229. @pyqtSlot()
  230. def resetAll(self):
  231. nodes = []
  232. for node in DepthFirstIterator(self.getController().getScene().getRoot()):
  233. if type(node) is not SceneNode or not node.getMeshData():
  234. continue
  235. nodes.append(node)
  236. if nodes:
  237. op = GroupedOperation()
  238. for node in nodes:
  239. op.addOperation(SetTransformOperation(node, Vector(), Quaternion(), Vector(1, 1, 1)))
  240. op.push()
  241. ## Reload all mesh data on the screen from file.
  242. @pyqtSlot()
  243. def reloadAll(self):
  244. nodes = []
  245. for node in DepthFirstIterator(self.getController().getScene().getRoot()):
  246. if type(node) is not SceneNode or not node.getMeshData():
  247. continue
  248. nodes.append(node)
  249. if not nodes:
  250. return
  251. for node in nodes:
  252. if not node.getMeshData():
  253. continue
  254. file_name = node.getMeshData().getFileName()
  255. if file_name:
  256. job = ReadMeshJob(file_name)
  257. job.finished.connect(lambda j: node.setMeshData(j.getResult()))
  258. job.start()
  259. ## Get logging data of the backend engine
  260. # \returns \type{string} Logging data
  261. @pyqtSlot(result=str)
  262. def getEngineLog(self):
  263. log = ""
  264. for entry in self.getBackend().getLog():
  265. log += entry.decode()
  266. return log
  267. recentFilesChanged = pyqtSignal()
  268. @pyqtProperty("QVariantList", notify = recentFilesChanged)
  269. def recentFiles(self):
  270. return self._recent_files
  271. @pyqtSlot("QStringList")
  272. def setExpandedCategories(self, categories):
  273. categories = list(set(categories))
  274. categories.sort()
  275. joined = ";".join(categories)
  276. if joined != Preferences.getInstance().getValue("cura/categories_expanded"):
  277. Preferences.getInstance().setValue("cura/categories_expanded", joined)
  278. self.expandedCategoriesChanged.emit()
  279. expandedCategoriesChanged = pyqtSignal()
  280. @pyqtProperty("QStringList", notify = expandedCategoriesChanged)
  281. def expandedCategories(self):
  282. return Preferences.getInstance().getValue("cura/categories_expanded").split(";")
  283. outputDevicesChanged = pyqtSignal()
  284. @pyqtProperty("QVariantMap", notify = outputDevicesChanged)
  285. def outputDevices(self):
  286. return self._output_devices
  287. @pyqtProperty("QStringList", notify = outputDevicesChanged)
  288. def outputDeviceNames(self):
  289. return self._output_devices.keys()
  290. @pyqtSlot(str, result = "QVariant")
  291. def getSettingValue(self, key):
  292. if not self.getActiveMachine():
  293. return None
  294. return self.getActiveMachine().getSettingValueByKey(key)
  295. ## Change setting by key value pair
  296. @pyqtSlot(str, "QVariant")
  297. def setSettingValue(self, key, value):
  298. if not self.getActiveMachine():
  299. return
  300. self.getActiveMachine().setSettingValueByKey(key, value)
  301. ## Add an output device that can be written to.
  302. #
  303. # \param id \type{string} The identifier used to identify the device.
  304. # \param device \type{StorageDevice} A dictionary of device information.
  305. # It should contains the following:
  306. # - function: A function to be called when trying to write to the device. Will be passed the device id as first parameter.
  307. # - description: A translated string containing a description of what happens when writing to the device.
  308. # - icon: The icon to use to represent the device.
  309. # - priority: The priority of the device. The device with the highest priority will be used as the default device.
  310. def addOutputDevice(self, id, device):
  311. self._output_devices[id] = device
  312. self.outputDevicesChanged.emit()
  313. ## Remove output device
  314. # \param id \type{string} The identifier used to identify the device.
  315. # \sa PrinterApplication::addOutputDevice()
  316. def removeOutputDevice(self, id):
  317. if id in self._output_devices:
  318. del self._output_devices[id]
  319. self.outputDevicesChanged.emit()
  320. @pyqtSlot(str)
  321. def writeToOutputDevice(self, device):
  322. self._output_devices[device]["function"](device)
  323. writeToLocalFileRequested = pyqtSignal()
  324. def _writeToLocalFile(self, device):
  325. self.writeToLocalFileRequested.emit()
  326. def _writeToSD(self, device):
  327. for node in DepthFirstIterator(self.getController().getScene().getRoot()):
  328. if type(node) is not SceneNode or not node.getMeshData():
  329. continue
  330. try:
  331. path = self.getStorageDevice("LocalFileStorage").getRemovableDrives()[device]
  332. except KeyError:
  333. Logger.log("e", "Tried to write to unknown SD card %s", device)
  334. return
  335. filename = os.path.join(path, node.getName()[0:node.getName().rfind(".")] + ".gcode")
  336. job = WriteMeshJob(filename, node.getMeshData())
  337. job._sdcard = device
  338. job.start()
  339. job.finished.connect(self._onWriteToSDFinished)
  340. return
  341. def _removableDrivesChanged(self):
  342. drives = self.getStorageDevice("LocalFileStorage").getRemovableDrives()
  343. for drive in drives:
  344. if drive not in self._output_devices:
  345. self.addOutputDevice(drive, {
  346. "id": drive,
  347. "function": self._writeToSD,
  348. "description": self._i18n_catalog.i18nc("Save button tooltip. {0} is sd card name", "Save to SD Card {0}").format(drive),
  349. "icon": "save_sd",
  350. "priority": 1
  351. })
  352. drives_to_remove = []
  353. for device in self._output_devices:
  354. if device not in drives:
  355. if self._output_devices[device]["function"] == self._writeToSD:
  356. drives_to_remove.append(device)
  357. for drive in drives_to_remove:
  358. self.removeOutputDevice(drive)
  359. def _onActiveMachineChanged(self):
  360. machine = self.getActiveMachine()
  361. if machine:
  362. Preferences.getInstance().setValue("cura/active_machine", machine.getName())
  363. self._volume.setWidth(machine.getSettingValueByKey("machine_width"))
  364. self._volume.setHeight(machine.getSettingValueByKey("machine_height"))
  365. self._volume.setDepth(machine.getSettingValueByKey("machine_depth"))
  366. disallowed_areas = machine.getSettingValueByKey("machine_disallowed_areas")
  367. areas = []
  368. if disallowed_areas:
  369. for area in disallowed_areas:
  370. polygon = []
  371. polygon.append(Vector(area[0][0], 0.2, area[0][1]))
  372. polygon.append(Vector(area[1][0], 0.2, area[1][1]))
  373. polygon.append(Vector(area[2][0], 0.2, area[2][1]))
  374. polygon.append(Vector(area[3][0], 0.2, area[3][1]))
  375. areas.append(polygon)
  376. self._volume.setDisallowedAreas(areas)
  377. self._volume.rebuild()
  378. if self.getController().getTool("ScaleTool"):
  379. bbox = self._volume.getBoundingBox()
  380. bbox.setBottom(0.0)
  381. self.getController().getTool("ScaleTool").setMaximumBounds(bbox)
  382. offset = machine.getSettingValueByKey("machine_platform_offset")
  383. if offset:
  384. self._platform.setPosition(Vector(offset[0], offset[1], offset[2]))
  385. else:
  386. self._platform.setPosition(Vector(0.0, 0.0, 0.0))
  387. def _onWriteToSDFinished(self, job):
  388. message = Message(self._i18n_catalog.i18nc("Saved to SD message, {0} is sdcard, {1} is filename", "Saved to SD Card {0} as {1}").format(job._sdcard, job.getFileName()))
  389. message.addAction(
  390. "eject",
  391. self._i18n_catalog.i18nc("Message action", "Eject"),
  392. "eject",
  393. self._i18n_catalog.i18nc("Message action tooltip, {0} is sdcard", "Eject SD Card {0}").format(job._sdcard)
  394. )
  395. message._sdcard = job._sdcard
  396. message.actionTriggered.connect(self._onMessageActionTriggered)
  397. message.show()
  398. def _onMessageActionTriggered(self, message, action):
  399. if action == "eject":
  400. self.getStorageDevice("LocalFileStorage").ejectRemovableDrive(message._sdcard)
  401. def _onFileLoaded(self, job):
  402. mesh = job.getResult()
  403. if mesh != None:
  404. node = SceneNode()
  405. node.setSelectable(True)
  406. node.setMeshData(mesh)
  407. node.setName(os.path.basename(job.getFileName()))
  408. op = AddSceneNodeOperation(node, self.getController().getScene().getRoot())
  409. op.push()
  410. def _onJobFinished(self, job):
  411. if type(job) is not ReadMeshJob:
  412. return
  413. f = QUrl.fromLocalFile(job.getFileName())
  414. if f in self._recent_files:
  415. self._recent_files.remove(f)
  416. self._recent_files.insert(0, f)
  417. if len(self._recent_files) > 10:
  418. del self._recent_files[10]
  419. pref = ""
  420. for path in self._recent_files:
  421. pref += path.toLocalFile() + ";"
  422. Preferences.getInstance().setValue("cura/recent_files", pref)
  423. self.recentFilesChanged.emit()