← Inbox

C3D-0636 · Clip flood/stream layers to SS-FLOODBOX done

Layers FLOOD-2013-EXIST-100YR, FLOOD-2013-FUTURE-100YR, FLOOD-2013-XSEC, NWI-WETLANDS, SS-STREAM CL, SS-STREAM BUFFER 75: add copies trimmed to the SS-FLOODBOX polyline (24383) on the same layers; move the full originals to new frozen <layer>-FULL layers. Text untouched. Reversible: thaw the -FULL layers.

Script file
C3D-0636.cs
Target
260714 - 285 Huckaby Rd 22x17.dwg · model space
Author
claude
Approved
yes · auto-auth
Timeout
60s

C# payload

var LAYS=new[]{"FLOOD-2013-EXIST-100YR","FLOOD-2013-FUTURE-100YR","FLOOD-2013-XSEC","NWI-WETLANDS","SS-STREAM CL","SS-STREAM BUFFER 75"};
ObjectId bid; Db.TryGetObjectId(new Handle(Convert.ToInt64("24383",16)),out bid);
var box=(Polyline)Tx.GetObject(bid,OpenMode.ForRead);
Log("box h=24383 layer="+box.Layer+" closed="+box.Closed+" nv="+box.NumberOfVertices+" area="+box.Area.ToString("F0"));
if(!box.Closed && box.GetPoint2dAt(0).GetDistanceTo(box.GetPoint2dAt(box.NumberOfVertices-1))>0.01){ Log("STOP - floodbox is not closed"); return; }
var poly=new List<Point2d>(); for(int i=0;i<box.NumberOfVertices;i++) poly.Add(box.GetPoint2dAt(i));
Func<Point2d,bool> inside=(p)=>{ bool c=false; for(int i=0,j=poly.Count-1;i<poly.Count;j=i++){ var a=poly[i]; var b=poly[j]; if(((a.Y>p.Y)!=(b.Y>p.Y)) && (p.X < (b.X-a.X)*(p.Y-a.Y)/(b.Y-a.Y)+a.X)) c=!c; } return c; };
var lt=(LayerTable)Tx.GetObject(Db.LayerTableId,OpenMode.ForRead);
var ms=(BlockTableRecord)Tx.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(Db),OpenMode.ForWrite);
// idempotency
foreach(var L in LAYS) if(lt.Has(L+"-FULL")){ Log("SKIP - "+L+"-FULL already exists (already clipped)"); return; }
// full layers, frozen, same colour/linetype
var full=new Dictionary<string,ObjectId>();
foreach(var L in LAYS){ if(!lt.Has(L)){ Log("no layer "+L); continue; } var src=(LayerTableRecord)Tx.GetObject(lt[L],OpenMode.ForRead);
  lt.UpgradeOpen(); var r=new LayerTableRecord{Name=L+"-FULL",Color=src.Color,LinetypeObjectId=src.LinetypeObjectId,LineWeight=src.LineWeight}; full[L]=lt.Add(r); Tx.AddNewlyCreatedDBObject(r,true); }
var targets=new List<Polyline>();
foreach(ObjectId id in ms){ var pl=Tx.GetObject(id,OpenMode.ForRead) as Polyline; if(pl!=null && LAYS.Contains(pl.Layer) && pl.ObjectId!=bid) targets.Add(pl); }
var stats=new SortedDictionary<string,int[]>(); // layer -> {originals, pieces kept}
foreach(var pl in targets){
  string L=pl.Layer; if(!stats.ContainsKey(L)) stats[L]=new int[2]; stats[L][0]++;
  var pts=new Point3dCollection(); pl.IntersectWith(box,Intersect.OnBothOperands,new Plane(),pts,IntPtr.Zero,IntPtr.Zero);
  var prms=new List<double>();
  foreach(Point3d p in pts){ var q=pl.GetClosestPointTo(p,Vector3d.ZAxis,false); double t=pl.GetParameterAtPoint(q); if(!prms.Any(x=>Math.Abs(x-t)<1e-7)) prms.Add(t); }
  prms.Sort();
  var pieces=new List<Autodesk.AutoCAD.DatabaseServices.Entity>();
  if(prms.Count==0){ var mid=pl.GetPointAtParameter((pl.StartParam+pl.EndParam)/2); if(inside(new Point2d(mid.X,mid.Y))) pieces.Add((Autodesk.AutoCAD.DatabaseServices.Entity)pl.Clone()); }
  else { var dc=pl.GetSplitCurves(new DoubleCollection(prms.ToArray()));
    foreach(Autodesk.AutoCAD.DatabaseServices.DBObject o in dc){ var c=o as Curve; if(c==null){ o.Dispose(); continue; }
      double len=0; try{ len=c.GetDistanceAtParameter(c.EndParam); }catch{}
      var m=c.GetPointAtDist(len/2); if(len>0.01 && inside(new Point2d(m.X,m.Y))) pieces.Add(c); else c.Dispose(); } }
  foreach(var e in pieces){ e.Layer=L; ms.AppendEntity(e); Tx.AddNewlyCreatedDBObject(e,true); stats[L][1]++; }
  pl.UpgradeOpen(); pl.LayerId=full[L];
}
foreach(var kv in full){ var r=(LayerTableRecord)Tx.GetObject(kv.Value,OpenMode.ForWrite); r.IsFrozen=true; }
foreach(var kv in stats) Log(kv.Key+": "+kv.Value[0]+" originals -> "+kv.Key+"-FULL (frozen); "+kv.Value[1]+" trimmed pieces kept inside the box");
int chk=0; foreach(ObjectId id in ms){ var pl=Tx.GetObject(id,OpenMode.ForRead) as Polyline; if(pl!=null && LAYS.Contains(pl.Layer) && pl.ObjectId!=bid){ var m=pl.GetPointAtDist(pl.Length/2); if(!inside(new Point2d(m.X,m.Y))) chk++; } }
Log("check: pieces on the visible layers whose midpoint is outside the box = "+chk+(chk==0?" (verified)":" CHECK"));
Ed.Regen();

Result

Status
success
Drawing file
C:\Users\sanja\OneDrive\_SurveyDisco\260714 - 285 Huckaby Rd, Brooks, GA 30205, USA\260714 - 285 Huckaby Rd 22x17.dwg
Message
Executed C3D-0636 (0 entities touched).
Entities
—
Duration
555 ms
Civil 3D
25.0.0.0

Log

box h=24383 layer=SS-FLOODBOX closed=False nv=6 area=483423
FLOOD-2013-EXIST-100YR: 6 originals -> FLOOD-2013-EXIST-100YR-FULL (frozen); 3 trimmed pieces kept inside the box
FLOOD-2013-FUTURE-100YR: 6 originals -> FLOOD-2013-FUTURE-100YR-FULL (frozen); 4 trimmed pieces kept inside the box
FLOOD-2013-XSEC: 32 originals -> FLOOD-2013-XSEC-FULL (frozen); 4 trimmed pieces kept inside the box
NWI-WETLANDS: 6 originals -> NWI-WETLANDS-FULL (frozen); 2 trimmed pieces kept inside the box
SS-STREAM BUFFER 75: 3 originals -> SS-STREAM BUFFER 75-FULL (frozen); 3 trimmed pieces kept inside the box
SS-STREAM CL: 1 originals -> SS-STREAM CL-FULL (frozen); 1 trimmed pieces kept inside the box
check: pieces on the visible layers whose midpoint is outside the box = 0 (verified)

Notes

What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.

No notes yet.