How can I debug ArcMap Add-In?

529
2
11-08-2012 10:13 AM
InsuHong1
New Contributor
Hello,

I implement following code to make add-in for ArcMap.

After user inputs from 'newform' window, geoprocessors do their job.

But when I run this code, ArcMap just crashed without any message or report.

I tried to debug it, but it's just same. Crashed. I checked debug setting, it was fine, exactly same as ESRI's instruction on web resource.

I will add more code after this, but crashing without any report like this, I cannot do programing?


It would be helpful if I know

1) why does current code make crash?

2) how can I debug add-in and find what is a problem, not just crash without any clue?


Thanks




namespace Convexpath_mk2
{
    public class runConvexPath : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        IGeoProcessor2 gp;
        IGeoProcessorResult result;
        IVariantArray parameters;
        struct pair
        {
            //definition of pair data type
        }
        struct coords
        {
            //definition of coords data type
        }

        public runConvexPath()
        {
            gp = new GeoProcessorClass();
            parameters = new VarArrayClass();
        }
        protected override void OnClick()
        {
            
            ConvexPathForm newForm = new ConvexPathForm();
            newForm.Text = "Test";
            newForm.ShowDialog();
            Dictionary<string, string> fcDict = new Dictionary<string, string>();
            fcDict = newForm.caDi();
            string path = fcDict["path"];
            gp.SetEnvironmentValue("workspace", path);
            gp.OverwriteOutput = true;
            gp.AddOutputsToMap = false;
            List<string> oriList = new List<string>();
            List<string> desList = new List<string>();
            Dictionary<pair, pairData> pairDict = new Dictionary<pair, pairData>();
            List<pair> pairList = new List<pair>();
            Dictionary<string, string> belongDict = new Dictionary<string,string>();;

            FCUtils fcUtils = new FCUtils();
            string idxOrigin = fcDict["idxOrigin"];
            string idxDestine = fcDict["idxDestine"];
            //Making Map boundary (polyline)

            string strBoundaryline = "boundaryline";
            parameters = new VarArrayClass();
            parameters.Add(fcDict["fcBoundary"]);
            parameters.Add(strBoundaryline);
            gp.Execute("FeatureToLine_management", parameters, null);
            
            
            //Making Barrier boundary (polyline)
            
            string strBarrierline = "barrierline";
            parameters = new VarArrayClass();
            parameters.Add(fcDict["fcInsideBarriers"]);
            parameters.Add(strBarrierline);
            gp.Execute("FeatureToLine_management", parameters, null);
            
            
            //Making boundary vertices (point) 
            
            string strBoundaryVertices = "boundaryVertices";
            parameters = new VarArrayClass();
            parameters.Add(fcDict["fcBoundary"]);
            parameters.Add(strBoundaryVertices);
            gp.Execute("FeatureVerticesToPoints_management", parameters, null);

            //Making fcMerged 

            string strMerged = "merged";
            parameters = new VarArrayClass();
            List <string> merged = new List<string>();
            merged.Add(fcDict["fcOrigin"]);
            merged.Add(fcDict["fcDestine"]);
            parameters.Add(merged);
            parameters.Add(strMerged);
            gp.Execute("Merge_management", parameters, null);


            //Making fcFillBoundary 
            string strFillBoundary = "FillBoundary";
            parameters = new VarArrayClass();
            parameters.Add(fcDict["fcBoundary"]);
            parameters.Add(strFillBoundary);
            gp.Execute("EliminatePolygonPart_management", parameters, null);

            //Making index field for boundary vertices 

            try
            {

                parameters = new VarArrayClass();
                parameters.Add(strBoundaryVertices);
                parameters.Add("id_f");
                parameters.Add("SHORT");
                gp.Execute("AddField_management", parameters, null);
            }
            catch (COMException comExc)
            {
                string errMsg = comExc.Message;
            }
            parameters = new VarArrayClass();
            parameters.Add(strBoundaryVertices);
            parameters.Add("id_f");
            parameters.Add("[OBJECTID]");
            parameters.Add("VB");
            gp.Execute("CalculateField_management", parameters, null);

            IGPUtilities2 gpUtils = new GPUtilitiesClass();
            gpUtils.Workspace = path;

            IFeatureClass fcOrigin = gpUtils.OpenFeatureClassFromString(fcDict["fcOrigin"]);
            IFeatureClass fcDestine = gpUtils.OpenFeatureClassFromString(fcDict["fcDestine"]);
            IFeatureClass fcBoundary = gpUtils.OpenFeatureClassFromString(fcDict["fcBoundary"]);
            IFeatureClass fcBarrier = gpUtils.OpenFeatureClassFromString(fcDict["fcBarrier"]);
            IFeatureClass fcBoundaryLine = gpUtils.OpenFeatureClassFromString(strBoundaryline);
            IFeatureClass fcBarrierLine = gpUtils.OpenFeatureClassFromString(strBarrierline);
            IFeatureClass fcBoundaryVertices = gpUtils.OpenFeatureClassFromString(strBoundaryVertices);
            IFeatureClass fcMerged = gpUtils.OpenFeatureClassFromString(strMerged);
            IFeatureCursor curBoundaryVertices;
            IFeature ifBoundaryVertices = null;
            IFeatureCursor oriCursor;
            IFeatureCursor desCursor;
            string oid;
            IFeature feature = null;
            oriCursor = fcOrigin.Search(null, false);
            int idFcOrigin_id_f = fcOrigin.Fields.FindField(idxOrigin);
            try
            {
                while ((feature = oriCursor.NextFeature()) != null)
                {
                    oid = (string)feature.get_Value(idFcOrigin_id_f);
                    oriList.Add(oid);
                    belongDict[oid] = "orgin";
                }
                Marshal.ReleaseComObject(oriCursor);
            }
            catch (COMException comExc)
            {
                string errMsg = comExc.Message;
            }
            
            desCursor = fcDestine.Search(null, false);
            try
            {
                while ((feature = desCursor.NextFeature()) != null)
                {
                    oid = (string)feature.get_Value((feature.Fields.FindField(idxDestine)));
                    desList.Add(oid);
                    belongDict[oid] = "destine";
                }
                Marshal.ReleaseComObject(desCursor);
            }
            catch (COMException comExc)
            {
                string errMsg = comExc.Message;
            }

            foreach (string i in oriList)
            {
                foreach (string j in desList)
                {
                    pair pKey = new pair(i, j);
                    pairList.Add(pKey);
                }
            }
            List<string> lines = new List<string>();
            int total_edges = 0;
            long start_clip = DateTime.Now.Ticks;
            foreach (pair i in pairList)
            {
                double euDistance = 0;
                IGeometry geo = new MultipointClass();
                IPointCollection5 pcTempPair = geo as IPointCollection5;
                ISpatialFilter sFilter1 = new SpatialFilterClass();
                sFilter1.GeometryField = fcMerged.ShapeFieldName;
                sFilter1.WhereClause = "id_f = '" + i.getOri + "' OR id_f = '" + i.getDes + "'";
                IFeatureCursor curMerged3 = fcMerged.Search(sFilter1, false);
                IFeature ifMerged = null;
                while ((ifMerged = curMerged3.NextFeature()) != null)
                {
                    IPoint ptIn = new PointClass();
                    ptIn = (IPoint)ifMerged.ShapeCopy;
                    pcTempPair.AddPoint(ptIn);
                }
            }

        }
        
        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }
         


    }
}

        

0 Kudos
2 Replies
RichWawrzonek
Occasional Contributor
Not sure where to begin. You have a lot to learn about writing good code. In general it is poor design to put a large script inside the onclick event of a button.
You make use of some try blocks which is good but maybe just wrap the whole thing in a try block and report the error in a message box so you know what the problem is.
It doesn't look like runConvexPath() is ever called. Therefore you have never created the geoprocessor object before you start using it in your onclick event. Don't instantiate these objects in runConvexPath(), just create them where you need them.
0 Kudos
InsuHong1
New Contributor
Not sure where to begin. You have a lot to learn about writing good code. In general it is poor design to put a large script inside the onclick event of a button.
You make use of some try blocks which is good but maybe just wrap the whole thing in a try block and report the error in a message box so you know what the problem is.
It doesn't look like runConvexPath() is ever called. Therefore you have never created the geoprocessor object before you start using it in your onclick event. Don't instantiate these objects in runConvexPath(), just create them where you need them.



Hi. Thank you for comment.

Originally, I wrote code as separated classes. But it crashed. When I tried to same code in current shape, strangely it worked. I don't know why. It supposed to be run well, because I implemented simple one geoprocessing, and that code just copy-paste from sample code! This is first time I ever use C# .net, so I don't know much. So I tried to follow examples, but it seems to me much of them not work well.

Now I can catch errors with try blocks (Thanks!). But I still don't get it why does it make error....

Original code was implemented in simple console application for my research. Now I want to implement it as Add-in, but my code does not work at all in Add-In project.
0 Kudos