Editor.SendCommand() - Force into document context

Editor.SendCommand() - Force into document context

jhdempsey
Advocate Advocate
888 Views
2 Replies
Message 1 of 3

Editor.SendCommand() - Force into document context

jhdempsey
Advocate
Advocate

We have an external tool that uses a pipe to communicate with AutoCAD

When I click something in that external tool, we get it to run a function in our AutoCAD plugin

 

However, when i try to run the ed.SendCommand() method, i get the "eInvalidInput" error

I know this error is related to not being in the document context, and is usually because the CommandFlags.Session has been set. But in my case, this isn't set (my method isn't even defined as an AutoCAD command), but I'm assuming that because of the way the function has been called, I'm in the application mode by default?

 

Ive tried Giles suggestion here, but that doesn't seem to bring me back into the document context

https://forums.autodesk.com/t5/net/switching-contexts/m-p/7328422/highlight/true#M54887

 

I would prefer to get this method working rather than use SendStringToExecute() if possible

 

Anything else I'm missing?

public static void PlaceBlock(string filepath, string blockName)
{
	try
	{
		var doc = Application.DocumentManager.MdiActiveDocument;
		Application.MainWindow.Focus();
		using(doc.LockDocument())
		{
			var ed = doc.Editor;
			ed.Command("-insert", @"C:\path\to\block\I_Furn_Misc_Pingpong.dwg");
		}
	}
	catch(System.Exception ex)
	{
		MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
	}
}

 

 

0 Likes
Accepted solutions (1)
889 Views
2 Replies
Replies (2)
Message 2 of 3

Norman_Yuan
Mentor
Mentor
Accepted solution

In spite I am not sure why/how your external app makes the call to the PlaceBlock() method in your .NET API plugin, I am sure you cannot use Editor.Command() if the call is not inside a CommandMethod in document context. 

If you have to use external app to run code in .NET plugin, the external app really should only call a command with SendStringToExecute(), and you can handle CommandEnded/Cancelled event in order to know the command execution is completed.

 

While direct calling methods in a .NET plugin, like your PlaceBlock(), from your external app's "pipe" is not very good practice, yes, it can be done in certain scenario, but just no Editor.Command(). If the PlaceBlock() is indeed what your business needs, you really should just write your own code to insert a block reference, rather than rely on Editor.Command().

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

jhdempsey
Advocate
Advocate

I did originally write code to insert a block through the .net api, but given our external app is essentially a tool palette, once the user has selected a block to insert, we want the behaviour to be as per how a user would normally insert a block, hence it seems easier to simply call the -INSERT command rather than write code which replicated it.

 

There is code in our autocad plugin that is listening out on the pipe for any incoming instructions. When it receives one, that listener function takes the information in the pipe and passes it into the PlaceBlock function.

 

I have now amended my code to use the SendStringToExecute() method. I just need to figure out how to deal with a few situations, but i’ll look round the forums before asking those questions

0 Likes