public static void Offset3DPolyline_V2(Document doc, ObjectId polylineId, double offsetDistance)
{
// Start a transaction
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
try
{
// Open the polyline for reading
Polyline3d polyline = tr.GetObject(polylineId, OpenMode.ForRead) as Polyline3d;
// Verify that the entity is a 3D polyline
if (polyline != null && polyline.PolyType == Poly3dType.SimplePoly)
{
// Open the BlockTable for writing
BlockTable bt = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite) as BlockTable;
// Open the current space block (ModelSpace) for writing
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
var Plane = polyline.GetPlane();
var normal = Plane.Normal;
// Offset the object in the first direction
foreach (ACAD_DBS.Polyline3d acEnt in polyline.GetOffsetCurvesGivenPlaneNormal(normal, offsetDistance))
{
// Add each offset object
btr.AppendEntity(acEnt);
tr.AddNewlyCreatedDBObject(acEnt, true);
}
// Offset the object in the second direction (negative offset)
foreach (ACAD_DBS.Polyline3d acEnt in polyline.GetOffsetCurvesGivenPlaneNormal(normal, -offsetDistance))
{
// Add each offset object
btr.AppendEntity(acEnt);
tr.AddNewlyCreatedDBObject(acEnt, true);
}
}
else
{
doc.Editor.WriteMessage("Selected entity is not a valid 3D polyline.");
}
// Commit the transaction
tr.Commit();
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage("Error: " + ex.Message);
}
}
}