Documents.Open fails, while ActiveDocument succeeds

Documents.Open fails, while ActiveDocument succeeds

oransen
Collaborator Collaborator
2,653 Views
7 Replies
Message 1 of 8

Documents.Open fails, while ActiveDocument succeeds

oransen
Collaborator
Collaborator

I'm at a loss as to why this is, here is the code, I've missed something important I imagine:

 

   {
            // 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;
     }

 

See the stuff around "#if false"

 

The crash occurs at Add ("TestLayer") as if AcadDoc is not valid.

 

The error message is (translated from Italian) the "called has rejected the caller's function call"

 

 

0 Likes
Accepted solutions (1)
2,654 Views
7 Replies
Replies (7)
Message 2 of 8

oransen
Collaborator
Collaborator

It looks like

 

 

AcadDoc = AcadApp.Documents.Open(@"D:\Temp\CSHARP.DWG");

 

 

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:

 

 

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");

 

 

I still think I am missing something...

 

 

 

0 Likes
Message 3 of 8

kean_walmsley
Autodesk
Autodesk
Accepted solution

Hi Owen,

 

Hopefully this will help:

 

https://www.keanw.com/2010/02/handling-com-calls-rejected-by-autocad-from-an-external-net-applicatio...

 

Best,

 

Kean



Kean Walmsley

Platform Architect & Evangelist, Autodesk Research

Blog | Twitter
Message 4 of 8

oransen
Collaborator
Collaborator

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".

 

I was hoping that I could simply do...

 

#using AutoCAD ;

 

...and call the various functions.

 

With your article I've modified the Open code like this:

 

            AcadDocument AcadDoc = null;

            int iNumTries = 0;
            const int ikMaxTries = 400;
            while (iNumTries < 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;
            }

 

...and it works. But it seems a lot of work just to open a drawing file.

 

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()?

 

0 Likes
Message 5 of 8

kean_walmsley
Autodesk
Autodesk

Hi Owen,

 

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.

Hopefully someone who has hit this issue more recently can chime in.

 

Best,

 

Kean



Kean Walmsley

Platform Architect & Evangelist, Autodesk Research

Blog | Twitter
Message 6 of 8

oransen
Collaborator
Collaborator

Thanks for all your help, I'd never have got this far without it.

 

It looks like this method of accessing AutoCAD with C# is never really used. There's lots of short examples, and that's that.

 

I'll go back to using C# as a DLL plugin, much safer!

 

0 Likes
Message 7 of 8

_gile
Consultant
Consultant

Hi,

 


@oransen  a écrit :

 

I'll go back to using C# as a DLL plugin, much safer!

 


I totally agree with this, anyway, here's a way with the COM API from a standalone application.

 

// 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);
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

oransen
Collaborator
Collaborator

Thanks for that, but it looks a bit tricky, and if I have to do it for other commands...

 

Or are you saying it will only happen with the initial Open...?

 

0 Likes