C3DO-0843 · 261103 legend: drop rows not on the drawing, add UG UNDERGROUND + CENTERLINE [C3D-LGEDIT2] done
11X17 legend (MText 2191D): removes POC, IPP, PP, CTP, FF, SBUC and the DROP INLET / FIRE HYDRANT / ELEVATION POINT / GAS METER / POWER POLE rows (their icons erased), adds UG UNDERGROUND after WM and a CENTERLINE sample line on SS-V-ROAD-CNTR; every remaining icon slid to stay on its row.
C# payload
string F(double v)=>v.ToString("0.###");
ObjectId H(string h)=>Db.GetObjectId(false,new Handle(Convert.ToInt64(h,16)),0);
ObjectId Lyr(string name,short c){ var lt=(LayerTable)Tx.GetObject(Db.LayerTableId,OpenMode.ForRead); if(lt.Has(name)) return lt[name]; lt.UpgradeOpen(); var r=new LayerTableRecord{Name=name,Color=Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci,c)}; var id=lt.Add(r); Tx.AddNewlyCreatedDBObject(r,true); Log("layer created "+name); return id; }
ObjectId Put(BlockTableRecord btr, Autodesk.AutoCAD.DatabaseServices.Entity e, string layer){ e.SetDatabaseDefaults(Db); e.Layer=layer; var id=btr.AppendEntity(e); Tx.AddNewlyCreatedDBObject(e,true); return id; }
var LM=LayoutManager.Current;
BlockTableRecord PS(string name){ var l=(Layout)Tx.GetObject(LM.GetLayoutId(name),OpenMode.ForRead); return (BlockTableRecord)Tx.GetObject(l.BlockTableRecordId,OpenMode.ForWrite); }
var BT=(BlockTable)Tx.GetObject(Db.BlockTableId,OpenMode.ForRead);
var MS=(BlockTableRecord)Tx.GetObject(BT[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
bool HasText(BlockTableRecord btr,string contains){ foreach(ObjectId id in btr){ var o=Tx.GetObject(id,OpenMode.ForRead); if(o is MText m && m.Contents.Contains(contains)) return true; if(o is DBText t && t.TextString.Contains(contains)) return true; } return false; }
// 261103 legend (11X17, MText 2191D): drop rows not on the drawing, add UG UNDERGROUND and a CENTERLINE sample.
// Icons are loose entities aligned to the MText rows: each is matched to its row by Y, then slid by that row's move.
var mt=(MText)Tx.GetObject(H("2191D"),OpenMode.ForWrite);
var ps=(BlockTableRecord)Tx.GetObject(mt.OwnerId,OpenMode.ForWrite);
Func<MText,Dictionary<string,double>> lines=m=>{ var d=new Dictionary<string,double>(); var c=new DBObjectCollection(); m.Explode(c); foreach(Autodesk.AutoCAD.DatabaseServices.DBObject o in c){ if(o is DBText t){ var k=t.TextString.Trim(); if(k!=""&&!d.ContainsKey(k)) d[k]=t.Position.Y; } o.Dispose(); } return d; };
var before=lines(mt);
if(!before.ContainsKey("BOUNDARY LINE")){ Log("STOP - BOUNDARY LINE row not found: "+string.Join(" | ",before.Keys)); return; }
// sample-line offset above the baseline, from the existing boundary sample (C-PROP-LINE line 2193F)
var bl=(Line)Tx.GetObject(H("2193F"),OpenMode.ForRead); double off=bl.StartPoint.Y-before["BOUNDARY LINE"];
var rows=mt.Contents.Split(new[]{@"\P"},StringSplitOptions.None).ToList();
string[] dropAbbr={"POC ","IPP ","PP ","CTP ","FF ","SBUC "}; string[] dropIcon={"DROP INLET","FIRE HYDRANT","ELEVATION POINT","GAS METER","POWER POLE"};
if(rows.Any(r=>r.Trim()=="CENTERLINE")){ Log("CENTERLINE row already there - nothing done"); Ed.Regen(); return; }
var iconRows=rows.Where(r=>r.StartsWith(" ")).Select(r=>r.Trim()).Where(k=>before.ContainsKey(k)).ToList();
// match every legend icon piece (left strip under the text, not the north arrow) to its row
var pieces=new List<(Autodesk.AutoCAD.DatabaseServices.Entity e,string row)>();
foreach(ObjectId id in ps){ if(id==mt.ObjectId) continue; var e=Tx.GetObject(id,OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity; if(e==null||e is Viewport) continue;
if(e.Layer=="BLOCK_NORTH_ARROW") continue; Extents3d x; try{ x=e.GeometricExtents; }catch{ continue; }
if(x.MinPoint.X<0.40||x.MaxPoint.X>0.75) continue; double cy=(x.MinPoint.Y+x.MaxPoint.Y)/2;
string best=null; double bd=9; foreach(var k in iconRows){ double dd=Math.Abs(cy-(before[k]+off)); if(dd<bd){bd=dd;best=k;} }
if(best==null||bd>0.09){ continue; }
pieces.Add((e,best)); }
foreach(var g in pieces.GroupBy(p=>p.row)) Log("icon row '"+g.Key+"': "+g.Count()+" pieces");
var unmatched=iconRows.Where(k=>!pieces.Any(p=>p.row==k)).ToList(); if(unmatched.Count>0) Log("icon rows with no pieces found: "+string.Join(", ",unmatched));
// new rows
var kept=new List<string>(); var dropped=new List<string>();
foreach(var r in rows){ var t=r.Trim(); if(dropAbbr.Any(a=>t.StartsWith(a))||dropIcon.Contains(t)){ dropped.Add(t); continue; } kept.Add(r); if(t.StartsWith("WM ")) kept.Add("UG UNDERGROUND"); }
if(!kept.Contains("UG UNDERGROUND")) kept.Insert(kept.FindLastIndex(r=>!r.StartsWith(" "))+1,"UG UNDERGROUND");
kept.Add(" CENTERLINE");
mt.Contents=string.Join(@"\P",kept);
var after=lines(mt);
int moved=0, erased=0;
foreach(var p in pieces){ p.e.UpgradeOpen();
if(dropIcon.Contains(p.row)){ p.e.Erase(); erased++; continue; }
if(!after.ContainsKey(p.row)){ Log("row lost after edit: "+p.row); continue; }
p.e.TransformBy(Matrix3d.Displacement(new Vector3d(0,after[p.row]-before[p.row],0))); moved++; }
// centerline sample, same length and offset as the boundary sample
var cl=new Line(new Point3d(bl.StartPoint.X,after["CENTERLINE"]+off,0),new Point3d(bl.EndPoint.X,after["CENTERLINE"]+off,0));
Put(ps,cl,"SS-V-ROAD-CNTR");
Log("dropped rows: "+string.Join(", ",dropped));
Log("added: UG UNDERGROUND (after WM), CENTERLINE sample on SS-V-ROAD-CNTR ("+cl.Handle+")");
Log("icon pieces moved "+moved+", erased "+erased+" (dropped rows)");
var chk=lines(mt); foreach(var p in pieces.Where(q=>!q.e.IsErased).GroupBy(q=>q.row).Select(g=>g.First())){ var x=p.e.GeometricExtents; Log("check '"+p.row+"' icon cy "+F((x.MinPoint.Y+x.MaxPoint.Y)/2)+" vs row+off "+F(chk[p.row]+off)); }
Ed.Regen();
Result
Log
icon row 'WATER VALVE': 1 pieces icon row 'DROP INLET': 1 pieces icon row 'SEWER MANHOLE': 1 pieces icon row 'FIRE HYDRANT': 1 pieces icon row 'ELEVATION POINT': 1 pieces icon row 'PROPERTY CORNER': 2 pieces icon row 'FENCE': 24 pieces icon row 'POWER POLE': 1 pieces icon row 'GAS METER': 1 pieces icon row 'BOUNDARY LINE': 1 pieces icon row 'ADJOINER LINE': 1 pieces dropped rows: IPP IRON PIN PROPOSED, POC POINT OF COMMENCEMENT, PP POWER POLE, CTP CRIMP TOP PIPE, FF FINISHED FLOOR, SBUC SOUTHERN BELL UNDERGROUND CABLE, DROP INLET, FIRE HYDRANT, ELEVATION POINT, POWER POLE, GAS METER added: UG UNDERGROUND (after WM), CENTERLINE sample on SS-V-ROAD-CNTR (2220A) icon pieces moved 30, erased 5 (dropped rows) check 'WATER VALVE' icon cy 13.669 vs row+off 13.667 check 'SEWER MANHOLE' icon cy 13.509 vs row+off 13.508 check 'PROPERTY CORNER' icon cy 13.356 vs row+off 13.349 check 'FENCE' icon cy 13.252 vs row+off 13.191 check 'BOUNDARY LINE' icon cy 13.032 vs row+off 13.032 check 'ADJOINER LINE' icon cy 12.873 vs row+off 12.873
Notes
What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.
No notes yet.