AnsweredAssumed Answered

Pro SDK Add-In Drawing Polylines to Invisible Features

Question asked by garydlester on Oct 15, 2018

I have a Pro Add-In that draws ploylines, I also have a tool that defines the set that the tool will draw through. The second tool does this by setting the definition query on the necessary layers. The problem is that after the layers are defined the tool still draws to features that should be queried out.

 

I have these exact same tool with almost identical logic written in python add-ins for ArcGIS Desktop and they work fine. I also have a Pro Add-in that implements a python tool to draw polyline and it respects the layer definition. So my question is why would this Pro SDK tool draw to features that are not on the map?

 

Here is the code to define the feature layers.

internal class Define : Button
    {
        protected async override void OnClick()
        {
            await SelectFeaturesAsync();
        }
        public Task SelectFeaturesAsync()
        {
            QueryFilter queryFilter = new QueryFilter();
            string query = null;
            var mapView = MapView.Active;
            return QueuedTask.Run(() =>
            {
                //Get the active map view.


                List<string> featureList = new List<string>();
                featureList.Add("WELD");
                featureList.Add("RED");
                featureList.Add("VLV");
                featureList.Add("FLN");
                featureList.Add("TEE");
                featureList.Add("BEND");

                var listLayers = mapView.Map.Layers.OfType<FeatureLayer>();
                foreach (var lyr in listLayers.ToList())
                {
                    if (featureList.Contains(lyr.Name, StringComparer.OrdinalIgnoreCase))
                    {
                        var selectionInMap = lyr.GetSelection();

                        if (selectionInMap.GetCount() > 0)
                        {
                            var oid = selectionInMap.GetObjectIDs().First();
                            query = $"OBJECTID={oid}";
                            foreach (var select in selectionInMap.GetObjectIDs())
                            {
                                query += $" OR OBJECTID={select.ToString()}";

                            }
                        }
                        else query = "OBJECTID IS NULL";
                        lyr.SetDefinitionQuery(query);
                    }
                }
            });
        }
    }

 

 

 

Here is the code to draw.

 

 

internal class Draw : Button
    {
        protected async override void OnClick()
        {
            await DrawPipeline();
           
        }
        public Task DrawPipeline()
        {
            return QueuedTask.Run(() =>
            {
                Dictionary<string, List<string>> initFields = new Dictionary<string, List<string>>();
                initFields["RED"] = new List<string>() { "OUTLET_DIAMETER", "OBJECTID" };
                initFields["TEE"] = new List<string>() { "INLET_OUTLET_DIAMETER", "OBJECTID" };
                initFields["BEND"] = new List<string>() { "DIAMETER", "OBJECTID" };
                initFields["FLN"] = new List<string>() { "DIAMETER", "OBJECTID" };
                initFields["WELD"] = new List<string>() { "DIAMETER", "OBJECTID" };
                initFields["VLV"] = new List<string>() { "SIZE", "OBJECTID" };
                Dictionary<string, List<string>> drawFields = new Dictionary<string, List<string>>();
                drawFields["RED"] = new List<string>() { "OUTLET_DIAMETER", "OBJECTID", "SHAPE@" };
                drawFields["
TEE"] = new List<string>() { "INLET_OUTLET_DIAMETER", "OBJECTID", "SHAPE@" };
                drawFields["
BEND"] = new List<string>() { "DIAMETER", "OBJECTID", "SHAPE@" };
                drawFields["
FLN"] = new List<string>() { "DIAMETER", "OBJECTID", "SHAPE@" };
                drawFields["
WELD"] = new List<string>() { "DIAMETER", "OBJECTID", "SHAPE@" };
                drawFields["
VLV"] = new List<string>() { "SIZE", "OBJECTID", "SHAPE@" };
                List<string> featureList = new List<string>();
                featureList.Add("
WELD");
                featureList.Add("RED");
                featureList.Add("VLV");
                featureList.Add("FLN");
                featureList.Add("TEE");
                featureList.Add("BEND");
                Dictionary<string, int> initOid = new Dictionary<string, int>();
                initOid["RED"] = 10000;
                initOid["TEE"] = 20000;
                initOid["BEND"] = 30000;
                initOid["FLN"] = 40000;
                initOid["WELD"] = 50000;
                initOid["VLV"] = 60000;
                Dictionary<int,MapPoint> pointDict = new Dictionary<int, MapPoint>();
                Dictionary<int, double> distDict = new Dictionary<int, double>();
                long startOid = 0;
                string startDia = null;
                var mapView = MapView.Active;
                var listLayers = mapView.Map.Layers.OfType<FeatureLayer>();
                foreach(var lyr in listLayers.ToList().Where(x => x.GetSelection().GetCount()>0))
                {
                    var layerSelected = lyr.GetSelection();
                    startOid = layerSelected.GetObjectIDs().First();
                    FeatureClass featureClass = lyr.GetFeatureClass();
                    using (Geodatabase gdb = featureClass.GetDatastore() as Geodatabase)
                    {
                        QueryDef queryDef = new QueryDef
                        {
                            Tables = lyr.Name,
                            WhereClause = $"OBJECTID={startOid}"
                        };
                        using (RowCursor rowCursor = gdb.Evaluate(queryDef, true))
                        {
                            while (rowCursor.MoveNext())
                            {
                                using (Row row = rowCursor.Current)
                                {
                                    Feature feature = row as Feature;
                                    ArcGIS.Core.Geometry.Geometry shape = feature.GetShape();
                                    MapPoint mapPoint = shape as MapPoint;
                                    startDia = Convert.ToString(row[initFields[lyr.Name][0]]);

                                    startOid = startOid + initOid[lyr.Name];
                                    pointDict[Convert.ToInt32(startOid)] = shape as MapPoint;

                                }
                            }
                        }
                    }
                    lyr.ClearSelection();
                }
                foreach (var lyr in listLayers.ToList())
                {
                    if (featureList.Contains(lyr.Name, StringComparer.OrdinalIgnoreCase))
                    {

                        if (lyr.Name == "VLV")
                        {
                            FeatureClass featureClass2 = lyr.GetFeatureClass();
                            using (Geodatabase gdb = featureClass2.GetDatastore() as Geodatabase)
                            {
                               
                                QueryDef queryDef = new QueryDef
                                {
                                    Tables=lyr.Name,
                                    WhereClause=$"{initFields[lyr.Name][0]}='{startDia.Replace("\"","")+"\""}'"
                                };
                                using(RowCursor rowCursor = gdb.Evaluate(queryDef, true))
                                {
                                    while (rowCursor.MoveNext())
                                    {
                                        using(Row row = rowCursor.Current)
                                        {
                                            Feature feature = row as Feature;
                                            ArcGIS.Core.Geometry.Geometry shape = feature.GetShape();
                                            MapPoint mapPoint = shape as MapPoint;
                                            var oid = feature["OBJECTID"];
                                            pointDict[Convert.ToInt32(oid)] = mapPoint;

                                        }
                                    }
                                }
                            }

                        }
                        else
                        {
                            FeatureClass featureClass3 = lyr.GetFeatureClass();
                            using(Geodatabase gdb = featureClass3.GetDatastore() as Geodatabase)
                            {
                                QueryDef queryDef = new QueryDef
                                {
                                    Tables = lyr.Name,
                                    WhereClause = $"{initFields[lyr.Name][0]}='{startDia.Replace("\"", "")}'"
                                };
                               
                                using (RowCursor rowCursor = gdb.Evaluate(queryDef, true))
                                {
                                    while (rowCursor.MoveNext())
                                    {
                                        using (Row row = rowCursor.Current)
                                        {
                                            Feature feature = row as Feature;
                                            Geometry shape = feature.GetShape();
                                            MapPoint mapPoint = shape as MapPoint;
                                            var oid = feature["OBJECTID"];
                                            pointDict[Convert.ToInt32(oid)] = mapPoint;

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                List<MapPoint> list = new List<MapPoint>();
                list.Add(pointDict[Convert.ToInt32(startOid)]);
                while (pointDict.Count()>1)
                {
                    var shape = pointDict[Convert.ToInt32(startOid)];
                    pointDict.Remove(Convert.ToInt32(startOid));
                    var X = shape.X;
                    var Y = shape.Y;
                    var Z = shape.Z;
                    foreach(KeyValuePair<int,MapPoint> point in pointDict)
                    {
                        var X2 = point.Value.X;
                        var Y2 = point.Value.Y;
                        var Z2 = point.Value.Z;
                        var squaredZDist = Math.Pow((Z - Z2), 2);
                        var squaredDist = Math.Pow(Math.Sqrt(Math.Pow(X - X2, 2) + Math.Pow(Y - Y2, 2)),2);
                        var threeDSquaredDist = Math.Sqrt(squaredDist + squaredZDist);
                        distDict[point.Key] = threeDSquaredDist;
                    }
                    var minValue = distDict.Min(x => x.Value);
                    var minPair = distDict
                                    .Where(x => x.Value == minValue)
                                    .First();
                    startOid = minPair.Key;
                    list.Add(pointDict[minPair.Key]);
                   
                    distDict.Clear();
                }
                Polyline polyline = PolylineBuilder.CreatePolyline(list);
                var pipeLayer = mapView.Map.Layers.OfType<FeatureLayer>()
                                                    .Where(x=>x.Name == "PIPELINE")
                                                    .First();
                var createFeatures = new EditOperation();
                createFeatures.Name = "Create Polyline";
                createFeatures.Create(pipeLayer, polyline);
                createFeatures.Execute();
            });
           
        }
    }

Outcomes