.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Data extraction by layer

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
1437 Views, 5 Replies

Data extraction by layer

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!

5 REPLIES 5
Message 2 of 6
SENL1362
in reply to: Anonymous

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().

Message 3 of 6
SENL1362
in reply to: SENL1362

                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());
                }

 

 

 

Message 4 of 6
_gile
in reply to: SENL1362

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
in reply to: _gile

Nice Gile
Message 6 of 6
Anonymous
in reply to: _gile

thank you!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost