.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
3184 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 2 of 24
Anonymous
in reply to: wperaud


Here's how you would do it in VB - I'll leave it to
you to convert.

 

Joe ...

 


size=2>
 


size=2>Imports
oAcadApp =
Autodesk.AutoCAD.ApplicationServices.Application


oAcadApp.DocumentManager.Open(


color=#a31515 size=2>"C:\Drawing1.dwg"
,
color=#0000ff size=2>False

size=2>,

size=2>""
)



style="BORDER-LEFT: #000000 2px solid; PADDING-LEFT: 5px; PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px">
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
Message 3 of 24
Anonymous
in reply to: wperaud

You can't open a drawing file from a command handler unless
you specify CommandFlags.Session in the CommandMethod
attribute.

--
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:6200812@discussion.autodesk.com...
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
Message 4 of 24
Anonymous
in reply to: wperaud

{quote}

Joe Sutphin wrote:

Here's how you would do it in VB - I'll leave it to you to convert.

{quote}

He doesn't have to convert it, because what you posted is the
C# code from his original post, converted to VB.NET, exactly.

ROTFL

--
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");
Message 5 of 24
Anonymous
in reply to: wperaud

You may have to do that in C# but not in VB.

Joe ...

"Tony Tanzillo" wrote in message
news:6201005@discussion.autodesk.com...
You can't open a drawing file from a command handler unless
you specify CommandFlags.Session in the CommandMethod
attribute.

--
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:6200812@discussion.autodesk.com...
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
Message 6 of 24
Anonymous
in reply to: wperaud

You must have seen yourself in the mirror - I'd be laughing too.

Joe ...


"Tony Tanzillo" wrote in message
news:6201006@discussion.autodesk.com...
{quote}

Joe Sutphin wrote:

Here's how you would do it in VB - I'll leave it to you to convert.

{quote}

He doesn't have to convert it, because what you posted is the
C# code from his original post, converted to VB.NET, exactly.

ROTFL

--
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");
Message 7 of 24
Anonymous
in reply to: wperaud

Wrong again, Bozo.

AutoCAD has no idea what langauge a .NET program was written in.

You're about as dumb as they get.

--
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");


"Joe Sutphin" wrote in message
news:6201060@discussion.autodesk.com...
You may have to do that in C# but not in VB.

Joe ...

"Tony Tanzillo" wrote in message
news:6201005@discussion.autodesk.com...
You can't open a drawing file from a command handler unless
you specify CommandFlags.Session in the CommandMethod
attribute.

--
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:6200812@discussion.autodesk.com...
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
Message 8 of 24
Anonymous
in reply to: wperaud

What's wrong Joe, err... I mean 'steve', you don't like it
when someone points out just how helpless you actually are?

You don't know what you're doing, and everyone around
here knows it, so just give it up.

--
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");


"Joe Sutphin" wrote in message
news:6201061@discussion.autodesk.com...
You must have seen yourself in the mirror - I'd be laughing too.

Joe ...


"Tony Tanzillo" wrote in message
news:6201006@discussion.autodesk.com...
{quote}

Joe Sutphin wrote:

Here's how you would do it in VB - I'll leave it to you to convert.

{quote}

He doesn't have to convert it, because what you posted is the
C# code from his original post, converted to VB.NET, exactly.

ROTFL

--
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");
Message 9 of 24
Anonymous
in reply to: wperaud

Obviously you're shooting off your mouth again without knowing what you're
talking about.

I run my code before I post it. Enough said?

It works just fine the way I posted. Once again you don't everything [or
even anything for that matter] especially when it comes to VB.


Joe ...


"Tony Tanzillo" wrote in message
news:6201116@discussion.autodesk.com...
Wrong again, Bozo.

AutoCAD has no idea what langauge a .NET program was written in.

You're about as dumb as they get.

--
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");


"Joe Sutphin" wrote in message
news:6201060@discussion.autodesk.com...
You may have to do that in C# but not in VB.

Joe ...

"Tony Tanzillo" wrote in message
news:6201005@discussion.autodesk.com...
You can't open a drawing file from a command handler unless
you specify CommandFlags.Session in the CommandMethod
attribute.

--
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:6200812@discussion.autodesk.com...
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
Message 10 of 24
Anonymous
in reply to: wperaud

You lame ass moron - you're the one that does know what they're talking
about.

Guess it's time for you to get up off the floor and figure out what you did
wrong.

Joe ...


"Tony Tanzillo" wrote in message
news:6201120@discussion.autodesk.com...
What's wrong Joe, err... I mean 'steve', you don't like it
when someone points out just how helpless you actually are?

You don't know what you're doing, and everyone around
here knows it, so just give it up.

--
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");


"Joe Sutphin" wrote in message
news:6201061@discussion.autodesk.com...
You must have seen yourself in the mirror - I'd be laughing too.

Joe ...


"Tony Tanzillo" wrote in message
news:6201006@discussion.autodesk.com...
{quote}

Joe Sutphin wrote:

Here's how you would do it in VB - I'll leave it to you to convert.

{quote}

He doesn't have to convert it, because what you posted is the
C# code from his original post, converted to VB.NET, exactly.

ROTFL

--
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");
Message 11 of 24
Anonymous
in reply to: wperaud


at the risk of starting an extended discussion
...

{quote}Re: How to open DWG file in Autocad 2009 in
C# ?

{quote}

 

to you, Does OPEN mean visible in the
Editor

or open in a database .. ?

 

{code}

       
//CodeHimBelongaKdub © 
June2009
       

        [CommandMethod("testOPEN",
CommandFlags.Session)]
        static
public void testyOpen()
       
{
           
DocumentCollection dm =
AcadApp.DocumentManager;
           
Editor ed = dm.MdiActiveDocument.Editor;

 


face=Arial>           
PromptOpenFileOptions POFO = new
PromptOpenFileOptions(
               
"Select a drawing file" );

 


face=Arial>           
POFO.InitialDirectory =
"F:\\_KDUB_DEV";
           
POFO.Filter = "AutoCAD DWG Files|*.dwg|AutoCAD DXF Files|*.dxf";

 


face=Arial>           
POFO.DialogCaption = "Select a file,
dude.";          

 


face=Arial>           
PromptFileNameResult pr = ed.GetFileNameForOpen(POFO);

 


face=Arial>            if
(pr.Status ==
PromptStatus.OK)
           
{
               
dm.Open(pr.StringResult);
           
}
        }

 

{code}

 



style="BORDER-LEFT: #000000 2px solid; PADDING-LEFT: 5px; PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px">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
Message 12 of 24
kdub_nz
in reply to: wperaud

I really dislike the formatting on this forum !

{code}
//CodeHimBelongaKdub © June2009

[CommandMethod("testOPEN", CommandFlags.Session)]
static public void testyOpen()
{
DocumentCollection dm = AcadApp.DocumentManager;
Editor ed = dm.MdiActiveDocument.Editor;

PromptOpenFileOptions POFO = new PromptOpenFileOptions(
"Select a drawing file" );

POFO.InitialDirectory = "F:\\_KDUB_DEV";
POFO.Filter = "AutoCAD DWG Files|*.dwg|AutoCAD DXF Files|*.dxf";

POFO.DialogCaption = "Select a file, dude.";

PromptFileNameResult pr = ed.GetFileNameForOpen(POFO);

if (pr.Status == PromptStatus.OK)
{
dm.Open(pr.StringResult);
}
}
{code}
//

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 13 of 24
Anonymous
in reply to: wperaud

{quote}

Obviously you're shooting off your mouth again without knowing what you're
talking about.

I run my code before I post it. Enough said?

It works just fine the way I posted. Once again you don't everything [or
even anything for that matter] especially when it comes to VB.

{quote}

What works fine just the way you posted? Opening the document?

Duh!

Merely opening the document is not the problem. It never was. That isn't
what doesn't work.

Activating the opened document in order to use it to do anything that
requires the document to be active, is the problem..... stupid.

And no, doing that does not work without the CommandFlags.Session attribute
on the calling code.

Once again, we get to see the 'true you'.

--
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");
Message 14 of 24
wperaud
in reply to: wperaud

First of all, thank you everyone.

Yes Kerry Brown, when i say that I want to open a file, I want it to be visible, and ready to be modified.

****
1 : I tried Joe Sutphin's solution.

I just had to convert the "Import" line into a "using" one 🙂

I get the same error just like I said :
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(file.Path);
--> Error in french "Contexte d'excecution Incorrect"
--> Must be quit the same in english : "Excecution Context Incorrect" ?

****
2 : I tried Kerry Brown's solution

If I copy/paste your code to my main class (defined by the "[assembly: CommandClass(typeof(MyClass))]") , it works.

Tony Tanzillo is right, CommandFlags.Session attribute has to be mentionned.

But this solution doesn't really solve my problem.

****
My main class (defined by the "[assembly: CommandClass(typeof(MyClass))]"), just enables a custom Ribbon and defines which action has to be called on each button's click.

I have a class that manages some local files. It presentes these files in a listview, and when the user double click on the item, I would like to get the filepath and send it to a method that only does "opening file".
I aldready get the filePath and send it to the method.

For my test, i copied/pasted your code Kenny. (because I works in the main class)

I tried :
-- adding the CommandFlags above the method
-- putting this method as a public static one in the main class
---> I keep having the "Execution Context Error"
Message 15 of 24
Anonymous
in reply to: wperaud

Yes, you do need the CommandFlags.Session attribute if the call to Open() is
made in the command method. But, I'm not clear on where your call to Open()
is coming from.

Is it coming from a function with the CommandMethod attribute, or from a
double-click handler of a form containing a ListView ? If the latter, is
the form being shown modally or modelessly?

The easiest way to get help on this, is to try to reproduce the error in a
sample project with the minimum code required to cause it to happen, and
post the project here.

--
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:6201429@discussion.autodesk.com...
First of all, thank you everyone. Yes Kerry Brown, when i say that I want to
open a file, I want it to be visible, and ready to be modified. **** 1 : I
tried Joe Sutphin's solution. I just had to convert the "Import" line into a
"using" one 🙂 I get the same error just like I said :
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(file.Path);
--> Error in french "Contexte d'excecution Incorrect" --> Must be quit the
same in english : "Excecution Context Incorrect" ? **** 2 : I tried Kerry
Brown's solution If I copy/paste your code to my main class (defined by the
"[assembly: CommandClass(typeof(MyClass))]") , it works. Tony Tanzillo is
right, CommandFlags.Session attribute has to be mentionned. But this
solution doesn't really solve my problem. **** My main class (defined by the
"[assembly: CommandClass(typeof(MyClass))]"), just enables a custom Ribbon
and defines which action has to be called on each button's click. I have a
class that manages some local files. It presentes these files in a listview,
and when the user double click on the item, I would like to get the filepath
and send it to a method that only does "opening file". I aldready get the
filePath and send it to the method. For my test, i copied/pasted your code
Kenny. (because I works in the main class) I tried : -- adding the
CommandFlags above the method -- putting this method as a public static one
in the main class ---> I keep having the "Execution Context Error"
Message 16 of 24
wperaud
in reply to: wperaud

I attached a zip contening 4 files (3 classes) :
--- Class RibbonActions
- RessourceDictionary.xaml : defines the GUI
- RibbonActions.cs : defines the behavior
--- Class Acad
Class that defines the assembly
--- Class FileManager
Class that defines a "file browser" (listView) and the behavior on a double click (Open the file)
The Browser is shown as modal :
{code}acadApp.ShowModalWindow(FilesManager); {code}
********
When I put the open file in the createRibbon() of the Acad class ((and add the commandFlags)) , it works
When I put the open file in the OpenFile(String filePath) of the FileManager class (( even with flags )), it doen't works (( Execution Context Incorrect ))

I hope it would help me being clear 🙂
Message 17 of 24
Anonymous
in reply to: wperaud

The reason you're having this problem is because WPF executes
control code and events in a non-AutoCAD thread, which means
that you can't do very much with the AutoCAD API from a handler
of a WPF UI element.

One way to deal with it is to define a standard AutoCAD command
(which can also have the CommandFlags.Session flag) that calls
the Open() method, and then invoke the command using the
DocumentCollection's SendStringToExecute() method from the
event handler of your ListBox.

Another way to do it, is to call the DocumentCollection's
ExecuteInApplicationContext() method, and pass it the method
that contains the call to Open(), but for that to work, you must
close your Window right after calling ExecuteInApplicationContext,
because the callback will not run until that happens.

--
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:6201743@discussion.autodesk.com...
I attached a zip contening 4 files (3 classes) : --- Class RibbonActions -
RessourceDictionary.xaml : defines the GUI - RibbonActions.cs : defines the
behavior --- Class Acad Class that defines the assembly --- Class
FileManager Class that defines a "file browser" (listView) and the behavior
on a double click (Open the file) The Browser is shown as modal :
{code}acadApp.ShowModalWindow(FilesManager); {code} ******** When I put the
open file in the createRibbon() of the Acad class ((and add the
commandFlags)) , it works When I put the open file in the OpenFile(String
filePath) of the FileManager class (( even with flags )), it doen't works
(( Execution Context Incorrect )) I hope it would help me being clear 🙂
Message 18 of 24
apt42
in reply to: wperaud

Database db = new Database();
db.ReadDwgFile(file.Path, FileOpenMode.OpenForReadAndAllShare, true, "");


Change it to :

Database db = new Database(false, true);

using (db)
{
try
{
db.ReadDwgFile(file.Path, FileOpenMode.OpenForReadAndAllShare, false, "");
}
catch (System.Exception)
{
ed.WriteMessage("Error in the location specified");

return;
}
.................
}

private static Editor ed
{
get
{
return Application.DocumentManager.MdiActiveDocument.Editor;
}
}
Message 19 of 24
wperaud
in reply to: wperaud

@apt42 :
I tried your solution.
Nothing happens. No error, but the drawing doesn't appear.

@Tony Tanzillo :
I tried the SendStringToExecute solution.
I think this solution best suits my application behavior (as I may have to open a file, from elsewhere in the application (( without the listview )) )
It works.

I just have a last question 🙂
When I use the SendStringToExecute("testOPEN", false, false, true);
The command is writen, but the editor is waiting me to press enter.
If I put the last parameter to false, the command isn't written anymore, but, How can I automatically press enter ? ( without doing it with my finger 🙂 )

Thank you all again for your help.
Message 20 of 24
Anonymous
in reply to: wperaud

SendStringToExecute("TESTOPEN\n", ...)

You have to include a newline at the end of each input.

--
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:6202235@discussion.autodesk.com...
@apt42 : I tried your solution. Nothing happens. No error, but the drawing
doesn't appear. @Tony Tanzillo : I tried the SendStringToExecute solution. I
think this solution best suits my application behavior (as I may have to
open a file, from elsewhere in the application (( without the listview )) )
It works. I just have a last question 🙂 When I use the
SendStringToExecute("testOPEN", false, false, true); The command is writen,
but the editor is waiting me to press enter. If I put the last parameter to
false, the command isn't written anymore, but, How can I automatically press
enter ? ( without doing it with my finger 🙂 ) Thank you all again for your
help.

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