document activate not setting ThisDrawing correctly

Hi Everyone,

I am working to migrate VBA code from Autocad. The code around copying objects from existing drawing to a new drawing is not working properly. On debugging I noticed that the ThisDrawing object is not set correctly after document.activate is after called. The code is working fine in Autocad. I am on BricsCAD 25.1.07

I found a workaround where I could use ThisDrawing.Application.ActivateDocument instead of ThisDrawing and it works. However, The code base is big and changing all the occurrences safely and correctly will take long time. I also noticed that the activate doesn't make the desired drawing fully active on the UI. Could that be related to the issue?

I will really appreciate someone could point me in the right direction.
Is there any method I could call after activate to fix this?

Set MainDrawingName = ThisDrawing.Name


' set existing document to a variable


Set currentDoc = AcadApplication.Documents(MainDrawingName)


' create a new document

Set NewDoc = AcadApplication.Documents.add("NewDocumentName.dwg")


' set the existing document as active again

AcadApplication.ActiveDocument = currentDoc


' ThisDrawing still shows NewDocumentName.dwg


' using currentDoc.Activate has the same effect as AcadApplication.ActiveDocument

' The events Private Sub AcadDocument_Activate() ... is not triggered as well

Many thanks in advance

Jiva

Comments

  • "AcadApplication.ActiveDocument = currentDoc"

    I actually have a unit test for this in Python, the syntax is different, but it’s still the same API

        def test_getset_active_document(self):
            axApp = Ap.Application.acadApplication()
            axDoc1 = axApp.activeDocument()
            docs = axApp.documents()
            newdoc = docs.add()
            assert newdoc.name() != axDoc1.name()
            axDoc2 = axApp.activeDocument()
            assert axDoc2.name() != axDoc1.name()
            axApp.setActiveDocument(axDoc1)
            axDoc3 = axApp.activeDocument()
            assert axDoc3.name() == axDoc1.name()
    

    test_core/test_PyAx/test_AxApplication.py::TestAxApplication::test_getset_active_document PASSED [ 40%]

    I didn't try activate yet, still working on my wrappers

  • Maybe totally incorrect, in lisp if you add another dwg need to refresh the active documents list so it sees say the 2 dwg's. Should then be able to go to either dwg.

  • I think this is a bug in AutoCAD, False in AutoCAD, True in BricsCAD

    CComQIPtr<IAcadApplication> acad(acedGetIDispatch(TRUE));
    if (acad)
    {
    CComQIPtr<IAcadDocuments> docs;
    if (auto hr = acad->get_Documents(&docs); hr != S_OK)
    return false; VARIANT vtempty; VariantInit(&vtempty); CComQIPtr<IAcadDocument> doc1; if (auto hr = docs->Add(vtempty, &doc1); hr != S_OK) return false; CComQIPtr<IAcadDocument> doc2; if (auto hr = docs->Add(vtempty, &doc2); hr != S_OK) return false; if (auto hr = acad->put_ActiveDocument(doc2); hr != S_OK) return false; CComQIPtr<IAcadDocument> doc3; if (auto hr = acad->get_ActiveDocument(&doc3); hr != S_OK) return false; acutPrintf(_T("\nDocs are equal %ls"), doc2 == doc3 ? _T("TRUE") : _T("FALSE")); }
    return true;

  • never mind, works in ACRX_CMD_SESSION

  • Thank you for the reply.

    I assume the python

            axDoc1 = axApp.activeDocument() is same as currentDoc = AcadApplication.ActiveDocument
    
            and axApp.setActiveDocument(axDoc1) is the same as   AcadApplication.ActiveDocument = currentDoc
    

    If it's working in python, then the issue could be just related to VBA?

    I managed to get it to work by calling

    currentDoc.Regen acActiveViewport after the currentDoc.Activate

    The regen seems to set the ThisDrawing variable to the correct ActiveDocument

    This has worked on the tests so far.

    It will great to know if someone had similar issue and if they found a better solution.

  • Right, the two methods are essentially the same. I don’t think It’s just a difference between VBA and Python, it’s the same interface.

    Is this a test outside of your main project? If not, make a separate test.

    My guess is that there’s something more to the picture, either you’re in a context that’s preventing a document switch, or something is holding on to a document lock.

  • For future reference,
    The main reason for the issue was the difference between autocad and bricscad on how document_activate works.

    The code from Autocad needs to be updated based on this guide

    https://developer.bricsys.com/bricscad/help/en_US/V25/DevRef/index.html?page=source%2FMethods_Select.htm

    The switching is error free after the code is updated accordingly.

  • ALANH
    edited November 18

    Maybe helpful. This is lisp but allows you to choose an open dwg. Save the two MULTI lisps to a support path as autoloaded. If you use copyclip before choosing the next dwg you should be able to paste to other dwg.

    ; simple jump to another dwg that is open
    ; By AlanH Nov 2023

    (defun c:gotodwg ( / lst x ans but cnt)

    (setq acDocs (vla-get-documents (vlax-get-acad-object)))
    (setq cnt (vlax-get acdocs 'count))

    (setq lst '())
    (setq x 0)
    (repeat cnt
    (setq lst (cons (vlax-get (vla-item acdocs x) 'name) lst))
    (setq x (1+ x))
    )
    (setq lst (reverse lst))

    (If (< cnt 16)
    (progn
    (if (not AH:Butts)(load "Multi radio buttons.lsp"))
    (setq lst (cons "Please choose " lst))
    (setq ans (ah:butts 1 "V" lst))
    (vla-activate (vla-item acdocs (- but 1)))
    )
    (progn
    (if (not AHlstbox)(load "Listbox-AH.lsp"))
    (setq ans (ahlstbox "Pick a dwg" lst 30 20))
    (vla-activate (vla-item acdocs ans))
    )
    )
    (princ)
    )
    (c:gotodwg)

    Limited testing manually paste worked, possible calling lisp

    (defun wow ( / )
    (setq ss (ssget))
    (command "copyclip" ss "")
    (load "gotodwg.lsp")
    (command "pasteclip" (getpoint "\npick a point"))
    (princ)
    )
    (wow)

    Limited testing.