Author Topic: Interop at startup  (Read 9769 times)

0 Members and 1 Guest are viewing this topic.

Chris K

  • Guest
Re: Interop at startup
« Reply #15 on: May 04, 2009, 10:54:06 AM »
Glenn, thanks that was the issue. 

Okay, so where I went wrong was connecting to AutoCAD using the wrong implementation of interoperation, I was trying to use an external connection while running the code from the application.  So, Acad had to finish its' startup before it is available for external connections.  That explains why it wouldn't work on the initial start-up, the session wasn't established yet.  Am I getting that correct?

You're using this:

Code: [Select]
// Cast the current autocad application from .net to COM interop
Autodesk.AutoCAD.Interop.AcadApplication oApp;
oApp = (Autodesk.AutoCAD.Interop.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17.2");

in a dll that runs INSIDE AutoCAD! You only need to use this construct if you're hooking into AutoCAD from an external process. This is a not the way to do what you want as you're ALREADY running inside AutoCAD in the first place!

You also go on to use this:
Code: [Select]
// Get the user preferences on the current profile
                AcadPreferences profile = oApp.Preferences;

                // Configure the default preferences
                try
                {
                    strPaths = oApp.Preferences.Files.SupportPath;
                    strPaths = System.IO.Directory.GetCurrentDirectory() + ";" + strPaths;
                    return strPaths;
                }
                catch (acRuntime.Exception e)
                {
                    MessageBox.Show(e.Message, "IMFTUtilities.ACADUtilities.Preferences.GetSupportPaths");
                    return string.Empty.ToString();
                }

when you could have ignored all that guff and just used this:
Code: [Select]
oApp = acApp.AcadApplication as AcadApplication;
string strPath = oApp.Preferences.Files.SupportPath;

It's late here and I'm tired, but that's pretty much how I remember it.

Glenn R

  • Guest
Re: Interop at startup
« Reply #16 on: May 04, 2009, 01:57:26 PM »
Essentially, yes.

Chris K

  • Guest
Re: Interop at startup
« Reply #17 on: May 04, 2009, 02:03:04 PM »
Cool, thanks.

Glenn R

  • Guest
Re: Interop at startup
« Reply #18 on: May 05, 2009, 06:11:42 PM »
Having reflected on this, it begs the question: why did you use what you did (GetActiveObject) in the first place?

Chris K

  • Guest
Re: Interop at startup
« Reply #19 on: May 06, 2009, 05:52:13 PM »
To be honest, it is an adaption of a vba routine containing similar code.  Looks like I did a poor job porting it to dotnet. I am, as I've said, very green at dotnet programming and C#.  Most of my programming experience has been with MSAccess.  Anything I've learned has been from examples here on The Swamp, a slough of Wrox books, a few AU handouts and a Jerry Winters VB.NET programming book.  I am always looking for more or better references, so if you have any suggestions, I'm game.  I would like to write solid code that will stand the test of time.  Maybe someday.

Hope this explains my errant ways.

Having reflected on this, it begs the question: why did you use what you did (GetActiveObject) in the first place?

Glenn R

  • Guest
Re: Interop at startup
« Reply #20 on: May 06, 2009, 06:26:09 PM »
Hey Chris,

Thanks for the clarification. I re-scanned the thread and I noticed you mentioned you were 'green' as you put it, but I didn't realise that you were coming from an MSAccess background, which I assume is VBA and that .NET and C# was knew to you. I applaud your drive to learn something new and to increase your understanding of AutoCAD programming. It's a big beast, but it's well worth it.

I and other members of this forum, highly recommend this book, as an excellent guide to C# and .NET in general. Personally, from what I have seen of Mr Winters writings, I would not recommend it.

As far as writing code that stands the test of time, I think that's what all here aim for. Keep at it - it's fun, well worth it and you'll get some very good advice from this forum, some knowlegeable people on the adesk forums themselves and Kean Walmsley's (from Autodesk) blog, Through the Interface.

Above all, search here and ask questions.

Cheers,
Glenn.

Chris K

  • Guest
Re: Interop at startup
« Reply #21 on: May 07, 2009, 06:21:55 PM »
Glenn,

Thanks for the feedback.  I'll pick-up Pro C#, looks interesting enough.  Thanks also for the other sources, I've seen Kean's blog, but so far it tends to be all little over my head.  Thanks again for the help.

Regards,
Chris