Compartilhar via


AddRule Class

Represents a rule that is called when an object is added to the model.

Inheritance Hierarchy

System.Object
  Microsoft.VisualStudio.Modeling.Rule
    Microsoft.VisualStudio.Modeling.AddRule
      Microsoft.VisualStudio.Modeling.Diagrams.CommentShapeAddRule
      Microsoft.VisualStudio.Modeling.Diagrams.NodeShape.ExpandCollapseNodeShapeWhenAddedToDiagramRule
      Microsoft.VisualStudio.Modeling.Diagrams.ParentShapeContainsNestedChildShapesAddRule
      Microsoft.VisualStudio.Modeling.Diagrams.ParentShapeHasRelativeChildShapesAddRule
      Microsoft.VisualStudio.Modeling.Diagrams.ShapeElementAddRule
      Microsoft.VisualStudio.Modeling.ElementDeserializedRule

Namespace:  Microsoft.VisualStudio.Modeling
Assembly:  Microsoft.VisualStudio.Modeling.Sdk.10.0 (in Microsoft.VisualStudio.Modeling.Sdk.10.0.dll)

Syntax

'Declaração
Public MustInherit Class AddRule _
    Inherits Rule
public abstract class AddRule : Rule
public ref class AddRule abstract : public Rule
[<AbstractClass>]
type AddRule =  
    class
        inherit Rule
    end
public abstract class AddRule extends Rule

The AddRule type exposes the following members.

Constructors

  Name Description
Protected method AddRule Initializes an instance of the AddRule class.

Top

Properties

  Name Description
Public property FireBefore true if this rule will be executed before the change occurs. (Inherited from Rule.)
Public property FireImmediately true if this rule will execute immediately the change occurs. (Inherited from Rule.)
Public property FireOnLocalCommit true if this rule will execute when the current transaction commits. (Inherited from Rule.)
Public property FireOnTopLevelCommit true if this rule will execute when the top level transaction commits. (Inherited from Rule.)
Public property FireTime Gets or sets when the rule should execute. Normally set by the RuleOn attribute. (Inherited from Rule.)
Public property IsEnabled Gets or sets whether the rule is enabled. Normally true by default, but you can initialize it to false in the RuleOn attribute. (Inherited from Rule.)
Public property Priority Gets the priority that is assigned to the rule. Helps to determine the order in which rules are executed at the end of a transaction. (Inherited from Rule.)

Top

Methods

  Name Description
Public method CompareTo(Object) Compares the rule to another object. (Inherited from Rule.)
Public method CompareTo(Rule) Compares the rule to another rule by their IDs. (Inherited from Rule.)
Public method ElementAdded Alerts listeners that a rule has been used.
Public method Equals(Object) Verifies whether the rule is equal to another object. (Inherited from Rule.)
Public method Equals(Rule) Verifies whether a rule is equal to another rule. (Inherited from Rule.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Gets the hash code for the rule. (Inherited from Rule.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

Rules of this type are triggered when an element, relationship, shape, connector or diagram is added to the model.

A RuleOnAttribute attribute placed on the class indicates what type of object the Rule works on and when the rule should fire.

The rule is called when the object of the specified type is added to the model, whether it is added in the UI or programmatically.

The rule is also triggered when an element is loaded from file. If you want to avoid responding when this happens, include this code in your ElementAdded:

// Ignore this call if we're currently loading a model:
    if (e.ModelElement.Store.TransactionManager
          .CurrentTransaction.IsSerializing) 
       return;

For more information and examples, see Rules Propagate Changes Within the Model.

Examples

In the following example, A Rule is defined that derives from AddRule. This rule sets the position of a Shape when it is added to the diagram.

The RuleOn attribute indicates that the rule should fire when the top level transaction commits.

[RuleOn(typeof(ParentShapeContainsNestedChildShapes), FireTime = TimeToFire.TopLevelCommit)]
public class ShapeAddedToDiagramRule : AddRule
{
  private double offset = 0.25;
  private PointD location = new PointD(0.25, 0.25);

  public override void ElementAdded(ElementAddedEventArgs e)
  {
    Shape shape = null;
    ParentShapeContainsNestedChildShapes nestedLink = e.ModelElement as ParentShapeContainsNestedChildShapes;
    if (nestedLink != null)
    {
      shape = nestedLink.NestedChildShapes as Shape;
    }

    if (shape != null && shape.Diagram != null)
    {
      // Expand the shape and move it to its new position
      shape.IsExpanded = true;
      shape.Location = new PointD(location.X, location.Y + offset);

      // Adjust the height offset for the size of the shape
      // (I'm assuming that the DefaultContainerMargin
      // provides for a decent spacing between the shapes)
      offset += shape.Size.Height + shape.Diagram.DefaultContainerMargin.Height;
    }
  }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.VisualStudio.Modeling Namespace

Other Resources

Rules Propagate Changes Within the Model