.NET

.NET

Reply
Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 1 of 6 (334 Views)
Accepted Solution

Execute Strings (Commands) Recursively.

334 Views, 5 Replies
09-06-2013 10:29 AM

Hey all,

 

How does one execute a command located inside of a .NET assembly from a recursive function in another .NET assembly?

 

I'm working on a program that will load a .NET Assembly, then calls one of the commands inside of it to make changes to the current document.

 

Anyways, I was able to succesfully load the assembly using this code:

try
{
    Assembly.LoadFrom(@"C:\acad\networkdll\MyDotNetAssembly.dll");
    ASSEMBLY_LOADED = true;
}
catch
{   //Load failed.
    ASSEMBLY_LOADED = false;
}

 

Now when I go to execute my command I use a function similar to this, and this works almost fine:

public void DoSomething(string currentDrawing)
{
... \\open acadDoc
acadDoc.SendStringToExecute("MYCOMMAND\n", true, false, true);
... \\Get next drawing file name or return if there is none.
DoSomething(nextDrawing);
}

 

The function that calls this "MYCOMMAND" is recursive, and it should attempt to call this command during each recursive call (and the original call).

Unfortunately, the command is called once, and it happens on the LAST recursive call only.

 

 

Here is the command that is called (withing the referenced .NET assembly.

[CommandMethod("MYCOMMAND", CommandFlags.Session|CommandFlags.Transparent)]
public void MYCOMMAND() { main(); }

private void main(bool showErrors = true)
{
    System.Windows.MessageBox.Show("MYCOMMAND, ShowErrors=" + showErrors);
... //Change drawing }

I've also testing with only the CommandFlags.Session flag only, this did not work too. 

 

The way my original program is ran is through a command. (Session flag only on that one as well).

 

 

For reference, here is the documentation for the CommandFlags enumeration.

 

If anyone could help me out here, it'd be greatly appreciated.

 

PS: In case it wasn't clear, I can call this command manually. It is just my program that cannot call it.

 

Thanks,

-Nicholas

 

Not necessarily a solution, but why not put the entire body of work into a method, with the command-line method just calling that function?  Then you don't need to bugger around with SendStringToExecute, command names, etc.  Just call the non-command method since it is doing exactly the same thing.

*Expert Elite*
dgorsman
Posts: 5,698
Registered: ‎10-12-2006
Message 2 of 6 (320 Views)

Re: Execute Strings (Commands) Recursively.

09-06-2013 11:40 AM in reply to: Millerni456

Not necessarily a solution, but why not put the entire body of work into a method, with the command-line method just calling that function?  Then you don't need to bugger around with SendStringToExecute, command names, etc.  Just call the non-command method since it is doing exactly the same thing.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 3 of 6 (319 Views)

Re: Execute Strings (Commands) Recursively.

09-06-2013 11:50 AM in reply to: dgorsman

I think I understand what you are saying...

 

So instead of NETLOAD'ing my external dll,  you are saying that I should include the .NET assembly as a project reference, then simply execute the function that performs all the work?

Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 4 of 6 (314 Views)

Re: Execute Strings (Commands) Recursively.

09-06-2013 12:20 PM in reply to: dgorsman

Thank you so much!  Not only have I fixed the problem, but now the integration of my programs is much more elegant.  I love it!

 

 

Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 5 of 6 (302 Views)

Re: Execute Strings (Commands) Recursively.

09-06-2013 01:14 PM in reply to: Millerni456

Not that it is a really big deal,  but I noticed some interesting behavior when running my application.

 

When I start up a fresh AutoCAD, my referened .NET assembly (not NETLOAD'ed) which has an AutoCAD command inside of it will not populate inside AutoCAD's command list.  This is as expected.

 

When I NETLOAD my application that references the .NET assembly, the external command is still not loaded.  This is as expected as well.

 

When I actually run the application (and let the code proceed to the point where the referenced .NET assembly is used), I notice that it changes!   The external command is now inside of AutoCAD's command list, yet I never "NETLOAD'ed" it in!

 

Like I said, this isn't a real problem, but it is interesting to know that my referenced .NET assembly is loaded behind the scenes just because I referenced it.

 

*Expert Elite*
Jeff_M
Posts: 4,519
Registered: ‎07-22-2003
Message 6 of 6 (283 Views)

Re: Execute Strings (Commands) Recursively.

09-06-2013 07:39 PM in reply to: Millerni456

You might find this similar thread interesting and/or enlightening:

http://www.theswamp.org/index.php?topic=44237.0

Jeff_M, also a frequent Swamper
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.