Hi everybody,
i coded this simple utility that allows the user to edit some dbtext without using the mouse (after selection).
It works but sometimes, it seems randomly, i get a fatal error.
This is the code
public void Run(object param)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "\nSelect texts";
pso.AllowDuplicates = false;
pso.AllowSubSelections = false;
SelectionFilter sf = new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "TEXT") });
PromptSelectionResult psr = ed.GetSelection(pso, sf);
if (psr.Status != PromptStatus.OK)
return;
foreach (SelectedObject so in psr.Value)
{
ed.SetImpliedSelection(new ObjectId[] { so.ObjectId });
executeSyncro("_textedit ");
}
ed.Regen();
}
void executeSyncro(string command)
{
AcadApplication app = (AcadApplication)Application.AcadApplication;
app.ActiveDocument.SendCommand(command);
}
The run function it's called from a commandmethod with the CommandFlags.Session flag.
Where's the problem?
Thanks
We have a technology inside of AutoCAD called fibers. This technology is used to handle our command line interface within the MDI, here's an example:
Open 2 documents, in document 1, start the line command, switch to document 2 (no command running) start the circle command, now switch back to document 1 (line command is still running) - very cool. What is happening is, the fiber technology is basically swapping the call stack in and out for us.
Now, Microsoft has stopped supporting fibers, most likely because fibers cause havoc with .NET, that's why we are removing fibers from AutoCAD.
Wherever we have .NET touching fibers, and where we have not coded defensively for that scenario, we have issues. For instance, you are all aware of the Visual Studio Debugger not hitting break points for .NET code right? Well, that's fibers rearing their ugly head again.
So, your code: starts a command from the session context (application context) which requires document command line input, which in turn calls the ActiveX API to pumps a command to the command line asynchronously from the while your .NET application is running - you a not just touching fibers from .NET but poking it!!
Try this:
1) Create the command as Modal (Document context)
2) PInvoke acedCmd() - http://adndevblog.typepad.com/autocad/2012/04/synchronously-send-and-wait-for-commands-in-autocad-us...
Hi,
Instead of P/Invoking acedCommand, you can use you can use the wrapper for the Editor.RunCommand() non-public method shared by Tony Tanzillo here:
http://www.theswamp.org/index.php?topic=43113.msg483306#msg483306
It defines a Command() extension method for the Editor class which accepts managed types arguments.
Using example assuming ed is the active document Editor and id the ObjectId of the entity to move (where "" (empty string) stands for Enter and "\\" for a pause for user input):
ed.Command("_move", id, "", Point3d.Origin, "\\");