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

Save, Close, and re-Open a Drawing

13 REPLIES 13
Reply
Message 1 of 14
mgorecki
1206 Views, 13 Replies

Save, Close, and re-Open a Drawing

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

13 REPLIES 13
Message 2 of 14
fieldguy
in reply to: mgorecki

You could try something like message 5 >>here<

Message 3 of 14
mgorecki
in reply to: fieldguy

Thanks, I'll check it out.

Message 4 of 14
mgorecki
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

Message 5 of 14
fieldguy
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.

Message 6 of 14
mgorecki
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?

Message 7 of 14
Hallex
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
Message 8 of 14
mgorecki
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?

Message 9 of 14
fieldguy
in reply to: mgorecki

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

Message 10 of 14
mgorecki
in reply to: fieldguy

Ok, cool.  Thank you.

Message 11 of 14
Hallex
in reply to: mgorecki

I'm wondering, have you tried my code

instead of your lesson for me?

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 12 of 14
mgorecki
in reply to: Hallex

Hi Hallex,

The SaveOpenClose in my code is just a sub.  In order for me to use the "CommandFlags.Sesssion" it would have to be in the main calling code.  If I put it there, other things stop working, like layertables in other subs.  That's why I was asking if a main command method could launch another command method.

Also, I'm not sure of any lesson for you that I posted.  I was just asking if a command method could launch another command method as I was having problems with putting the "CommandFlags.Sessions" in the main command method.

You've helped me before and I'm really greatful for all the help.  No slight ever intended.

Message 13 of 14
Hallex
in reply to: mgorecki

Hi, mate, please do not make an offence on me

I just  didn't understand your task completely,

I've used local command name in my code

Cheers Smiley Happy

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 14 of 14
mgorecki
in reply to: Hallex

No worries, have a great day!  Smiley Happy

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