.NET

Reply
Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 1 of 14 (509 Views)

Save, Close, and re-Open a Drawing

509 Views, 13 Replies
07-20-2012 08:22 AM

Hello,
I would like to have a sub that gets the drawing name, saves it, closes the drawing, then re-opens it (because of a 2010 bug).
This is what I have, but from what I've read I need to have "commandflags.session" to work outside of the drawing.  How would I use it for this sub?  All I've seen is the commandflags.session included in the CommandMethod.
This code is in a sub, in a Module.  The only CommandMethod I have is the command I use to start the code in the "Class" module.  I tried putting it in that command, but it didn't work as it now had a problem with the layertable in another sub.

 

' This will save the current drawing, close the drawing, then re-open it
    Public Sub SaveCloseOpenDwg()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acDocMgr As DocumentCollection = Application.DocumentManager
        Dim strDWGName As String = acDoc.Name

        ' Save the active drawing
        acDoc.Database.SaveAs(strDWGName, True, DwgVersion.Current, acDoc.Database.SecurityParameters)
        acDocMgr.CloseAll()
        acDocMgr.Open(strDWGName, False)
    End Sub

 I run the program from a drawing rthat is currently open/active.  Is there a method that would let me save the current drawing, open a new drawing, close my original, then re-open the original and close the new drawing?  I know that sounds cludgy, but it's there's a bug in AutoCad 2010, that doesn't update the layermanger that I need to work around.  I found if I manually save the design, close and re-open it, the layer manager clears up.  This doesn't happen in AutoCad 2008 or 2012.

Thanks,

Mark

Valued Mentor
fieldguy
Posts: 371
Registered: ‎03-31-2005
Message 2 of 14 (501 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 09:21 AM in reply to: mgorecki

You could try something like message 5 >>here<

Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 3 of 14 (492 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 10:27 AM in reply to: fieldguy

Thanks, I'll check it out.

Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 4 of 14 (484 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 12:59 PM in reply to: fieldguy

Hi Fieldguy,

I'm not sure I fully understand your code.  it looks like it has an open/active drawing (drawing1) and it opens another drawing (drawing2 based on "map2diso.dwt")  It can do stuff then closes and discards drawing2.  Then it opens a third drawing, (drawing3 based on template "map2diso.dwt") does stuff and then closes/discards.  Has the original drawig1 been open all this time?  Is there a way to open the blank drawing2, close drawing1, then re-open drawing1 and close/discard drawing2?

 

Private Sub testsub(ByVal data As Object)
        'in this example data is not used
        Dim cmddiavalue As Integer = CInt(acapp.Application.GetSystemVariable("CMDDIA")?)
        acapp.Application.SetSystemVariable("CMDDIA", 0)
        Dim acdocmgr As acapp.DocumentCollection = acapp.Application.DocumentManager
        Dim acdoc As acapp.Document = acdocmgr.MdiActiveDocument <--Does this set the current drawing as acdoc?
        Dim templatepath As String = "map2diso.dwt"
        acdoc = acdocmgr.Add(templatepath) <--Adds "map2diso.dwt" to the document collection?
        acdocmgr.MdiActiveDocument = acdoc <--Makes "map2diso.dwt" the active document?  Is the original drawing still open?  If so, how do I close it?
        'drawing2 is now active
        Using acdoclock As acapp.DocumentLock = acdoc.LockDocument
            Dim currentobjs As New ObjectIdCollection()
            'query and save
        End Using
        acdoc.CloseAndDiscard() <--Closes the document "map2diso.dwt"?
        acdoc = acdocmgr.Add(templatepath) <--Not sure what drawing is being set as current here
        acdocmgr.MdiActiveDocument = acdoc
        'drawing3
        Using acdoclock As acapp.DocumentLock = acdoc.LockDocument
            Dim currentobjs As New ObjectIdCollection()
            'query and save
        End Using
        acdoc.CloseAndDiscard()
        acapp.Application.SetSystemVariable("CMDDIA", cmddiavalue)
        Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt(?)
        'return to drawing1 or whatever doc was before <--Being as drawing 1 was never closed, is it just made current again?
    End Sub

 

Thanks,

Mark

Valued Mentor
fieldguy
Posts: 371
Registered: ‎03-31-2005
Message 5 of 14 (480 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 01:16 PM in reply to: mgorecki

Hi Mark.  I should have tried this before I posted.  You are correct that the code creates new blank dwgs and saves them with the original drawing not touched in any way.  When the code is finished, the user is returned to the original dwg.  When I searched for information on "executeinapplicationcontext", there was a post from Tony T that warned against manipulating the doc that is active before that call.  It definitely generates a fatal exception if you try to close it - locked or not.  My guess is that acad has a lock on the dwg already and won't allow that level of access (ie. close). 

 

I'll keep digging because I have a use for this as well, and there should be a way to do it.

Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 6 of 14 (475 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 01:57 PM in reply to: mgorecki

In the "Class" module, I added Commandflags.Session:

<CommandMethod("ADP", CommandFlags.Session)> _
PublicSub ADP()

  

I'm trying to use it because I need to close the current/active drawing, but I then get an error with a layertable in a subsequent sub - see attached jpg.

 

What do I need to do in this instance?

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 14 (468 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 02:19 PM in reply to: mgorecki
    <CommandMethod("SaveCloseOpen", "sco", CommandFlags.Session)> _
    Public Sub SaveCloseOpenDwg()
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
        Dim strDWGName As String = acDoc.Name
        Using dcoclock As DocumentLock = acDoc.LockDocument()
            ''do your job here
            ' Save the active drawing
            acDoc.Database.SaveAs(strDWGName, True, DwgVersion.Current, acDoc.Database.SecurityParameters)
        End Using
        acDocMgr.CloseAll()
        acDocMgr.Open(strDWGName, False)
    End Sub

 

I tested it on my A2010, Relese mode, see if this working on your end

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 8 of 14 (461 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 02:28 PM in reply to: Hallex

Hi Hallex,

Do you know if a program commandMethod can launch another commandMethod?

For example,

Public

<CommandMethod("ADP")> _

  PublicSub ADP()

     :

   launch SaveCloseOpen

  End Sub

 

<CommandMethod("SaveCloseOpen", "sco", CommandFlags.Session)> _
  Public Sub SaveCloseOpenDwg()

     :

  End Sub


 Also, will this work if the program was launched inside the active drawing?

Valued Mentor
fieldguy
Posts: 371
Registered: ‎03-31-2005
Message 9 of 14 (457 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 02:36 PM in reply to: mgorecki

You have to lock the document before your layertable transaction.  Release the lock after you commit the transaction.

Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 10 of 14 (452 Views)

Re: Save, Close, and re-Open a Drawing

07-20-2012 02:50 PM in reply to: fieldguy

Ok, cool.  Thank you.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.