70002889 I want to create a custom rect action

Article 70002889
Type HowTo
Product Engine
Date Added 5/29/2026 12:00:00 AM
Fixed 12.0.03 (5/29/2026 12:00:00 AM)
Submitted by Peter Chanios

Summary

I want to create a custom rect action. The user is asked for the First Insertion Point. Then the user is asked for a second point that will provide the width and the rotation of the rect. Lastly the user is asked for a third point that will provide the Height of the rect. This last point must also draw the final rect on the screen with a user action.

Solution

Please check the code below

private bool NewRectCommand()
{
    doc.Prompt("Insertion Point of Rect:");
    gPoint InsertionPt = doc.ActionUtility.getUserPoint() as gPoint;
    if (InsertionPt == null) return false;
    doc.Prompt(null);

    gPoint SecondPointWidth = null;
    doc.Prompt("Second Point for Width and Rotation of Rect:");
    StatusCode status = doc.ActionUtility .getUserRefPoint (InsertionPt, out SecondPointWidth);
    doc.Prompt(null);
    if (status != StatusCode.Success) return false;

    StatusCode ret = StatusCode.Other;
    ActionMyRect myAction = new ActionMyRect (InsertionPt, SecondPointWidth, doc.ActiveLayOut);
    doc.ActionAdd(myAction);
    ret = myAction.WaitToFinish();
    if (ret == VectorDraw.Actions.StatusCode.Success)
    {
        vdRect rect = myAction.Entity as vdRect;
        if (rect == null) return false;
        vdRect Myrect = rect.Clone(doc) as vdRect;
        Myrect.Transformby(doc.User2WorldMatrix);
        doc.ActiveLayOut.Entities.AddItem (Myrect);
        doc.ActionDrawFigure(Myrect);
        return true;
    }
    return false;
}

public class ActionMyRect : VectorDraw.Professional.Actions.ActionEntity
{
    /// 
    /// A vdRect object created from the action.
    /// 
    protected vdRect rect;
    gPoint FirstPt = null;
    gPoint SecondPt = null;
    /// 
    /// Initializes the object with the given parameters.
    /// 
    /// First Insertion Point of rect
    /// Second Point where we calculate the rotation and the width of the rect.
    /// The vdLayout object where the action takes place.
    public ActionMyRect(gPoint InsertionPoint, gPoint SecondPoint, vdLayout layout)
        : base(InsertionPoint, layout)
    {
        ValueTypeProp |= valueType.REFPOINT;
        rect = new vdRect();
        rect.SetUnRegisterDocument(layout.Document);
        rect.setDocumentDefaults();
        rect.InsertionPoint = InsertionPoint;
        rect.Rotation = InsertionPoint.GetAngle (SecondPoint);
        rect.Width = InsertionPoint.Distance2D(SecondPoint);
        FirstPt = new gPoint(InsertionPoint);
        SecondPt = new gPoint(SecondPoint);
    }
    /// 
    /// the reference point of the action in World Coordinate System.
    /// 
    public override gPoint ReferencePoint
    {
        get
        {
            return base.ReferencePoint;
        }
        set
        {
            base.ReferencePoint = value;
            if (ReferencePoint != null && this.Entity != null && Layout != null)
            {
                rect.InsertionPoint = Layout.Document.World2UserMatrix.Transform(ReferencePoint);
            }
        }
    }
    /// 
    /// Get a value that represents if the rubber line is drawn or not.Returns false since a rubber line is needed.
    /// 
    public override bool HideRubberLine
    {
        get
        {
            return false;
        }
    }
    /// 
    /// The entity which is currently selected for this action.Returns the created rect.
    /// 
    public override vdFigure Entity
    {
        get
        {
            return rect;
        }
    }
    /// 
    /// Called when the mouse position changes.We calculate the Height of the rect from the distance of the cursor from the Initial rect width line.
    /// 
    /// A gPoint representing the new mouse position in User Coordinate System.
    protected override void OnMyPositionChanged(gPoint newPosition)
    {
        vdLine l = new vdLine ();
        l.StartPoint = FirstPt;
        l.EndPoint = SecondPt;
        gPoint WorldNewPos = Layout.Document.User2WorldMatrix.Transform(newPosition);
        l.Transformby(Layout.Document.User2WorldMatrix);
        double side = l.TestOffsetSide(WorldNewPos);

        if (side > 0.0)
        {
            rect.Height = Globals.distPointFromLine(newPosition, FirstPt, SecondPt);
        }
        else
        {
            rect.Height = -Globals.distPointFromLine(newPosition, FirstPt, SecondPt);
        }


    }
    /// 
    /// Get a value that represents if the entity should be updated after each position change.Returns true since an update is needed.
    /// 
    public override bool needUpdate
    {
        get
        {
            return true;
        }
    }
}

Send comments on this topic.