点击“道路要素”或者“红绿灯要素”旁边的打开按钮,即可导入shp文件并添加进当前图层中。
代码如下:
private void pictureBox3_Click(object sender, EventArgs e)
{
IWorkspaceFactory shpFactory = new ShapefileWorkspaceFactoryClass();
OpenFileDialog ofDialog = new OpenFileDialog();
ofDialog.Title = "输入ArcMap文档名称";
ofDialog.Filter = "ArcView Shape文件|*.shp";
ofDialog.Multiselect = true;//可以多选
if (ofDialog.ShowDialog() == DialogResult.OK)
{
string fileName = ofDialog.FileName;
string filePath = fileName.Substring(0, fileName.LastIndexOf('\\'));
IWorkspace shpWorkspace = shpFactory.OpenFromFile(filePath, 0);
// 选择多个shape文件(取Map名称)
string[] files = ofDialog.FileNames;
IEnumDataset ds = shpWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
IDataset featureClass = ds.Next();
IMxDocument imd = (IMxDocument)xxxxxxxxxx.Document;
while (featureClass != null)
{
if (IsFileExists(featureClass.Name, files))
{
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.Name = featureClass.Name;
featureLayer.FeatureClass = featureClass as IFeatureClass;
ILayer layer = featureLayer as ILayer;
//将每一个选择的shp文件添加进图层
imd .AddLayer(layer);
}
featureClass = ds.Next();
}
IMap pmap = imd.FocusMap;
IMxdContents mxdContents = imd.FocusMap as IMxdContents;
IActiveView pActiveView = (IActiveView)pmap;
yunxingcanshu.layerroadname = pmap.get_Layer(0).Name;//默认为第一个添加的shp文件
comboBox1.Items.Add(yunxingcanshu.layerroadname);//更新图层选择下拉框
comboBox1.SelectedIndex = 0;
pActiveView.ContentsChanged();
}
}
private bool IsFileExists(string value, string[] files)
{
foreach (string file in files)
{
if (file.IndexOf(value + ".shp") > 0)
return true;
}
return false;
}