Author Topic: Cumulative and total sum of the distances indicated with selected points  (Read 3226 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
Cumulative and total sum of the distances indicated with selected points
Code: [Select]
        <CommandMethod("cud")> _
        Public Shared Sub CumulDist()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim db As Database = doc.Database

            Dim points As New Point3dCollection()

            Dim ed As Editor = doc.Editor

            Dim ptres2 As PromptPointResult
            Dim lst As List(Of Point3d)
            Dim cd As Double = 0
            Dim pto1 As New PromptPointOptions(vbLf & "Select first point, or press Enter to close: ")

            Dim ptres1 As PromptPointResult = ed.GetPoint(pto1)

            If ptres1.Status = PromptStatus.OK Then
                lst = New List(Of Point3d)
                Dim basept As Point3d = ptres1.Value
                lst.Add(basept)
                points.Add(basept)

                Do
                    Dim pto2 As New PromptPointOptions(vbLf & "Next point or [Enter to exit]: ", "Enter")

                    pto2.Keywords.[Default] = "Enter"

                    pto2.BasePoint = basept

                    pto2.UseBasePoint = True

                    ptres2 = ed.GetPoint(pto2)

                    basept = ptres2.Value

                    If ptres2.Status <> PromptStatus.Keyword Then
                        lst.Add(basept)
                        points.Add(basept)
                    End If
                   
                    If lst.Count = 2 Then
                        Dim td As Double = lst(0).DistanceTo(lst(1))
                        cd += td
                        ed.WriteMessage("Latest distance = {0:f3};  Cumulative distance = {1:f3}", td, cd)
                        lst.RemoveAt(0)
                    End If
                Loop While ptres2.Status <> PromptStatus.Keyword
                ed.WriteMessage("Total distance = {0:f3}", cd)

            End If
        End Sub

C#
Code: [Select]
        [CommandMethod("cud")]
        public static void CumulDist()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Point3dCollection points = new Point3dCollection();

            Editor ed = doc.Editor;

            PromptPointResult ptres2 = default(PromptPointResult);

            List<Point3d> lst = default(List<Point3d>);

            double cd = 0;

            PromptPointOptions pto1 = new PromptPointOptions("\nSelect first point, or press Enter to close: ");

            PromptPointResult ptres1 = ed.GetPoint(pto1);

            if (ptres1.Status == PromptStatus.OK)
            {
                lst = new List<Point3d>();

                Point3d basept = ptres1.Value;

                lst.Add(basept);

                points.Add(basept);

                do
                {
                    PromptPointOptions pto2 = new PromptPointOptions("\nNext point or [Enter to exit]: ", "Enter");

                    pto2.Keywords.Default = "Enter";

                    pto2.BasePoint = basept;

                    pto2.UseBasePoint = true;

                    ptres2 = ed.GetPoint(pto2);

                    basept = ptres2.Value;

                    if (ptres2.Status != PromptStatus.Keyword)
                    {
                        lst.Add(basept);
                        points.Add(basept);
                    }

                    if (lst.Count == 2)
                    {
                        double td = lst[0].DistanceTo(lst[1]);
                        cd += td;
                        ed.WriteMessage("\nLatest distance = {0:f3};  Cumulative distance = {1:f3}", td, cd);
                        lst.RemoveAt(0);
                    }
                } while (ptres2.Status != PromptStatus.Keyword);
                ed.WriteMessage("\nTotal distance = {0:f3}", cd);
            }
        }