模型轉檔視窗程式


建立專案檔

[info] 小提示:
範例程式碼:Github
目標是使用 MicroSoft Visual Studio 2019(或以上之版本)建立一個名為 Windows Forms App 的應用程式。

步驟 1. 首先新增 Windows Forms App(.Net Framework) 專案,在此請使用[.Net Framework 4.7.2](含)以上的版本,接著為專案命名。本範例命名為 ModelConverterApp。

步驟 2. 請在右邊[方案總管]中找到[參考],點擊滑鼠右鍵後點選[加入參考…],視窗開啟後,選擇[瀏覽]頁籤,至安裝目錄下找到TMPEngine.dll檔案並按下[確定],即可將 TMPEngine.dll 加入參考。

PluginSample

步驟 3. 加入參考後請於 Form 的設計視窗點擊滑鼠右鍵,並按下[檢視程式碼]在上方加入以下程式碼:

using PilotGaea.Serialize;
using PilotGaea.TMPEngine;
using PilotGaea.Geometry;

步驟 4. 請在組態管理員選擇適當的方案平台,設定為`x64 執行模式,並選擇 Release 方案組態。

ModelFileConversionApp

編寫轉檔程式

步驟 1. 建立 UI,於工具箱新增 ListBox、ProgressBar、ComboBox、Label、Button 控制項至表單中,將 ListBox 控制項的名稱設置為"listBox_Main",並雙擊 Button 控制項以建立按鈕點擊事件函式。

ModelFileConversionApp

步驟 2. 於Form1()加入功能列表。

public Form1() {
    InitializeComponent();

    // 加入功能列表
    List<string> featureNames = new List<string>();
    featureNames.Add("來源KMZ");
    featureNames.Add("輸出OGC I3S");
    featureNames.Add("輸出OGC 3DTiles");
    comboBox_Features.Items.AddRange(featureNames.ToArray());
    comboBox_Features.SelectedIndex = 0;
}

步驟 3. 在點擊事件函式中建立名為 maker 的 CModelMaker 物件,並將 UI 預設為關閉。

private void button_Start_Click(object sender, EventArgs e) {
    EnableUI(false);
    // 將來源資料輸出成Model圖層
    System.Environment.CurrentDirectory = @"C:\Program Files\PilotGaea\TileMap"; // 為了順利存取安裝目錄下的相關DLL
    CModelMaker maker = new CModelMaker();
    Stopwatch m_Stopwatch = new Stopwatch();
}
private void EnableUI(bool enable)
{
    button_Start.Enabled = enable;
    comboBox_Features.Enabled = enable;
}

步驟 4. 為 maker 物件建立監聽事件,並於類別中新增以下四項事件函式。

m_Maker.CreateLayerCompleted += M_Maker_CreateLayerCompleted; 
m_Maker.EndNewEntityCompleted += M_Maker_EndNewEntityCompleted;
m_Maker.ProgressMessageChanged += M_Maker_ProgressMessageChanged;
m_Maker.ProgressPercentChanged += M_Maker_ProgressPercentChanged;

建立圖層完畢回傳設定:

private void M_Maker_CreateLayerCompleted(string LayerName, bool Success, string ErrorMessage)
{
    m_Stopwatch.Stop();
    string message = string.Format("轉檔{0}", (Success ? "成功" : "失敗"));
    listBox_Main.Items.Add(message);
    message = string.Format("耗時{0}分。", m_Stopwatch.Elapsed.TotalMinutes.ToString("0.00"));
    listBox_Main.Items.Add(message);
}

進度訊息回傳設定:

private void M_Maker_ProgressMessageChanged(string Message)
{
    listBox_Main.Items.Add(Message);
}

進度回傳設定:

private void M_Maker_ProgressPercentChanged(double Percent)
{
    progressBar_Main.Value = Convert.ToInt32(Percent);
}

步驟 5. 新增圖層,呼叫 maker.Create。

  1. LayerName 為目標圖層名稱,可隨意取名。若 EXPORT_TYPE 不為 LET_DB,則為輸出檔名。
  2. DbPath 為目標圖層的資料庫路徑,欲儲存的資料庫。若 EXPORT_TYPE 不為 LET_DB,則此字串為輸出路徑。
  3. TerrainName 為來源地形的名稱, 請確認資料庫中的地形高程名稱。
  4. TerrainDBFile 為來源地形高程的資料庫路徑。
  5. EXPORT_TYPE為要輸出的類型,有 LET_DB、LET_OGCI3S、LET_OGC3DTILES。預設 LET_DB。
  6. FieldNames 為目標圖層的欄位名稱陣列。
  7. FieldTypes 為目標圖層的欄位類型陣列。
// 設定必要參數
CModelMaker.MODEL_CREATE_PARAM createParam = new CModelMaker.MODEL_CREATE_PARAM();
createParam.DbPath = string.Format(@"{0}\..\output\model_maker.DB", Application.StartupPath);
createParam.LayerName = "test";
createParam.TerrainDBFile = string.Format(@"{0}\..\data\terrain_maker\terrain.DB", Application.StartupPath);
createParam.TerrainName = "terrain";
createParam.ExportType = EXPORT_TYPE.LET_DB;
// 設定進階參數
switch (comboBox_Features.SelectedIndex)
{
    case 0: // 基本
        break;
    case 1: // 輸出OGC I3S
        createParam.ExportType = EXPORT_TYPE.LET_OGCI3S;
        createParam.LayerName = "model_maker_ogci3s";
        //會在destPath目錄下產生layerName.slpk
        break;
    case 2: // 輸出OGC 3DTiles
        createParam.ExportType = EXPORT_TYPE.LET_OGC3DTILES;
        createParam.LayerName ="model_maker_ogc3dtiles";
        // 會在destPath目錄下產生layerName資料夾
        break;
}

步驟 6. 開始執行非同步轉檔。

m_Stopwatch.Restart();
// 開始非同步轉檔
bool ret = m_Maker.Create(createParam);
string message = string.Format("Create{0}", (ret ? "通過" : "失敗"));
listBox_Main.Items.Add(message);
ret = m_Maker.NewEntity(sourceData);
message = string.Format("NewEntity{0}", (ret ? "成功" : "失敗"));
listBox_Main.Items.Add(message);
ret = m_Maker.EndCreate();
message = string.Format("EndCreate{0}", (ret ? "通過" : "失敗"));
listBox_Main.Items.Add(message);
Copyright © PilotGaea 2022 Version:13.0 all right reserved,powered by Gitbook修訂時間: 2024-01-31 10:01:42