Вы находитесь на странице: 1из 5

Remove Duplicates Spatial Objects

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;

namespace RemoveDuplicates
{
/// <summary>
/// Command that works in ArcMap/Map/PageLayout
/// </summary>
[Guid("255d7b9a-c53b-4d04-b2ed-653c2f7896cb")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("RemoveDuplicates.DeleteDuplicates")]
public sealed class DeleteDuplicates : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);

//
// TODO: Add any COM registration code here
//
}

[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);

//
// TODO: Add any COM unregistration code here
//
}

#region ArcGIS Component Category Registrar generated code


/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code
editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type
registerType)
{
string regKey =
string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Register(regKey);
ControlsCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category
unregistration -
/// Do not modify the contents of this method with the code
editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type
registerType)
{
string regKey =
string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Unregister(regKey);
ControlsCommands.Unregister(regKey);
}

#endregion
#endregion

private IApplication pApp = null;

private IHookHelper m_hookHelper = null;


public DeleteDuplicates()
{
//
// TODO: Define values for the public properties
//
base.m_category = "GIS"; //localizable text
base.m_caption = "RemoveDuplicates"; //localizable text
base.m_message = "This should work in
ArcMap/MapControl/PageLayoutControl"; //localizable text
base.m_toolTip = "Removes Duplicate Spatial Objects";
//localizable text
base.m_name = "DeleteDuplicates"; //unique id, non-
localizable (e.g. "MyCategory_MyCommand")

try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(),
bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid
Bitmap");
}
}

#region Overriden Class Methods

/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;

pApp = hook as IApplication;

if (pApp == null)
return;

// TODO: Add other initialization code


}

/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add DeleteDuplicates.OnClick implementation

IMxDocument pMxDoc =(IMxDocument) pApp.Document;

int flag = 0;

IRelationalOperator pRop;

IFeatureLayer pFeatLayer;

IFeatureClass pFeatClass;

IFeatureCursor pFeatCursor;

IFeatureCursor pFeatCursor1;

IFeature pFeature;

IFeature pFeature1;

int j = 0;

pFeatLayer =
(IFeatureLayer)pMxDoc.FocusMap.get_Layer(0);

pFeatClass = pFeatLayer.FeatureClass;
j =pFeatClass.FeatureCount(null);

pFeatCursor = pFeatClass.Update(null, false);

pFeature = pFeatCursor.NextFeature();

while (pFeature!=null)
{

pRop = (IRelationalOperator)pFeature.Shape;

pFeatCursor1 =
(IFeatureCursor)pFeatClass.Update(null, false);

pFeature1 = pFeatCursor1.NextFeature();

while(pFeature1!=null)
{

if (pRop.Equals(pFeature1.Shape))
{
if(pFeature.OID != pFeature1.OID)
{
flag = 1;
pFeature.Delete();
//------- Comment it if There are more than
two duplicated shapes
break; // TODO: might not be correct.
Was : Exit Do
}
}

pFeature1 = pFeatCursor1.NextFeature();
}

pFeature = pFeatCursor.NextFeature();

if (flag == 1)
{
MessageBox.Show("There were " + (j -
pFeatClass.FeatureCount(null)) + " Features duplicated / " + j + "
Features in this Layer ");
}
else{
MessageBox.Show( "This Layer is out of any
duplicated Features");
}

pMxDoc.ActivatedView.Refresh();

#endregion
}
}

Вам также может понравиться