public static Response AutoNumber(int PartCount)
{
partCount = PartCount;
ILayer mLayer = GetLayerByName("范围线");
ILayer pLayer = GetLayerByName("界址点");
if (mLayer != null)
{
if (pLayer != null)
{
string name = "";
if (PartCount != 0)
{
name = "下一部件";
}
if (MessageBox.Show("请拉框选择" + name + "开始编号的起点!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
operation = "选择编号起点";
GetAllLayer(listLayer, Selectable);
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
pFeatureLayer.Selectable = true;
}
else
{
operation = "";
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加载界址点!", "提示", MessageBoxButtons.OK);
}
}
else
{
MessageBox.Show("请先加载范围线!", "提示", MessageBoxButtons.OK);
}
return new Response(Constant.Success, "");
}
private void aecMap_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
string operation = PageManager.operation;
if (operation == "选择编号起点")
{
IEnvelope pEnv = MapControl.TrackRectangle();
MapControl.Map.SelectByShape(pEnv, null, true);
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
ISelection selection = MapControl.Map.FeatureSelection;
PageManager.AutoNumberGetPoint(selection, PageManager.partCount);
}
MapControl.ActiveView.Refresh();
}
public static Response AutoNumberGetPoint(ISelection selection, int PartCount)
{
IEnumFeatureSetup enumFeatureSetup = selection as IEnumFeatureSetup;
enumFeatureSetup.AllFields = true;
IEnumFeature enumFeature = enumFeatureSetup as IEnumFeature;
IFeature feature = enumFeature.Next();
if (feature != null)
{
if (MessageBox.Show("是否将当前选择点设为编号的起点!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
ProcessBar processBar = new ProcessBar();
processBar.Show();
processBar.Update();
int featureID = (int)feature.get_Value(0);
ILayer pLayer = GetLayerByName("界址点");
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
int Count = pFeatureClass.FeatureCount(null);
ILayer mLayer = GetLayerByName("范围线");
IFeatureLayer mFeatureLayer = mLayer as IFeatureLayer;
IFeatureClass mFeatureClass = mFeatureLayer.FeatureClass;
IFeature mFeature = mFeatureClass.GetFeature(0);
IGeometry mGeometry = mFeature.Shape as IGeometry;
IGeometryCollection mGeoColl = mGeometry as IGeometryCollection;
CheckUpdate(pFeatureClass, mFeatureClass, Count, PartCount);
SerialNumber(pFeatureClass, featureID, mGeoColl, Count, PartCount);
processBar.Close();
for (int i = 0; i < Selectable.Count;i++ )
{
string LayerName = listLayer[i].ToString();
ILayer pLayerName = GetLayerByName(LayerName);
IFeatureLayer pFeatureLayerName = pLayerName as IFeatureLayer;
pFeatureLayerName.Selectable = Selectable[i];
}
}
}
return new Response(Constant.Success, "");
}
public static void CheckUpdate(IFeatureClass pFeatureClass, IFeatureClass mFeatureClass, int Count, int PartCount)
{
IFeature mFeature = mFeatureClass.GetFeature(0);
IGeometry mGeometry = mFeature.Shape as IGeometry;
IGeometryCollection mGeoColl = mGeometry as IGeometryCollection;
int Field = pFeatureClass.FindField("Rank");
if (Field == -1)
{
IFieldEdit pFieldEdit = new FieldClass();
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pFieldEdit.Name_2 = "Rank";
IClass pClass = pFeatureClass as IClass;
pClass.AddField((IField)pFieldEdit);
}
int Field1 = pFeatureClass.FindField("Distance");
if (Field1 == -1)
{
IFieldEdit pFieldEdit = new FieldClass();
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
pFieldEdit.Name_2 = "Distance";
IClass pClass = pFeatureClass as IClass;
pClass.AddField((IField)pFieldEdit);
}
if (mGeoColl.GeometryCount >= 2)
{
int mField = pFeatureClass.FindField("Part");
if (mField == -1)
{
IFieldEdit pFieldEdit = new FieldClass();
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
pFieldEdit.Name_2 = "Part";
IClass pClass = pFeatureClass as IClass;
pClass.AddField((IField)pFieldEdit);
}
for (int l = 0; l < Count; l++)
{
int PartValue = (int)pFeatureClass.GetFeature(l).get_Value(7);
if (PartValue == 0)
{
for (int i = 0; i < mGeoColl.GeometryCount; i++)
{
IGeometry pGeometry = mGeoColl.get_Geometry(i);
IPointCollection mPointCollection = pGeometry as IPointCollection;
IPolygon pPolygon = new PolygonClass();
IGeometry Geometry = pPolygon as IGeometry;
IPointCollection pPointCollection = Geometry as IPointCollection;
pPointCollection.AddPointCollection(mPointCollection);
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pPolygon as IGeometry;
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
pFeature.set_Value(7, i + 1);
pFeature.Store();
pFeature = pFeatureCursor.NextFeature();
}
}
}
}
}
for (int i = 0; i < Count; i++)
{
int RankValue = (int)pFeatureClass.GetFeature(i).get_Value(5);
if (RankValue == 0)
{
PointRank(pFeatureClass, mFeatureClass);
}
}
if (PartCount == 0)
{
int RankValueSum = 0;
for (int i = 0; i < Count; i++)
{
RankValueSum += (int)pFeatureClass.GetFeature(i).get_Value(5);
}
if (RankValueSum != (1 + Count) * Count / 2)
{
PointRank(pFeatureClass, mFeatureClass);
}
}
}
public static void SerialNumber(IFeatureClass pFeatureClass, int featureID, IGeometryCollection mGeoColl, int Count, int PartCount)
{
IDataset dataset = pFeatureClass as IDataset;
IWorkspace workspace = dataset.Workspace;
IWorkspaceEdit pWorkspaceEdit = workspace as IWorkspaceEdit;
pWorkspaceEdit.StartEditing(false);
pWorkspaceEdit.StartEditOperation();
int LastPartCount = PartCount;
int DifferenceVale = (int)pFeatureClass.GetFeature(featureID).get_Value(5) - 1 - PartCount;
if (mGeoColl.GeometryCount >= 2)
{
int part = (int)pFeatureClass.GetFeature(featureID).get_Value(7);
for (int i = 0; i < Count; i++)
{
int PartValue = (int)pFeatureClass.GetFeature(i).get_Value(7);
if (PartValue == part)
{
PartCount++;
}
}
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "Part =" + "" + part + "";
IFeatureCursor pFeatureCursor = pFeatureClass.Search(queryFilter, false);
IFeature Feature = pFeatureCursor.NextFeature();
while (Feature != null)
{
int RankValue = (int)Feature.get_Value(5);
if (RankValue > DifferenceVale + LastPartCount)
{
int Pointrank = RankValue - DifferenceVale;
Feature.set_Value(5, Pointrank);
Feature.set_Value(4, "J" + Pointrank);
Feature.Store();
}
else
{
int Pointrank = RankValue - DifferenceVale + PartCount - LastPartCount;
Feature.set_Value(5, Pointrank);
Feature.set_Value(4, "J" + Pointrank);
Feature.Store();
}
Feature = pFeatureCursor.NextFeature();
}
}
else
{
for (int i = 0; i < Count; i++)
{
int RankValue = (int)pFeatureClass.GetFeature(i).get_Value(5);
if (RankValue > DifferenceVale)
{
int Pointrank = RankValue - DifferenceVale;
pFeatureClass.GetFeature(i).set_Value(5, Pointrank);
pFeatureClass.GetFeature(i).set_Value(4, "J" + Pointrank);
pFeatureClass.GetFeature(i).Store();
}
else
{
int Pointrank = RankValue - DifferenceVale + Count;
pFeatureClass.GetFeature(i).set_Value(5, Pointrank);
pFeatureClass.GetFeature(i).set_Value(4, "J" + Pointrank);
pFeatureClass.GetFeature(i).Store();
}
}
}
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);
MapControl.Map.ClearSelection();
MapControl.ActiveView.Refresh();
if (mGeoColl.GeometryCount >= 2 && Count > PartCount)
{
AutoNumber(PartCount);
}
else
{
MessageBox.Show("编号完成!", "提示", MessageBoxButtons.OK);
operation = "";
}
}
private static IFeatureClass PointRank(IFeatureClass pFeatureClass, IFeatureClass mFeatureClass)
{
for (int k = 0; k < mFeatureClass.FeatureCount(null); k++ )
{
IFeature mFeature = mFeatureClass.GetFeature(k);
IPointCollection mPointCollection = mFeature.Shape as IPointCollection;
List<double> DistanceList = new List<double>();
IPoint outPoint = new PointClass();
IPolyline pPolyline = new PolylineClass();
IGeometry pGeometry = pPolyline as IGeometry;
IPointCollection pPointCollection = pGeometry as IPointCollection;
pPointCollection.AddPointCollection(mPointCollection);
double distanceAlongCurve = 0;
for (int i = 0; i < pFeatureClass.FeatureCount(null); i++)
{
IFeature Feature = pFeatureClass.GetFeature(i);
IPoint pPoint = Feature.Shape as IPoint;
pPolyline.QueryPointAndDistance(esriSegmentExtension.esriNoExtension, pPoint, false, outPoint, ref distanceAlongCurve, 0, false);
double Distance = Math.Round(distanceAlongCurve, 0, MidpointRounding.AwayFromZero);
Feature.set_Value(6, Distance);
Feature.Store();
DistanceList.Add(Distance);
}
DistanceList.Sort();
for (int i = 0; i < DistanceList.Count; i++)
{
double Distance = DistanceList[i];
for (int l = 0; l < pFeatureClass.FeatureCount(null); l++)
{
IFeature Feature = pFeatureClass.GetFeature(l);
if (Distance == (double)Feature.get_Value(6))
{
IPoint point = Feature.Shape as IPoint;
Feature.set_Value(5, i + 1);
Feature.set_Value(3, point.Y);
Feature.set_Value(2, point.X);
Feature.Store();
break;
}
}
}
}
return pFeatureClass;
}