Sunday 26 May 2019

arcgis 10.0 - arcobject.net Addin Toolbar only works once


I created a toolbar using ArcObjects 10.0 which does some heavy processing in the onClick event of the only button it includes. It works fine however it works only once. If I click again on the button nothing happens. To run it again I need to restart ArcMap and run it again. I cannot understand this behavior?


i am working for Electrical Data set, which is having a tree like network, and network should be downstream, there are some lines which lies opposite direction in it. need to identify those lines. below is the example.


enter image description here



below is the process flow i am using in my tool.


enter image description here


Below is the Code linked to click event of the button:


public WrongDirection()
{
//Setting Status Bar
IStatusBar pstatusbar = ArcMap.Application.StatusBar;
pstatusbar.ShowProgressBar("Running wrong Direction..", 0, 17, 1, true);



try
{

//Define IMxdocument and Imap
IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
IMap map = mxdoc.FocusMap;
if (map.Layer[0] == null)
{ return; }




// Setting GXDialog for Output
IGxDialog pGXDialog = new GxDialogClass();
pGXDialog.AllowMultiSelect = false;
if (pGXDialog.DoModalSave(0) != true)
{
pstatusbar.HideProgressBar();
return;
}


pGXDialog.ButtonCaption = "Save";
pGXDialog.Title = "Save Wrong Direction Points Feature";
pGXDialog.RememberLocation = true;
IGxObjectFilter gxobjFilter = new GxFilterShapefilesClass();
pGXDialog.ObjectFilter = gxobjFilter;
pGXDialog.DoModalSave(0);
IGxObject pGxObject = pGXDialog.FinalLocation;
String s, Fp;
s = pGXDialog.Name;
Fp = pGxObject.FullName + "\\" + s;

pstatusbar.StepProgressBar();




// Merging HT Overhead Line and HT Underground Line
List STRHT = new List();
for (int i = 0; i < map.LayerCount; i++)
{
if (map.Layer[i].Name == "HT Overhead Line")

{
ILayer HTOverheadLine = map.Layer[i];
IFeatureLayer fl1 = new FeatureLayerClass();
fl1 = HTOverheadLine as IFeatureLayer;
IDataset dataset = (ESRI.ArcGIS.Geodatabase.IDataset)(HTOverheadLine);
STRHT.Add(dataset.Workspace.PathName + "\\" + ((IDataset)fl1.FeatureClass).BrowseName);
}
else
{
if (map.Layer[i].Name == "HT Underground Line")

{
ILayer HTUndergroundLine = map.Layer[i];
IFeatureLayer fl2 = new FeatureLayerClass();
fl2 = HTUndergroundLine as IFeatureLayer;
IDataset dataset = (ESRI.ArcGIS.Geodatabase.IDataset)(HTUndergroundLine);
STRHT.Add(dataset.Workspace.PathName + "\\" + ((IDataset)fl2.FeatureClass).BrowseName);
}
}
}
string combindedStringHT = string.Join(";", STRHT.ToArray());

IGeoProcessor gp = new GeoProcessorClass();
gp.AddOutputsToMap = true;
gp.OverwriteOutput = true;
IVariantArray parameters = new VarArrayClass();
parameters.Add(combindedStringHT);
parameters.Add(@"D:\MergeHT.shp");
gp.Execute("Merge_management", parameters, null);
pstatusbar.StepProgressBar();



// Deleting extra field from mergeHT
IGPUtilities2 gpUtils = new GPUtilitiesClass();
IFeatureClass featureClass = gpUtils.OpenFeatureClassFromString(@"D:\MergeHT.shp");
IFields2 fields = featureClass.Fields as IFields2;
IField2 nameField = null;
for (int fieldIndex = fields.FieldCount - 1; fieldIndex > -1; fieldIndex = fieldIndex - 1)
{
nameField = fields.Field[fieldIndex] as IField2;
if (nameField.Name != "FID" && nameField.Name != "Shape" && nameField.Name != "OBJECTID" && nameField.Name != "SUBTYPECD")
{

featureClass.DeleteField(nameField);
}
}
pstatusbar.StepProgressBar();



//Merging LT Overhead and LT underground into one
List STRLT = new List();
for (int i = 0; i < map.LayerCount; i++)

{
if (map.Layer[i].Name == "LT Overhead Line")
{
ILayer LTOverheadLine = map.Layer[i];
IFeatureLayer fl3 = new FeatureLayerClass();
fl3 = LTOverheadLine as IFeatureLayer;
IDataset dataset = (ESRI.ArcGIS.Geodatabase.IDataset)(LTOverheadLine);
STRLT.Add(dataset.Workspace.PathName + "\\" + ((IDataset)fl3.FeatureClass).BrowseName);
}
else

{
if (map.Layer[i].Name == "LT Underground Line")
{
ILayer LTUndergroundLine = map.Layer[i];
IFeatureLayer fl4 = new FeatureLayerClass();
fl4 = LTUndergroundLine as IFeatureLayer;
IDataset dataset = (ESRI.ArcGIS.Geodatabase.IDataset)(LTUndergroundLine);
STRLT.Add(dataset.Workspace.PathName + "\\" + ((IDataset)fl4.FeatureClass).BrowseName);
}
}

}
string combindedStringLT = string.Join(";", STRLT.ToArray());
IGeoProcessor gp7 = new GeoProcessorClass();
gp7.AddOutputsToMap = true;
gp7.OverwriteOutput = true;
IVariantArray parameters7 = new VarArrayClass();
parameters7.Add(combindedStringLT);
parameters7.Add(@"D:\MergeLT.shp");
gp7.Execute("Merge_management", parameters7, null);
pstatusbar.StepProgressBar();


// Deleting extra field from mergeLT
IGPUtilities2 gpUtilsLT = new GPUtilitiesClass();
IFeatureClass featureClassLT = gpUtilsLT.OpenFeatureClassFromString(@"D:\mergeLT.shp");
IFields2 fieldsLT = featureClassLT.Fields as IFields2;
IField2 nameFieldLT = null;
for (int fieldIndex = fieldsLT.FieldCount - 1; fieldIndex > -1; fieldIndex = fieldIndex - 1)
{
nameFieldLT = fieldsLT.Field[fieldIndex] as IField2;
if (nameFieldLT.Name != "FID" && nameFieldLT.Name != "Shape" && nameFieldLT.Name != "OBJECTID" && nameFieldLT.Name != "SUBTYPECD")

{
featureClassLT.DeleteField(nameFieldLT);
}
}
pstatusbar.StepProgressBar();


//Merging LT and HT into single Merge
IGeoProcessor gp10 = new GeoProcessorClass();
gp10.OverwriteOutput = true;

IVariantArray parameters10 = new VarArrayClass();
string STRM = "D:\\MergeHT.shp;D:\\MergeLT.shp";
parameters10.Add(STRM);
parameters10.Add(@"in_memory\Merge");
gp10.Execute("Merge_management", parameters10, null);
pstatusbar.StepProgressBar();


// running Feature to Vertices GP tool
IGeoProcessor gp2 = new GeoProcessorClass();

gp2.AddOutputsToMap = true;
gp2.OverwriteOutput = true;
IVariantArray parameters2 = new VarArrayClass();
parameters2.Add(@"in_memory\Merge");
parameters2.Add(@"in_memory\Vertices");
parameters2.Add("END");
gp2.Execute("FeatureVerticesToPoints_management", parameters2, null);
pstatusbar.StepProgressBar();



// running Feature to CollectEvents GP tool
IGeoProcessor gp3 = new GeoProcessorClass();
gp3.AddOutputsToMap = true;
gp3.OverwriteOutput = true;
IVariantArray parameters3 = new VarArrayClass();
parameters3.Add(@"in_memory\Vertices");
parameters3.Add(Fp);
gp3.Execute("CollectEvents_stats", parameters3, null);
pstatusbar.StepProgressBar();


// Deleting Records which are not duplicates by using Featurecursor.
IGPUtilities2 gpUtilsWW = new GPUtilitiesClass();
IFeatureClass featureClassww = gpUtilsLT.OpenFeatureClassFromString(Fp);
IQueryFilter qF = new QueryFilterClass();
qF.WhereClause = "ICOUNT = 1";
IFeatureCursor featureCursor = featureClassww.Search(qF, false);
IFeature feature = null;
while ((feature = featureCursor.NextFeature()) != null)
{
feature.Delete();

}
pstatusbar.StepProgressBar();


// deleting MeregeHT Shape
IGeoProcessor gp5 = new GeoProcessorClass();
gp5.AddOutputsToMap = true;
gp5.OverwriteOutput = true;
IVariantArray parameters5 = new VarArrayClass();
parameters5.Add(@"D:\MergeHT.shp");

gp5.Execute("Delete_management", parameters5, null);
pstatusbar.StepProgressBar();


// deleting MeregeLT Shape
IGeoProcessor gp8 = new GeoProcessorClass();
gp8.AddOutputsToMap = true;
gp8.OverwriteOutput = true;
IVariantArray parameters8 = new VarArrayClass();
parameters8.Add(@"D:\MergeLT.shp");

gp5.Execute("Delete_management", parameters8, null);
pstatusbar.StepProgressBar();

// deleting MeregeLT Shape
IGeoProcessor gp9 = new GeoProcessorClass();
gp9.AddOutputsToMap = true;
gp9.OverwriteOutput = true;
IVariantArray parameters9 = new VarArrayClass();
parameters9.Add(@"in_memory\Merge");
gp9.Execute("Delete_management", parameters9, null);

pstatusbar.StepProgressBar();

// deleting Vertices Shape
IGeoProcessor gp6 = new GeoProcessorClass();
gp6.AddOutputsToMap = true;
gp6.OverwriteOutput = true;
IVariantArray parameters6 = new VarArrayClass();
parameters6.Add(@"in_memory\Vertices");
gp6.Execute("Delete_management", parameters6, null);
pstatusbar.StepProgressBar();


// Removing Layer from TOC
for (int i = map.LayerCount - 1; i > -1; i--)
{
if (map.Layer[i].Valid)
{
}
else
{
ILayer DeleteLayer = map.Layer[i];

map.DeleteLayer(DeleteLayer);
}
}
pstatusbar.StepProgressBar();

// Refreshing Active View
mxdoc.ActiveView.Refresh();
pstatusbar.StepProgressBar();
pstatusbar.HideProgressBar();


}
catch (Exception ex)
{
pstatusbar.HideProgressBar();
MessageBox.Show("Error: " + ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...