Results 1 to 5 of 5

Thread: Drawing Rectangle Which Dynamically Updates View

  1. #1
    Member
    Join Date
    2009-12
    Posts
    21
    Login to Give a bone
    0

    Default Drawing Rectangle Which Dynamically Updates View

    Hi,

    I'm not entirely sure how to try and describe what I'm looking to do so hopefully this will make sense......

    Is is possible for me to click on a point on the screen and then as I drag the cursor to another location have it draw a rectangle which dynamically adjusts it's length as I move the mouse. The point I first click will be the middle of the rectangles end line. A second click at another point completes the rectangle.

    Is this possible?

    Thanks

  2. #2
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Drawing Rectangle Which Dynamically Updates View

    Hi,

    This can be done using a Jig.

    If I don't misunderstand what you're trying to do, here's an example.

    Code:
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.Runtime;
    using static System.Math;
    
    using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
    
    [assembly: CommandClass(typeof(RectangleJigSample.Commands))]
    
    namespace RectangleJigSample
    {
        public class Commands
        {
            double halfWidth = 100.0; // default value
    
            [CommandMethod("CMD")]
            public void Cmd()
            {
                var doc = AcAp.DocumentManager.MdiActiveDocument;
                var db = doc.Database;
                var ed = doc.Editor;
    
                var pointResult = ed.GetPoint("\nRectangle start point: ");
                if (pointResult.Status != PromptStatus.OK) return;
    
                var distOptions = new PromptDistanceOptions("\nRectangle half width: ");
                distOptions.AllowNegative = false;
                distOptions.AllowZero = false;
                distOptions.BasePoint = pointResult.Value;
                distOptions.UseBasePoint = true;
                distOptions.DefaultValue = halfWidth;
                distOptions.UseDefaultValue = true;
                var distResult = ed.GetDistance(distOptions);
                if (distResult.Status != PromptStatus.OK) return;
    
                var basePt = pointResult.Value.TransformBy(ed.CurrentUserCoordinateSystem);
                halfWidth = distResult.Value;
    
                using (var tr = db.TransactionManager.StartTransaction())
                using (var pline = new Polyline(4))
                {
                    pline.AddVertexAt(0, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.AddVertexAt(1, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.AddVertexAt(2, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.AddVertexAt(3, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.Closed = true;
                    var jig = new RectangleJig(pline, basePt, halfWidth);
                    var result = ed.Drag(jig);
                    if (result.Status == PromptStatus.OK)
                    {
                        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        curSpace.AppendEntity(pline);
                        tr.AddNewlyCreatedDBObject(pline, true);
                    }
                    tr.Commit();
                }
            }
        }
    
        class RectangleJig : EntityJig
        {
            Polyline pline;
            Point3d basePt, dragPt;
            Point2d start;
            double halfWidth;
            const double HALFPI = 0.5 * PI;
    
            public RectangleJig(Polyline pline, Point3d basePt, double halfWidth) : base(pline)
            {
                this.pline = pline;
                this.basePt = basePt;
                this.halfWidth = halfWidth;
                start = new Point2d(basePt.X, basePt.Y);
            }
    
            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                var msg = "\nRectangle end point: ";
                var options = new JigPromptPointOptions(msg);
                options.UserInputControls =
                  (UserInputControls.Accept3dCoordinates);
                options.BasePoint = basePt;
                options.UseBasePoint = true;
                options.Cursor = CursorType.RubberBand;
                var result = prompts.AcquirePoint(options);
                if (dragPt.DistanceTo(result.Value) < Tolerance.Global.EqualPoint)
                {
                    return SamplerStatus.NoChange;
                }
                else
                {
                    dragPt = result.Value;
                }
                return SamplerStatus.OK;
            }
    
            protected override bool Update()
            {
                var angle = Vector3d.XAxis.GetAngleTo(basePt.GetVectorTo(dragPt), Vector3d.ZAxis);
                var end = new Point2d(dragPt.X, dragPt.Y);
                pline.SetPointAt(0, Polar(start, angle - HALFPI, halfWidth));
                pline.SetPointAt(1, Polar(end, angle - HALFPI, halfWidth));
                pline.SetPointAt(2, Polar(end, angle + HALFPI, halfWidth));
                pline.SetPointAt(3, Polar(start, angle + HALFPI, halfWidth));
                return true;
            }
    
            private Point2d Polar(Point2d pt, double angle, double dist)
            {
                return new Point2d(pt.X + dist * Cos(angle), pt.Y + dist * Sin(angle));
            }
        }
    }

  3. #3
    Member
    Join Date
    2009-12
    Posts
    21
    Login to Give a bone
    0

    Default Re: Drawing Rectangle Which Dynamically Updates View

    Quote Originally Posted by _gile View Post
    Hi,

    This can be done using a Jig.

    If I don't misunderstand what you're trying to do, here's an example.

    Code:
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.Runtime;
    using static System.Math;
    
    using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
    
    [assembly: CommandClass(typeof(RectangleJigSample.Commands))]
    
    namespace RectangleJigSample
    {
        public class Commands
        {
            double halfWidth = 100.0; // default value
    
            [CommandMethod("CMD")]
            public void Cmd()
            {
                var doc = AcAp.DocumentManager.MdiActiveDocument;
                var db = doc.Database;
                var ed = doc.Editor;
    
                var pointResult = ed.GetPoint("\nRectangle start point: ");
                if (pointResult.Status != PromptStatus.OK) return;
    
                var distOptions = new PromptDistanceOptions("\nRectangle half width: ");
                distOptions.AllowNegative = false;
                distOptions.AllowZero = false;
                distOptions.BasePoint = pointResult.Value;
                distOptions.UseBasePoint = true;
                distOptions.DefaultValue = halfWidth;
                distOptions.UseDefaultValue = true;
                var distResult = ed.GetDistance(distOptions);
                if (distResult.Status != PromptStatus.OK) return;
    
                var basePt = pointResult.Value.TransformBy(ed.CurrentUserCoordinateSystem);
                halfWidth = distResult.Value;
    
                using (var tr = db.TransactionManager.StartTransaction())
                using (var pline = new Polyline(4))
                {
                    pline.AddVertexAt(0, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.AddVertexAt(1, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.AddVertexAt(2, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.AddVertexAt(3, Point2d.Origin, 0.0, 0.0, 0.0);
                    pline.Closed = true;
                    var jig = new RectangleJig(pline, basePt, halfWidth);
                    var result = ed.Drag(jig);
                    if (result.Status == PromptStatus.OK)
                    {
                        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        curSpace.AppendEntity(pline);
                        tr.AddNewlyCreatedDBObject(pline, true);
                    }
                    tr.Commit();
                }
            }
        }
    
        class RectangleJig : EntityJig
        {
            Polyline pline;
            Point3d basePt, dragPt;
            Point2d start;
            double halfWidth;
            const double HALFPI = 0.5 * PI;
    
            public RectangleJig(Polyline pline, Point3d basePt, double halfWidth) : base(pline)
            {
                this.pline = pline;
                this.basePt = basePt;
                this.halfWidth = halfWidth;
                start = new Point2d(basePt.X, basePt.Y);
            }
    
            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                var msg = "\nRectangle end point: ";
                var options = new JigPromptPointOptions(msg);
                options.UserInputControls =
                  (UserInputControls.Accept3dCoordinates);
                options.BasePoint = basePt;
                options.UseBasePoint = true;
                options.Cursor = CursorType.RubberBand;
                var result = prompts.AcquirePoint(options);
                if (dragPt.DistanceTo(result.Value) < Tolerance.Global.EqualPoint)
                {
                    return SamplerStatus.NoChange;
                }
                else
                {
                    dragPt = result.Value;
                }
                return SamplerStatus.OK;
            }
    
            protected override bool Update()
            {
                var angle = Vector3d.XAxis.GetAngleTo(basePt.GetVectorTo(dragPt), Vector3d.ZAxis);
                var end = new Point2d(dragPt.X, dragPt.Y);
                pline.SetPointAt(0, Polar(start, angle - HALFPI, halfWidth));
                pline.SetPointAt(1, Polar(end, angle - HALFPI, halfWidth));
                pline.SetPointAt(2, Polar(end, angle + HALFPI, halfWidth));
                pline.SetPointAt(3, Polar(start, angle + HALFPI, halfWidth));
                return true;
            }
    
            private Point2d Polar(Point2d pt, double angle, double dist)
            {
                return new Point2d(pt.X + dist * Cos(angle), pt.Y + dist * Sin(angle));
            }
        }
    }
    Many, many thanks for this _gile.

    I am getting an error here
    Code:
    var doc = AcAp.DocumentManager.MdiActiveDocument;
    where VisualStudio is complaining that AcAp is inaccessible due to it's protection level.

    Any idea what that might be?

  4. #4
    Member
    Join Date
    2009-12
    Posts
    21
    Login to Give a bone
    0

    Default Re: Drawing Rectangle Which Dynamically Updates View

    Quote Originally Posted by cosmarchy View Post
    Many, many thanks for this _gile.

    I am getting an error here
    Code:
    var doc = AcAp.DocumentManager.MdiActiveDocument;
    where VisualStudio is complaining that AcAp is inaccessible due to it's protection level.

    Any idea what that might be?
    Sussed, it. Problem was with me not copying everything as I should have (missed a line out )

    Now to see if I can get it to work

  5. #5
    Member
    Join Date
    2009-12
    Posts
    21
    Login to Give a bone
    0

    Default Re: Drawing Rectangle Which Dynamically Updates View

    Horray, I've got it working.

    It's not quite what I was looking for, but I have to say a big thanks to _gile for his time and effort getting me where I currently am.

    I need to make, what I believe, is a minor tweak but first I have to understand what is going on. When I click F1 in visualstudio when the cursor is on a keyword, I get a visualstudio webpage - I was expecting to get help files.

    Does anyone know where I can get the developers reference and integrate it into visualstudio? Is this possible?

    Thanks

Similar Threads

  1. Replies: 6
    Last Post: 2011-04-25, 07:55 PM
  2. Replies: 3
    Last Post: 2009-04-15, 07:30 PM
  3. Drawing Customised Rectangle
    By david.brissenden in forum VBA/COM Interop
    Replies: 3
    Last Post: 2008-08-18, 10:13 AM
  4. Drawing hatched rectangle
    By mathias2014 in forum AutoLISP
    Replies: 10
    Last Post: 2007-09-12, 03:59 PM
  5. rotate when drawing a rectangle
    By GuyR in forum Revit Architecture - Wish List
    Replies: 6
    Last Post: 2005-08-16, 06:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •