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