PandaGIS的SkyLine开发教程(2)__把三维场景下的3D模型和面对象保存成矢量图层。
虽说TerraExplorer提供了把3D模型和面对象保存成Shp图层的功能,但是能够保存的属性字段是固定的,
有的我们需要的属性无法导出到Shp数据中。
比如我们把建筑图层导出成Shp图层,其中建筑底部高度这个属性就无法导出。
当我们把建筑图层用Shp数据做索引,以流的方式导入三维场景中的时候就会发现我们无法控制建筑的底部高度。
在导出面图层的时候也会遇到类似的问题,因此需要写个导出shp图层的工具,能够导出所有需要的属性字段。
这里下面提供相关样例代码,使用SkyLine和MapObject的接口进行开发。
一、点图层的导出,主要用于导出建筑物图层
if (pObj.ObjectType == ObjectTypeCode.OT_MODEL) //确定对象是三维模型
{
ITerrainModel2 pModel = pObj as ITerrainModel2;
Field fld;
MapObjects2.Point pt = new MapObjects2.Point();
object dX = new double(), dY = new double(), dElevation = new double();
object dYaw = new double(), dPitch = new double(), dRoll = new double();
pModel.GetPosition(out dX, out dY, out dElevation, out dYaw, out dPitch, out dRoll);
pt.X = (double)dX;
pt.Y = (double)dY;
recs.AddNew();
fld = recs.Fields.Item("Shape");
fld.Value = pt;
fld = recs.Fields.Item("Name");
fld.Value = m_InformationTree.GetItemName(iItemID);
fld = recs.Fields.Item("FILENAME");
//修改模型的路径
if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() != "" && textBox1.Text.Trim() !=null&& textBox2.Text.Trim() !=null)
{
string FileName = pModel.ModelFileName;
if(FileName.Contains(textBox1.Text.Trim()))
{
fld.Value = FileName.Replace(textBox1.Text.Trim(), textBox2.Text.Trim());
}
else
{
MessageBox.Show("原有对象路径中不含有字符串:"+textBox1.Text.Trim()+",请重新输入。");
return;
}
}
else
{
fld.Value = pModel.ModelFileName;
}
fld = recs.Fields.Item("ALTITUDE");
fld.Value = dElevation;
fld = recs.Fields.Item("YAW");
fld.Value = dYaw; ;
fld = recs.Fields.Item("PITCH");
fld.Value = dPitch;
fld = recs.Fields.Item("ROLL");
fld.Value = dRoll;
fld = recs.Fields.Item("SCALE");
fld.Value = pModel.ScaleFactor;
fld = recs.Fields.Item("TOOLTIP");
fld.Value = pModel.ToolTipText;
fld = recs.Fields.Item("DISTANCE");
fld.Value = pModel.Distance;
fld = recs.Fields.Item("MAXDIST");
fld.Value = pModel.MaxVisibilityDistance;
fld = recs.Fields.Item("MINDIST");
fld.Value = pModel.MinVisibilityDistance;
recs.Update();
}
二、面图层的 可用于导出道路面图层等。
if (pObj.ObjectType == ObjectTypeCode.OT_POLYGON)//确定是对象是多边形对象
{
MapObjects2.Polygon ply = new Polygon();
Field fld;
ITerrainPolyline5 pPolygon = (TerraExplorerX.ITerrainPolyline5)m_InformationTree.GetObjectEx(iItemID, (object)"");
object dX = new double(), dY = new double(), dElevation = new double();
object dYaw = new double(), dPitch = new double(), dRoll = new double();
pPolygon.GetPosition(out dX, out dY, out dElevation, out dYaw, out dPitch, out dRoll);
MapObjects2.Point pt = new MapObjects2.Point();//新建点
object douX,douY,douH;
MapObjects2.Points pts = new Points();//新建点集
int intNumPoint = pPolygon.NumOfVertices;
for (int i = 0; i < intNumPoint; i++)
{
pPolygon.GetVertex(i, out douX, out douH, out douY);
pt.X = (double)douX;
pt.Y = (double)douY;
pts.Add(pt);
}
ply.Parts.Add(pts);//添加点集
recs.AddNew();
fld = recs.Fields.Item("Shape");
fld.Value = ply;
fld = recs.Fields.Item("Name");
fld.Value = m_InformationTree.GetItemName(iItemID);
fld = recs.Fields.Item("TextureFil");
if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() != "" && textBox1.Text.Trim() != null && textBox2.Text.Trim() != null)
{
string FileName = pPolygon.TextureFileName;
if (FileName.Contains(textBox1.Text.Trim()))
{
fld.Value = FileName.Replace(textBox1.Text.Trim(), textBox2.Text.Trim());
}
else
{
MessageBox.Show("原有对象路径中不含有字符串:" + textBox1.Text.Trim() + ",请重新输入。");
return;
}
}
else
{
fld.Value = pPolygon.TextureFileName;
}
fld = recs.Fields.Item("TilingMeth");
switch (pPolygon.TextureTilingMethod.ToString())
{
case "TM_TILES_PER_AXIS":
fld.Value = "TM_TILES_PER_AXIS";
break;
case "TM_METERS_PER_TILE":
fld.Value = "TM_METERS_PER_TILE";
break;
default:
break;
}
fld = recs.Fields.Item("TextureX");
fld.Value = pPolygon.TextureScaleX;
fld = recs.Fields.Item("TextureY");
fld.Value = pPolygon.TextureScaleY;
fld = recs.Fields.Item("TextureR");
fld.Value = pPolygon.TextureRotateAngle;
fld = recs.Fields.Item("ToolTip");
fld.Value = pPolygon.ToolTipText;
fld = recs.Fields.Item("Distance");
fld.Value = pPolygon.Distance;
fld = recs.Fields.Item("MaxDistan");
fld.Value = pPolygon.MaxVisibilityDistance;
fld = recs.Fields.Item("MinDistan");
fld.Value = pPolygon.MinVisibilityDistance;
fld = recs.Fields.Item("AltitudeMe");
switch (pPolygon.HeightStyle)
{
case HeightStyleCode.HS_POLYGON_TERRAIN_RELATIVE:
fld.Value = "相对于地面";
break;
case HeightStyleCode.HSC_TERRAIN_ABSOLUTE:
fld.Value = "绝对高度";
break;
case HeightStyleCode.HS_POLYGON_PIVOT_RELATIVE:
fld.Value = "相对于支点";
break;
case HeightStyleCode.HS_ON_TERRAIN:
fld.Value = "沿地面";
break;
default:
break;
}
fld = recs.Fields.Item("Altitude");
fld.Value = dElevation;
recs.Update();
}
有的我们需要的属性无法导出到Shp数据中。
比如我们把建筑图层导出成Shp图层,其中建筑底部高度这个属性就无法导出。
当我们把建筑图层用Shp数据做索引,以流的方式导入三维场景中的时候就会发现我们无法控制建筑的底部高度。
在导出面图层的时候也会遇到类似的问题,因此需要写个导出shp图层的工具,能够导出所有需要的属性字段。
这里下面提供相关样例代码,使用SkyLine和MapObject的接口进行开发。
一、点图层的导出,主要用于导出建筑物图层
if (pObj.ObjectType == ObjectTypeCode.OT_MODEL) //确定对象是三维模型
{
ITerrainModel2 pModel = pObj as ITerrainModel2;
Field fld;
MapObjects2.Point pt = new MapObjects2.Point();
object dX = new double(), dY = new double(), dElevation = new double();
object dYaw = new double(), dPitch = new double(), dRoll = new double();
pModel.GetPosition(out dX, out dY, out dElevation, out dYaw, out dPitch, out dRoll);
pt.X = (double)dX;
pt.Y = (double)dY;
recs.AddNew();
fld = recs.Fields.Item("Shape");
fld.Value = pt;
fld = recs.Fields.Item("Name");
fld.Value = m_InformationTree.GetItemName(iItemID);
fld = recs.Fields.Item("FILENAME");
//修改模型的路径
if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() != "" && textBox1.Text.Trim() !=null&& textBox2.Text.Trim() !=null)
{
string FileName = pModel.ModelFileName;
if(FileName.Contains(textBox1.Text.Trim()))
{
fld.Value = FileName.Replace(textBox1.Text.Trim(), textBox2.Text.Trim());
}
else
{
MessageBox.Show("原有对象路径中不含有字符串:"+textBox1.Text.Trim()+",请重新输入。");
return;
}
}
else
{
fld.Value = pModel.ModelFileName;
}
fld = recs.Fields.Item("ALTITUDE");
fld.Value = dElevation;
fld = recs.Fields.Item("YAW");
fld.Value = dYaw; ;
fld = recs.Fields.Item("PITCH");
fld.Value = dPitch;
fld = recs.Fields.Item("ROLL");
fld.Value = dRoll;
fld = recs.Fields.Item("SCALE");
fld.Value = pModel.ScaleFactor;
fld = recs.Fields.Item("TOOLTIP");
fld.Value = pModel.ToolTipText;
fld = recs.Fields.Item("DISTANCE");
fld.Value = pModel.Distance;
fld = recs.Fields.Item("MAXDIST");
fld.Value = pModel.MaxVisibilityDistance;
fld = recs.Fields.Item("MINDIST");
fld.Value = pModel.MinVisibilityDistance;
recs.Update();
}
二、面图层的 可用于导出道路面图层等。
if (pObj.ObjectType == ObjectTypeCode.OT_POLYGON)//确定是对象是多边形对象
{
MapObjects2.Polygon ply = new Polygon();
Field fld;
ITerrainPolyline5 pPolygon = (TerraExplorerX.ITerrainPolyline5)m_InformationTree.GetObjectEx(iItemID, (object)"");
object dX = new double(), dY = new double(), dElevation = new double();
object dYaw = new double(), dPitch = new double(), dRoll = new double();
pPolygon.GetPosition(out dX, out dY, out dElevation, out dYaw, out dPitch, out dRoll);
MapObjects2.Point pt = new MapObjects2.Point();//新建点
object douX,douY,douH;
MapObjects2.Points pts = new Points();//新建点集
int intNumPoint = pPolygon.NumOfVertices;
for (int i = 0; i < intNumPoint; i++)
{
pPolygon.GetVertex(i, out douX, out douH, out douY);
pt.X = (double)douX;
pt.Y = (double)douY;
pts.Add(pt);
}
ply.Parts.Add(pts);//添加点集
recs.AddNew();
fld = recs.Fields.Item("Shape");
fld.Value = ply;
fld = recs.Fields.Item("Name");
fld.Value = m_InformationTree.GetItemName(iItemID);
fld = recs.Fields.Item("TextureFil");
if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() != "" && textBox1.Text.Trim() != null && textBox2.Text.Trim() != null)
{
string FileName = pPolygon.TextureFileName;
if (FileName.Contains(textBox1.Text.Trim()))
{
fld.Value = FileName.Replace(textBox1.Text.Trim(), textBox2.Text.Trim());
}
else
{
MessageBox.Show("原有对象路径中不含有字符串:" + textBox1.Text.Trim() + ",请重新输入。");
return;
}
}
else
{
fld.Value = pPolygon.TextureFileName;
}
fld = recs.Fields.Item("TilingMeth");
switch (pPolygon.TextureTilingMethod.ToString())
{
case "TM_TILES_PER_AXIS":
fld.Value = "TM_TILES_PER_AXIS";
break;
case "TM_METERS_PER_TILE":
fld.Value = "TM_METERS_PER_TILE";
break;
default:
break;
}
fld = recs.Fields.Item("TextureX");
fld.Value = pPolygon.TextureScaleX;
fld = recs.Fields.Item("TextureY");
fld.Value = pPolygon.TextureScaleY;
fld = recs.Fields.Item("TextureR");
fld.Value = pPolygon.TextureRotateAngle;
fld = recs.Fields.Item("ToolTip");
fld.Value = pPolygon.ToolTipText;
fld = recs.Fields.Item("Distance");
fld.Value = pPolygon.Distance;
fld = recs.Fields.Item("MaxDistan");
fld.Value = pPolygon.MaxVisibilityDistance;
fld = recs.Fields.Item("MinDistan");
fld.Value = pPolygon.MinVisibilityDistance;
fld = recs.Fields.Item("AltitudeMe");
switch (pPolygon.HeightStyle)
{
case HeightStyleCode.HS_POLYGON_TERRAIN_RELATIVE:
fld.Value = "相对于地面";
break;
case HeightStyleCode.HSC_TERRAIN_ABSOLUTE:
fld.Value = "绝对高度";
break;
case HeightStyleCode.HS_POLYGON_PIVOT_RELATIVE:
fld.Value = "相对于支点";
break;
case HeightStyleCode.HS_ON_TERRAIN:
fld.Value = "沿地面";
break;
default:
break;
}
fld = recs.Fields.Item("Altitude");
fld.Value = dElevation;
recs.Update();
}