读取文本坐标绘制图形,关键代码:
private void btnExpTXTCoord_Click(object sender, EventArgs e)
{
}
List
pointCoord = GeometryHelperClass.ReadTextCoords();
//坐标点序列构建多边形
IGeometry ge = GeometryHelperClass.PolygonFromPoints(pointCoord);
//编辑Feature
EditFeatureShape(ge);
//更新界面
GetFigureSpotCoodInfo(this.p_Feature);
public static List ReadTextCoords()
{
System.Windows.Forms.OpenFileDialog open = new
System.Windows.Forms.OpenFileDialog();
open.Multiselect = false;
open.Filter = "文本文件(*.txt)|*.txt";
if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
return PointsFromText(open.FileName);
else
return null;
}
///
/// 从文本文件获取点坐标序列
///
/// 坐标文本文件
/// 点序列
private static List PointsFromText(string fileName)
{
List lines = FileHelper.ReadLines(fileName);
List pointList = new List();
PointCoord point;
string[] pointArray;
foreach (string line in lines)
{
pointArray = line.Split(new char[] { ',' });
point.ID = pointArray[0];
point.X = Convert.ToDouble(pointArray[1]);
point.Y = Convert.ToDouble(pointArray[2]);
pointList.Add(point);
}
return pointList;
}
///
/// 坐标点序列构建多边形
///
///
PointCoord 的泛型集合
///
public static IGeometry PolygonFromPoints(List
polygonCoord)
{
ESRI.ArcGIS.esriSystem.WKSPoint[] pointArray = new
ESRI.ArcGIS.esriSystem.WKSPoint[polygonCoord.Count + 1];
for (int i = 0; i < pointArray.Length; i++)
{
}
if (i == pointArray.Length - 1) //闭合点
{
}
pointArray[i] = new ESRI.ArcGIS.esriSystem.WKSPoint();
pointArray[i].X = polygonCoord[0].X;
pointArray[i].Y = polygonCoord[0].Y;
break;
pointArray[i] = new ESRI.ArcGIS.esriSystem.WKSPoint();
pointArray[i].X = polygonCoord[i].X;
pointArray[i].Y = polygonCoord[i].Y;
// 构造 Polygon 集合图形
IPointCollection4 pointCollection = new PolygonClass();
//adds WKSpointZs to pointCollection
IGeometryBridge2 geometryBridge = new GeometryEnvironmentClass();
geometryBridge.SetWKSPoints(pointCollection, ref pointArray);
return pointCollection as IGeometry;
}
private void EditFeatureShape(IGeometry ge)
{
IWorkspaceEdit m_WorkspaceEdit = GetWorkspaceEdit();
m_WorkspaceEdit.StartEditing(true);
m_WorkspaceEdit.StartEditOperation();
IFeature p_Feature = p_FeatureLayer.FeatureClass.CreateFeature();
p_Feature.Shape = ge;
p_Feature.Store();
m_WorkspaceEdit.StopEditOperation();
m_WorkspaceEdit.StopEditing(true);
p_mActiveView.Refresh();
}
public void GetFigureSpotCoodInfo(IFeature pFeature)
{
if (pFeature == null) return;
figureSpotEditForm.Text = pFeature.get_Value(7).ToString();
IPolygon m_Polygon = (IPolygon)p_Feature.Shape;
m_PointCollection = (IPointCollection)m_Polygon;
//m_PointCollection = (IPointCollection)pFeature.Shape;
DataTable da = new DataTable();
DataColumn dcol1 = new DataColumn();
dcol1.ColumnName = "节点序号";
DataColumn dcol2 = new DataColumn();
dcol2.ColumnName = "X坐标";
DataColumn dcol3 = new DataColumn();
dcol3.ColumnName = "Y坐标";
da.Columns.AddRange(new DataColumn[] { dcol1, dcol2, dcol3 });
for (int i = 0; i < m_PointCollection.PointCount-1; i++)
{
DataRow dr = da.NewRow();
dr[0] = i;
dr[1] =
CommonToolsClass.ConvertDecToLongandLat(m_PointCollection.Point[i].X);
dr[2] =
CommonToolsClass.ConvertDecToLongandLat(m_PointCollection.Point[i].Y);
da.Rows.Add(dr);
//INT(A1)&"度"&INT((A1-INT(A1))*60)&"分
"&ROUND(((A1-INT(A1))*60-INT((A1-INT(A1))*60))*60,0)&"秒"
#region MyRegion
//listView1.Columns.Clear();
//ColumnHeader colX = new ColumnHeader();
//colX.Width = 130;
//colX.Text = "X坐标";
//ColumnHeader colY = new ColumnHeader();
//colY.Text = "Y坐标";
//colY.Width = 130;
//listView1.Columns.AddRange(new ColumnHeader[] { colX,colY });
//listView1.View = View.Details;
//ListViewItem lvi = new ListViewItem(new string[]
{ m_PointCollection.Point[i].X.ToString(), m_PointCollection.Point[i].Y.ToString() });
//listView1.Items.Add(lvi);
////listView1.Items.Add(m_PointCollection.Point[i].X.ToString());
////listView1.Items.Add(m_PointCollection.Point[i].Y.ToString());
//var de = m_PointCollection.Point[i].X;
#endregion
}
dataGridView1.DataSource = da;
dataGridView1.Columns[0].Width = 100;
dataGridView1.Columns[1].Width = 170;
dataGridView1.Columns[2].Width = 170;
}
读取CAD中的Ploygon图层,通过提取IGeometry来创建新的Feature
private void btnExpCADCoord_Click(object sender, EventArgs e)
{
}
IGeometry ge = AddCadLayer();
if (ge == null) return;
EditFeatureShape(ge);
//更新界面
GetFigureSpotCoodInfo(this.p_Feature);
public IGeometry AddCadLayer()
{
System.Windows.Forms.OpenFileDialog opendlg = new
System.Windows.Forms.OpenFileDialog();
opendlg.Filter = "CAD图形文件 (*.dwg)|*.dwg";
opendlg.RestoreDirectory = true;
opendlg.Title = "加载 AutoCAD 图形:";
opendlg.Multiselect = false;
//打开CAD文件
if (opendlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
return AddCADLayersFromFile(opendlg.FileName);
return null;
}
///
/// 加载CAD中的面图层,返回第一个图形的geometry.
///
///
pPoint = new PointClass();
pPoint.X = m_PointCollection.Point[j].X;
pPoint.Y = m_PointCollection.Point[j].Y;
points.Add(pPoint);
//str+=m_PointCollection.Point[j].X+"==="+m_PointCollection.Point[j].Y;
//str += "\r\n";
}
ge = GeometryHelperClass.PolygonFromPoints(points);
}
}
}
}
catch (Exception ex)
{
}
throw;
return ge;
}
Arcgis engine 读取 datagridview 中的数据,将其输出到文本
///
/// 将datagridview中的数据导出到txt坐标文本
///
///
///
private void btnExport_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() != DialogResult.OK)
return;
string savePath = saveFileDialog.FileName;
string txt = "";
txt = "节点序号" + "
" + "X坐标" + "
" + "Y
坐标";
txt += "\r\n";
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
txt += dataGridView1[0, i].Value.ToString() + "
" +
dataGridView1[1, i].Value.ToString() + "
" + dataGridView1[2, i].Value.ToString();
txt += "\r\n";
}
StreamWriter sw = new StreamWriter(savePath);
sw.Write(txt);
sw.Close();
}