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.