Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SW API - Get Next Component (only visible ones)?

Status
Not open for further replies.

pyroclasm

Computer
May 17, 2005
24
CA
Hey Guys,

I'm working with SW2006. I created a macro on a button in SolidWork to traverse through already open documents, while collecting info on them. My form is actually a lot more complicated than this one, but this form illustrates my problem the easiest. My problem is that when I press the Next command button on my form, if an Assembly is open, it will open ALL the components of that Assembly, making them all visible. I don't want that, I want only the open/visible documents to be traversed. I know Assembly components are open in memory but aren't visible. If I have 3 part, 1 drawing, and 1 Assembly document, then when I traverse through and get to that Assembly, BOOM...all of its components are visible and instead of traversing through 5 documents, I'm traversing through hundreds.

On my form I have 2 Labels, 2 TextBoxes (txtTitle, txtType), and 1 Command Button (btnNext).

EXAMPLE OF MY FORM:
---------------------------------------------------------------
| MY FORM |
---------------------------------------------------------------
| |
|ACTIVE COMPONENT: Example.SLDPRT |
| |
|TYPE: Part (SLDPRT) |
| ___________________ _ |
| | GET NEXT COMPONENT | |
| ------------------------------- |
| |
----------------------------------------------------------------



Code:
'GLOBAL VARIABLES
Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim strDocType(1 To 3) As String
'END GLOBAL VARIABLES


Private Sub btnNext_Click()

    Set swModelDoc = swModelDoc.GetNext

    'check if there is a next document
    If swModelDoc Is Nothing Then

        'If no more documents, start from first document again.
        Set swModelDoc = swApp.GetFirstDocument
        
        swDocUpdate

    Else  'display next document

        swDocUpdate
        
    End If

End Sub

Private Sub UserForm_initialize()

    checkActiveDocType
        
End Sub

Sub checkActiveDocType()

    'get SolidWorks
    Set swApp = Application.SldWorks

    'get first document
    Set swModelDoc = swApp.GetFirstDocument
    
    'check for active doc
    If swModelDoc Is Nothing Then
    
        MsgBox "No active documents."
        
        'no sense to go on - end macro
        End
        
    End If
         
    'initilize doc types
    strDocType(1) = "Part (SLDPRT)"
    strDocType(2) = "Assembly (SLDASM)"
    strDocType(3) = "Drawing (SLDDRW)"
    
    'display first document
    swDocUpdate
        
End Sub
Sub swDocUpdate()

    Dim ActivateErrors As Long

    'make document active
    swApp.ActivateDoc2 swModelDoc.GetTitle, True, ActivateErrors

    'add check of ActivateErrors here

    'get doc type
    txtType.Text = strDocType(swModelDoc.GetType)

    'get doc title
    txtTitle.Text = swModelDoc.GetTitle

End Sub
 
Replies continue below

Recommended for you

pyroclasm,

Right now the only way I see around this is to traverse the assembly itself and test each component for visibility.

SA
 
SolidAir said:
Right now the only way I see around this is to traverse the assembly itself and test each component for visibility.

I can see making a kind of loop to traverse through the active Assembly, but if you test each component for it's visibility won't that affect the open assembly's components. Like if you test each component in the Assembly and set it's visibility to Hidden, I would think that the components will be Hidden in the Assembly document.

I don't really know, I'm not too API savvy. Is there any other ways to do what I need?

Pyroclasm
 
Pyroclasm,

Testing for visiblity should not affect the open assembly's components (I am not advocating that you change an assembly components display state). It just tells you to ignore them. I am at work right now so I cannot take the time to write an example. I will try tonight at home.

SA
 
You're going to have to change your code structure somewhat in order to accomplish your goal. Basically, you need to not run the subroutine swdocupdate unless the document currently referenced by swModelDoc is visible. You can test that by checking the swModelDoc.Visible property. Then you'll have to add loops to skip over all the non-visible documents so that when the user presses the button the macro doesn't just check the next document, find that it's not visible, and stop.
 
Pyroclasm,

handleman is correct. I mis-read what you were trying to do. Below is some sample code that prints out which models are visible and which are not.

Code:
Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2

Sub main()

    Set swApp = Application.SldWorks
    
    Set swModelDoc = swApp.GetFirstDocument
    
    Do Until swModelDoc Is Nothing
    
        Debug.Print swModelDoc.GetTitle & "-" & swModelDoc.Visible
        
        Set swModelDoc = swModelDoc.GetNext
        
    Loop
    
End Sub

SA
 
Thanks SolidAir for the code example. I knew there had to be a way to see if a Doc is vibile or not in VB. Now I have find a way to impliment that into my code. Hmmm.

Thanks fcsuper, that is great. I reviewed the code quickly, and it doesn't look too complicated, but still advanced for me. But I will definately study the code and try to fit it in with what I need.

I don't have time now, so I'm gonna sift the code later.

Robert

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top