Author Topic: From Com to Managed  (Read 7839 times)

0 Members and 1 Guest are viewing this topic.

Draftek

  • Guest
From Com to Managed
« on: October 06, 2005, 09:14:08 AM »
I'm writing a c# batch program for autocad. I wanted to start from a windows app and run autocad from the app so I'm using the Com interop. I also want to take advantage of the managed classes once I have the connection.

The developer help says:
Quote
To get the .NET object from a COM object, use the FromAcadXxx static function. For example, Database.FromAcadDatabase gets the .NET database object from the COM database object.
I have this so far:
Code: [Select]
Autodesk.AutoCAD.Interop.AcadApplication oApp = oAcad.Application;
Autodesk.AutoCAD.Interop.AcadDocument oDoc = oApp.ActiveDocument;
Autodesk.AutoCAD.Interop.Common.AcadDatabase oData = oDoc.Database;
Autodesk.AutoCAD.DatabaseServices.Database nDatabase = Database.FromAcadDatabase(oDoc.Database);

The last line give me an exception: "The cast is not valid"

Am I doing this wrong?

TR

  • Guest
Re: From Com to Managed
« Reply #1 on: October 06, 2005, 09:42:08 AM »
Are you trying to run managed code from a standalone .exe file? If so, I don't think that's possible. As far as I know managed code needs to be loaded via the netload command or from the registry (like the layer manager is).


If you are trying this from a netloaded .dll forget everything I just said.

Draftek

  • Guest
Re: From Com to Managed
« Reply #2 on: October 06, 2005, 09:53:23 AM »
You are right.

So, in order to get managed code from com, you must first start with managed code?
 
That sux, nor does it make any sense, to me.

Thanks.

TR

  • Guest
Re: From Com to Managed
« Reply #3 on: October 06, 2005, 10:09:01 AM »
If you accessing the drawing via COM Interop why not just continue doing what you need to do in a COM fashion. If you reflect on Autodesk.AutoCAD.Interop and Autodesk.AutoCAD.Interop.Common you will see that everything you need is accessible to you.

From what I understand the acmgd.dll and acdbmgd.dll contain the code required to run your applications from within AutoCAD, allowing you to define commands and such. It's intent was a .NET interface comparable to ObjectARX. Of course I may be wrong.

Draftek

  • Guest
Re: From Com to Managed
« Reply #4 on: October 06, 2005, 10:30:28 AM »
Your right. I think I am stupid and still thinking in 'vb6' activex mode.

As I'm developing in .Net, I was going to write utility classes like I did in vb6 (com). I could pass the autocad application to the com (in vb6) object and use my functions no matter whether I was in or out of process with AutoCAD.

Now, it appears for .Net, I will have one method for a com connection and a different one for managed code to do the same thing.

Glenn R

  • Guest
Re: From Com to Managed
« Reply #5 on: October 07, 2005, 07:57:20 AM »
Have you tried this?

Autodesk.AutoCAD.DatabaseServices.Database nDatabase = (Database)Database.FromAcadDatabase(oDoc.Database);

Glenn R

  • Guest
Re: From Com to Managed
« Reply #6 on: October 07, 2005, 09:18:19 AM »
Upon further thought, I have a question...Why run AutoCAD from an out of process client, when it's clearly on the machine in the first place?

Do everything inside AutoCAD to begin with...load your program in autocad and away you go...that's how a I run my C# batches. Just today I knocked up a quick dialog interface batch program to run through a selected set of drawings and change the PROJECTNAME system variable in each.
I fed it a job we received from an external source (200 drawings) and it did it in less than 20 seconds.

When you run in process compared to out of process, the speed difference in my tests (in a few languages) has been an increase by a factor of 10 AT LEAST (especially the 'C' family).

Cheers,
Glenn.

PS I don't think what you want to do will work...the .NET managed environment is inherently designed to run IN PROCESS, not OUT.
Your .exe is firing Acad from out fo process, then trying to access .NET stuff from out of proces.............

Draftek

  • Guest
Re: From Com to Managed
« Reply #7 on: October 07, 2005, 04:26:21 PM »
Thanks, Glenn.

Yes, I tried the casting to no avail.

And yes, I came to the same conclusion. I'm just going to start all batches from Autocad now.

Thanks for answering.