Author Topic: Creating a "CommandMethod" through code.  (Read 3952 times)

0 Members and 1 Guest are viewing this topic.

TR

  • Guest
Creating a "CommandMethod" through code.
« on: July 21, 2007, 12:57:45 AM »
I have a demo of AutoCAD 2007 installed on my system and within a few weeks I will have 2008 electrical installed. Now that I have a version of AutoCAD that doesn't have a crippled version of the .NET API (ACAD 2005) I have decided to start working on my PyAcad.NET project again. With this being said I am trying to find a way to create a command method through code. IronPython doesn't support .NET attributes so I'm out of luck there but I'm sure there is a way to define them without using attributes. Since my python code is compiled when executed I should be able to somehow plugin to the command registration stuff I'm thinking.


Any thoughts?


note:
1) You can download a binary package for AutoCAD 2007 and/or the source from the google code page linked above.
2) I asked this in another post but my ideas have changed a bit.


Edit by MP: Fixed the url.
« Last Edit: July 21, 2007, 10:20:14 AM by MP »

TR

  • Guest
Re: Creating a "CommandMethod" through code.
« Reply #1 on: July 21, 2007, 10:54:01 AM »
Thanks for fixing that for me Michael. :)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8746
  • AKA Daniel
Re: Creating a "CommandMethod" through code.
« Reply #2 on: July 21, 2007, 12:08:18 PM »
Question, are you referencing a compiled .NET .DLL or are you still compiling/executing on loading of the file?

TR

  • Guest
Re: Creating a "CommandMethod" through code.
« Reply #3 on: July 21, 2007, 03:05:44 PM »
I''m compiling and executing when loading the files.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8746
  • AKA Daniel
Re: Creating a "CommandMethod" through code.
« Reply #4 on: July 21, 2007, 03:44:17 PM »
Yeah, that’s a tough one. I don’t quite follow what your intentions are.

There are a couple of different ways you can define a new command, but in all cases, you would need to provide a pointer or reference to the method you wish to invoke. Even a wrapper method would need to know what to invoke when called.

You might be able to get around not having an attribute by putting a prefix on the method such as “cmd:”,
upon loading an assembly…
Code: [Select]
System::Reflection::Assembly::LoadFrom(fileName);

You could probably scan all the method names and add them as AutoCAD commands.

But you are doing something entirely different right?

Code: [Select]
CompiledCode cc = engine.CompileFile(ofd.FileName);
cc.Execute();

I am not sure where you want to point your new commands at.
« Last Edit: July 21, 2007, 03:45:20 PM by Danielm103 »

TR

  • Guest
Re: Creating a "CommandMethod" through code.
« Reply #5 on: July 21, 2007, 04:08:46 PM »
What I'm currently doing is not really close to what me final goal is. Right now I'm just running python files via an Open File Dialog because it was easy to do for testing out code. However my real goal is to develop a loader or something like how lisp is now. It can be loaded and define commands. Once it's loaded the command can then be executed at any time.

If I could figure out how to create a command I would turn it into a decorator and use it like this in code:
Code: [Select]
@commandmethod('testcommand')
def mytest():
    print "test command worked"

Thus testcommand entered on the command line would call the mytest function and print to the command line.
« Last Edit: July 21, 2007, 04:11:42 PM by Tim Riley »

TR

  • Guest
Re: Creating a "CommandMethod" through code.
« Reply #6 on: July 23, 2007, 02:53:16 PM »
I've been doing some digging using Lutz Roeder's .NET Reflector and it seems that the CommandMethod attribute registers the command using the acmgd.Autodesk.AutoCAD.Runtime.ICommandLineCallable interface. However I have yet to figure out how to use this to register a command via python code. Any thoughts?

MickD

  • King Gator
  • Posts: 3639
  • (x-in)->[process]->(y-out) ... simples!
Re: Creating a "CommandMethod" through code.
« Reply #7 on: July 25, 2007, 06:26:09 PM »
Tim, how does Iron Python 'integrate' with .net, after a quick look I couldn't find anything definite to say whether it runs in the clr or through the python interpreter still.
The reason I ask is that if it's still interpreted I don't think you will have much luck working as closely as you like, either you will have to create a command in .net to call a method in your python scripts or get a python script to call a .net command - swings and roundabouts really...

I know it's not the solution you're after but perhaps you can create a 'pyrun' command in .net say that asks the user for a method to run from python and runs it. To make it more like a real command the user can create a 2/3 line lisp that achieves the same as a real command, just create a lisp for each method. In fact you could get python to write such a lisp file quite easily as you know ;).

Anyway, food for thought, hope you work it out.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

TR

  • Guest
Re: Creating a "CommandMethod" through code.
« Reply #8 on: July 25, 2007, 10:43:03 PM »
Mick:

I'm not 100% certain but I think it runs in the CLR. From the old IronPython page:
Quote
Integrated with the Common Language Runtime - IronPython code can easily use CLR libraries and Python classes can extend CLR classes.

I'm also thinking this as I am able to consume any .NET library via IP's built in clr module.

MickD

  • King Gator
  • Posts: 3639
  • (x-in)->[process]->(y-out) ... simples!
Re: Creating a "CommandMethod" through code.
« Reply #9 on: July 25, 2007, 10:53:42 PM »
ok, thanks Tim, so you can write .net in Python syntax/language - cool!

In that case, how about writing real basic 'commands' to register with acad in a c#/vb dll that call methods in your ref'd in  IP dll?
Again it's not as direct as you would like but it may work, the command methods would basically be a template/copy and paste affair for those not familiar with c#/vb with a little instruction.

"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8746
  • AKA Daniel
Re: Creating a "CommandMethod" through code.
« Reply #10 on: July 25, 2007, 11:17:58 PM »
If your python code were compiled into a .NET assembly, It probably would not be too difficult to create something like a pyload command that would load and register your new commands similar to netload.

Dan