Author Topic: ( Challenge ) Shortest & Longest Pline  (Read 13507 times)

0 Members and 1 Guest are viewing this topic.

LE3

  • Guest
Re: ( Challenge ) Shortest & Longest Pline
« Reply #45 on: February 07, 2013, 11:35:15 AM »
Here it is the C# solution and the dll in case anyone wants to run the test for this command.

Note, it does a filter to ignore some layers.

Command name: SLP

Quote
Command: slp
Elapsed=68msecs. 79msecs // here it is returning also the timing using the system variable millisecs- value on the right.

Code: [Select]
        [CommandMethod("SLP")]
        public void cmd_shortestLongestPlinesOnLayer()
        {
            var e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            using (var tr = e.Document.Database.TransactionManager.StartTransaction())
            {
                var layerTable = tr.GetObject(e.Document.Database.LayerTableId, OpenMode.ForRead) as LayerTable;
                var sw = Stopwatch.StartNew();
                var st = (int)AcadApp.GetSystemVariable("millisecs");
                foreach (var layerId in layerTable)
                {
                    var layer = tr.GetObject(layerId, OpenMode.ForRead) as LayerTableRecord;
                    if (layer.Name.Equals("Defpoints") || layer.IsFrozen || layer.IsOff || layer.Name.Contains("|"))
                        continue;
                    var pairs = new Dictionary<double, Polyline3d>();
                    PromptSelectionResult psr;
                    if (SelectionOfPLines(e, layer.Name, out psr)) continue;
                    foreach (var id in psr.Value.GetObjectIds())
                    {
                        var pline = tr.GetObject(id, OpenMode.ForWrite, false) as Polyline3d;
                        if (pline.Layer != layer.Name) continue;
                        pairs.Add(pline.Length, pline);
                        pline.Erase(true);
                    }
                    psr.Value.Dispose();
                    if (pairs.Count <= 0) continue;
                    var dict = pairs.Keys;
                    var maxLine = dict.Max();
                    var minLine = dict.Min();
                    var ll = pairs[maxLine];
                    var sl = pairs[minLine];
                    ll.Erase(false);
                    sl.Erase(false);
                    pairs.Clear();
                }
                sw.Stop();
                var et = (int)AcadApp.GetSystemVariable("millisecs");
                e.WriteMessage("\nElapsed={0}msecs. {1}msecs \n", sw.ElapsedMilliseconds, (et - st).ToString());
                tr.Commit();
            }
        }

        private static bool SelectionOfPLines(Editor e, string layerName, out PromptSelectionResult psr)
        {
            TypedValue[] tv = { new TypedValue((int)DxfCode.Start, "POLYLINE"), new TypedValue((int)DxfCode.LayerName, layerName) };
            var filter = new SelectionFilter(tv);
            psr = e.SelectAll(filter);
            return psr.Status != PromptStatus.OK;
        }