.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C# Offset multiple object types to both sides

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
c_attle
2850 Views, 4 Replies

C# Offset multiple object types to both sides

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(); } }

 

 

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: c_attle

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

Message 3 of 5
c_attle
in reply to: Anonymous

That did the trick! Thank you for your help!! Smiley Very Happy

Message 4 of 5
heavnhell
in reply to: c_attle

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;

 

Message 5 of 5
insmuratturna
in reply to: c_attle

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);
}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost