Author Topic: Count total length using LINQ  (Read 1635 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
Count total length using LINQ
« on: May 04, 2013, 08:44:36 PM »
   C#       
   
Code: [Select]
        //using System.Linq;
        [CommandMethod("TotalLengthCS")]
        public void testTotalLengthCS()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("\tTotal Length:\n------------------------------------");
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    //------------------------------------------------------------------
                    // Create filter object, this filter can have any types to select, i.e. curves
                    SelectionFilter filter = new SelectionFilter(new TypedValue[]
                    {
                         new TypedValue(0, "LINE,ARC,SPLINE,CIRCLE,ELLIPSE,*POLYLINE") /*, new TypedValue(410, "~Model"),//<-- paper space only//*/
                    }
                    );
                    // Select objects on screen
                    PromptSelectionResult psr = ed.GetSelection(filter);
                    // Check if selection is succeed
                    if (psr.Status != PromptStatus.OK) return;
                    // See here for more: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b        101 LINQ Samples Csharp
// Grouping objects by name with sum of lengths
                       var data =
                            from e in psr.Value.GetObjectIds()
                            let ln= e.GetObject(OpenMode.ForRead) as Curve
                            group ln by ln.GetRXClass().DxfName into g
                            select new { ObjectName = g.Key, Subtotal = g.Sum(x => Math.Abs(x.GetDistanceAtParameter(x.EndParam - x.StartParam))) };

                    foreach (var n in data)
                    {
                        sb.AppendLine(string.Format("{0}\t{1:f6}", n.ObjectName, n.Subtotal));
                    }
                    System.Windows.Forms.MessageBox.Show(sb.ToString());
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());

            }
        }

   VB.NET
Code: [Select]
        ''Imports System.Linq
        <CommandMethod("TotalLengthVB")> _
        Public Sub testTotalLengthVB()
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
            Dim sb As New StringBuilder
            sb.AppendLine(vbTab + "Total Length:" + vbTab + "------------------------------------")
            Using tr As Transaction = db.TransactionManager.StartTransaction
                Dim tv() As TypedValue = {New TypedValue(0, "LINE,ARC,SPLINE,CIRCLE,ELLIPSE,*POLYLINE")}
                Dim sf As New SelectionFilter(tv)
                Dim psr As PromptSelectionResult = ed.GetSelection(sf)
                If psr.Status <> PromptStatus.OK Then Return

                ''  See here for more: http://msdn.microsoft.com/en-us/vstudio/bb688088.aspx        101 LINQ Samples VB.NET
                ''  Grouping objects by name with sum of lengths
                Dim subtotals = From id In psr.Value.GetObjectIds _
             Let curve = CType(tr.GetObject(id, OpenMode.ForRead), Curve) _
             Group curve By curve.GetRXClass().DxfName Into grp = Group _
             Select ObjectName = grp.First, Subtotal = grp.Sum(Function(x) Math.Abs(x.GetDistanceAtParameter(x.EndParam - x.StartParam)))

                For Each n In subtotals
                    sb.AppendLine(String.Format("{0}" + vbTab + "{1:f6}", n.ObjectName, n.Subtotal))
                Next

                System.Windows.Forms.MessageBox.Show(sb.ToString())
            End Using
        End Sub