http://www.sohu.com/a/236241465_100000368git
Cura是Ultimaker公司開發的3D打印開源軟件,在全部的3D打印開源軟件中應屬上乘之做,頗有研究的價值。國內很多高校對其有深刻研究,研究成果體如今其畢業論文之中。國內互聯網上也有不少文章對其代碼進行了剖析。github
最近一段時間,讀了幾篇論文,分析了Cura的部分代碼,打算寫上若干篇分析文章。這裏的視角與他人不同,並不具體講代碼,不拘泥於具體的切片算法,而是經過使用軟件與閱讀代碼後總結描述其軟件的設計與架構。算法
所分析的代碼是Cura最新的代碼,分析過程當中直接同步GitHub上的代碼庫,發現軟件開發目前很活躍的,代碼提交比較頻繁,開發庫中的版本已經合併到3.4了。編程
本篇主要描述:json
Cura軟件簡介api
Cura軟件實際上包含兩個軟件:Cura與CuraEngine。前者是圖形界面程序,後者是專用於切片的後臺程序,前者依賴於後者,在切片時將調用後者。架構
兩個軟件使用的編程語言與第三方庫app
Curafrontend |
CuraEnginesocket |
|
開源庫網址 | https://github.com/Ultimaker/Cura | https://github.com/Ultimaker/CuraEngine |
開發語言 |
Python,細緻點說還有Qt的QML語言 |
C++ |
第三方庫 |
PyQt,QtQuick,Protobuf,Arcus |
Protobuf,Arcus,clipper,rapidjson |
圖形界面 |
使用的是QtQuick而不是傳統的QtWidgets,圖形界面很新潮 |
控制檯程序,無圖形界面 |
第三方庫的功用
開源庫 |
說明 |
Protobuf |
Google的開源庫,用於Cura與CuraEngine之間的結構數據序列化,簡單說就是保證兩個軟件之間能夠互相傳遞結構化的數據。目前支持多種語言,這裏就使用了C++,Python |
Arcus |
包含C++代碼與Python3的綁定,基於Protobuf庫在一個線程中建立一個socket,而後發送與接受結構化的消息數據 |
PyQt,QtQuick |
Python語言下Qt庫的包裝,也就是可使用Python語言訪問Qt相關庫了,Cura使用了QtQuick圖形界面庫 |
rapidjson |
C++庫,用於解讀JSON格式的文件,CuraEngine使用它解讀打印機配置參數的JSON文件 |
clipper |
C++庫,一個用於二維圖形裁剪與偏移等功能的算法庫,CuraEngine對模型上切出來的全部切片調用此庫進行二維圖形操做 |
在編譯CuraEngine軟件時也可不啓用Arcus與protobuf庫,這樣編譯出來的軟件將致使Cura沒法調用它,而只能單獨使用。 CuraEngine軟件命令行接口
CuraEngine是一個控制檯程序,在cmd中運行CuraEngine help後,將打印其用法信息:
usage:
CuraEngine help
Show thishelp message
CuraEngine connect<host>[:<port>] [-j <settings.def.json>]
--connect<host>[:<port>]
Connect to<host> via a command socket,
instead ofpassing information via the command line
-j<settings.def.json>
Loadsettings.json file to register all settings and their defaults
-v
Increasethe verbose level (show log messages).
-m<thread_count>
Set thedesired number of threads. Supports only a single digit.
CuraEngine slice[-v] [-p] [-j <settings.json>] [-s<settingkey>=<value>] [-g] [-e<extruder_nr>] [-o<output.gcode>] [-l <model.stl>] [--next]
-v
Increasethe verbose level (show log messages).
-m<thread_count>
Set the desired number of threads.
-p
Logprogress information.
-j
Loadsettings.def.json file to register all settings and their defaults.
-s<setting>=<value>
Set asetting to a value for the last supplied object,
extruder train, or general settings.
-l<model_file>
Load an STLmodel.
-g
Switchsetting focus to the current mesh group only.
Used forone-at-a-time printing.
-e<extruder_nr>
Switchsetting focus to the extruder train with the given number.
--next
Generategcode for the previously supplied mesh group and append that to
the gcodeof further models for one-at-a-time printing.
-o<output_file>
Specify afile to which to write the generated gcode.
The settings are appended to the last supplied object:
CuraEngine slice [general settings]
-g [currentgroup settings]
-e0[extruder train 0 settings]
-lobj_inheriting_from_last_extruder_train.stl [object settings]
--next[next group settings]
... etc.
In order to load machine definitions from customlocations, you need to create the environment variable CURA_ENGINE_SEARCH_PATH,which should contain all search paths delimited by a (semi-)colon.
上面打印了三種命令行接口,實際上讀其源代碼發現有四種接口的,只是第四種適合於開發者使用。
下面羅列CuraEngine的四種命令行調用接口:
參數 |
說明 |
help |
第一種接口用法。打印上面的幫助信息。 |
connect |
第二種接口用法。在Cura中執行切片功能時就是這種用法。典型的調用方式是:CuraEngine.exe connect 127.0.0.1:49674 -j 「d:Program FilesUltimaker Cura 3.3resourcesdefinitionsfdmprinter.def.json」 「」 注意上面並無傳遞stl文件之類的參數,模型數據是經過socket傳遞的,也即上面提到的Arcus與Protobuf庫提供的功能。-j指定打印機的json文件。 |
slice |
第三種用法。不是經過Cura調用,直接傳遞stl模型文件等參數,進行切片操做。 CuraEngine slice[-v] [-p] [-j <settings.json>] [-s<settingkey>=<value>] [-g] [-e<extruder_nr>] [-o<output.gcode>] [-l <model.stl>] [--next] 這種方式的命令行參數比較多,具體的例子之後提供。這裏再提供一個經過閱讀代碼發現的一個用法:-v 能夠打印出日誌信息,但不會打印調試信息,當你要更多的輸出日誌信息時,能夠連寫兩個-v參數。 |
analyse |
第四種用法,不看源代碼是不知道這個用法的,主要給開發者用。能夠輸出打印機設置json文件中的信息。調用方法:CuraEngineanalyse[json][output.gv][engine_settings]-[p|i|e|w] p=showparent-childrelations i=show inheritance function e=show error functions w=show warning functions 示例:CuraEngine analyse /home/cubetan/code/fdmprinter.def.json /home/cubetan/code/output.gv -p -i -e -w 生成的output.gv文件可經過dot程序建立一個圖片文件,如dot output.gv > abc.png |
Cura軟件代碼整體評價
Cura軟件新版本根據 https://github.com/Ultimaker/Cura 中的描述:
This is the new, shiny frontend for Cura. Check daid/LegacyCurafor the legacy Cura that everyone knows and loves/hates. We re-worked the whole GUI code at Ultimaker, because the old code started to become unmaintainable.
Cura代碼作了所有重寫,之前用的是Python下的wxWidgets實現圖形界面,實在維護不下去了,只好重寫。如今用的是PyQt+QUICK實現,Cura的代碼可讀性不錯,註釋量偏少。閱讀此代碼若是不深究三維渲染,則須要對Python,Qt,QML,QUICK有必定基礎,不然還須要深刻研究Uranium(https://github.com/Ultimaker/Uranium)。
CuraEngine爲了追求切片效率,代碼是所有使用C++編寫,代碼可讀性很好,註釋較多,也能發現一些設計缺陷,之後會提到。在Windows下使用VC編譯器編譯不了,須要修改代碼,我是在Ubuntu系統下編譯的。CuraEngine是一個控制檯程序,代碼的主線邏輯很是簡單,從main函數入手,順着slice命令行調用模式的流程往下看,可很快了解代碼頂層的設計實現。