AutoCAD Document Events

Discussion in 'AutoCAD' started by Kenneth Hutson, Mar 14, 2005.

  1. Hi group,

    I would like to move a drawing to another folder after it has closed. In
    anticipation of this, I have trapped the BeginDocClose event. When this
    event fires, I get the Fullname property of the ActiveDocument and form a
    string for the destination filename. I store these in variable names which
    I have declared in the declaration section of the Class Module.

    My plan was to then rely on the EndCommand event and test to see if
    CommandName = "CLOSE". Then FileCopy to put the closed drawing in a
    different folder.

    Current (simplified) code similar to this:

    Public WithEvents acaddoc as AcadDocument
    Public copysource as Variant
    Public copydestination as Variant

    Private Sub acaddoc_BeginDocClose(Cancel As Boolean)
    copysource = ThisDrawing.Fullname
    copydestination = "SomePath\" & ThisDrawing.Name
    End Sub

    Private Sub acaddoc_EndCommand(ByVal CommandName As String)
    If CommandName = "CLOSE" Then
    FileCopy copysource, copydestination
    End If
    End Sub

    However, it seems that the firing of the EndCommand event does not
    necessarily mean that AutoCAD has released it's file lock on the drawing in
    question. How is it possible to determine when AutoCAD has actually
    "finished" closing the drawing so that it can be moved?

    Thanks in advance,
    Kenneth Hutson
    San Antonio, TX
     
    Kenneth Hutson, Mar 14, 2005
    #1
  2. Kenneth Hutson

    MP Guest

    What about a timer loop with a check for the dwgname.dwl file being deleted?
    in most cases acad will delete that file after closing the dwg file.
    (assuming 2004/5 - whichever version started using the dwl files.)
     
    MP, Mar 15, 2005
    #2
  3. Try using the Application's EndCommand event, not the Document's. The doc's
    endcommand fires as the document is still open otherwise it couldn't
    trigger the event.

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Mar 15, 2005
    #3
  4. Thanks for the suggestions. Tried the Application_EndCommand. Seems to
    interfere somehow because I get "AutoCAD is busy" dialog most times. Tried
    timer loop but this time could vary. Didn't work anyway. Search for .dwl
    proved fruitless too. Whatever I try throws AutoCAD for a loop. I first
    receive a dialog File I/O error. I never get a chance to dismiss it because
    another dialog pops up immediately afterwards. Second dialog notifies of an
    exception at a memory address.

    Seems that EndCommand works normally for other commands I have tried. I
    think it would propably be good if a EndClose event were added to the model.
    Something which would fire when AutoCAD has "completely" closed a drawing
    file and released it's locks.

    Thanks,
    Kenneth Hutson
    San Antonio, TX
     
    Kenneth Hutson, Mar 15, 2005
    #4
  5. Ken, are you doing this in vba or full vb?

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Mar 15, 2005
    #5
  6. Kenneth Hutson

    Mikko Guest

    Why not a saveas into the folder you need and then just close the drawing.
     
    Mikko, Mar 15, 2005
    #6
  7. Mike,

    Main app is VB. You could call it a document managenent app using a
    database. We have a central server for drawings but we work on them
    locally. Main app copies the file from the server to a local workstation at
    the user request and opens it in AutoCAD. Need to return the drawing to the
    server on close.

    Figured it was easiest to tap AutoCAD events through VBA to determine when a
    drawing is ready to go back (i.e. user closed it). In this case, let VBA
    put the drawing back.

    Also tried setting up a class module with AutoCAD events in my VB app. I
    get events to fire on only one drawing.

    Cheers,
    Kenneth Hutson
    San Antonio, TX
     
    Kenneth Hutson, Mar 15, 2005
    #7
  8. Hi Mikko,
    That would work to get the drawing where it needs to be. This will be on a
    network server. However, and I guess I didn't mention this, I also want to
    delete the closed drawing.
    Thanks,
    Kenneth Hutson
    San Antonio, TX
     
    Kenneth Hutson, Mar 15, 2005
    #8
  9. Kenneth Hutson

    Mikko Guest

    Here is a quick 5 minute demo that saves a dwg, closes it, moves it to one of our network drives and kills the original save. Something to play with.

    Imports AutoCAD
    Imports System.IO


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ACADApp As AutoCAD.AcadApplication
    Dim acadDoc As AcadDocument
    Dim acadUtl As AcadUtility
    Dim acadMoS As AcadModelSpace
    ACADApp = GetObject(, "AutoCAD.Application.16.1")
    acadDoc = ACADApp.ActiveDocument
    acadUtl = ACADApp.ActiveDocument.Utility
    acadMoS = ACADApp.ActiveDocument.ModelSpace
    Dim startPnt As Object
    Dim endPnt As Object
    Dim endPoint(2) As Double
    startPnt = acadUtl.GetPoint(, "Select Start Point: ")
    endPnt = acadUtl.GetPoint(startPnt, "Select End Point: ")
    endPoint(0) = endPnt(0)
    endPoint(1) = endPnt(1)
    endPoint(2) = endPnt(2)
    Dim lineObj As AcadLine
    lineObj = acadMoS.AddLine(startPnt, endPnt)
    ACADApp.ZoomAll()
    acadDoc.SaveAs("c:\temp\bogus.dwg")
    acadDoc.Close()
    Call MoveDWG("c:\temp\bogus.dwg")
    End Sub

    Sub MoveDWG(ByVal WhatDwg As String)
    Dim MoveItHere As String = "j:\0well\bogus.dwg" 'your server location
    Try
    File.Copy(WhatDwg, MoveItHere)
    Kill(WhatDwg)
    Catch ex As Exception
    MsgBox("What the heck")
    Finally
    MsgBox("OK")
    End Try
    End Sub
     
    Mikko, Mar 15, 2005
    #9
  10. Mikko,
    I gotcha. Interesting. Is this what .Net looks like? I havent seen Try
    before. I assume the VB equivalent is On Error Resume Next....

    Anyway, right now I am thinking of a different tack. Trap the BeginDocClose
    event, get my source and destination filenames established. Write them to a
    text file. Then from the VB app, provide a button to process the text file
    when the coast is clear.. I hate to leave it up to the user to remember to
    push though.

    Thanks,
    Kenneth
     
    Kenneth Hutson, Mar 15, 2005
    #10
  11. Ok, then you need to use the events from full vb.The trick is to create a
    class that manages the references to each doc. If you're on ADN, there is
    an example up there for you.

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Mar 15, 2005
    #11
  12. Hey Mike,

    No ADN here. That's a pay thing right? Man we are still running release
    2000 in this office. HA!

    Wondering what difference it would make if the event were fired from VB or
    VBA. Wouldn't it still boil down to be AutoCAD's End command event in the
    end? Anyway, thanks for all the tips and ideas guys.

    Respectfully,
    Kenneth Hutson
    San Antonio, TX
     
    Kenneth Hutson, Mar 15, 2005
    #12
  13. From full vb you can monitor the end command from the application level
    then either have IO code to watch for the file to actually be "closed" or
    use the timer idea. Actually, you could use the error to your advantage and
    just loop the file.move until it occurs without error

    Yeah ADN is $ but worth it if you do a lot of code. Sorry to hear about
    2000!

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Mar 15, 2005
    #13
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.