模型批量旋轉

今天介紹下超圖三維模型批量旋轉

先看一下繞某一點旋轉180度的效果,右邊旋轉前,左邊旋轉後

該demo主要涉及兩個知識點

1. 臨時數據源的創建

2. 批量旋轉的實現

界面如下:

可以選場景中的模型圖層(僅支持地理座標系),可以自定義旋轉衷心,也可以以數據集中心點爲旋轉中心。
可以另存,也可以保存至源數據。

臨時數據源的創建

創建臨時數據源

臨時數據源主要保存一些中間數據,在關閉工作空間時不會保存臨時數據源的數據。

主要代碼:

     private void CreateTempDatasource()
    {
        foreach (Datasource item in m_sceneControl.Scene.Workspace.Datasources)
        {
            //存在臨時數據源時就不需要創建
            if (item.ConnectionInfo.Server.Contains(m_datasrouceName))
            {
                m_tempDatasource = item;
                break;
            }
        }
        if (m_tempDatasource == null)
        {
            DatasourceConnectionInfo info = new DatasourceConnectionInfo();
            //string m_datasrouceName = "臨時數據源"
            info.Alias = m_datasrouceName;
            // string m_server=":memory:"
            info.Server = m_server;
            m_tempDatasource = m_sceneControl.Scene.Workspace.Datasources.Create(info);
        }
    }

創建的臨時數據源如下(tempData_爲創建的臨時數據集):

值得注意的是,當臨時數據源過大時會在本地(指定路徑,具體路徑記不清楚了)創建一個文本型數據原,但是關閉工作空間會自動刪除該文件。故在判斷數據源是不是臨時數據源不能只判斷server。

創建臨時數據集

 //創建臨時數據集
    private DatasetVector CreateDatasetVector(Datasource desDatasource, string datasetName, bool isTemp)
    {
        DatasetVector dataset = null;
        if (isTemp)
        {
            desDatasource.Datasets.Delete(datasetName);
        }
        DatasetVectorInfo info = new DatasetVectorInfo();
        info.Name = datasetName;
        info.Type = DatasetType.Model;
        dataset = m_tempDatasource.Datasets.Create(info);
        dataset.PrjCoordSys = new PrjCoordSys(PrjCoordSysType.EarthLongitudeLatitude);
        return dataset;
    }

new PrjCoordSys(PrjCoordSysType.EarthLongitudeLatitude)表示wgs84地理座標系,單位爲度。

批量旋轉的實現

模型批量旋轉的實現實際上分爲三步:

  • 1.記錄模型初始位置並將模型移動到旋轉中心
  • 2.旋轉模型(RotationZ)
  • 3.旋轉模型原始點(繞旋轉中心)並將模型移動到旋轉後的點

主要代碼:

  private void ModelRotate(DatasetVector selectedDataset, DatasetVector desDataset)
    {
        //旋轉角度
        double rorationAngle = (double)m_numericUpDownAngle.Value;
        //目標數據集           
        Recordset srcRecordset = selectedDataset.GetRecordset(false, CursorType.Static);
        //desDataset臨時數據集
        Recordset desRecordset = desDataset.GetRecordset(false, CursorType.Dynamic);
        desRecordset.Batch.Begin();
        m_progressBar.Maximum = srcRecordset.RecordCount;
        m_progressBar.Value = 0;
        while (!srcRecordset.IsEOF)
        {
            GeoModel3D model3D = srcRecordset.GetGeometry() as GeoModel3D;
            if (model3D == null)
            {
                srcRecordset.MoveNext();
                m_progressBar.Value++;
                continue;
            }
            // 1.記錄模型初始位置並將模型移動到旋轉中心
            Point3D originalPos = model3D.Position;
            model3D.Position = new Point3D(m_rotateCenter.X, m_rotateCenter.Y, 0);
            // 2.旋轉模型(RotationX)
            model3D.RotationZ += rorationAngle;
            // 3.旋轉模型原始點(繞旋轉中心)並將模型移動到旋轉後的點
            GeoPoint newPos = new GeoPoint(originalPos.X, originalPos.Y);
            newPos.Rotate(m_rotateCenter, rorationAngle);
            model3D.Position = new Point3D(newPos.X, newPos.Y, originalPos.Z);

            desRecordset.AddNew(model3D);
            m_progressBar.Value++;
            srcRecordset.MoveNext();
        }
        desRecordset.Batch.Update();
        desRecordset.Dispose();
        desRecordset = null;
        srcRecordset.Dispose();
        srcRecordset = null;
    }

由於三維沒有繞某一點旋轉,所以需要將點轉成二維點(GeoPoint2D)再旋轉。

好了,就介紹這麼多了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章