I'm trying to finish this program that will offset multiple object types to both sides of the line, polyline, etc. It currently works for only one object type. I want it to allow all of the normal object types like the regular offset command. Any help would be greatly appreciated.
[CommandMethod("MLO")] public void MapLinesOffset() { Document doc = acApp.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { PromptEntityOptions peOpt = new PromptEntityOptions("\nSelect object: "); peOpt.AllowNone = false; peOpt.SetRejectMessage("\nNo object selected"); peOpt.AddAllowedClass(typeof(Polyline), true); // The next lines are to allow for other objects
// THESE ARE THE OBJECTS IM TRYING TO ADD
peOpt.AddAllowedClass(typeof(Circle), true); peOpt.AddAllowedClass(typeof(Line), true); peOpt.AddAllowedClass(typeof(Polyline2d), true); peOpt.AddAllowedClass(typeof(Polyline3d), true); peOpt.AddAllowedClass(typeof(Ellipse), true); peOpt.AddAllowedClass(typeof(Spline), true); PromptEntityResult peRes = ed.GetEntity(peOpt); if (peRes.Status == PromptStatus.OK) { PromptDoubleOptions pdOpt = new PromptDoubleOptions("\nSpecify offset distance: "); pdOpt.AllowNegative = false; pdOpt.AllowNone = false; pdOpt.AllowZero = false; PromptDoubleResult pdRes = ed.GetDouble(pdOpt); if (pdRes.Status == PromptStatus.OK) { Entity selEnt = (Entity)peRes.ObjectId.GetObject( OpenMode.ForRead); BlockTable bt = (BlockTable)db.BlockTableId.GetObject( OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject( db.CurrentSpaceId, OpenMode.ForWrite); Line acLine = (Line)selEnt; // Offset the object in the first direction foreach (Entity acEnt in acLine.GetOffsetCurves(pdRes.Value)) { // Add each offset object btr.AppendEntity(acEnt); tr.AddNewlyCreatedDBObject(acEnt, true); } // Now offset the object in the second direction foreach (Entity acEnt in acLine.GetOffsetCurves(-pdRes.Value)) { // Add each offset object btr.AppendEntity(acEnt); tr.AddNewlyCreatedDBObject(acEnt, true); } } } tr.Commit(); } }
Solved! Go to Solution.
Try casting to a curve instead of an line.
Set the second argument of AddAllowedClass to false so it does not have be an exact match
Thanks for your question, I just need it. And I suggest that you can modify as below:
1.Add Arc Case
peOpt.AddAllowedClass(typeof(Arc), true);
2.Change the type to base type
Line acLine = (Line)selEnt;
var acLine = selEnt as curve;
my solution :
ent = (Entity)tr.GetObject(id, OpenMode.ForWrite, true); if (ent.Id.ObjectClass.DxfName == "LWPOLYLINE" || ent.Id.ObjectClass.DxfName == "POLYLINE") { entc = ent.Clone() as Entity; Polyline km = (Polyline)entc; foreach (Entity ntc in km.GetOffsetCurves( value)) { ecs = ntc.GeometricExtents; btr.AppendEntity(ntc); tr.AddNewlyCreatedDBObject(ntc, true); } } else if (ent.Id.ObjectClass.DxfName == "CIRCLE") { Circle entt = (Circle)tr.GetObject(id, OpenMode.ForWrite, true); Circle enta = entt.Clone() as Circle; btr.AppendEntity(enta); tr.AddNewlyCreatedDBObject(enta, true); }