Select to view content in your preferred language

How to convert PolygonZM to Polygon

1374
3
Jump to solution
07-07-2021 10:40 AM
Rconrad
Emerging Contributor

I am writing an add-in that calculates the cost for a spray job.

This next block of code works perfectly for adding a polygon feature to my objClasses list,

But only when the feature's shape is "Polygon", not "Polygon ZM" which is the default shape for the sprayer's control to export.

I would like to know is there a way to modify my code slightly to include a Polygon ZM feature, or will I have to edit the Map and order to disable the Z and M axis.

IMap focusMap = ArcMap.Document.FocusMap;
IEnumFeature enumFeature = focusMap.FeatureSelection as IEnumFeature;
enumFeature?.Reset();
if (enumFeature is IEnumFeatureSetup enumFeatSetup)
{
enumFeatSetup.AllFields = true;
}

double totalArea = 0;
int featCount = 0;

try
{
// List of Object Classes to run the Calculate Field tool on
List<IObjectClass> objClasses = new List<IObjectClass>();

// step through feature selection and add to Object Classes list if they're a polygon.
IFeature feature = enumFeature?.Next();
while (feature != null)
{
System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
currentFeatureClassAlias = feature.Class.AliasName; // keeping track of this for error messages.

// Check if a feature is a polygon and that its object class is not already added to the objClasses list.
// If no layer is specified, add the feature's object class to the objClasses list if the previous two conditions are met.
// If a layer has been specified, check that the feature is a member of that layer before adding its object class to the list.
if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon && !objClasses.Contains(feature.Class) && (feature.Class.AliasName == onLayer?.Name || onLayer == null))
{
objClasses.Add(feature.Class);
}
feature = enumFeature.Next();
}

// Break out if no compatible features are selected.
if (objClasses.Count < 1)
{
MessageBox.Show($"No polygon features selected from {onLayer?.Name} layer. Cannot calculate area.");
return;
}

Rconrad_0-1625678904350.png

Rconrad_2-1625679436843.png

I would greatly appreciate any help with this. Thanks 🙂

 

 

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

Your code works fine on ArcGIS 10.3 if omit part of condition checking:

                if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon && !objClasses.Contains(feature.Class))
                {
                    objClasses.Add(feature.Class);
                }

So issue is with layer name and not with geometry type

View solution in original post

0 Kudos
3 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Your code works fine on ArcGIS 10.3 if omit part of condition checking:

                if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon && !objClasses.Contains(feature.Class))
                {
                    objClasses.Add(feature.Class);
                }

So issue is with layer name and not with geometry type

0 Kudos
Rconrad
Emerging Contributor

That's exactly what I did and it worked just as needed. Don't know why I thought I needed to convert it. Thanks.

0 Kudos
DuncanHornby
MVP Notable Contributor

A side note when posting code in a question, please use the syntax highlighter, it makes your code much more readable. You might need to click on the ... button to expand the menu and see the </> button.

0 Kudos