SW API - Get Next Component (only visible ones)?
SW API - Get Next Component (only visible ones)?
(OP)
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 | |
| ------------------------------- |
| |
----------------------------------------------------------------
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
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






RE: SW API - Get Next Component (only visible ones)?
Right now the only way I see around this is to traverse the assembly itself and test each component for visibility.
SA
RE: SW API - Get Next Component (only visible ones)?
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
RE: SW API - Get Next Component (only visible ones)?
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
RE: SW API - Get Next Component (only visible ones)?
RE: SW API - Get Next Component (only visible ones)?
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 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
RE: SW API - Get Next Component (only visible ones)?
Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
http://sw.fcsuper.com/index.php
RE: SW API - Get Next Component (only visible ones)?
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