.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to open a dwg file

6 REPLIES 6
Reply
Message 1 of 7
Anonymous
6924 Views, 6 Replies

how to open a dwg file

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.

6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: Anonymous

Have you looked at the developer's guide

Message 3 of 7
Anonymous
in reply to: Anonymous

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
in reply to: Anonymous

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

Message 5 of 7
jamierobertson1
in reply to: Anonymous

Try .NET framework 3.5.

Message 6 of 7
norman.yuan
in reply to: Anonymous

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

Message 7 of 7
Anonymous
in reply to: norman.yuan

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost