<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Create topology with different feature classes both as single and multiple then run validate topology to find the errors using ArcObjects in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/create-topology-with-different-feature-classes/m-p/1702747#M20713</link>
    <description>&lt;P&gt;&lt;BR /&gt;I am writing code using ArcObjects 10.8.1 to find and delete the topology with a name and recreate a new topology with same name but with single feature class or different set of feature classes. However I am getting error code [-2147215017] error message [Exception from HRESULT: 0x80041957]. Here someone help with a clean code or fail safe code to implement the requirement.&lt;/P&gt;&lt;P&gt;Partial Code&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;public void RegisterClassToTopology(IFeatureDataset featureDataset, ITopology topology, IFeatureClass featureClass)&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;// 3. Verify the class belongs to the same Feature Dataset&lt;BR /&gt;if (featureClass.FeatureDataset.Name != featureDataset.Name)&lt;BR /&gt;{&lt;BR /&gt;throw new Exception("Feature Class must reside in the same Feature Dataset as the topology.");&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// 4. Add the Feature Class to the topology framework&lt;BR /&gt;// Parameters: (IFeatureClass, Weight, XYRank, ZRank)&lt;BR /&gt;if (!IsClassAlreadyInTopology(topology, featureClass))&lt;BR /&gt;{&lt;BR /&gt;topology.AddClass(featureClass, 5, 1, 1, true);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;System.Diagnostics.Debug.WriteLine("Feature class successfully added to topology via code.");&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;System.Diagnostics.Debug.WriteLine($"Failed to add class: {ex.Message}");&lt;BR /&gt;throw;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;public ITopologyRule AddTopologyRule(IFeatureDataset featureDataset, ITopology topology, IFeatureClass sfeatureClass, IFeatureClass cfeatureClass)&lt;BR /&gt;{&lt;BR /&gt;// 1. Cast to Topology Container&lt;BR /&gt;ISchemaLock schemaLock = (ISchemaLock)featureDataset;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// 2. Request an Exclusive Schema Lock to allow modifications&lt;BR /&gt;schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;//topology.AddClass(sourcefc, 5, 1, 1, false);&lt;BR /&gt;RegisterClassToTopology(featureDataset, topology, sfeatureClass);&lt;/P&gt;&lt;P&gt;if (cfeatureClass != null)&lt;BR /&gt;//topology.AddClass(connectedfc, 5, 1, 1, false);&lt;BR /&gt;RegisterClassToTopology(featureDataset, topology, cfeatureClass);&lt;/P&gt;&lt;P&gt;ITopologyRuleContainer ruleContainer = (ITopologyRuleContainer)topology;&lt;/P&gt;&lt;P&gt;esriTopologyRuleType ruletype = GetTopologyRuleType(sfeatureClass, cfeatureClass);&lt;/P&gt;&lt;P&gt;ITopologyRule rule = new TopologyRuleClass();&lt;/P&gt;&lt;P&gt;rule.Name = "Must Not Overlap";&lt;BR /&gt;rule.TopologyRuleType = ruletype;&lt;BR /&gt;rule.OriginClassID = sfeatureClass.FeatureClassID;&lt;/P&gt;&lt;P&gt;if (cfeatureClass != null)&lt;BR /&gt;{&lt;BR /&gt;rule.DestinationClassID = cfeatureClass.FeatureClassID;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// HANDLE SUBTYPES SAFELY&lt;BR /&gt;if (rule.OriginSubtypeSpecified)&lt;BR /&gt;{&lt;BR /&gt;ISubtypes subtypes = (ISubtypes)sfeatureClass;&lt;BR /&gt;rule.OriginSubtype = subtypes.HasSubtype ? subtypes.DefaultSubtypeCode : 0;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (cfeatureClass != null &amp;amp;&amp;amp; rule.DestinationSubtypeSpecified)&lt;BR /&gt;{&lt;BR /&gt;ISubtypes subtypes = (ISubtypes)cfeatureClass;&lt;BR /&gt;rule.DestinationSubtype = subtypes.HasSubtype ? subtypes.DefaultSubtypeCode : 0;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// CHECK EXISTING RULES PROPERLY&lt;BR /&gt;IEnumRule existingRules = ruleContainer.Rules;&lt;BR /&gt;existingRules.Reset();&lt;/P&gt;&lt;P&gt;IRule existingRule = existingRules.Next();&lt;/P&gt;&lt;P&gt;while (existingRule != null)&lt;BR /&gt;{&lt;BR /&gt;ITopologyRule thisrule = existingRule as ITopologyRule;&lt;BR /&gt;if (thisrule.TopologyRuleType == rule.TopologyRuleType &amp;amp;&amp;amp;&lt;BR /&gt;thisrule.OriginClassID == rule.OriginClassID &amp;amp;&amp;amp;&lt;BR /&gt;thisrule.DestinationClassID == rule.DestinationClassID)&lt;BR /&gt;{&lt;BR /&gt;Console.WriteLine("Rule already exists. Skipping.");&lt;BR /&gt;return thisrule;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;existingRule = existingRules.Next();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// ADD RULE ONLY ONCE&lt;BR /&gt;ruleContainer.AddRule(rule);&lt;/P&gt;&lt;P&gt;Console.WriteLine("Rule added successfully.");&lt;BR /&gt;return rule;&lt;BR /&gt;}&lt;BR /&gt;catch (System.Runtime.InteropServices.COMException comEx)&lt;BR /&gt;{&lt;BR /&gt;// Extract the specific HRESULT code&lt;BR /&gt;int errorCode = comEx.ErrorCode;&lt;BR /&gt;string errorMessage = comEx.Message;&lt;/P&gt;&lt;P&gt;Console.WriteLine($"Failed to add topology rule. Code: {errorCode}. Message: {errorMessage}");&lt;BR /&gt;throw;&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;System.Diagnostics.Debug.WriteLine($"Failed to add class: {ex.Message}");&lt;BR /&gt;throw;&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;// 5. CRITICAL: Always downgrade the lock back to Shared&lt;BR /&gt;schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;// The code has to handle single feature class and multiple feature class as well&lt;BR /&gt;public void CreateTopologyWithCrossorSameDatasetRules(IFeatureDataset featureDataset, string sourcefcname, string connectedfcname, ref StreamWriter sw)&lt;BR /&gt;{&lt;BR /&gt;// 1. Cast to topology container&lt;BR /&gt;ITopologyContainer2 topoContainer = (ITopologyContainer2)featureDataset;&lt;/P&gt;&lt;P&gt;DeleteTopologyIfExists(topoContainer, TEMP_TOPOLOGY_NAME);&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;// 2. Create topology&lt;BR /&gt;ITopology topology = topoContainer.CreateTopology(&lt;BR /&gt;TEMP_TOPOLOGY_NAME,&lt;BR /&gt;0.001, // Cluster tolerance&lt;BR /&gt;-1, // XY ranks default&lt;BR /&gt;""); // Config keyword&lt;/P&gt;&lt;P&gt;// 3. Open feature classes (must be in same feature dataset)&lt;BR /&gt;IFeatureWorkspace fws = (IFeatureWorkspace)featureDataset.Workspace;&lt;/P&gt;&lt;P&gt;IFeatureClass sourcefc = fws.OpenFeatureClass(sourcefcname);&lt;BR /&gt;IFeatureClass connectedfc = null;&lt;/P&gt;&lt;P&gt;if (!sourcefcname.Equals(connectedfcname))&lt;BR /&gt;{&lt;BR /&gt;connectedfc = fws.OpenFeatureClass(connectedfcname);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;ITopologyRule addedrule = AddTopologyRule(featureDataset, topology, sourcefc, connectedfc);&lt;/P&gt;&lt;P&gt;// 6. Validate topology&lt;BR /&gt;IEnvelope envelope = ((IGeoDataset)featureDataset).Extent;&lt;BR /&gt;ISpatialReference datasetspatialreference = ((IGeoDataset)featureDataset).SpatialReference;&lt;/P&gt;&lt;P&gt;IWorkspaceEdit wsEdit = (IWorkspaceEdit)featureDataset.Workspace;&lt;/P&gt;&lt;P&gt;wsEdit.StartEditing(false);&lt;BR /&gt;wsEdit.StartEditOperation();&lt;/P&gt;&lt;P&gt;topology.ValidateTopology(envelope);&lt;/P&gt;&lt;P&gt;wsEdit.StopEditOperation();&lt;BR /&gt;wsEdit.StopEditing(true);&lt;/P&gt;&lt;P&gt;wsEdit.AbortEditOperation();&lt;BR /&gt;wsEdit.StopEditing(false);&lt;/P&gt;&lt;P&gt;// Assuming you have an ITopology object named pTopology&lt;BR /&gt;IErrorFeatureContainer pErrorContainer = topology as IErrorFeatureContainer;&lt;/P&gt;&lt;P&gt;// Define an envelope to check for errors (or use the validated area)&lt;BR /&gt;//IEnvelope pEnvelope = topology.Extent;&lt;/P&gt;&lt;P&gt;// Retrieve errors within the area&lt;BR /&gt;IEnumTopologyErrorFeature pEnumTopoError = pErrorContainer.ErrorFeatures[&lt;BR /&gt;datasetspatialreference,&lt;BR /&gt;addedrule,&lt;BR /&gt;envelope,&lt;BR /&gt;true, // Include errors&lt;BR /&gt;true // Include exceptions&lt;BR /&gt;];&lt;/P&gt;&lt;P&gt;ITopologyErrorFeature pTopoError = pEnumTopoError.Next();&lt;/P&gt;&lt;P&gt;while (pTopoError != null)&lt;BR /&gt;{&lt;BR /&gt;// Access error details&lt;BR /&gt;int errorID = pTopoError.OriginOID;&lt;BR /&gt;int desterrorID = pTopoError.DestinationOID;&lt;/P&gt;&lt;P&gt;IFeature originfeature = sourcefc.GetFeature(errorID);&lt;BR /&gt;IFeature destfeature = connectedfc.GetFeature(desterrorID);&lt;/P&gt;&lt;P&gt;IGeometry ogeom = originfeature.Shape;&lt;BR /&gt;IGeometry dgeom = destfeature.Shape;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;ITopologicalOperator targetTopoOp = (ITopologicalOperator)ogeom;&lt;BR /&gt;IGeometry overlapGeometry = targetTopoOp.Intersect(dgeom, esriGeometryDimension.esriGeometry1Dimension);&lt;BR /&gt;double overlapLength = 0.0d;&lt;BR /&gt;double overlapPercent = 0.0f;&lt;/P&gt;&lt;P&gt;IPolyline overlapPolyline = (IPolyline)overlapGeometry;&lt;BR /&gt;double overlaplength = overlapPolyline.Length;&lt;BR /&gt;overlapLength = Math.Round(overlaplength, ROUND_OFF_VALUE);&lt;BR /&gt;ICurve lgeomcurve = ogeom as ICurve;&lt;BR /&gt;ICurve interCurve = overlapGeometry as ICurve;&lt;BR /&gt;// 4. Calculate Percentage&lt;BR /&gt;overlapPercent = Math.Round((interCurve.Length / lgeomcurve.Length) * 100, ROUND_OFF_VALUE);&lt;/P&gt;&lt;P&gt;esriTopologyRuleType ruleID = pTopoError.TopologyRuleType;&lt;BR /&gt;//IGeometry errorShape = pTopoError.Shape;&lt;/P&gt;&lt;P&gt;// Process error (e.g., log to file, highlight in map)&lt;BR /&gt;Console.WriteLine($"Error ID: {errorID}, Rule Type: {ruleID}");&lt;/P&gt;&lt;P&gt;dataGridViewOverlapping.Rows.Add(errorID, desterrorID, sourcefc.AliasName, connectedfc.AliasName, overlapLength, overlapPercent, "Overlapping");&lt;BR /&gt;string resultValue = errorID + "," + desterrorID + "," + sourcefc.AliasName + "," + connectedfc.AliasName + "," + overlapLength + "," + overlapPercent + "," + "Overlapping";&lt;BR /&gt;sw.WriteLine(resultValue);&lt;/P&gt;&lt;P&gt;pTopoError = pEnumTopoError.Next();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;//schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);&lt;BR /&gt;}&lt;BR /&gt;catch (System.Runtime.InteropServices.COMException comEx)&lt;BR /&gt;{&lt;BR /&gt;// Extract the specific HRESULT code&lt;BR /&gt;int errorCode = comEx.ErrorCode;&lt;BR /&gt;string errorMessage = comEx.Message;&lt;/P&gt;&lt;P&gt;Console.WriteLine($"Failed to add topology rule. Code: {errorCode}. Message: {errorMessage}");&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// This function finds the topology by name and deletes if it exists.&lt;BR /&gt;public void DeleteTopologyIfExists(ITopologyContainer topologyContainer, string topologyName)&lt;BR /&gt;{&lt;BR /&gt;if (topologyContainer != null)&lt;BR /&gt;{&lt;BR /&gt;ITopology topology = null;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// 3. Find and Delete Topology&lt;BR /&gt;topology = topologyContainer.get_TopologyByName(topologyName);&lt;BR /&gt;if (topology != null)&lt;BR /&gt;{&lt;BR /&gt;IDataset dataset = (IDataset)topology;&lt;BR /&gt;if (dataset.CanDelete())&lt;BR /&gt;{&lt;BR /&gt;dataset.Delete();&lt;BR /&gt;System.Diagnostics.Debug.WriteLine(topologyName + " deleted successfully.");&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;ITopology removedtopology = topologyContainer.TopologyByName[topologyName];&lt;BR /&gt;}&lt;BR /&gt;catch (System.Runtime.InteropServices.COMException ex)&lt;BR /&gt;{&lt;BR /&gt;System.Diagnostics.Debug.WriteLine("Topology not found or cannot be deleted: " + ex.Message);&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;if (topology != null)&lt;BR /&gt;{&lt;BR /&gt;Marshal.ReleaseComObject(topology);&lt;BR /&gt;GC.Collect();&lt;BR /&gt;GC.WaitForPendingFinalizers();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 May 2026 11:43:45 GMT</pubDate>
    <dc:creator>NinadPandit</dc:creator>
    <dc:date>2026-05-19T11:43:45Z</dc:date>
    <item>
      <title>Create topology with different feature classes both as single and multiple then run validate topology to find the errors using ArcObjects</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/create-topology-with-different-feature-classes/m-p/1702747#M20713</link>
      <description>&lt;P&gt;&lt;BR /&gt;I am writing code using ArcObjects 10.8.1 to find and delete the topology with a name and recreate a new topology with same name but with single feature class or different set of feature classes. However I am getting error code [-2147215017] error message [Exception from HRESULT: 0x80041957]. Here someone help with a clean code or fail safe code to implement the requirement.&lt;/P&gt;&lt;P&gt;Partial Code&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;public void RegisterClassToTopology(IFeatureDataset featureDataset, ITopology topology, IFeatureClass featureClass)&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;// 3. Verify the class belongs to the same Feature Dataset&lt;BR /&gt;if (featureClass.FeatureDataset.Name != featureDataset.Name)&lt;BR /&gt;{&lt;BR /&gt;throw new Exception("Feature Class must reside in the same Feature Dataset as the topology.");&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// 4. Add the Feature Class to the topology framework&lt;BR /&gt;// Parameters: (IFeatureClass, Weight, XYRank, ZRank)&lt;BR /&gt;if (!IsClassAlreadyInTopology(topology, featureClass))&lt;BR /&gt;{&lt;BR /&gt;topology.AddClass(featureClass, 5, 1, 1, true);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;System.Diagnostics.Debug.WriteLine("Feature class successfully added to topology via code.");&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;System.Diagnostics.Debug.WriteLine($"Failed to add class: {ex.Message}");&lt;BR /&gt;throw;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;public ITopologyRule AddTopologyRule(IFeatureDataset featureDataset, ITopology topology, IFeatureClass sfeatureClass, IFeatureClass cfeatureClass)&lt;BR /&gt;{&lt;BR /&gt;// 1. Cast to Topology Container&lt;BR /&gt;ISchemaLock schemaLock = (ISchemaLock)featureDataset;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// 2. Request an Exclusive Schema Lock to allow modifications&lt;BR /&gt;schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;//topology.AddClass(sourcefc, 5, 1, 1, false);&lt;BR /&gt;RegisterClassToTopology(featureDataset, topology, sfeatureClass);&lt;/P&gt;&lt;P&gt;if (cfeatureClass != null)&lt;BR /&gt;//topology.AddClass(connectedfc, 5, 1, 1, false);&lt;BR /&gt;RegisterClassToTopology(featureDataset, topology, cfeatureClass);&lt;/P&gt;&lt;P&gt;ITopologyRuleContainer ruleContainer = (ITopologyRuleContainer)topology;&lt;/P&gt;&lt;P&gt;esriTopologyRuleType ruletype = GetTopologyRuleType(sfeatureClass, cfeatureClass);&lt;/P&gt;&lt;P&gt;ITopologyRule rule = new TopologyRuleClass();&lt;/P&gt;&lt;P&gt;rule.Name = "Must Not Overlap";&lt;BR /&gt;rule.TopologyRuleType = ruletype;&lt;BR /&gt;rule.OriginClassID = sfeatureClass.FeatureClassID;&lt;/P&gt;&lt;P&gt;if (cfeatureClass != null)&lt;BR /&gt;{&lt;BR /&gt;rule.DestinationClassID = cfeatureClass.FeatureClassID;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// HANDLE SUBTYPES SAFELY&lt;BR /&gt;if (rule.OriginSubtypeSpecified)&lt;BR /&gt;{&lt;BR /&gt;ISubtypes subtypes = (ISubtypes)sfeatureClass;&lt;BR /&gt;rule.OriginSubtype = subtypes.HasSubtype ? subtypes.DefaultSubtypeCode : 0;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (cfeatureClass != null &amp;amp;&amp;amp; rule.DestinationSubtypeSpecified)&lt;BR /&gt;{&lt;BR /&gt;ISubtypes subtypes = (ISubtypes)cfeatureClass;&lt;BR /&gt;rule.DestinationSubtype = subtypes.HasSubtype ? subtypes.DefaultSubtypeCode : 0;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// CHECK EXISTING RULES PROPERLY&lt;BR /&gt;IEnumRule existingRules = ruleContainer.Rules;&lt;BR /&gt;existingRules.Reset();&lt;/P&gt;&lt;P&gt;IRule existingRule = existingRules.Next();&lt;/P&gt;&lt;P&gt;while (existingRule != null)&lt;BR /&gt;{&lt;BR /&gt;ITopologyRule thisrule = existingRule as ITopologyRule;&lt;BR /&gt;if (thisrule.TopologyRuleType == rule.TopologyRuleType &amp;amp;&amp;amp;&lt;BR /&gt;thisrule.OriginClassID == rule.OriginClassID &amp;amp;&amp;amp;&lt;BR /&gt;thisrule.DestinationClassID == rule.DestinationClassID)&lt;BR /&gt;{&lt;BR /&gt;Console.WriteLine("Rule already exists. Skipping.");&lt;BR /&gt;return thisrule;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;existingRule = existingRules.Next();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// ADD RULE ONLY ONCE&lt;BR /&gt;ruleContainer.AddRule(rule);&lt;/P&gt;&lt;P&gt;Console.WriteLine("Rule added successfully.");&lt;BR /&gt;return rule;&lt;BR /&gt;}&lt;BR /&gt;catch (System.Runtime.InteropServices.COMException comEx)&lt;BR /&gt;{&lt;BR /&gt;// Extract the specific HRESULT code&lt;BR /&gt;int errorCode = comEx.ErrorCode;&lt;BR /&gt;string errorMessage = comEx.Message;&lt;/P&gt;&lt;P&gt;Console.WriteLine($"Failed to add topology rule. Code: {errorCode}. Message: {errorMessage}");&lt;BR /&gt;throw;&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;System.Diagnostics.Debug.WriteLine($"Failed to add class: {ex.Message}");&lt;BR /&gt;throw;&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;// 5. CRITICAL: Always downgrade the lock back to Shared&lt;BR /&gt;schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;// The code has to handle single feature class and multiple feature class as well&lt;BR /&gt;public void CreateTopologyWithCrossorSameDatasetRules(IFeatureDataset featureDataset, string sourcefcname, string connectedfcname, ref StreamWriter sw)&lt;BR /&gt;{&lt;BR /&gt;// 1. Cast to topology container&lt;BR /&gt;ITopologyContainer2 topoContainer = (ITopologyContainer2)featureDataset;&lt;/P&gt;&lt;P&gt;DeleteTopologyIfExists(topoContainer, TEMP_TOPOLOGY_NAME);&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;// 2. Create topology&lt;BR /&gt;ITopology topology = topoContainer.CreateTopology(&lt;BR /&gt;TEMP_TOPOLOGY_NAME,&lt;BR /&gt;0.001, // Cluster tolerance&lt;BR /&gt;-1, // XY ranks default&lt;BR /&gt;""); // Config keyword&lt;/P&gt;&lt;P&gt;// 3. Open feature classes (must be in same feature dataset)&lt;BR /&gt;IFeatureWorkspace fws = (IFeatureWorkspace)featureDataset.Workspace;&lt;/P&gt;&lt;P&gt;IFeatureClass sourcefc = fws.OpenFeatureClass(sourcefcname);&lt;BR /&gt;IFeatureClass connectedfc = null;&lt;/P&gt;&lt;P&gt;if (!sourcefcname.Equals(connectedfcname))&lt;BR /&gt;{&lt;BR /&gt;connectedfc = fws.OpenFeatureClass(connectedfcname);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;ITopologyRule addedrule = AddTopologyRule(featureDataset, topology, sourcefc, connectedfc);&lt;/P&gt;&lt;P&gt;// 6. Validate topology&lt;BR /&gt;IEnvelope envelope = ((IGeoDataset)featureDataset).Extent;&lt;BR /&gt;ISpatialReference datasetspatialreference = ((IGeoDataset)featureDataset).SpatialReference;&lt;/P&gt;&lt;P&gt;IWorkspaceEdit wsEdit = (IWorkspaceEdit)featureDataset.Workspace;&lt;/P&gt;&lt;P&gt;wsEdit.StartEditing(false);&lt;BR /&gt;wsEdit.StartEditOperation();&lt;/P&gt;&lt;P&gt;topology.ValidateTopology(envelope);&lt;/P&gt;&lt;P&gt;wsEdit.StopEditOperation();&lt;BR /&gt;wsEdit.StopEditing(true);&lt;/P&gt;&lt;P&gt;wsEdit.AbortEditOperation();&lt;BR /&gt;wsEdit.StopEditing(false);&lt;/P&gt;&lt;P&gt;// Assuming you have an ITopology object named pTopology&lt;BR /&gt;IErrorFeatureContainer pErrorContainer = topology as IErrorFeatureContainer;&lt;/P&gt;&lt;P&gt;// Define an envelope to check for errors (or use the validated area)&lt;BR /&gt;//IEnvelope pEnvelope = topology.Extent;&lt;/P&gt;&lt;P&gt;// Retrieve errors within the area&lt;BR /&gt;IEnumTopologyErrorFeature pEnumTopoError = pErrorContainer.ErrorFeatures[&lt;BR /&gt;datasetspatialreference,&lt;BR /&gt;addedrule,&lt;BR /&gt;envelope,&lt;BR /&gt;true, // Include errors&lt;BR /&gt;true // Include exceptions&lt;BR /&gt;];&lt;/P&gt;&lt;P&gt;ITopologyErrorFeature pTopoError = pEnumTopoError.Next();&lt;/P&gt;&lt;P&gt;while (pTopoError != null)&lt;BR /&gt;{&lt;BR /&gt;// Access error details&lt;BR /&gt;int errorID = pTopoError.OriginOID;&lt;BR /&gt;int desterrorID = pTopoError.DestinationOID;&lt;/P&gt;&lt;P&gt;IFeature originfeature = sourcefc.GetFeature(errorID);&lt;BR /&gt;IFeature destfeature = connectedfc.GetFeature(desterrorID);&lt;/P&gt;&lt;P&gt;IGeometry ogeom = originfeature.Shape;&lt;BR /&gt;IGeometry dgeom = destfeature.Shape;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;ITopologicalOperator targetTopoOp = (ITopologicalOperator)ogeom;&lt;BR /&gt;IGeometry overlapGeometry = targetTopoOp.Intersect(dgeom, esriGeometryDimension.esriGeometry1Dimension);&lt;BR /&gt;double overlapLength = 0.0d;&lt;BR /&gt;double overlapPercent = 0.0f;&lt;/P&gt;&lt;P&gt;IPolyline overlapPolyline = (IPolyline)overlapGeometry;&lt;BR /&gt;double overlaplength = overlapPolyline.Length;&lt;BR /&gt;overlapLength = Math.Round(overlaplength, ROUND_OFF_VALUE);&lt;BR /&gt;ICurve lgeomcurve = ogeom as ICurve;&lt;BR /&gt;ICurve interCurve = overlapGeometry as ICurve;&lt;BR /&gt;// 4. Calculate Percentage&lt;BR /&gt;overlapPercent = Math.Round((interCurve.Length / lgeomcurve.Length) * 100, ROUND_OFF_VALUE);&lt;/P&gt;&lt;P&gt;esriTopologyRuleType ruleID = pTopoError.TopologyRuleType;&lt;BR /&gt;//IGeometry errorShape = pTopoError.Shape;&lt;/P&gt;&lt;P&gt;// Process error (e.g., log to file, highlight in map)&lt;BR /&gt;Console.WriteLine($"Error ID: {errorID}, Rule Type: {ruleID}");&lt;/P&gt;&lt;P&gt;dataGridViewOverlapping.Rows.Add(errorID, desterrorID, sourcefc.AliasName, connectedfc.AliasName, overlapLength, overlapPercent, "Overlapping");&lt;BR /&gt;string resultValue = errorID + "," + desterrorID + "," + sourcefc.AliasName + "," + connectedfc.AliasName + "," + overlapLength + "," + overlapPercent + "," + "Overlapping";&lt;BR /&gt;sw.WriteLine(resultValue);&lt;/P&gt;&lt;P&gt;pTopoError = pEnumTopoError.Next();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;//schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);&lt;BR /&gt;}&lt;BR /&gt;catch (System.Runtime.InteropServices.COMException comEx)&lt;BR /&gt;{&lt;BR /&gt;// Extract the specific HRESULT code&lt;BR /&gt;int errorCode = comEx.ErrorCode;&lt;BR /&gt;string errorMessage = comEx.Message;&lt;/P&gt;&lt;P&gt;Console.WriteLine($"Failed to add topology rule. Code: {errorCode}. Message: {errorMessage}");&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// This function finds the topology by name and deletes if it exists.&lt;BR /&gt;public void DeleteTopologyIfExists(ITopologyContainer topologyContainer, string topologyName)&lt;BR /&gt;{&lt;BR /&gt;if (topologyContainer != null)&lt;BR /&gt;{&lt;BR /&gt;ITopology topology = null;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// 3. Find and Delete Topology&lt;BR /&gt;topology = topologyContainer.get_TopologyByName(topologyName);&lt;BR /&gt;if (topology != null)&lt;BR /&gt;{&lt;BR /&gt;IDataset dataset = (IDataset)topology;&lt;BR /&gt;if (dataset.CanDelete())&lt;BR /&gt;{&lt;BR /&gt;dataset.Delete();&lt;BR /&gt;System.Diagnostics.Debug.WriteLine(topologyName + " deleted successfully.");&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;ITopology removedtopology = topologyContainer.TopologyByName[topologyName];&lt;BR /&gt;}&lt;BR /&gt;catch (System.Runtime.InteropServices.COMException ex)&lt;BR /&gt;{&lt;BR /&gt;System.Diagnostics.Debug.WriteLine("Topology not found or cannot be deleted: " + ex.Message);&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;if (topology != null)&lt;BR /&gt;{&lt;BR /&gt;Marshal.ReleaseComObject(topology);&lt;BR /&gt;GC.Collect();&lt;BR /&gt;GC.WaitForPendingFinalizers();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 11:43:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/create-topology-with-different-feature-classes/m-p/1702747#M20713</guid>
      <dc:creator>NinadPandit</dc:creator>
      <dc:date>2026-05-19T11:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create topology with different feature classes both as single and multiple then run validate topology to find the errors using ArcObjects</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/create-topology-with-different-feature-classes/m-p/1702748#M20714</link>
      <description>&lt;P&gt;In addition to above, the line of code that generates error is mentioned below.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;// ADD RULE ONLY ONCE&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ruleContainer.AddRule(rule);&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2026 11:56:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/create-topology-with-different-feature-classes/m-p/1702748#M20714</guid>
      <dc:creator>NinadPandit</dc:creator>
      <dc:date>2026-05-19T11:56:49Z</dc:date>
    </item>
  </channel>
</rss>

