.NET

Reply
Valued Contributor
mbujak
Posts: 53
Registered: ‎07-07-2004
Message 1 of 6 (231 Views)

What is happening in DocumentCreated?

231 Views, 5 Replies
09-23-2012 05:20 AM

This is unexpected. I have a custom application class called TheApp. In it I have a private bool property called IsAssembly. In my command I set this value to true then create a new document. In the event handler, I check to see if the IsAssembly is true but it returns false. The only places this property is set is in the command and the event handler.

 

Any ideas?

 

[code]

[CommandMethod("IFX_NEW_ASSEMBLY", CommandFlags.Session)]

public void NewAssembly()

{

     IsAssembly = true;

     Application.DocumentManager.Add(null);

}

[/code]

 

I have a handler for DocumentCreated

[code]

private void OnNewDocument(object sender, DocumentCollectionEventArgs args)

{

       if(IsAssembly)

       {

            ....Do something.

            IsAssembly = false;

       }

}

[/code]

 

-Mike

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 6 (220 Views)

Re: What is happening in DocumentCreated?

09-23-2012 07:28 AM in reply to: mbujak

Your CommandMethod is not static.

 

When a CommandMethod is not static (shared in VB), AutoCAD creates multiple instances of the class containing the command method, one for each document the command is used in, and invokes the method on the instance associated with the active document. In that case, the instance of the class which the command method is invoked on, is not an instance that you created yourself.

 

If you use non-static command methods in a class, you shouldn't create instances of the class yourself, because AutoCAD will do that for you when one of the commands is issued.

 

 

Valued Contributor
mbujak
Posts: 53
Registered: ‎07-07-2004
Message 3 of 6 (195 Views)

Re: What is happening in DocumentCreated?

09-23-2012 06:42 PM in reply to: DiningPhilosopher

Well that is interesting. So, that defeats the purpose of my custom app class. I had the application class managing custom document classes that I was associating with the autocad Document through a dictionary. hmmm, need to redesign a little bit then.

 

Thanks

 

- Mike

ADN Support Specialist
Balaji_Ram
Posts: 700
Registered: ‎03-21-2011
Message 4 of 6 (162 Views)

Re: What is happening in DocumentCreated?

09-26-2012 06:10 AM in reply to: mbujak

Have you looked at this post ?

http://adndevblog.typepad.com/autocad/2012/08/managing-events-at-a-per-document-level.html

 

It can help in holding per document values.



Balaji
Developer Technical Services
Autodesk Developer Network

Valued Contributor
mbujak
Posts: 53
Registered: ‎07-07-2004
Message 5 of 6 (152 Views)

Re: What is happening in DocumentCreated?

09-26-2012 09:39 AM in reply to: Balaji_Ram

That is interesting. It is almost exactly what I had written. Instead of a controller, I called the class AppData. That contained a Document/DocData dictionary and I used the Activated and ToBeDestroyed events to keep my dictionary "up-to-date". But, why would this be needed if the Document itself has a UserData property. The dictionary at that point is redundant. Not I simply create the custom DocData object and set the UserData property on the Document itself.

 

Thanks for the link though :smileyhappy:...

 

-Mike

 

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 6 of 6 (146 Views)

Re: What is happening in DocumentCreated?

09-26-2012 11:34 AM in reply to: mbujak

It's quite possible that the blog post predated the addition of the UserData property.

 

Keep in mind that some of the material being posted on that blog is dated.

 

I also used a dictionary originally, but switched over to using the Document's UserData hashtable, using the type of the per-document data object as the key.  I think it's preferable to a dedicated dictionary, because it makes the instance of the per-document data object easer to reach from any code that knows the type.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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