1. 程式人生 > >3D打印開源軟件Cura分析(1) 【轉】

3D打印開源軟件Cura分析(1) 【轉】

ali output 價值 下使用 網址 打印機 env src .com

http://www.sohu.com/a/236241465_100000368

Cura是Ultimaker公司開發的3D打印開源軟件,在所有的3D打印開源軟件中應屬上乘之作,很有研究的價值。國內不少高校對其有深入研究,研究成果體現在其畢業論文之中。國內互聯網上也有很多文章對其代碼進行了剖析。

最近一段時間,讀了幾篇論文,分析了Cura的部分代碼,打算寫上若幹篇分析文章。這裏的視角與他人不一樣,並不具體講代碼,不拘泥於具體的切片算法,而是通過使用軟件與閱讀代碼後總結描述其軟件的設計與架構。

所分析的代碼是Cura最新的代碼,分析過程中直接同步GitHub上的代碼庫,發現軟件開發目前很活躍的,代碼提交比較頻繁,開發庫中的版本已經合並到3.4了。

本篇主要描述:

  1. Cura軟件使用的開發語言以及使用的第三方開源庫;
  2. 切片引擎CuraEngine程序的四種命令行參數調用接口;
  3. Cura軟件代碼總體評價。

Cura軟件簡介

Cura軟件實際上包含兩個軟件:Cura與CuraEngine。前者是圖形界面程序,後者是專用於切片的後臺程序,前者依賴於後者,在切片時將調用後者。

技術分享圖片

兩個軟件使用的編程語言與第三方庫

Cura

CuraEngine

開源庫網址 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命令行調用模式的流程往下看,可很快了解代碼頂層的設計實現。

3D打印開源軟件Cura分析(1) 【轉】