.NET

Reply
Valued Contributor
MRiemenCAD
Posts: 65
Registered: ‎03-31-2003
Message 1 of 19 (817 Views)

Custom Commands within Namespace

817 Views, 18 Replies
05-03-2012 09:29 AM

Initiated a project with the AutoDesk .net templates for VB.net. I've kept the ExtensionApplication loader that's part of the template, however commands that reside within my other classes aren't recognized. The extension is running because it's responding proplerly to events that trigger, however I can't call my custom command names from the command line. Could this be a function of the namespace that the commands are in?

 

Thanks in advance for your help.

 

Namespace IWC

    ' This class is instantiated by AutoCAD once and kept alive for the 
    ' duration of the session. If you don't do any one time initialization 
    ' then you should remove this class.
    Public Class MyPlugin

        Implements IExtensionApplication


        Public Sub Initialize() Implements IExtensionApplication.Initialize

            Dim NewLayer = New IWCLayer
            NewLayer.IWCLayerSysStart()

        End Sub


        Public Sub Terminate() Implements IExtensionApplication.Terminate
            MsgBox("Layer System Terminating", MsgBoxStyle.Critical)
        End Sub
    End Class

End Namespace





Namespace IWC

    ' This class is instantiated by AutoCAD for each document when
    ' a command is called by the user the first time in the context
    ' of a given document. In other words, non static data in this class
    ' is implicitly per-document!

    Public Class IWCLayer

        <CommandMethod("I3")> _
        Public Sub I3()
            If ReturnStandardLayername(3) = False Then
                ChangeCurrentLayer(AddLayer(IWCGetStandardLayer(3)))
            Else
                ChangeCurrentLayer(ReturnStandardLayername(3))
            End If
        End Sub
        Public Sub RegEvent()
            '**IWC**
            'registeres event handlers for start and end commands

            Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
            AddHandler Doc.CommandWillStart, AddressOf IWCLayerSysCheck
            AddHandler Doc.CommandEnded, AddressOf IWCLayerReturn
            AddHandler Doc.CommandCancelled, AddressOf IWCLayerReturn

        End Sub

    End Class


End Namespace

 

*Expert Elite*
norman.yuan
Posts: 1,021
Registered: ‎04-27-2009
Message 2 of 19 (811 Views)

Re: Custom Commands within Namespace

05-03-2012 10:38 AM in reply to: MRiemenCAD

try add this line on top of the class:

 

<Assembly: CommandClass(GetType(IWC.IWCLayer))>

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

Re: Custom Commands within Namespace

05-03-2012 12:05 PM in reply to: MRiemenCAD

I'm thinking maybe you already have another class with the CommandClass Attribute?

 

The commands should be found and loaded with or without the CommandClass attribute, unless there is already another class decorated as a CommandClass. 

 

You can only have one Class labled as a CommandClass, and if you have one, AutoCAD will look for command methods in that class and that class only.  It won't even look anywhere else.  If you remove the CommandClass attribute, AutoCAD will find any properly declared commands in any Public Class or Public Module.

Dave O.                                                                  Sig-Logos32.png
Valued Contributor
MRiemenCAD
Posts: 65
Registered: ‎03-31-2003
Message 4 of 19 (785 Views)

Re: Custom Commands within Namespace

05-04-2012 07:02 AM in reply to: chiefbraincloud

Thanks for the responses. Makes sence. Can you tell me what a best practice might be for managing commands within a project? Should I be collecting all commands within a single command class or module? Seems a bit too messy to have them interspersed within individual classes.

 

I appreciate your help.

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 5 of 19 (770 Views)

Re: Custom Commands within Namespace

05-06-2012 01:30 AM in reply to: chiefbraincloud

chiefbraincloud wrote:You can only have one Class labled as a CommandClass, and if you have one, AutoCAD will look for command methods in that class and that class only.  It won't even look anywhere else.  If you remove the CommandClass attribute, AutoCAD will find any properly declared commands in any Public Class or Public Module.


No, You can have any number of classes with the CommandClass attribute applied to them.

Distinguished Mentor
andrewpuller3811
Posts: 777
Registered: ‎07-30-2008
Message 6 of 19 (764 Views)

Re: Custom Commands within Namespace

05-06-2012 03:20 AM in reply to: MRiemenCAD

Do you have "Imports Autodesk.AutoCAD.Runtime" at the start of your class? This is need for the <CommandMethod("I3")> unless you define it as <Autodesk.AutoCAD.Runtime.CommandMethod("I3")>

 

Otherwise you should check out this post by Kean Walmsley http://through-the-interface.typepad.com/through_the_interface/2006/09/optimizing_the_.html

 

He goes over ExtensionApplication and CommandClass attributes and their use with examples.

If this fixed your issue, click on "Accept as Solution"

Andrew Puller
Maitland, NSW, Australia
Windows 7 Enterprise 64bit
Intel core i7 2600 @ 3.40 GHz with 16GB Ram
Civil 3d 2013 64bit
*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 7 of 19 (739 Views)

Re: Custom Commands within Namespace

05-07-2012 08:47 AM in reply to: DiningPhilosopher

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.

Dave O.                                                                  Sig-Logos32.png
Valued Contributor
MRiemenCAD
Posts: 65
Registered: ‎03-31-2003
Message 8 of 19 (734 Views)

Re: Custom Commands within Namespace

05-07-2012 08:55 AM in reply to: MRiemenCAD

Thanks for your post. I appreciate the link too. I've spent alot of time on Kean's blog but didn't think to look for this issue there. 

 

I've had success with other custom commands running in other applications, it's only when I begin a new project using the AutoDesk supplied template that I'm running into this issue. I've even tried starting anew project, not changing any code and just debugging to see if I can get the AutoDesk generic commands to run but no luck. Odd....

 

I'm working thorugh VB.Net Express so the AutoDesk template is nice as it comes pre-configured with the project files already altered for debugging within AutoCAD.

 

That said, I deffinetly don't know enough about the ExtensionApplication attribute nor the CommandClass so I'll review Kean's information closely. 

 

thanks again for taking the time to reply

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

Re: Custom Commands within Namespace

05-07-2012 09:12 AM in reply to: DiningPhilosopher

Based on Kean's blog, and the comments at the bottom, it appears the SDK DID lie to me.

 

Great, now I want to go back and add CommandClass Attributes to all my code, to optimize the loading.  I do still believe that it is true, that if you have (one or more) Classes decorated with the CommandClass Attribute, that AutoCAD will not look for commands in Classes that are not decorated.

 

I have a bunch of commands defined in Modules, so I may be stuck leaving it the way it is.

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

Re: Custom Commands within Namespace

05-07-2012 10:01 AM in reply to: chiefbraincloud

Your quote from SDK document is intersting, and I certainly did not notice that before. I though the [assembly: CommandClass(....)] is only for optimize the way how AutoCAD can quickly find custom command. But how to interprete "An appliction" that "can designate one, and only one, type as ..." might open for debate. Since I had never run into an issue of my custom commands not being recognised by AutoCAD due to my code error, I decide to do a quick test.

 

So, I create a project, with 3 classes. Both classes have a command method in it. I did not place the 2 class in separate code file (*.cs, in my case, beause I uses C# more than VB.NET), becase I do not think, separate code files or not in the same assembly does not matter here.

 

Then, I tried:

1. place one [assembly: CommandClass(....)] there, so that only one class is designated as CommandClass;

 

2. place as many [assembly: CommandClass(...)] there, as the classes, as long as there is "Commandmethod" is defined in that class;

 

3. no [assembly: CommandClass(...)] used at all.

 

The result of test:

 

1. Indeed, if you only use one [assembly: CommandClass(...)], then AutoCAD only recognise the command methods iin the specified single commandclass.

 

2. All command methods in the classes specifed in multiple [assembly: CommandClass(...)] can be recognised by AutoCAD.

 

3. All command methods can be recognised by AutoCAD.

 

The conclusion:

 

The quote from SDK only be correct if one uses only one [assembly: CommandClass(...)] line. But, it is more the consuquence of using one [assembly: CommandClass(...)], rather than the requirement that the quote sounds. So, I'd say the SDK quote is wrong.

 

Using [assembly: CommandClass(....)] is more like indexing the classes that have executable custom command, so that AutoCAD do not have to scan loaded assembly for commandmethod from begining to its end. It will only scann specified classes, and one can use multiple [assembly: CommandClass(...)] iin a assembly, should commandmethods are scattered in multiple classes. But If one only use one [asembly;CommandClass(...)], then AutoCAD only scan one classes.

 

If one do not use [assembly: CommandClass(...)], AutoCAD will scan all classes in loaded assemblies and all CommandMethods can be found. This may take AutoCAD longer time to identify a command if there are a lot of assemblies cotaining a lot of classes loaded.

 

In the case of the OP, it might be that he has one [assembly: CommandClass(...)] specified, but later added another class with commandmethod, thus the commandmethod is not recoginised. Add one more [assembly: CommadClass(...)], or remove it entirely would solve his issue.

 

here is the code I did the test:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(CommandClassTest.MyCommandClass1))]
[assembly: CommandClass(typeof(CommandClassTest.MyCommandClass2))]
[assembly: CommandClass(typeof(CommandClassTest.MyCommandClass3))]

namespace CommandClassTest
{
    public class MyCommandClass1
    {
        [CommandMethod("CmdInClass1")]
        public static void MyCmd()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            ed.WriteMessage("\nCommand in MyCommandClass1 executed\n");
        }
    }

    public class MyCommandClass2
    {
        [CommandMethod("CmdInClass2")]
        public static void MyCmd()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            ed.WriteMessage("\nCommand in MyCommandClass2 executed\n");
        }
    }

    public class MyCommandClass3
    {
        [CommandMethod("CmdInClass3")]
        public static void MyCmd()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            ed.WriteMessage("\nCommand in MyCommandClass3 executed\n");
        }
    }
}

 Try to comment out 1, 2 or al of the [assembly:CommandClass(...)] to see what command is available.

 

 

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.