-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEraseForm.cs
More file actions
185 lines (145 loc) · 5.72 KB
/
Copy pathEraseForm.cs
File metadata and controls
185 lines (145 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
namespace ArcEngine
{
public partial class EraseForm : Form
{
private IHookHelper _hookHelper;
private AddLayer _addLayer;
private IMap _map;
public EraseForm(IHookHelper hookHelper, AddLayer addLayer)
{
InitializeComponent();
_hookHelper = hookHelper;
_addLayer = addLayer;
_map = hookHelper.FocusMap;
}
private void EraseForm_Load(object sender, EventArgs e)
{
// 添加 输入元素、擦除元素
for (int i = 0; i < _map.LayerCount; i++)
{
inputBox.Items.Add(_map.Layer[i].Name);
eraseBox.Items.Add(_map.Layer[i].Name);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SeletPath();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
SeletPath();
}
/// <summary>
/// 设置默认输出元素
/// </summary>
private void SeletPath()
{
if (inputBox.Text != "" && eraseBox.Text != "")
{
string path = System.IO.Path.GetTempPath();
outputBox.Text = System.IO.Path.Combine(path, String.Format("{0}_{1}_erase.shp", inputBox.Text, eraseBox.Text));
}
}
/// <summary>
/// 选择自定义保存路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 选择_Click(object sender, EventArgs e)
{
//set the output layer
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.CheckPathExists = true;
saveFileDialog.Filter = "Shapefile (*.shp)|*.shp";
saveFileDialog.OverwritePrompt = true;
saveFileDialog.Title = "Output Layer";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.FileName = String.Format("{0}_{1}_erase.shp", inputBox.Text, eraseBox.Text);
DialogResult dr = saveFileDialog.ShowDialog();
if (dr == DialogResult.OK)
outputBox.Text = saveFileDialog.FileName;
}
private void 确定_Click(object sender, EventArgs e)
{
IFeatureLayer inputLayer = GetLayer(inputBox.Text);
IFeatureLayer eraseLayer = GetLayer(eraseBox.Text);
string path = outputBox.Text;
string folder = System.IO.Path.GetDirectoryName(path);
string filename = System.IO.Path.GetFileName(path);
// IWorkspaceName workspaceName = new WorkspaceNameClass();
// workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";
// workspaceName.PathName = folder;
//
// IFeatureClassName featureClassName = new FeatureClassNameClass();
// featureClassName.FeatureType = esriFeatureType.esriFTSimple;
// featureClassName.ShapeFieldName = "Shape";
// featureClassName.ShapeType = inputLayer.FeatureClass.ShapeType;
//
// IDatasetName datasetName = featureClassName as IDatasetName;
// datasetName.Name = filename;
// datasetName.WorkspaceName = workspaceName;
// IFeatureLayer outLayer = new FeatureLayerClass();
// IFeatureClass outClass = null;
// outClass = EraseRun(inputLayer.FeatureClass, eraseLayer.FeatureClass, outClass);
EraseRun(inputLayer.FeatureClass, eraseLayer.FeatureClass, outputBox.Text);
}
/// <summary>
///
/// </summary>
/// <param name="name">图层名</param>
/// <returns>图层名对应图层</returns>
private IFeatureLayer GetLayer(string name)
{
for (int i = 0; i < _map.LayerCount; i++)
{
if (_map.Layer[i].Name == name)
{
return _map.Layer[i] as IFeatureLayer;
}
}
Show("没有找到图层 !");
return null;
}
private void EraseRun(IFeatureClass in_features, IFeatureClass erase_features, object out_feature_class)
{
// IAoInitialize m_AoInitialize = new AoInitializeClass();
// esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
//
// licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
Erase erase = new Erase()
{
in_features = in_features,
erase_features = erase_features,
out_feature_class = out_feature_class
};
Geoprocessor processor = new Geoprocessor();
processor.OverwriteOutput = true;
try
{
IGeoProcessorResult result = processor.Execute(erase, new TrackCancelClass()) as IGeoProcessorResult;
}
catch (Exception e)
{
Show(e.ToString());
throw;
}
}
private void Show(string s)
{
MessageBox.Show(s);
}
}
}