.NET

.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 8 (1,015 Views)
Accepted Solution

Open a drawing file programmatically

1015 Views, 7 Replies
04-09-2012 10:57 PM

Dear. Developpers.

 

I am trying to open a drawing file programmatically(automatically).

So for example.

 

If I have a file name "test.dwg" and run some custom command like 'openfile", AutoCAD opens "test.dwg" without any prompt.

 

The following code DOESN'T do the job I want.

 

Application.DocumentManager.MdiActiveDocument.SendStringToExecute("OPEN", true, false, true)

 

The problem is I have to hit enter after the above code is run.

 

Would there be any way to do this?

Use following code where DictionaryEntry.Key is the name of the drawing to be opened (full name incl. path)

ForEach DictionaryEntry In DictionaryDrawingNames

     If DictionaryEntry.Value = TrueThen

          CurrentDrawings.Open(DictionaryEntry.Key,False)

  EndIf

Next

Cheers

Hi,

 

depending on the session-flag in the command-definition when opening a drawing it will be made the active one.

 

If you have a list of drawings already opened and want to make a specific one the active one then set the Application.DocumentManager.MdiActiveDocument = newDoc

or use the COM-version like

ctype(newDoc,Interop.AcadDocument).Activate

 

- alfred -

Active Member
TobyRosenberg8728
Posts: 7
Registered: ‎03-14-2012
Message 2 of 8 (1,014 Views)

Re: Open a drawing file programmatically

04-09-2012 11:08 PM in reply to: dynamicscope

Use following code where DictionaryEntry.Key is the name of the drawing to be opened (full name incl. path)

ForEach DictionaryEntry In DictionaryDrawingNames

     If DictionaryEntry.Value = TrueThen

          CurrentDrawings.Open(DictionaryEntry.Key,False)

  EndIf

Next

Cheers

Active Member
TobyRosenberg8728
Posts: 7
Registered: ‎03-14-2012
Message 3 of 8 (1,013 Views)

Re: Open a drawing file programmatically

04-09-2012 11:09 PM in reply to: TobyRosenberg8728

Sorry: forgot to put in the definition for CurrentDrawings which is

Dim CurrentDrawings AsDocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager

Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 4 of 8 (1,007 Views)

Re: Open a drawing file programmatically

04-09-2012 11:33 PM in reply to: TobyRosenberg8728

Great~! Thx.

 

Could I ask one more question?

 

Do you also know how to open on a currently opened document(window)?

For example, when you first run AutoCAD, "drawing1.dwg" document is opened by default.

So I want to place(or replace) my 'test.dwg" on that document.

 

The code above opens the file in a new window.

 

Thank you a lot for your help. =)

*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 5 of 8 (991 Views)

Re: Open a drawing file programmatically

04-10-2012 12:56 AM in reply to: dynamicscope

Hi,

 

>> The problem is I have to hit enter after the above code is run

You have not sent the "new line" to the commandline, you just sent "OPEN", if you want to work with SendStringToExecute (what I would not prefere if there is another solution available), then do it including sending the carridge-return:

Application.DocumentManager.MdiActiveDocument.SendStringToExecute("OPEN" & vbCr, true, false, true)

 

>> Do you also know how to open on a currently opened document(window)?

>> For example, when you first run AutoCAD, "drawing1.dwg" document is opened by default.

>> So I want to place(or replace) my 'test.dwg" on that document.

Activate the "test.dwg" and close the other drawing? But I'm not sure if that is what you are asking for?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 6 of 8 (980 Views)

Re: Open a drawing file programmatically

04-10-2012 02:05 AM in reply to: dynamicscope

Thank you.

 

How can I active a document?

*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 7 of 8 (977 Views)

Re: Open a drawing file programmatically

04-10-2012 02:19 AM in reply to: dynamicscope

Hi,

 

depending on the session-flag in the command-definition when opening a drawing it will be made the active one.

 

If you have a list of drawings already opened and want to make a specific one the active one then set the Application.DocumentManager.MdiActiveDocument = newDoc

or use the COM-version like

ctype(newDoc,Interop.AcadDocument).Activate

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
stevenh0616
Posts: 22
Registered: ‎04-12-2011
Message 8 of 8 (737 Views)

Re: Open a drawing file programmatically

12-12-2012 01:32 PM in reply to: TobyRosenberg8728

This doesn't seem to work for 2013. Says that the CurrentDrawings.open is not a member of the DocumentCollection. Did they move it in the API??

Steve

Windows 7 Enterprise 64-bit SP1
Intel Core i7 CPU X920 @ 1.73 GHz
16.00 GB RAM
Civil 3D 2013 - 2015

Check out my AutoCAD, Civil 3D, and Map Apps on the Autodesk Exchange Store at: Autodesk Exchange Store
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.