OpenDocs = swApp.EnumDocuments2()

Discussion in 'SolidWorks' started by Bates, Nigel, Dec 22, 2003.

  1. Bates, Nigel

    Bates, Nigel Guest

    i'm trying to get the list of open docs (open windows) and i think this
    is the function to do the job, anyone got a hint on gettig it going. It
    only returns 'empty' for me.

    What im trying to do:
    I'm using API to reload assemblies and children 'read-only' or
    're-write', which requires the part to be open and active, but then
    close it down after the change if it was not previously open.

    EnumDocuments2() seems the ideal function for seeing which docs are open
    at the start and hence then deduce which ones to close down when
    completed.

    cheers
    nigel
     
    Bates, Nigel, Dec 22, 2003
    #1
  2. Bates, Nigel

    Heikki Leivo Guest

    EnumDocuments2() seems the ideal function for seeing which docs are open
    If you refer to API help, you can use EnumDocuments2.Next method to get an
    array of document, but you have to decide beforehand how many open documents
    you want to retrieve. This is not very elegant, since you don't know how
    many open documents there are.. The code required would be, for example...

    Dim arrDocuments() As SldWorks.ModelDoc2
    Dim lngCount As Long
    arrDocuments = swApp.EnumDocuments2.Next(10, arrDocuments, lngCount)

    .... Then you could use lngCount variable to determine how many documents
    were actually retrieved. In my opinion, more clever would be to use a
    collection object to store the open models and use while loop to retrieve
    the open documents; for example

    Dim colModels As Collection
    'Create an instance of collection object
    Set colModels = New Collection
    'Get first open document from SW
    Dim objModel as SldWorks.ModelDoc2
    Set objModel = swApp.GetFirstDocument
    'Loop through open models
    While Not objModel Is Nothing
    'Add model to collection
    colModels.Add objModel
    'Get next open model
    Set objModel = objModel.GetNext
    Wend

    By using collection object to store models, you could easily close them all
    by using For Each loop, such as...

    For Each objModel In colModels
    objModel.Close
    Next

    Hope this helps!

    -h-
     
    Heikki Leivo, Dec 22, 2003
    #2
  3. Bates, Nigel

    Bates, Nigel Guest

    like it, many thanks!!!
    shame solidworks application cant reload parent and children read-only /
    read-write through the user-interface!!!

    -----Original Message-----
    From: Heikki Leivo [mailto:]
    Posted At: 22 December 2003 13:11
    Posted To: solidworks
    Conversation: OpenDocs = swApp.EnumDocuments2()
    Subject: Re: OpenDocs = swApp.EnumDocuments2()

    If you refer to API help, you can use EnumDocuments2.Next method to get
    an
    array of document, but you have to decide beforehand how many open
    documents
    you want to retrieve. This is not very elegant, since you don't know how
    many open documents there are.. The code required would be, for
    example...

    Dim arrDocuments() As SldWorks.ModelDoc2
    Dim lngCount As Long
    arrDocuments = swApp.EnumDocuments2.Next(10, arrDocuments, lngCount)

    .... Then you could use lngCount variable to determine how many documents
    were actually retrieved. In my opinion, more clever would be to use a
    collection object to store the open models and use while loop to
    retrieve
    the open documents; for example

    Dim colModels As Collection
    'Create an instance of collection object
    Set colModels = New Collection
    'Get first open document from SW
    Dim objModel as SldWorks.ModelDoc2
    Set objModel = swApp.GetFirstDocument
    'Loop through open models
    While Not objModel Is Nothing
    'Add model to collection
    colModels.Add objModel
    'Get next open model
    Set objModel = objModel.GetNext
    Wend

    By using collection object to store models, you could easily close them
    all
    by using For Each loop, such as...

    For Each objModel In colModels
    objModel.Close
    Next

    Hope this helps!

    -h-
     
    Bates, Nigel, Dec 22, 2003
    #3
  4. Bates, Nigel

    Bates, Nigel Guest

    just realised thats its not actually the 'open docs' i'm trying to get,
    its the solidworks windows 1,2,3.... etc, have you got any magic for
    that one??
    cheers
    nigel

    -----Original Message-----
    From: Heikki Leivo [mailto:]
    Posted At: 22 December 2003 13:11
    Posted To: solidworks
    Conversation: OpenDocs = swApp.EnumDocuments2()
    Subject: Re: OpenDocs = swApp.EnumDocuments2()

    If you refer to API help, you can use EnumDocuments2.Next method to get
    an
    array of document, but you have to decide beforehand how many open
    documents
    you want to retrieve. This is not very elegant, since you don't know how
    many open documents there are.. The code required would be, for
    example...

    Dim arrDocuments() As SldWorks.ModelDoc2
    Dim lngCount As Long
    arrDocuments = swApp.EnumDocuments2.Next(10, arrDocuments, lngCount)

    .... Then you could use lngCount variable to determine how many documents
    were actually retrieved. In my opinion, more clever would be to use a
    collection object to store the open models and use while loop to
    retrieve
    the open documents; for example

    Dim colModels As Collection
    'Create an instance of collection object
    Set colModels = New Collection
    'Get first open document from SW
    Dim objModel as SldWorks.ModelDoc2
    Set objModel = swApp.GetFirstDocument
    'Loop through open models
    While Not objModel Is Nothing
    'Add model to collection
    colModels.Add objModel
    'Get next open model
    Set objModel = objModel.GetNext
    Wend

    By using collection object to store models, you could easily close them
    all
    by using For Each loop, such as...

    For Each objModel In colModels
    objModel.Close
    Next

    Hope this helps!

    -h-
     
    Bates, Nigel, Dec 22, 2003
    #4
  5. Bates, Nigel

    TheTick Guest

    'excerpt from a macro that lists enumerated documents
    'EnumDocuments2 is an object with its own methods. You need to use the
    'EnumDocuments2.Next method to cycle through the ModelDoc objects.

    Private Sub cmdEnum_Click()
    Dim Files as Variant
    Dim f As Long
    Dim eList As SldWorks.EnumDocuments2
    Dim Celt As Long
    Dim rGelt As SldWorks.ModelDoc2
    Dim pCeltFetched As Long
    Dim DocList() As String
    Dim DocCounter As Long

    Celt = 1
    DocCounter = -1

    Set eList = swApp.EnumDocuments2
    eList.Reset

    Do
    eList.Next Celt, rGelt, pCeltFetched
    If pCeltFetched < 1 Then Exit Do
    DocCounter = DocCounter + 1
    ReDim Preserve DocList(0 To DocCounter)
    DocList(DocCounter) = rGelt.GetPathName
    Loop


    Files = DocList
    'lboDocs is a ListBox in a form
    lboDocs.Clear
    lboDocs.List = Files

    End Sub

    http://www.esoxrepublic.com
     
    TheTick, Dec 22, 2003
    #5
  6. Bates, Nigel

    Heikki Leivo Guest

    just realised thats its not actually the 'open docs' i'm trying to get,
    Well, not any magic but maybe you could add only those modeldocs to the
    collection which appear to be visible. In the example I wrote earlier, it
    would be...

    While Not objModel Is Nothing
    'Check if model has a window open...
    If objModel.Visible = True Then colModels.Add objModel
    'Get next open model
    Set objModel = objModel.GetNext
    Wend

    Then you would have only those models stored which have an open window.
    Note, that the code above stores the actual modeldoc objects to the
    collection, so if you have to close the documents and open them later, the
    model objects become invalid. In a such case you should store something else
    in the collection, such as model filenames, eg. colModels.Add
    objModel.GetPathName.

    Hope this helps!

    -h-
     
    Heikki Leivo, Dec 22, 2003
    #6
  7. Option Explicit

    Private swapp As SldWorks.SldWorks
    Private EnumDoc As SldWorks.EnumDocuments2

    Private Sub main()

    Set swapp = CreateObject("SldWorks.Application")

    Set EnumDoc = swapp.EnumDocuments2

    End Sub

    hope this helps
     
    Sean Phillips, Dec 22, 2003
    #7
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.