Author Topic: Total length and area of curves  (Read 591 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
Total length and area of curves
« on: December 12, 2012, 05:12:12 PM »
You can add other entity type to build selection,
here is 2 small programs, tested on A2010
Code: [Select]
    [CommandMethod("tot")]
       
        public void testTotalLength()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            double total = 0;

            Transaction tr = doc.TransactionManager.StartOpenCloseTransaction();

            using (tr)
            {

                TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "*LINE,ARC") };

                SelectionFilter flt = new SelectionFilter(tv);

                PromptSelectionResult res = ed.GetSelection(flt);

                if (res.Status != PromptStatus.OK) return;

                SelectionSet sset = res.Value;

                if (sset.Count == 0) return;

                foreach (SelectedObject sobj in sset)
                {
                    Entity ent = (Entity)tr.GetObject(sobj.ObjectId, OpenMode.ForRead);

                    Curve curv = ent as Curve;

                    if (curv != null)

                        total += curv.GetDistanceAtParameter(curv.EndParam);
                }
                ed.WriteMessage("\nTotal Length:\t{0:f3}\n",total);
            }
        }
      [CommandMethod("toa")]

      public void testTotalArea()
      {
          Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

          Database db = doc.Database;

          Editor ed = doc.Editor;

          double total = 0;

          Transaction tr = doc.TransactionManager.StartOpenCloseTransaction();

          using (tr)
          {
              // build filter to select closed curves and circles
              TypedValue[] tv = new TypedValue[]
              { new TypedValue(-4, "<or"),
                  new TypedValue(-4, "<and"),
                  new TypedValue(0, "*LINE"),
                  new TypedValue(70, 1), new TypedValue(-4, "and>"),
                  new TypedValue(0, "CIRCLE"),
                  new TypedValue(-4, "or>") };

              SelectionFilter flt = new SelectionFilter(tv);

              PromptSelectionResult res = ed.GetSelection(flt);

              if (res.Status != PromptStatus.OK) return;

              SelectionSet sset = res.Value;

              if (sset.Count == 0) return;

              foreach (SelectedObject sobj in sset)
              {
                  Entity ent = (Entity)tr.GetObject(sobj.ObjectId, OpenMode.ForRead);

                  Curve curv = ent as Curve;

                  if (curv != null)

                      total += curv.Area;
              }
              ed.WriteMessage("\nTotal Area:\t{0:f3}\n", total);
          }
      }