<?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: Documents.Open fails, while ActiveDocument succeeds in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862159#M18023</link>
    <description>&lt;P&gt;Hi Owen,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This post (and my knowledge of the subject) is now over a decade old, so I'm afraid I'm going to be of limited help.&lt;BR /&gt;&lt;BR /&gt;Hopefully someone who has hit this issue more recently can chime in.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kean&lt;/P&gt;</description>
    <pubDate>Wed, 11 Nov 2020 16:00:36 GMT</pubDate>
    <dc:creator>kean_walmsley</dc:creator>
    <dc:date>2020-11-11T16:00:36Z</dc:date>
    <item>
      <title>Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9855553#M18019</link>
      <description>&lt;P&gt;I'm at a loss as to why this is, here is the code, I've missed something important I imagine:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;   {
            // Open AutoCAD and get it as a variable in C#...
            AcadApplication AcadApp;
            AcadApp = new AcadApplication();

            // Make sure the AutoCAD windows are visible
            // (But note that making it invisible probably speeds up processing.)
            AcadApp.Application.Visible = true;

            AcadDocument AcadDoc;
#if false
            // Get the active document, which in this case is the default empty document which
            // This uses the default document AutoCAD starts with, and works
            AcadDoc = AcadApp.ActiveDocument;
#else
            // This opens a document, but any attempt to use it fails
            AcadDoc = AcadApp.Documents.Open(@"D:\Temp\CSHARP.DWG");
#endif
            AcadDoc.Layers.Add("TestLayer");

            // Model space is actually where most of the entities (LINE,CIRCLE,POINT etc) in an AutoCAD DWG file live.
            var ModelSpace = AcadDoc.ModelSpace;
     }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the stuff around "#if false"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The crash occurs at Add ("TestLayer") as if AcadDoc is not valid.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error message is (translated from Italian) the "called has rejected the caller's function call"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 11:13:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9855553#M18019</guid>
      <dc:creator>oransen</dc:creator>
      <dc:date>2020-11-09T11:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9861400#M18020</link>
      <description>&lt;P&gt;It looks like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;AcadDoc = AcadApp.Documents.Open(@"D:\Temp\CSHARP.DWG");&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;does not on its own create a valid AcadDocument. I've found that if I do some "dummy" work before using the AcadDoc then all works fine:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;AcadDocument AcadDoc = AcadApp.Documents.Add();
AcadDoc.Activate();
AcadDoc.Close(false);

// With the above three "dummy" lines above the Open works fine...
AcadDoc = AcadApp.Documents.Open(@"C:\DisegniSviluppati\04.DWG");&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I still think I am missing something...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 10:38:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9861400#M18020</guid>
      <dc:creator>oransen</dc:creator>
      <dc:date>2020-11-11T10:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9861644#M18021</link>
      <description>&lt;P&gt;Hi Owen,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hopefully this will help:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.keanw.com/2010/02/handling-com-calls-rejected-by-autocad-from-an-external-net-application.html" target="_blank"&gt;https://www.keanw.com/2010/02/handling-com-calls-rejected-by-autocad-from-an-external-net-application.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kean&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 12:17:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9861644#M18021</guid>
      <dc:creator>kean_walmsley</dc:creator>
      <dc:date>2020-11-11T12:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9861879#M18022</link>
      <description>&lt;P&gt;Thanks for the pointers. I'd seen you article, but hand not understood it was relevant. I still don't understand all the COM/NET stuff, but from the article you are saying that AutoCAD may not be ready when I make my call to, in this case Open, and I should try again "later".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was hoping that I could simply do...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;#using AutoCAD ;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...and call the various functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With your article I've modified the Open code like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;            AcadDocument AcadDoc = null;

            int iNumTries = 0;
            const int ikMaxTries = 400;
            while (iNumTries &amp;lt; ikMaxTries)
            {
                bool bError = false ;
                try
                {
                    AcadDoc = AcadApp.Documents.Open(@"C:\DisegniSviluppati\04.DWG");
                    AcadDoc.Activate();
                }

                catch (Exception e1)
                {
                    Debug.WriteLine("Catch on try " + iNumTries.ToString() + ", exception: " + e1.Message + "\n");
                    bError = true;
                }

                if (!bError)
                {
                    break;
                }

                if (iNumTries == ikMaxTries)
                {
                    break;
                }

                iNumTries++;
            }

            if (iNumTries == ikMaxTries)
            {
                return;
            }
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...and it works. But it seems a lot of work just to open a drawing file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does the "rejected calls problem" mean I have to have a lot of wrappers and/or deeply understand the article you posted? Or is it just a problem with Open()?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 14:12:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9861879#M18022</guid>
      <dc:creator>oransen</dc:creator>
      <dc:date>2020-11-11T14:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862159#M18023</link>
      <description>&lt;P&gt;Hi Owen,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This post (and my knowledge of the subject) is now over a decade old, so I'm afraid I'm going to be of limited help.&lt;BR /&gt;&lt;BR /&gt;Hopefully someone who has hit this issue more recently can chime in.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kean&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 16:00:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862159#M18023</guid>
      <dc:creator>kean_walmsley</dc:creator>
      <dc:date>2020-11-11T16:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862329#M18024</link>
      <description>&lt;P&gt;Thanks for all your help, I'd never have got this far without it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It looks like this method of accessing AutoCAD with C# is never really used. There's lots of short examples, and that's that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll go back to using C# as a DLL plugin, much safer!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 17:04:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862329#M18024</guid>
      <dc:creator>oransen</dc:creator>
      <dc:date>2020-11-11T17:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862574#M18025</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1206331"&gt;@oransen&lt;/a&gt;&amp;nbsp; a écrit&amp;nbsp;:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll go back to using C# as a DLL plugin, much safer!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I totally agree with this, anyway, here's a way with the COM API from a standalone application.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;// get or create AutoCAD.Application
AcadApplication acApp = null;
const string strProgId = "AutoCAD.Application";

try
{
	acApp = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch
{
	try
	{
		acApp = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
	}
	catch
	{
		MessageBox.Show("Cannot create an instance of 'AutoCAD.Application'.");
		return;
	}
}
try
{
	//wait for AutoCAD is visible
	while (true)
	{
		try { acApp.Visible = true; break; }
		catch { }
	}

	acApp.Documents.Open(@"D:\Temp\CSHARP.DWG", true);

	var acadDoc = acApp.ActiveDocument;
	// do your stuff with acadDoc here
}
catch (System.Exception ex)
{
	MessageBox.Show("Error: " + ex.Message);
}&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 11 Nov 2020 18:37:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862574#M18025</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-11-11T18:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: Documents.Open fails, while ActiveDocument succeeds</title>
      <link>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862676#M18026</link>
      <description>&lt;P&gt;Thanks for that, but it looks a bit tricky, and if I have to do it for other commands...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or are you saying it will only happen with the initial Open...?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 19:18:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/documents-open-fails-while-activedocument-succeeds/m-p/9862676#M18026</guid>
      <dc:creator>oransen</dc:creator>
      <dc:date>2020-11-11T19:18:31Z</dc:date>
    </item>
  </channel>
</rss>

