Data extraction by layer

Data extraction by layer

Anonymous
Not applicable
2,023 Views
5 Replies
Message 1 of 6

Data extraction by layer

Anonymous
Not applicable

Hi, I just discovered that I can extract data from autocad and that is making my work easier. There is just one thing I wish I could do to fully parameterize the extraction. I'm trying to add the lenghts of all the polylines that exist in each layer. I can extract all of them and sort them by layer, but I haven't figured out a way to automatically add them to get the total lenght by layer. Is there a way that I can do this?

Thank you!

0 Likes
2,024 Views
5 Replies
Replies (5)
Message 2 of 6

SENL1362
Advisor
Advisor
create dictionary <string, double>. key==layerName, value += each poly length
after collecting each length write the total length for each layer.
You can print the grand total using myDict.Values.Sum().

0 Likes
Message 3 of 6

SENL1362
Advisor
Advisor
                var plLayers = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase);
                using (Transaction tr=db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord ms=(BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),OpenMode.ForRead);
                    foreach (ObjectId id in ms)
                    {
                        if (id.ObjectClass.DxfName == "LWPOLYLINE")
                        {
                            var pl = tr.GetObject(id, OpenMode.ForRead) as Polyline;
                            if (pl != null)
                            {
                                if (!plLayers.ContainsKey(pl.Layer))
                                    plLayers.Add(pl.Layer, 0);
                                plLayers[pl.Layer] += pl.Length;
                            }
                        }
                    }
                    tr.Commit();
                }
                if (plLayers.Count > 0)
                {
                    foreach (var kvp in plLayers.OrderBy(kvp => kvp.Key))
                        ed.WriteMessage("\n {0}=={1}", kvp.Key, kvp.Value);
                    ed.WriteMessage("\n Total Length== {0}", plLayers.Values.Sum());
                }

 

 

 

0 Likes
Message 4 of 6

_gile
Consultant
Consultant

Hi,

 

If you prefer a more functional / declarative way of writing using Linq:

 

        public Dictionary<string, double> GetPlineLengthsbyLayer(Database db)
        {
            RXClass plineclass = RXClass.GetClass(typeof(Polyline));
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                return
                    ((BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead))
                    .Cast<ObjectId>()
                    .Where(id => id.ObjectClass == plineclass)
                    .Select(id => (Polyline)tr.GetObject(id, OpenMode.ForRead))
                    .GroupBy(pl => pl.Layer)
                    .ToDictionary(grp => grp.Key, grp => grp.Select(pl => pl.Length).Sum());
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 6

SENL1362
Advisor
Advisor
Nice Gile
0 Likes
Message 6 of 6

Anonymous
Not applicable
thank you!
0 Likes