Author Topic: Creates a rectangle from the mid points of opposing sides  (Read 1473 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Creates a rectangle from the mid points of opposing sides
« on: April 27, 2013, 11:40:46 AM »
Creates a rectangle from the mid points of opposing sides and then specify the length of the sides
inspired by  David Bethel
as posted at http://www.cadtutor.net/forum/showthread.php?63494-Lisp-for-rectangle

   C#
Code: [Select]
       
[CommandMethod("P2Sides")]
        public void testDrawRectangleBySides()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptPointOptions ppo = new PromptPointOptions("\nCenter of the first side: ");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;
            Point3d p1 = ppr.Value;
            ppo = new PromptPointOptions("\nCenter of the seconf side: ");
            ppo.UseBasePoint = true;
            ppo.BasePoint = p1;
            ppo.UseDashedLine = true;
            ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;

            Point3d p2 = ppr.Value;
            if (p1.X == p2.X || p1.Y == p2.Y)
            {
                ed.WriteMessage("\nInvalid coordinate specification");
                return;
            }
            ed.DrawVector(p1, p2, 1, true);
            PromptDistanceOptions pdo = new PromptDistanceOptions("\nOpposite width: ");
            pdo.BasePoint = p2;
            pdo.DefaultValue = 100;
            pdo.UseDefaultValue = true;
            PromptDoubleResult pdr = ed.GetDistance(pdo);
            if (pdr.Status != PromptStatus.OK) return;
            double leg = p1.DistanceTo(p2);
            double wid = pdr.Value;
            ed.WriteMessage("\n\tLength:\t{0:f3}\tWidth:{1:f3}\n", leg, wid);
            Plane plan = new Plane(Point3d.Origin, Vector3d.ZAxis);
            double ang = p1.GetVectorTo(p2).AngleOnPlane(plan);
            Point3dCollection pts = new Point3dCollection();
            Point3d c1 = PolarPoint(p1, ang - Math.PI / 2, wid / 2);
            Point3d c4 = PolarPoint(p1, ang + Math.PI / 2, wid / 2);
            Point3d c2 = PolarPoint(c1, ang, leg);
            Point3d c3 = PolarPoint(c4, ang, leg);
            pts.Add(c1); pts.Add(c2); pts.Add(c3); pts.Add(c4);
            Polyline poly = new Polyline();
            int idx = 0;
            foreach (Point3d p in pts)
            {
                Point2d pp = new Point2d(p.X, p.Y);
                poly.AddVertexAt(idx, pp, 0, 0, 0);
                idx += 1;
            }
            poly.Closed = true;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AppendEntity(poly);
                tr.AddNewlyCreatedDBObject(poly, true);
                tr.Commit();

            }//end using transaction

        }

        // by Tony Tanzillo
        public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
        {
            return new Point3d(
            basepoint.X + (distance * Math.Cos(angle)),
            basepoint.Y + (distance * Math.Sin(angle)),
            basepoint.Z);
        }


      VB.NET
Code: [Select]
        <CommandMethod("P2Sides")> _
        Public Sub testDrawRectangleBySides()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim ppo As New PromptPointOptions(vbLf & "Center of the first side: ")
            Dim ppr As PromptPointResult = ed.GetPoint(ppo)
            If ppr.Status <> PromptStatus.OK Then
                Return
            End If
            Dim p1 As Point3d = ppr.Value
            ppo = New PromptPointOptions(vbLf & "Center of the seconf side: ")
            ppo.UseBasePoint = True
            ppo.BasePoint = p1
            ppo.UseDashedLine = True
            ppr = ed.GetPoint(ppo)
            If ppr.Status <> PromptStatus.OK Then
                Return
            End If

            Dim p2 As Point3d = ppr.Value
            If p1.X = p2.X OrElse p1.Y = p2.Y Then
                ed.WriteMessage(vbLf & "Invalid coordinate specification")
                Return
            End If
            ed.DrawVector(p1, p2, 1, True)
            Dim pdo As New PromptDistanceOptions(vbLf & "Opposite width: ")
            pdo.BasePoint = p2
            pdo.DefaultValue = 100
            pdo.UseDefaultValue = True
            Dim pdr As PromptDoubleResult = ed.GetDistance(pdo)
            If pdr.Status <> PromptStatus.OK Then
                Return
            End If
            Dim leg As Double = p1.DistanceTo(p2)
            Dim wid As Double = pdr.Value
            ed.WriteMessage(vbLf & vbTab & "Length:" & vbTab & "{0:f3}" & vbTab & "Width:{1:f3}" & vbLf, leg, wid)
            Dim plan As New Plane(Point3d.Origin, Vector3d.ZAxis)
            Dim ang As Double = p1.GetVectorTo(p2).AngleOnPlane(plan)
            Dim pts As New Point3dCollection()
            Dim c1 As Point3d = PolarPoint(p1, ang - Math.PI / 2, wid / 2)
            Dim c4 As Point3d = PolarPoint(p1, ang + Math.PI / 2, wid / 2)
            Dim c2 As Point3d = PolarPoint(c1, ang, leg)
            Dim c3 As Point3d = PolarPoint(c4, ang, leg)
            pts.Add(c1)
            pts.Add(c2)
            pts.Add(c3)
            pts.Add(c4)
            Dim poly As New Polyline()
            Dim idx As Integer = 0
            For Each p As Point3d In pts
                Dim pp As New Point2d(p.X, p.Y)
                poly.AddVertexAt(idx, pp, 0, 0, 0)
                idx += 1
            Next
            poly.Closed = True

            Using tr As Transaction = doc.TransactionManager.StartTransaction()
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                btr.AppendEntity(poly)
                tr.AddNewlyCreatedDBObject(poly, True)

                tr.Commit()
            End Using
            'end using transaction
        End Sub

        ' by Tony Tanzillo
        Public Shared Function PolarPoint(basepoint As Point3d, angle As Double, distance As Double) As Point3d
            Return New Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z)
        End Function