C3D-CURBOFF2 · Flip curb offsets to correct side done
Erases the 125 wrong-side offsets and redraws them on the opposite side. 0.5ft face + 2.0ft flowline unchanged.
C# payload
// Fix: the curb offsets went the WRONG WAY. Erase the offsets created by C3D-CURBOFF and
// redo them with the direction flipped - offset toward the BUILDING side, not away.
// Distances unchanged: 0.5' (top face of curb, close to TBC) and 2.0' (flowline).
// Identifies the offsets to erase by handle range (they are the newest entities on the layer).
// Host owns Tx/ModelSpace - no using/Commit here.
const string LAYER = "SS-V-ROAD-CURB";
const double MINLEN = 3.0, MAXLEN = 500.0;
// building centroid
double bx = 0, by = 0; int bn = 0;
foreach (ObjectId id in ModelSpace)
{
var e = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
if (e == null || e.Layer != "SS-V-BLDG-OTLN") continue;
var p = e as Polyline;
if (p == null) continue;
for (int i = 0; i < p.NumberOfVertices; i++) { var v = p.GetPoint2dAt(i); bx += v.X; by += v.Y; bn++; }
}
bx /= bn; by /= bn;
// collect all curb polylines, note the 125 newest = the bad offsets
var all = new List<(ObjectId id, long h, double len)>();
foreach (ObjectId id in ModelSpace)
{
var e = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
if (e == null || e.Layer != LAYER) continue;
var p = e as Polyline;
if (p == null) continue;
all.Add((id, System.Convert.ToInt64(e.Handle.ToString(), 16), p.Length));
}
all.Sort((a,b) => a.h.CompareTo(b.h));
Log($"{all.Count} Polylines on layer; erasing the {125} newest (the bad offsets).");
int erased = 0;
for (int i = all.Count - 125; i < all.Count; i++)
{
if (i < 0) continue;
var w = (Autodesk.AutoCAD.DatabaseServices.Entity)Tx.GetObject(all[i].id, OpenMode.ForWrite);
w.Erase();
erased++;
}
Log($"Erased {erased} bad offsets.");
// re-offset the remaining sources, FLIPPED
var srcs = new List<ObjectId>();
foreach (ObjectId id in ModelSpace)
{
var e = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
if (e == null || e.Layer != LAYER || e.IsErased) continue;
var p = e as Polyline;
if (p == null || p.Length < MINLEN || p.Length > MAXLEN) continue;
srcs.Add(id);
}
Log($"{srcs.Count} curb Polylines to re-offset.");
double DistFromBldg(Autodesk.AutoCAD.DatabaseServices.Entity e)
{
var p = e as Polyline;
if (p == null || p.NumberOfVertices == 0) return -1;
double mx = 0, my = 0;
for (int i = 0; i < p.NumberOfVertices; i++) { var v = p.GetPoint2dAt(i); mx += v.X; my += v.Y; }
mx /= p.NumberOfVertices; my /= p.NumberOfVertices;
return System.Math.Sqrt((mx-bx)*(mx-bx) + (my-by)*(my-by));
}
int made = 0, failed = 0;
foreach (var id in srcs)
{
var pl = (Polyline)Tx.GetObject(id, OpenMode.ForRead);
int sign = 0;
try
{
var pos = pl.GetOffsetCurves(0.5);
var neg = pl.GetOffsetCurves(-0.5);
double dp = pos.Count > 0 ? DistFromBldg((Autodesk.AutoCAD.DatabaseServices.Entity)pos[0]) : -1;
double dn = neg.Count > 0 ? DistFromBldg((Autodesk.AutoCAD.DatabaseServices.Entity)neg[0]) : -1;
if (dp < 0 && dn < 0) { failed++; continue; }
sign = (dp >= dn) ? -1 : 1; // FLIPPED from the previous run
}
catch { failed++; continue; }
foreach (double d in new double[] { 0.5, 2.0 })
{
try
{
var curves = pl.GetOffsetCurves(sign * d);
foreach (Autodesk.AutoCAD.DatabaseServices.Entity oe in curves)
{
oe.Layer = LAYER;
ModelSpace.AppendEntity(oe);
Tx.AddNewlyCreatedDBObject(oe, true);
made++;
}
}
catch { failed++; }
}
}
Log($"Created {made} offsets on the FLIPPED side (0.5' face + 2.0' flowline). {failed} failures.");
Result
Log
188 Polylines on layer; erasing the 125 newest (the bad offsets). Erased 125 bad offsets. 62 curb Polylines to re-offset. Created 126 offsets on the FLIPPED side (0.5' face + 2.0' flowline). 0 failures.
Notes
What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.
No notes yet.