<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Setting global variables on opening a drawing (C#) in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521927#M69755</link>
    <description>Thanks greatly, that worked perfectly.</description>
    <pubDate>Tue, 14 Jul 2009 17:13:00 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2009-07-14T17:13:00Z</dc:date>
    <item>
      <title>Setting global variables on opening a drawing (C#)</title>
      <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521925#M69753</link>
      <description>Hi All&lt;BR /&gt;
&lt;BR /&gt;
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". &lt;BR /&gt;
&lt;BR /&gt;
My code is posted below; any insight would be greatly appreciated.&lt;BR /&gt;
&lt;BR /&gt;
{code}&lt;BR /&gt;
    public class Init : Autodesk.AutoCAD.Runtime.IExtensionApplication&lt;BR /&gt;
    {&lt;BR /&gt;
        Database db = HostApplicationServices.WorkingDatabase;&lt;BR /&gt;
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;&lt;BR /&gt;
&lt;BR /&gt;
        public void Initialize()&lt;BR /&gt;
        {&lt;BR /&gt;
            // Notify the user of the start of initialization&lt;BR /&gt;
            ed.WriteMessage("\nIMine Initialization... ");&lt;BR /&gt;
&lt;BR /&gt;
            // Set system variables in accordance with mine standards&lt;BR /&gt;
            Application.SetSystemVariable("MENUECHO", 1);&lt;BR /&gt;
            Application.SetSystemVariable("SDI", 1);&lt;BR /&gt;
            Application.SetSystemVariable("PSLTSCALE", 0);&lt;BR /&gt;
&lt;BR /&gt;
            // Start a callback function to detect the opening of a new drawing&lt;BR /&gt;
            Application.DocumentManager.DocumentActivated +=new DocumentCollectionEventHandler(resetVariables);&lt;BR /&gt;
&lt;BR /&gt;
            // Notify the user of completion of initialization&lt;BR /&gt;
            ed.WriteMessage("Complete!\n");&lt;BR /&gt;
        }&lt;BR /&gt;
&lt;BR /&gt;
        public void Terminate(){}&lt;BR /&gt;
&lt;BR /&gt;
        public void resetVariables(object sender, DocumentCollectionEventArgs e)&lt;BR /&gt;
        {&lt;BR /&gt;
            ed.WriteMessage("\nIntercepted a DocumentCollection event");&lt;BR /&gt;
        }&lt;BR /&gt;
    }&lt;BR /&gt;
{code}&lt;BR /&gt;
&lt;BR /&gt;
- Iain</description>
      <pubDate>Mon, 13 Jul 2009 18:43:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521925#M69753</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-07-13T18:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Setting global variables on opening a drawing (C#)</title>
      <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521926#M69754</link>
      <description>public void resetVariables(object sender, DocumentCollectionEventArgs e)&lt;BR /&gt;
{&lt;BR /&gt;
			if (e.Document != null)&lt;BR /&gt;
			{&lt;BR /&gt;
				Editor ed = e.Document.Editor;&lt;BR /&gt;
				Database db = e.Document.Database;&lt;BR /&gt;
				ed.WriteMessage("\nIntercepted a DocumentCollection event");&lt;BR /&gt;
			}&lt;BR /&gt;
&lt;BR /&gt;
}&lt;BR /&gt;
&lt;BR /&gt;
It's not safe to declare and initialize:&lt;BR /&gt;
Database db = HostApplicationServices.WorkingDatabase;&lt;BR /&gt;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;&lt;BR /&gt;
&lt;BR /&gt;
These are being initialized to the current/active doc and can change, go out of scope, and/or be garbage collected.&lt;BR /&gt;
Think about the dwg that these were initailized from, if that dwg is closed then those variables are no longer valid.&lt;BR /&gt;
Initailize them where you are going to need them and just let them be garbage collected.</description>
      <pubDate>Tue, 14 Jul 2009 15:21:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521926#M69754</guid>
      <dc:creator>cadMeUp</dc:creator>
      <dc:date>2009-07-14T15:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: Setting global variables on opening a drawing (C#)</title>
      <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521927#M69755</link>
      <description>Thanks greatly, that worked perfectly.</description>
      <pubDate>Tue, 14 Jul 2009 17:13:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521927#M69755</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-07-14T17:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Setting global variables on opening a drawing (C#)</title>
      <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521928#M69756</link>
      <description>I don't think you can say that works perfectly.&lt;BR /&gt;
&lt;BR /&gt;
You shouldn't be using the DocumentActivated event for what you're doing.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2009&lt;BR /&gt;
Supporting AutoCAD 2000 through 2009&lt;BR /&gt;
&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
Introducing AcadXTabs 2010:&lt;BR /&gt;
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm&lt;BR /&gt;
&lt;BR /&gt;
Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;IMCKILLIP&gt; wrote in message news:6218410@discussion.autodesk.com...&lt;BR /&gt;
Thanks greatly, that worked perfectly.&lt;/IMCKILLIP&gt;</description>
      <pubDate>Wed, 15 Jul 2009 11:54:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521928#M69756</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-07-15T11:54:23Z</dc:date>
    </item>
    <item>
      <title>Re: Setting global variables on opening a drawing (C#)</title>
      <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521929#M69757</link>
      <description>Ok, I'll explain what I'm trying to do.&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Do you have a suggestion as to a better way to do this?&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Thanks again for the assistance.</description>
      <pubDate>Thu, 16 Jul 2009 10:57:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521929#M69757</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-07-16T10:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: Setting global variables on opening a drawing (C#)</title>
      <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521930#M69758</link>
      <description>The DocumentActivated event fires every time you switch documents or switch &lt;BR /&gt;
applications and bring AutoCAD into the foreground, so that isn't a good &lt;BR /&gt;
signal.&lt;BR /&gt;
&lt;BR /&gt;
You can use the DocumentCreated event, but also need to distinguish between &lt;BR /&gt;
a new drawing (e.g., NEW command) or an existing one that was opened.&lt;BR /&gt;
&lt;BR /&gt;
-- &lt;BR /&gt;
http://www.caddzone.com&lt;BR /&gt;
&lt;BR /&gt;
AcadXTabs: MDI Document Tabs for AutoCAD 2009&lt;BR /&gt;
Supporting AutoCAD 2000 through 2009&lt;BR /&gt;
&lt;BR /&gt;
http://www.acadxtabs.com&lt;BR /&gt;
&lt;BR /&gt;
Introducing AcadXTabs 2010:&lt;BR /&gt;
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm&lt;BR /&gt;
&lt;BR /&gt;
Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;IMCKILLIP&gt; wrote in message news:6219590@discussion.autodesk.com...&lt;BR /&gt;
Ok, I'll explain what I'm trying to do.&lt;BR /&gt;
&lt;BR /&gt;
I'm trying to force certain variables to be set at the opening of AutoCAD &lt;BR /&gt;
and the opening of any new drawing. This must include "opening" a drawing &lt;BR /&gt;
through a third-party layer-loading tool, which loads a template and inserts &lt;BR /&gt;
layers from a network drive.&lt;BR /&gt;
&lt;BR /&gt;
Do you have a suggestion as to a better way to do this?&lt;BR /&gt;
&lt;BR /&gt;
I was just watching events using the tool from the SDK and trying to pick &lt;BR /&gt;
one I could use as a trigger. The previous solution seems to work, but I am &lt;BR /&gt;
curious why it shouldn't be used; I'd much rather my code be as clean an &lt;BR /&gt;
proper as possible.&lt;BR /&gt;
&lt;BR /&gt;
Thanks again for the assistance.&lt;/IMCKILLIP&gt;</description>
      <pubDate>Thu, 16 Jul 2009 20:09:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521930#M69758</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-07-16T20:09:06Z</dc:date>
    </item>
    <item>
      <title>Re: Setting global variables on opening a drawing (C#)</title>
      <link>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521931#M69759</link>
      <description>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.</description>
      <pubDate>Fri, 17 Jul 2009 13:29:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/setting-global-variables-on-opening-a-drawing-c/m-p/2521931#M69759</guid>
      <dc:creator>cadMeUp</dc:creator>
      <dc:date>2009-07-17T13:29:03Z</dc:date>
    </item>
  </channel>
</rss>

