how to open a dwg file

how to open a dwg file

Anonymous
Not applicable
8,198 Views
6 Replies
Message 1 of 7

how to open a dwg file

Anonymous
Not applicable

I try to write a program in C# to open dwg files from a dirctory and do some work after open it such as zome extend and save them.

 

To open file I wrote bellow code:

 

DocumentCollection

dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;

dm.Open(

"C:\\test.dwg",true,"");

But its show an error when its run : Its say: Invalid execution context.

Is any idea how to open cad files by c# please?  Many thanks Man Happy in advance.

0 Likes
8,199 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Have you looked at the developer's guide

0 Likes
Message 3 of 7

Anonymous
Not applicable

Hi.

 

Try this:

 

        [CommandMethod("OpenDrawing", CommandFlags.Session)]
        public static void OpenDrawing()
        {
            string strFileName = "C:\\yourfile.dwg";
            DocumentCollection acDocMgr = Application.DocumentManager;

            if (File.Exists(strFileName))
            {
                acDocMgr.Open(strFileName, false);
            }
            else
            {
                acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName + " does not exist.");
            }
        }

Message 4 of 7

Anonymous
Not applicable

Goodmorning all,

sorry for my english but i'm learning it.

I have to open a file dwg with VS2010.i have installed autocad 2010,framework 4 but the program doesn't work.

I'm start to learn programming few day ago, so probably i wrong something. Are there someone that can help me?

i found to autodesk .net guide the code to opens a dwg file. i used as reference acmgd.dll and acdbmgd.dll that i founf on obyect ARX 2010 in inc-x64 directory. (windows7 64 bit).

I compiled programm (0 errors) but when i try to debug it there is an error that i don't understand. i've done a screen on attached.

 

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;


namespace programma
{
    class programm
    {
        static void Main(string[] args)
        {
            OpenDrawing();
        }

        [CommandMethod("OpenDrawing", CommandFlags.Session)]
        public static void OpenDrawing()
        {
            string strFileName = "C:\\prova.dwg";
            DocumentCollection acDocMgr = Application.DocumentManager;

            if (File.Exists(strFileName))
            {
                acDocMgr.Open(strFileName, false);
            }
            else
            {
                acDocMgr.MdiActiveDocument.Editor.WriteMessage("Fi​le " + strFileName + " does not exist.");
            }
        }
    }

}

 

where is the mistake?

thanks for all reply

0 Likes
Message 5 of 7

jamierobertson1
Enthusiast
Enthusiast

Try .NET framework 3.5.

0 Likes
Message 6 of 7

Norman_Yuan
Mentor
Mentor

Seeing static void Main(...) in your code, I assume you are doing a stand-alone exe app (an console exe app?), thus, your code will not work.

 

You CANNOT use AutoCAD .NET API (acdbmgd.dll/acmgd.dll) in external application. You need to create a class library project to produce a DLL and load it inside AutoCAD with AutoCAD command "NETLOAD". Also, you'd better set your project to target .NET3.x.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

Anonymous
Not applicable

already run with NET framework 3.5. and didn't work.

 

ah, so i can't create a program in vs2010 that can open a dwx without open autocad or while autocad isn't running,is it?

because i have to create (or find) a vs2010 program that can view a .dwg file without open autocad.

i through it was the way.

Do you know something about it?

thank you

0 Likes