Author Topic: Display sum of selected dimensions  (Read 1184 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
Display sum of selected dimensions
« on: February 15, 2013, 09:30:41 PM »
Code: [Select]
        [CommandMethod("DisplayDimSum", "dds", CommandFlags.Modal | CommandFlags.UsePickSet)]
        public void SumOfDims()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            double sum = 0;
            TypedValue[] values = new TypedValue[] { new TypedValue(0, "DIMENSION") };
            SelectionFilter filter = new SelectionFilter(values);
            PromptSelectionOptions opts = new PromptSelectionOptions();

            opts.MessageForRemoval = "\nMust be a type of Dimension!";
            opts.MessageForAdding = "\nSelect dimensions to reset : ";
            opts.PrepareOptionalDetails = false;
            opts.SingleOnly = false;
            opts.SinglePickInSpace = false;
            opts.AllowDuplicates = true;
            PromptSelectionResult result = ed.GetSelection(opts, filter);
            if (result.Status != PromptStatus.OK) return;

            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    SelectionSet sset = result.Value;
                    foreach (SelectedObject selobj in sset)
                    {
                        DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForWrite, false) as DBObject;
                        Dimension dim = obj as Dimension;

                        if (dim != null)
                        {
                            sum += dim.Measurement;

                        }
                    }

                    tr.Commit();
                }
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(string.Format("Sum of dimensions = {0:f3}", sum));
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nProblem with selection dimensions.\n");
                ed.WriteMessage(ex.Message);
            }
        }
« Last Edit: February 15, 2013, 09:32:46 PM by fixo »