ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Core.Data.Topology Namespace / TopologyDefinition Class / GetRules Method
Example

In This Topic
    GetRules Method (TopologyDefinition)
    In This Topic
    Gets the TopologyRules defined for this Topology. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax

    Return Value

    The TopologyRules defined for this Topology.
    Exceptions
    ExceptionDescription
    A geodatabase-related exception has occurred.
    Example
    GetTopologyRules
    using (TopologyDefinition topologyDefinition = topology.GetDefinition())
    {
      IReadOnlyList<TopologyRule> rules = topologyDefinition.GetRules();
    
      Console.WriteLine($"There are {rules.Count} topology rules defined for the topology:");
      Console.WriteLine("ID \t Origin Class \t Origin Subtype \t Destination Class \t Destination Subtype \t Rule Type");
    
      foreach (TopologyRule rule in rules)
      {
        Console.Write($"{rule.ID}");
    
        Console.Write(!String.IsNullOrEmpty(rule.OriginClass) ? $"\t{rule.OriginClass}" : "\t\"\"");
    
        Console.Write(rule.OriginSubtype != null ? $"\t{rule.OriginSubtype.GetName()}" : "\t\"\"");
    
        Console.Write(!String.IsNullOrEmpty(rule.DestinationClass) ? $"\t{rule.DestinationClass}" : "\t\"\"");
    
        Console.Write(rule.DestinationSubtype != null ? $"\t{rule.DestinationSubtype.GetName()}" : "\t\"\"");
    
        Console.Write($"\t{rule.RuleType}");
    
        Console.WriteLine();
      }
    }
    
    MarkAndUnmarkAsErrors
    // Get all the errors due to features violating the "PointProperlyInsideArea" topology rule.
    
    using (TopologyDefinition topologyDefinition = topology.GetDefinition())
    {
      TopologyRule pointProperlyInsideAreaRule = topologyDefinition.GetRules().First(rule => rule.RuleType == TopologyRuleType.PointProperlyInsideArea);
    
      ErrorDescription errorDescription = new ErrorDescription(topology.GetExtent())
      {
        TopologyRule = pointProperlyInsideAreaRule
      };
    
      IReadOnlyList<TopologyError> errorsDueToViolatingPointProperlyInsideAreaRule = topology.GetErrors(errorDescription);
      Console.WriteLine($"There are {errorsDueToViolatingPointProperlyInsideAreaRule.Count} feature violating the 'PointProperlyInsideArea' topology rule.");
    
      // Mark all errors from features violating the 'PointProperlyInsideArea' topology rule as exceptions.
    
      foreach (TopologyError error in errorsDueToViolatingPointProperlyInsideAreaRule)
      {
        topology.MarkAsException(error);
      }
    
      // Now verify all the errors from features violating the 'PointProperlyInsideArea' topology rule have indeed been
      // marked as exceptions.
      //
      // By default, ErrorDescription is initialized to ErrorType.ErrorAndException.  Here we want ErrorType.ErrorOnly.
    
      errorDescription = new ErrorDescription(topology.GetExtent())
      {
        ErrorType = ErrorType.ErrorOnly,
        TopologyRule = pointProperlyInsideAreaRule
      };
    
      IReadOnlyList<TopologyError> errorsAfterMarkedAsExceptions = topology.GetErrors(errorDescription);
      Console.WriteLine($"There are {errorsAfterMarkedAsExceptions.Count} feature violating the 'PointProperlyInsideArea' topology rule after all the errors have been marked as exceptions.");
    
      // Finally, reset all the exceptions as errors by unmarking them as exceptions.
    
      foreach (TopologyError error in errorsDueToViolatingPointProperlyInsideAreaRule)
      {
        topology.UnmarkAsException(error);
      }
    
      IReadOnlyList<TopologyError> errorsAfterUnmarkedAsExceptions = topology.GetErrors(errorDescription);
      Console.WriteLine($"There are {errorsAfterUnmarkedAsExceptions.Count} feature violating the 'PointProperlyInsideArea' topology rule after all the exceptions have been reset as errors.");
    }
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also