分享给有需要的人,代码质量勿喷。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System.IO;
namespace shp矢量点转Tin
{
public partial class FormMain : Form
{
public FormMain()
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);//重要的一句话
InitializeComponent();
}
//选择shp矢量点文件
IFeatureClass xjFeatureClass;
private void 打开txtToolStripMenuItem_Click(object sender, EventArgs e)
{
this.axSceneControl1.Scene.ClearLayers();
OpenFileDialog xjShpFileDialogshp = new OpenFileDialog();
xjShpFileDialogshp.Title = "打开矢量点数据";
xjShpFileDialogshp.Filter = "矢量文件(*.shp)|*.shp";
if (xjShpFileDialogshp.ShowDialog() == DialogResult.OK)
{
string xjShpFileFullPath = xjShpFileDialogshp.FileName;
shp点文件全路径ToolStripMenuItem.Text = xjShpFileFullPath;
string xjShpFolder = System.IO.Path.GetDirectoryName(xjShpFileFullPath);
string xjShpFilename = System.IO.Path.GetFileName(xjShpFileFullPath);
IWorkspaceFactory xjWsF = new ShapefileWorkspaceFactory();
IFeatureWorkspace xjFeatureWorkspace = (IFeatureWorkspace)xjWsF.OpenFromFile(xjShpFolder, 0);
IWorkspace xjws = xjWsF.OpenFromFile(xjShpFolder, 0);
IFeatureWorkspace xjFWs = xjws as IFeatureWorkspace;
xjFeatureClass = xjFWs.OpenFeatureClass(xjShpFilename);
IFeatureLayer xjFeatureLayer = new FeatureLayer();
xjFeatureLayer.FeatureClass = xjFeatureClass;
xjFeatureLayer.Name = xjFeatureClass.AliasName;
ILayer xjShpLayer = xjFeatureLayer as ILayer;
this.axSceneControl1.Scene.AddLayer(xjShpLayer, false);
}
}
//选择Tin保存路径
private void 选择Tin保存路径ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog xjTinSaveDialog = new SaveFileDialog();
xjTinSaveDialog.Title = "选择TIN文件保存路径";
xjTinSaveDialog.Filter = "TIN(*.tin)|*.tin";
if (xjTinSaveDialog.ShowDialog() == DialogResult.OK)
{
string xjTINSavepath = xjTinSaveDialog.FileName;
tin保存路径ToolStripMenuItem.Text = xjTINSavepath;
}
}
//转换并显示Tin
IGeoDataset xjGeoDataSet;
IEnvelope xjEnvelopeExtent;
IFields xjFields;
IField xjField;
private void 转换ToolStripMenuItem_Click(object sender, EventArgs e)
{
//1、创建Tin
string TinSavePath = tin保存路径ToolStripMenuItem.Text;
xjGeoDataSet = xjFeatureClass as IGeoDataset;
xjEnvelopeExtent = new EnvelopeClass();
xjEnvelopeExtent = xjGeoDataSet.Extent;
xjFields = xjFeatureClass.Fields;
xjField = xjFields.get_Field(4);//设置所用字段:height
ITinEdit xjTinEdit = new TinClass();
xjTinEdit.InitNew(xjEnvelopeExtent);
try
{
xjTinEdit.AddFromFeatureClass(xjFeatureClass, null, xjField, null,
esriTinSurfaceType.esriTinMassPoint);
}
catch
{
MessageBox.Show("创建TIN失败");
}
xjTinEdit.SaveAs(TinSavePath);
xjTinEdit.StopEditing(false);
//2、加载TIN
IWorkspaceFactory TinWsF = new TinWorkspaceFactory();
ITinWorkspace TinWs;
ITin xjTin;
ITinLayer TinLayer;
ILayer xjLayer;
FileInfo fileinfo = new FileInfo(TinSavePath);
if (TinWsF.IsWorkspace(fileinfo.DirectoryName))
{
TinWs = TinWsF.OpenFromFile(fileinfo.DirectoryName, 0) as ITinWorkspace;
xjTin = TinWs.OpenTin(fileinfo.Name);
TinLayer = new TinLayerClass();
TinLayer.Dataset = xjTin;
xjLayer = TinLayer as ILayer;
//显示
this.axSceneControl1.Scene.AddLayer(xjLayer, true);
this.axSceneControl1.SceneGraph.RefreshViewers();
}
}
}
}
VS2010+ArcEngine10.1窗体+代码:点击打开链接
VS2012+ArcEngine10.2窗体+代码:点击打开链接