.NET

.NET

Reply
Contributor
johnsnow
Posts: 18
Registered: ‎07-01-2005
Message 1 of 5 (628 Views)
Accepted Solution

How to switch between multiple open documents?

628 Views, 4 Replies
12-05-2012 03:01 PM

Forgive me, I am a total  beginner at this vb.net thing.  I have a vba routine that I have used for years to execute a lisp routine on an open document, then switch to the next already open document, and continue until the routine has been performed on each already open documents.  For the life of me, I cannot figure out how in vb.net to get a document collection and make each document the active document so that I can execute the lisp routine.

 

VBA code:

=====================================================

Option Explicit
    Dim objAcadApp As AcadApplication       'Acad application object
    Dim objAcadDocs As AcadDocuments        'Acad documents collection
    Dim objAcadDoc As AcadDocument          'Acad document object
    Dim i As Integer
    
   
 Public Sub FixAllIds()

    'Get Acad objects
    Set objAcadApp = GetObject(, "autoCAD.application") 'get Acad object
    Set objAcadDocs = objAcadApp.Documents  'get Acad Documents collection
    
    'step through open drawings
    For i = 0 To (objAcadDocs.Count - 1)
        'for each drawing run c:fix-id15
        Set objAcadDoc = objAcadDocs.Item(i)
        objAcadDoc.Activate
        'load the routine

        objAcadDoc.SendCommand "lo" & vbCr & "fix-id15" & vbCr
        'run the routine

        objAcadDoc.SendCommand "fix-id15" & vbCr
        
        Next i
        
End Sub

=====================================================

 

Thanks in advance for any advice or pointers!

You also need to enable switching:

Application.DocumentManager.DocumentActivationEnabled = True
Moderator
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 2 of 5 (614 Views)

Re: How to switch between multiple open documents?

12-05-2012 09:47 PM in reply to: johnsnow

AutoCAD VBA to VB.NET Migration Basics


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Contributor
johnsnow
Posts: 18
Registered: ‎07-01-2005
Message 3 of 5 (520 Views)

Re: How to switch between multiple open documents?

04-11-2013 08:08 AM in reply to: johnsnow

I watched the video, but as far as I can tell he only shows how to get the event flag showing that the user has switched the active document.  He doesn't show the code for how to switch focus from one open drawing to another already open drawing.

 

My goal is to perform various actions on a subset of drawings, which will vary from project to project.  All of the examples I have looked at show how to open, change, then close specific drawings by file name.  I don't want to hard-code the file names into my .net project, I just want to perform an action on files already open, one-by-one.

Valued Mentor
jeff
Posts: 338
Registered: ‎05-12-2009
Message 4 of 5 (511 Views)

Re: How to switch between multiple open documents?

04-11-2013 10:01 AM in reply to: johnsnow

Note:

If you execute the following code without the CommandsFlags.Session after it switches to another document the code will not continue until to you activate the document that the command was started in. So you have Drawing1-Drawing10 and Drawing1 is active. Without the Sessions flag Drawing2 will become active and your no longer in the "Document Context" of Drawing1. Once you switch back to Drawing1 then the code will continue executing and Drawing3 would become active, etc........

By specifying Sessions flag it will execute in "Application context" and only pause until the alert box is closed so to modify any of the documents you would have to explicitly lock them.

 

Sorry for not giving a more complete exampe but after I write about 10 lines of VB I start to throw up in my mouth.

 

        <CommandMethod("MyCommand", CommandFlags.Session)> _
        Public Sub MyCommand() ' This method can have any name
            Dim docMgr As DocumentCollection = Application.DocumentManager
            For Each doc As Document In docMgr
                Application.DocumentManager.MdiActiveDocument = doc
                Application.ShowAlertDialog(doc.Name)
            Next
        End Sub

 

You can also find your answers @ TheSwamp
Moderator
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 5 of 5 (504 Views)

Re: How to switch between multiple open documents?

04-11-2013 11:05 AM in reply to: jeff

You also need to enable switching:

Application.DocumentManager.DocumentActivationEnabled = True

Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.