Author Topic: CommandMethod w/o using Attribute  (Read 5185 times)

0 Members and 1 Guest are viewing this topic.

TR

  • Guest
CommandMethod w/o using Attribute
« on: August 10, 2006, 02:08:02 PM »
Does anyone know of a way of creating a CommandMethod without using the CommandMethodAttribute? I'm looking for a way to dynamically create create AutoCAD Commands based on parsing a text file. Is this possible or am I out of luck?

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: CommandMethod w/o using Attribute
« Reply #1 on: August 10, 2006, 05:39:08 PM »
I've thought about this myself Tim and I reckon it could be done but when I thought about it a bit more it seemed a moot point. If you need the command you must have the routine written anyway.
If you only want to load a certain few commands on loading at run time that's a bit different I guess. The easiest way would be to define some type of flag into a method of say your startup class like so -

if(this command is in list)
    [CommandMethod("test")]
    static void MyTest()    //simple function to link to main function
    {
        call the real function():
    }

I haven't tested this but I think you can put the att's where ever required in your assembly but I haven't tried nesting them in a method.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Glenn R

  • Guest
Re: CommandMethod w/o using Attribute
« Reply #2 on: August 10, 2006, 06:56:37 PM »
I think in theory this could be done, as you can emit code on the fly with reflection I believe...howeverAutoCAD might not like this as the command is registered on the command stack upon load of the assembly.......just thinking out loud....

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: CommandMethod w/o using Attribute
« Reply #3 on: August 10, 2006, 07:49:11 PM »
True, Autocad may read the attributes from the assembly and add the commands regardless of how they are used as they are still attributes open to inspection with reflection.
Perhaps the flag thing could still work if you load all the commands but do not run them if not 'approved' say -

[CommandMethod("test")]
static void MyTest()
{
    if(!m_testCommand)// private bool set on loading app
     {
          ed.WriteMessage("Unknown Command \"TEST\""); //looks like acad
          return;
     }
     
.....
}
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

TR

  • Guest
Re: CommandMethod w/o using Attribute
« Reply #4 on: August 11, 2006, 08:31:00 AM »
Mick and Glen:

Thanks for the feedback. The reason I'm asking is a project I've been working on (occasionally) is an attempt to create an add-on that would allow Python as a scripting language for AutoCAD via embedding IronPython. IP doesn't support .NET attributes so I'm out of luck there, but I was thinking that perhaps there was a way I could use Python attributes and generate my command methods from there.

Draftek

  • Guest
Re: CommandMethod w/o using Attribute
« Reply #5 on: August 11, 2006, 09:13:24 AM »
You might be able to wrap the 'addCommand' arx method and expose it to your .net code. There may be some translation problems on some of the arguments but I think that would work.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: CommandMethod w/o using Attribute
« Reply #6 on: August 11, 2006, 09:57:52 AM »
....IP doesn't support .NET attributes so I'm out of luck there, but I was thinking that perhaps there was a way I could use Python attributes and generate my command methods from there.

Ok, can you do your grunt work in IP and create a .net class lib/dll then create a simple C#/vb.net class lib that 'ref's' in the methods in your IP dll?
You may have to issue both dll's to the user but only need to load the C# one with the att's to run it and load the commands.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

smcclure

  • Guest
Re: CommandMethod w/o using Attribute
« Reply #7 on: August 14, 2006, 09:42:40 AM »
Does anyone know of a way of creating a CommandMethod without using the CommandMethodAttribute? I'm looking for a way to dynamically create create AutoCAD Commands based on parsing a text file. Is this possible or am I out of luck?

There is a topical thread on the AutoCAD group I started a little while ago.

http://discussion.autodesk.com/thread.jspa?threadID=484828

It basically ended with no as the answer. The reason, from what I understand, is because .NET has no mechanism to attach attributes at runtime (simply because they are not supposed to change) there is little way to trick the .NET ACAD API into thinking there is a non-existent attribute.

When it comes to trying to wrap acedCommand, I think it would be extremely complex and not really guaranteed to work.