.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Setting global variables on opening a drawing (C#)

6 REPLIES 6
Reply
Message 1 of 7
imckillip
1554 Views, 6 Replies

Setting global variables on opening a drawing (C#)

Hi All

I'm trying to have AutoCAD call a function (resetVariables) when a new drawing is opened. Currently, AutoCAD loads fine, NETLOAD is successful, and I can run several other commands from the same code, but when I open a drawing, the drawing loads then immediately gives an AutoCAD "FATAL ERROR".

My code is posted below; any insight would be greatly appreciated.

{code}
public class Init : Autodesk.AutoCAD.Runtime.IExtensionApplication
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

public void Initialize()
{
// Notify the user of the start of initialization
ed.WriteMessage("\nIMine Initialization... ");

// Set system variables in accordance with mine standards
Application.SetSystemVariable("MENUECHO", 1);
Application.SetSystemVariable("SDI", 1);
Application.SetSystemVariable("PSLTSCALE", 0);

// Start a callback function to detect the opening of a new drawing
Application.DocumentManager.DocumentActivated +=new DocumentCollectionEventHandler(resetVariables);

// Notify the user of completion of initialization
ed.WriteMessage("Complete!\n");
}

public void Terminate(){}

public void resetVariables(object sender, DocumentCollectionEventArgs e)
{
ed.WriteMessage("\nIntercepted a DocumentCollection event");
}
}
{code}

- Iain
6 REPLIES 6
Message 2 of 7
cadMeUp
in reply to: imckillip

public void resetVariables(object sender, DocumentCollectionEventArgs e)
{
if (e.Document != null)
{
Editor ed = e.Document.Editor;
Database db = e.Document.Database;
ed.WriteMessage("\nIntercepted a DocumentCollection event");
}

}

It's not safe to declare and initialize:
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

These are being initialized to the current/active doc and can change, go out of scope, and/or be garbage collected.
Think about the dwg that these were initailized from, if that dwg is closed then those variables are no longer valid.
Initailize them where you are going to need them and just let them be garbage collected.
Message 3 of 7
imckillip
in reply to: imckillip

Thanks greatly, that worked perfectly.
Message 4 of 7
Anonymous
in reply to: imckillip

I don't think you can say that works perfectly.

You shouldn't be using the DocumentActivated event for what you're doing.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


wrote in message news:6218410@discussion.autodesk.com...
Thanks greatly, that worked perfectly.
Message 5 of 7
imckillip
in reply to: imckillip

Ok, I'll explain what I'm trying to do.

I'm trying to force certain variables to be set at the opening of AutoCAD and the opening of any new drawing. This must include "opening" a drawing through a third-party layer-loading tool, which loads a template and inserts layers from a network drive.

Do you have a suggestion as to a better way to do this?

I was just watching events using the tool from the SDK and trying to pick one I could use as a trigger. The previous solution seems to work, but I am curious why it shouldn't be used; I'd much rather my code be as clean an proper as possible.

Thanks again for the assistance.
Message 6 of 7
Anonymous
in reply to: imckillip

The DocumentActivated event fires every time you switch documents or switch
applications and bring AutoCAD into the foreground, so that isn't a good
signal.

You can use the DocumentCreated event, but also need to distinguish between
a new drawing (e.g., NEW command) or an existing one that was opened.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


wrote in message news:6219590@discussion.autodesk.com...
Ok, I'll explain what I'm trying to do.

I'm trying to force certain variables to be set at the opening of AutoCAD
and the opening of any new drawing. This must include "opening" a drawing
through a third-party layer-loading tool, which loads a template and inserts
layers from a network drive.

Do you have a suggestion as to a better way to do this?

I was just watching events using the tool from the SDK and trying to pick
one I could use as a trigger. The previous solution seems to work, but I am
curious why it shouldn't be used; I'd much rather my code be as clean an
proper as possible.

Thanks again for the assistance.
Message 7 of 7
cadMeUp
in reply to: imckillip

Check out the attached file for a general way of going about it. The 'DocumentMonitor' class could handle the third-party layer loading possibly, and set-up your variables. The 'DocumentMonitor' keeps it mostly clean from your IExtensionApplication. This is one general way, anyway.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost