Author Topic: AutoCAD DotNet commands all stopped working suddenly???!!!  (Read 6063 times)

0 Members and 1 Guest are viewing this topic.

NickMerchant

  • Guest
AutoCAD DotNet commands all stopped working suddenly???!!!
« on: June 14, 2012, 07:41:38 PM »
The subject line says it all, mostly. I've been developing a solution for a while. After a build today, all of the commands suddenly stopped working in AutoCAD. I loaded an earlier version of the .DLL and the commands work.

What the hell did I do?

Please help...

Jeff H

  • Needs a day job
  • Posts: 6150
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #1 on: June 14, 2012, 07:49:39 PM »
Not much info but did you add a method with a CommandMethodAttribute to another class and not add another CommandClassAttribute for it, or did not have a CommandClassAttribute in first place?

TheMaster

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #2 on: June 14, 2012, 09:16:08 PM »
The subject line says it all, mostly. I've been developing a solution for a while. After a build today, all of the commands suddenly stopped working in AutoCAD. I loaded an earlier version of the .DLL and the commands work.

What the hell did I do?

Please help...

If you have an IExtensionApplication in your assembly, put a try/catch block around the entire implementation of Initialize(), and display a message in the catch block if it's reached.  If an exception is raised in the Initialize() method, AutoCAD catches it, fails to register commands and does not bother to tell you anything about it:

Code - C#: [Select]
  1. public class MyApplication : IExtensionApplication
  2. {
  3.    public void Intitialize()
  4.    {
  5.         try
  6.         {
  7.              // TODO: do all initialization here
  8.         }
  9.         catch( System.Exception e )
  10.         {
  11.              Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  12.                  "\n\nError in {0}.Initialize():\n{1}",
  13.                  this.GetType().FullName,
  14.                  e.ToString()
  15.              );
  16.         }
  17.     }
  18.     ...
  19. }
  20.        
  21.  

Jeff H

  • Needs a day job
  • Posts: 6150
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #3 on: June 14, 2012, 09:54:18 PM »
Has anyone tried or know of a good event, where AutoCAD calls Initialize() and you could subscribe to another event then do your initialization there?.
 
I thought about the Idle event but thought there would be too many issues and one of the issues Tony solved here
http://www.theswamp.org/index.php?topic=41536.msg466400#msg466400
 
 
Basiclly to maybe get around all initialization, command registration, etc.... depending on if a exception is thrown during Initialize() method .
 
But to maybe better explain myself and quickly code half-arsed example something like any class that needs to do something right after loading implement some interface, something like
 
Code - C#: [Select]
  1.     public interface IExtApplication
  2.     {
  3.         void Intialize();
  4.         void Terminate();
  5.     }
  6.  

 
Code - C#: [Select]
  1.         void IExtensionApplication.Initialize()
  2.         {
  3.             Application.Idle  = new EventHandler(Application_Idle);
  4.         }
  5.  
  6.         void Application_Idle(object sender, EventArgs e)
  7.         {
  8.             Type[] types = Assembly.GetExecutingAssembly().GetTypes();
  9.             foreach (Type type in types)
  10.             {
  11.                 if (type.GetInterface(typeof(IExtApplication).ToString()) != null)
  12.                 {
  13.                     IExtApplication extApps = Activator.CreateInstance(type) as IExtApplication;
  14.                     extApps.Intialize();
  15.                 }
  16.             }
  17.             Application.Idle -= new EventHandler(Application_Idle);
  18.         }
  19.  
  20.  

 
So all intialization does not depend on one method.
 
 

TheMaster

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #4 on: June 15, 2012, 01:19:31 AM »
Has anyone tried or know of a good event, where AutoCAD calls Initialize() and you could subscribe to another event then do your initialization there?.
 
I thought about the Idle event but thought there would be too many issues and one of the issues Tony solved here
http://www.theswamp.org/index.php?topic=41536.msg466400#msg466400
 
 
Basiclly to maybe get around all initialization, command registration, etc.... depending on if a exception is thrown during Initialize() method .
 
But to maybe better explain myself and quickly code half-arsed example something like any class that needs to do something right after loading implement some interface, something like
 
So all intialization does not depend on one method.

If there's an exception being thrown by the code called from Initialize(), it will happen regardless of where the code is called from, so changing the caller doesn't really address the underlying problem (the code is broken).

I don't see much of a problem with using try/catch in Initialize() to check for exceptions, and I usually derive my IExtensionApplication from a base class that abstracts the try/catch:

Code - C#: [Select]
  1.  
  2.  
  3.  
  4. public abstract class ExtensionApplication : IExtensionApplication
  5. {
  6.   void IExtensionApplication.Initialize()
  7.   {
  8.     try
  9.     {
  10.       this.Initialize();
  11.     }
  12.     catch( System.Exception ex )
  13.     {
  14.       Console.Beep();
  15.       WriteMessage( "\nAn error occured while loading {0}:\n\n{1}",
  16.         this.GetType().Assembly.Location,
  17.         ex.ToString()
  18.       );
  19.       throw ex;
  20.     }
  21.   }
  22.  
  23.   static void WriteMessage( string fmt, params object[] args )
  24.   {
  25.       Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
  26.         .MdiActiveDocument.Editor.WriteMessage( fmt, args );
  27.   }
  28.  
  29.  
  30.   // Derived types must override this to do initialization
  31.   // and do not need to worry about catching exceptions:
  32.  
  33.   protected abstract void Initialize();
  34.  
  35.   // Derived types can optionally override this to do finalization:
  36.   protected virtual void Terminate()
  37.   {
  38.   }
  39.  
  40.   void IExtensionApplication.Terminate()
  41.   {
  42.     this.Terminate();
  43.     if( Terminating != null )
  44.       Terminating( this, EventArgs.Empty );
  45.     Terminating = null;
  46.   }
  47.  
  48.   // This event can be handled as an alternative way
  49.   // of doing termination handling:
  50.  
  51.   public static event EventHandler Terminating = null;
  52.  
  53. }
  54.  
  55. // Example use:
  56.  
  57. [assembly: ExtensionApplication( typeof( MyApplication ) )]
  58.  
  59. public class MyApplication : ExtensionApplication
  60. {
  61.   protected override void Initialize()
  62.   {
  63.     // Do initialization here
  64.   }
  65. }
  66.  
  67.  
« Last Edit: June 15, 2012, 02:25:54 AM by TheMaster »

NickMerchant

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #5 on: June 15, 2012, 02:03:33 PM »
I appreciate all of the feedback and help. Thank you.

Today, while trying to debug this issue, I got the following error message in the VS error list:

Error   1   Cannot register assembly "E:\ASE Development\C#.net\Compilations\CsControls.dll". Could not load file or assembly 'Acdbmgd, Version=18.2.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets

3341   9   CS



NickMerchant

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #6 on: June 15, 2012, 02:50:35 PM »
FWIW I was able to get rid of the error mentioned in the previous post, but still, none of my commands are being recognized inside AutoCAD. I tried a different project and that one works fine.


OMG, this is so frustrating. I guess if I knew what I was doing it would be easier. Sorry for wasting everyone's time.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #7 on: June 15, 2012, 03:06:44 PM »
Nick,
Can you post your project?

NickMerchant

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #8 on: June 15, 2012, 05:11:47 PM »
Hi Jeff.

I remember you from the AutoCAD DG's. I hope all is well with you.

I fixed the problem (Like I knew what I was doing). As soon as I removed the class containing the

" public class CivilDotNet : IExtensionApplication"

statement, the commands were recognized again. But now I have no interface with the Civil 3D database. I must've added something in that class using incorrect syntax. But what's so strange is that there were no build errors.

I've attached the class that gave me problems.

I know what I want to do CAN be done, easily no less. The problem is that I'm still learning C# and I know just enough to use the language, but not enough to thoroughly understand everything I'm doing. I just need to keep reading.

For example, I don't understand why the class must contain the appendage after the name, eg.,
"MyClassName : IExtensionApplication"
I'm just guessing it has something to do with the Civil 3D libraries, but it's all still a mystery to me at this point.

I could do this 10 times faster and easier using Visual Lisp (ActiveX), but I'm trying to develop exclusively in DotNet now.
 Developing standalones for Windows is very easy, but when you want to auutomate another application like AutoCAD or Excel, the code complexity quickly accelerates to a level that I don't understand.


NickMerchant

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #9 on: June 15, 2012, 05:36:39 PM »
Well, I've isolated the cause of the commands not being registered in AutoCAD. When I commented out the "IExtensionApplication" suffix from the class name, the commands stopped working:

Works!  :-)
  public class CivilDotNet
  {

Breaks all my commands!
  public class CivilDotNet : IExtensionApplication
  {

n.yuan

  • Bull Frog
  • Posts: 348
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #10 on: June 15, 2012, 06:02:07 PM »
Do you have some code in the Innitialize() method of the implemented IExtensionApplication interface? if yes, an if you have carefully read the reply from The Master, you would have solved your issue earlier.

If you have code inside Innitialize(), and the code could run into exception, you must enclose the code in try...catch block in order for the Initialize() method complete gracefully. If any exception is not caught in Initialize(), AutoCAD would silently let the assebly loading fail and thus, the commnd methods defined in that assembly would be not recognized.

TheMaster

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #11 on: June 15, 2012, 09:47:19 PM »
Well, I've isolated the cause of the commands not being registered in AutoCAD. When I commented out the "IExtensionApplication" suffix from the class name, the commands stopped working:

Works!  :-)
  public class CivilDotNet
  {

Breaks all my commands!
  public class CivilDotNet : IExtensionApplication
  {

Your problem isn't the IExtensionApplication that you commented out, it is
the code in the Initialize() method that is throwing an exception when it
runs. If that code is supposed to be doing something, then there's a bug
in it, but fixing the bug and disabling the code from running are two different
things. I can stop errors that result from bugs in my code by commenting out
all the code, but then the code doesn't run at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #12 on: June 15, 2012, 10:56:47 PM »

If there's an exception being thrown by the code called from Initialize(), it will happen regardless of where the code is called from, so changing the caller doesn't really address the underlying problem (the code is broken).

I don't see much of a problem with using try/catch in Initialize() to check for exceptions, and I usually derive my IExtensionApplication from a base class that abstracts the try/catch:

I see.
I was really confused about that for some reason, and never could understand how it would happen, but
I thought I read somewhere even if you caught the exception AutoCAD would not load your commands, which never made sense because how would it know there was a exception unless it bubbled back up to Intialize method.
 

TheMaster

  • Guest
Re: AutoCAD DotNet commands all stopped working suddenly???!!!
« Reply #13 on: June 16, 2012, 02:04:17 PM »

If there's an exception being thrown by the code called from Initialize(), it will happen regardless of where the code is called from, so changing the caller doesn't really address the underlying problem (the code is broken).

I don't see much of a problem with using try/catch in Initialize() to check for exceptions, and I usually derive my IExtensionApplication from a base class that abstracts the try/catch:

I see.
I was really confused about that for some reason, and never could understand how it would happen, but
I thought I read somewhere even if you caught the exception AutoCAD would not load your commands, which never made sense because how would it know there was a exception unless it bubbled back up to Intialize method.

AutoCAD should register commands if an exception in Initialize() is hancled, but you might notice in the class I posted that I throw the exception again, because all I'm trying to do is find out it happened. If there's an exception being thrown, I don't want commands registered because there's something wrong that needs to be fixed.