Author Topic: Regular polygons example  (Read 1020 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
Regular polygons example
« on: February 21, 2013, 08:19:48 AM »
          C#
Code: [Select]
       // 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);
        }
        [CommandMethod("preg", CommandFlags.UsePickSet)]
        public void testPolygonRegular()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptIntegerOptions pio = new PromptIntegerOptions("");
            pio.Message = "\nNumber of sides: ";

            // Restrict input to positive and non-negative values
            pio.AllowZero = false;
            pio.AllowNegative = false;
            // Add default value
            pio.DefaultValue = 3;
            pio.AllowNone = true;
            // Get the value entered by the user
            PromptIntegerResult ires = ed.GetInteger(pio);
            if (ires.Status != PromptStatus.OK) return;
            int n = ires.Value;

            PromptDoubleOptions pdo = new PromptDoubleOptions("\nEnter radius: ");
            pdo.AllowZero = false;
            pdo.AllowNegative = false;
            pdo.AllowNone = true;
            pdo.UseDefaultValue = true;
            pdo.DefaultValue = 3.0;

            PromptDoubleResult res;
            res = ed.GetDouble(pdo);
            if (res.Status != PromptStatus.OK) return;

            double rad = res.Value;

            double ang = Math.PI * 2 / n;
            Point3d cp, p1,p2;
            // Center point
            cp = new Point3d(0, 0,0);

            Polyline poly = new Polyline(n);
            Polyline poly2 = new Polyline(n);
            for (int th = 0; th < n; th++)
            {
                // Circumscribed
                p1 = PolarPoint(cp, th * ang, rad / Math.Cos(ang / 2)).TransformBy(Matrix3d.Identity);
                poly.AddVertexAt(th, new Point2d(p1.X, p1.Y), 0, 0, 0);
                // Inscribed
                 p2 = PolarPoint(cp, th * ang, rad).TransformBy(Matrix3d.Identity);
                poly2.AddVertexAt(th, new Point2d(p2.X, p2.Y), 0, 0, 0);
            }
            poly.Closed = true;
            poly.ColorIndex = 1;
            poly2.Closed = true;
            poly2.ColorIndex = 3;
            // Add circle for imagination only
            Circle circ = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, rad);
            circ.ColorIndex = 5;
            Transaction tr = doc.TransactionManager.StartTransaction();

            using (tr)
            {
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AppendEntity(poly);
                tr.AddNewlyCreatedDBObject(poly, true);
                btr.AppendEntity(poly2);
                tr.AddNewlyCreatedDBObject(poly2, true);
                btr.AppendEntity(circ);
                tr.AddNewlyCreatedDBObject(circ, true);
                tr.Commit();
            }
        }
      VB.NET
Code: [Select]
        ' 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

        <CommandMethod("preg", CommandFlags.UsePickSet)> _
        Public Sub testPolygonRegular()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim pio As New PromptIntegerOptions("")
            pio.Message = vbLf & "Number of sides: "

            ' Restrict input to positive and non-negative values
            pio.AllowZero = False
            pio.AllowNegative = False
            ' Add default value
            pio.DefaultValue = 3
            pio.AllowNone = True
            ' Get the value entered by the user
            Dim ires As PromptIntegerResult = ed.GetInteger(pio)
            If ires.Status <> PromptStatus.OK Then
                Return
            End If
            Dim n As Integer = ires.Value

            Dim pdo As New PromptDoubleOptions(vbLf & "Enter radius: ")
            pdo.AllowZero = False
            pdo.AllowNegative = False
            pdo.AllowNone = True
            pdo.UseDefaultValue = True
            pdo.DefaultValue = 3.0

            Dim res As PromptDoubleResult
            res = ed.GetDouble(pdo)
            If res.Status <> PromptStatus.OK Then
                Return
            End If

            Dim rad As Double = res.Value

            Dim ang As Double = Math.PI * 2 / n
            Dim cp As Point3d, p1 As Point3d, p2 As Point3d
            ' Center point
            cp = New Point3d(0, 0, 0)

            Dim poly As New Polyline(n)
            Dim poly2 As New Polyline(n)
            For th As Integer = 0 To n - 1
                ' Circumscribed
                p1 = PolarPoint(cp, th * ang, rad / Math.Cos(ang / 2)).TransformBy(Matrix3d.Identity)
                poly.AddVertexAt(th, New Point2d(p1.X, p1.Y), 0, 0, 0)
                ' Inscribed
                p2 = PolarPoint(cp, th * ang, rad).TransformBy(Matrix3d.Identity)
                poly2.AddVertexAt(th, New Point2d(p2.X, p2.Y), 0, 0, 0)
            Next
            poly.Closed = True
            poly.ColorIndex = 1
            poly2.Closed = True
            poly2.ColorIndex = 3
            ' Add circle for imagination only
            Dim circ As New Circle(New Point3d(0, 0, 0), Vector3d.ZAxis, rad)
            circ.ColorIndex = 5
            Dim tr As Transaction = doc.TransactionManager.StartTransaction()

            Using tr
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                btr.AppendEntity(poly)
                tr.AddNewlyCreatedDBObject(poly, True)
                btr.AppendEntity(poly2)
                tr.AddNewlyCreatedDBObject(poly2, True)
                btr.AppendEntity(circ)
                tr.AddNewlyCreatedDBObject(circ, True)
                tr.Commit()
            End Using
        End Sub