drawing rectangle in autocad via choosing 2 points

drawing rectangle in autocad via choosing 2 points

Anonymous
Not applicable
3,725 Views
5 Replies
Message 1 of 6

drawing rectangle in autocad via choosing 2 points

Anonymous
Not applicable

how do i draw a rectangle like it draws the autocad. i mean choose 1st point and then choose 2nd point. i know how to get points coordinates but i dont know how to draw all 4 lines at the right angle when i picked 2nd point

any suggestions?

upd. am i right to use Editor.GetCorner() method?

0 Likes
Accepted solutions (1)
3,726 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Editor.GetCorner displays a rectangular rubberband which is always parallel to the window sides.

AutoCAD draws the rectangles parallel to the X and Y axis of the current UCS.

So, with an UCS which X and Y axis are not parallel to the window, the rubberband will be different from the rectangle.

 

Try this:

 

        [CommandMethod("DRAWRECT")]
        public void DrawRectangle()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var ppr = ed.GetPoint("\nFirst corner: ");
            if (ppr.Status != PromptStatus.OK)
                return;
            var pt1 = ppr.Value;

            ppr = ed.GetCorner("\nOpposite corner: ", pt1);
            if (ppr.Status != PromptStatus.OK)
                return;
            var pt2 = ppr.Value;

            double x1 = pt1.X;
            double y1 = pt1.Y;
            double x2 = pt2.X;
            double y2 = pt2.Y;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var pline = new Polyline(4);
                pline.AddVertexAt(0, new Point2d(x1, y1), 0.0, 0.0, 0.0);
                pline.AddVertexAt(1, new Point2d(x2, y1), 0.0, 0.0, 0.0);
                pline.AddVertexAt(2, new Point2d(x2, y2), 0.0, 0.0, 0.0);
                pline.AddVertexAt(3, new Point2d(x1, y2), 0.0, 0.0, 0.0);
                pline.Closed = true;
                pline.TransformBy(ed.CurrentUserCoordinateSystem);
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }
        }

To be closer to the AutoCAD RECTANG command, you should use a Jig, but it is a more complex task.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6

jsucheckiM6JVK
Explorer
Explorer

I work in the granite industry. How would I create a custom tab that makes a rectangle 4" inches in height from the length of a line and offset it 25" above the line?

0 Likes
Message 4 of 6

ActivistInvestor
Mentor
Mentor

You're asking this question in a discussion group focused on programming, but your question could be more about simple customization using macros or scripting.

 

Are you asking how to write a program to do this, or a macro or script that does it?

 

 

0 Likes
Message 5 of 6

jsucheckiM6JVK
Explorer
Explorer

How can I learn where to make a macro or script that could do it? I have two or three other things id like to ask about that could make my job easier 🙂

0 Likes
Message 6 of 6

ActivistInvestor
Mentor
Mentor

The general customization discussion forum would be a good place to ask.

0 Likes