.NET

Reply
*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 11 of 19 (687 Views)

Re: Custom Commands within Namespace

05-07-2012 10:31 AM in reply to: norman.yuan

Yes, The SDK is incorrect.  It is possible that it used to be correct, but based on Kean's post, it has been incorrect since at least ACAD 2010.  (And I was looking at the 2013 SDK, It says the same thing going all the way back to 2009 SDK, at least)

 

I was always aware that if you had (any) CommandClass Attribute in your assembly, then only commands in that (those) class(es) would be found. 

 

Until today, I thought there could be only one, that is the part the help files mislead me on.

 

I still have a question regarding Modules.  I have many commands in Modules.  Most of them in fact.  The only commands I have in classes are commands where an instance of the class needs to be created (like forms, or jigs), and even in some of those cases, I have a short CommandMethod in a Module that handles creating and destroying the instance of the Class.

 

I am relatively certain that I can not apply a CommandClass Attribute to a Module, so I guess that means I am either stuck leaving my code the way it is, or I have to change my Modules to classes, which I don't want to do.

Dave O.                                                                  Sig-Logos32.png
*Expert Elite*
norman.yuan
Posts: 1,016
Registered: ‎04-27-2009
Message 12 of 19 (680 Views)

Re: Custom Commands within Namespace

05-07-2012 12:51 PM in reply to: chiefbraincloud

Module in VB.NET is alsoa class, which is equivalent to C# static class. That is, all the methods in Module are static/Shared method.

 

Yes, you can place <Assembly: CommandClass(...)> in Module, just as you place on top of a class. And yes, it is the same as with multiple CommandClass, you also use multiple <Assembly: CommandClass(...)>. However, my test does reveil ONE THING: <Assembly: CommandClass(GetType(NamesapceName.ModuleName))> is required if the commandmethod is in a module, not a class, this is different from C#. So, although I said in VB.NET, Module is also a class, but AutoCAD does not search module for CommandMethod, unless the module is decorated with <Assembly: CommandClass(...)>.

 

Another point one may want to pay attention when place CommandMethod iin VB.NET Module: all methods in module is static/Shared method. While in Acad .NET API, there is different when commandmethod is declared as static/Shared, or not. If one want to take advantage of non-static/Shared commandmethod (i.e. get the CommandClass instantiated per document, instead of against all document, then one cannot use Module. So, in general, I would not recommed to use Module to declare commandmethod, unless you absolutely know that you do not need document-level of commandClass instance. But why impose such self-limitation?

 

Here is the code I tested in VB.NET Module:

 

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime

<Assembly: CommandClass(GetType(CommadClassInVBModuleTest.CommandModule1))> 
<Assembly: CommandClass(GetType(CommadClassInVBModuleTest.CommandModule2))> 

Module CommandModule1

    <CommandMethod("MyCmd1")> _
    Public Sub DoCommand1()
        Dim dwg As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = dwg.Editor

        ed.WriteMessage(vbCrLf & "Command in CommandModule1" & vbCrLf)

    End Sub

End Module

Module CommandModule2

    <CommandMethod("MyCmd2")> _
    Public Sub DoCommand1()
        Dim dwg As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = dwg.Editor

        ed.WriteMessage(vbCrLf & "Command in CommandModule2" & vbCrLf)

    End Sub

End Module

 

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 13 of 19 (679 Views)

Re: Custom Commands within Namespace

05-07-2012 01:02 PM in reply to: norman.yuan

"but AutoCAD does not search module for CommandMethod, unless the module is decorated with <Assembly: CommandClass(...)>."

 

I'm not sure how you arrived at that conclusion, but I speak from (plenty of) experience, Commands in a Module will be found without the CommandClass Attribute.  I have at least one Module in every assembly I have, and there are no CommandClasss Attributes in any of those assemblies (because I thought you could only have one, or none).

 

My module commands get loaded just fine.  (As I commented earlier in the thread, the Module does have to be declared Public, which is not the default)

 

 

Dave O.                                                                  Sig-Logos32.png
*Expert Elite*
norman.yuan
Posts: 1,016
Registered: ‎04-27-2009
Message 14 of 19 (676 Views)

Re: Custom Commands within Namespace

05-07-2012 01:21 PM in reply to: chiefbraincloud

If you run the VB.NET code in my previous reply by commenting out the <Assembly: CommandClass...> on top of the module, AutoCAD will not find the 2 ommand(MyCmd1 and MyCmd2): the usual "Unknown command "xxxx" message is shown at command line.

 

I tested with AutoCAD Map2012, 64bit on Win7.

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 15 of 19 (675 Views)

Re: Custom Commands within Namespace

05-07-2012 01:24 PM in reply to: norman.yuan

Those modules are not Public.

 

Add the Public Modifier, comment out your CommandClass attributes and AutoCAD will find the commands.

Dave O.                                                                  Sig-Logos32.png
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 16 of 19 (625 Views)

Re: Custom Commands within Namespace

08-12-2012 08:59 PM in reply to: chiefbraincloud

chiefbraincloud wrote:

This is a Quote from the ObjectARX SDK, AutoCAD Managed Class Reference.

 

"This custom attribute class is used to mark a type as the application's command class. An application may designate one, and only one, type as its command class. AutoCAD looks for an application's command methods on the type that bears this attribute."

 

While I agree with those that think the help files are incomplete, I've not known them to lie to me before.


 

The help files are wrong, and please don't make the mistake of giving them the same legitimacy as one might give holy books. I've lost count of how many errata I've found (and been bitten by) in the managed API docs, not to mention the sample applications, labs, and so forth.

 

It also might help if you knew that if that was the intention (allow only one CommandClassAttribute per assembly), then the designer would have specified the AllowMultiple=false property in the [AttributeUsage] attribute applied to the CommandClassAttribute's definition (from Reflector):

 

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple=true, Inherited=false)]
public sealed class CommandClassAttribute : Attribute
{
   // Fields
   private Type type_;

   // Methods
   public CommandClassAttribute(Type name);

   // Properties
   public Type Type { get; }
}

 

 

 

 

Active Contributor
thoman999
Posts: 37
Registered: ‎06-22-2010
Message 17 of 19 (462 Views)

Re: Custom Commands within Namespace

04-24-2013 05:57 PM in reply to: chiefbraincloud

I know this is an old thread but I'll post anyway just for searchers.

 

In working with Map3D 2013 the <CommandMethod ...> directive is not covered by the acmgd.dll and acdbmgd.dll references. I also had to load accoremgd.dll

 

Just an observation

 

tom

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 18 of 19 (453 Views)

Re: Custom Commands within Namespace

04-24-2013 08:36 PM in reply to: thoman999

Yes, that is new to the 2013 API.

Dave O.                                                                  Sig-Logos32.png
Valued Mentor
jeff
Posts: 331
Registered: ‎05-12-2009
Message 19 of 19 (436 Views)

Re: Custom Commands within Namespace

04-25-2013 11:15 PM in reply to: chiefbraincloud

If you look at code in link from reflector you see how Autocad loads your assembly and explains results.

--Checks is assembly contains CommandClassAttribute if it does it will check only the classes defined in each attribute and if not will examine each type in the assembly

 

http://www.theswamp.org/index.php?topic=44237.msg494891#msg494891

You can also find your answers @ TheSwamp
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.