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

How to open DWG file in Autocad 2009 in C# ?

23 REPLIES 23
Reply
Message 1 of 24
wperaud
3220 Views, 23 Replies

How to open DWG file in Autocad 2009 in C# ?

Hello,

I'm developping a dll for Autocad 2009.

I would like to know, how to open a DWG file ?

I tried :
Database db = new Database();
db.ReadDwgFile(file.Path, FileOpenMode.OpenForReadAndAllShare, true, "");
---> does nothing

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(file.Path);
--> Error concerning Execution Context.

Thank you
23 REPLIES 23
Message 21 of 24
wperaud
in reply to: wperaud

Oh !! So obvious !! I don't know why I didn't think about it ^^

Thank you
Message 22 of 24
ViktorCAD
in reply to: wperaud

Tony, help me understand your answer (its been several months since i did anything to autocad .net api)...

I'm seeing this problem in my project, i have a form with a list box (non-wpf) and in the double click event of the listbox i wired it up to go to a function that opens a file. I do something like this:

docs = Application.DocumentManager
docs.Open(fname, False, "")

Of course like you mention this opens the file but it is not active, and I understand the reason because the main command that executes my form is not run out of a session but out of a document.

So i'll make another command, that runs in session and have it open a document, right? but how do i pass it the filename? i've never done that...

so in my command:

_
Public Sub OpenDocumentCmd()


End Sub

Can i just pass to the sub?

Thanks,
Viktor.
Message 23 of 24
Anonymous
in reply to: wperaud

First, if your dialog box is being shown modally, nothing is going to happen
until it's closed, so do you really want to use a modal dialog?

The command you invoke from your control event handler via
SendStringToExeucte() can access whatever data it needs, from static members
whose values you set just before you invoke the command.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


wrote in message news:6204854@discussion.autodesk.com...
Tony, help me understand your answer (its been several months since i did
anything to autocad .net api)... I'm seeing this problem in my project, i
have a form with a list box (non-wpf) and in the double click event of the
listbox i wired it up to go to a function that opens a file. I do something
like this: docs = Application.DocumentManager docs.Open(fname, False, "") Of
course like you mention this opens the file but it is not active, and I
understand the reason because the main command that executes my form is not
run out of a session but out of a document. So i'll make another command,
that runs in session and have it open a document, right? but how do i pass
it the filename? i've never done that... so in my command: _ Public Sub
OpenDocumentCmd() End Sub Can i just pass to the sub? Thanks, Viktor.
Message 24 of 24
wperaud
in reply to: wperaud

Here is an example :
{code}
public class FileManager{

// Defines the current file managed by the application
public static String CurrentFilePath;

// Set an open the current file
public void OpenFile(String filePath)
{
// Set
this.CurrentFilePath = filePath;
// Call the Autocad custom command in order to open the file
acadApp.DocumentManager.MdiActiveDocument.SendStringToExecute("OpenFile\n", false, false, false);
}

}
{code}

{code}
[CommandMethod("OpenFile", CommandFlags.Session)]
static public void OpenFile()
{
DocumentCollection dm = acadApp.DocumentManager;
if (System.IO.File.Exists(FileManager.CurrentFilePath)
dm.Open(FileManager.CurrentFilePath, false);
}
{code}

Instead of a static attribute or method, the path can be retrieved thanks to a method.
As an example, I use the Singleton Pattern to the file manager, and get the filepath like this :
{code}
FileManager.Instance().GetCurrentFilePath();
{code}

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