Eng-Tips is the largest forum for Engineering Professionals on the Internet.

Members share and learn making Eng-Tips Forums the best source of engineering information on the Internet!

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

API to list hiden components 2

Status
Not open for further replies.

macPT

Mechanical
Joined
Oct 22, 2002
Messages
607
Location
PT
Hi guys!

I'm trying to make a macro to transverse an assembly to give me a list of components or subassemblies that have been suppressed or hided (in the design process).

The problem is that I don't get any information about the hide state. I'm using Component2.GetVisibility ( config_opt, config_names ) but this always returns empty arrays.

Can you help me on this? What am I doing wrong?

Regards
 
Try working with these maybe


' The suppression information possible for Components. For use with auComponent_c::Suppression.

Public Enum swComponentSuppressionState_e
swComponentSuppressed = 0 ' Fully suppressed - nothing is loaded
swComponentLightweight = 1 ' Featherweight - only graphics data is loaded
swComponentFullyResolved = 2 ' Fully resolved - model is completly loaded
End Enum

' The visibility information possible for Components. For use with auComponent_c::Visibility.
Public Enum swComponentVisibilityState_e
swComponentHidden = 0
swComponentVisible = 1
End Enum
 
How about a macro that builds a list of the component name, shown/hidden status, and the suppressed/resolved status for each component, and then outputs it to a text file? Copy the below and paste into a new macro.

I modified/write this a year ago. I am still learning and feel very green around the guru's here, so use at your own risk (but I haven't had any problems with it).

Later,
Ken


'******************
'Derived from:
'Traversing an Assembly (VB)
'This example illustrates how to recursively visit each component in an assembly.
'Get the current model and traverse all its components
'
'***
'Changed to visit each component (assembly and part) and write it's visiblility properties
' to a matrix, then store that to a data file as:
' 1st line: AssemblyName, ActiveConfiguration, Current/OutputMatrixLength
' All Other lines: ComponentName, Show/Hide, Suppressed/Resolved, Doc Type (Part, Assy)-->Not currently available
'
'***
' Things that don't work
' 1) Used a Matrix instead of multiple arrays. Arrays would've been better since they can be resized.
' -Program will only work for the first 100000 components. Program will work with more but *you* must manually update matrix size before running macro.
' 2) Doesn't write Assembly Configuration
' 3) Doesn't write Doc Type for components

Dim swApp As Object
Dim AssemblyDoc As Object
Dim Configuration As Object
Dim RootComponent As Object
Dim SelMgr As Object
Dim selEntity As Object
Dim selType As Long
Dim owningComponent As Object
Dim ComponentName As String
Dim void As Variant

Dim AllComponentsCurrentStateStatusMatrix(100000, 3) As Variant
'Below Needs Fixed - Change to be Dynamic Matrix Size, or count components and then declare Matrix at correct size
Dim AssemblyName, MySaveAsFilename As String
Sub main()

Set swApp = Application.SldWorks
Set AssemblyDoc = swApp.ActiveDoc

'Make sure this is an assembly
While (AssemblyDoc.GetType <> swDocASSEMBLY) 'Make sure this is an assembly
MsgBox "This macro only works for Assembly Files.", vbOKOnly + vbExclamation, "Invalid File Type"
AssemblyDoc.ClearSelection
Exit Sub
Wend

MySaveAsFilename = "C:\temp\AssemblyComponentStatus.dat"

AssemblyName = AssemblyDoc.GetTitle 'Current Assy Name
AllComponentsCurrentStateStatusMatrix(0, 0) = AssemblyName

'Find the Root Component
Set Configuration = AssemblyDoc.GetActiveConfiguration()

'Write Configuration Name to Matrix to be Verified during unhiding of components
'Below Needs Fixed
'AllComponentsCurrentStateStatusMatrix(0, 1) = AssemblyDoc.GetActiveConfiguration()

Set RootComponent = Configuration.GetRootComponent()
'Recursively traverse the component
If Not RootComponent Is Nothing Then
TraverseComponent 1, RootComponent, 1
End If

'Write Data to file
Open MySaveAsFilename For Output As #1 'Open file for output.
i = 0
While i <= AllComponentsCurrentStateStatusMatrix(0, 2)
Write #1, AllComponentsCurrentStateStatusMatrix(i, 0), AllComponentsCurrentStateStatusMatrix(i, 1), AllComponentsCurrentStateStatusMatrix(i, 2), AllComponentsCurrentStateStatusMatrix(i, 3) 'Write comma-delimited data.
i = i + 1
Wend
Close #1 'Close file.

swApp.SendMsgToUser "Component status written to: " & MySaveAsFilename
AssemblyDoc.ClearSelection
AssemblyDoc.EditRebuild
End Sub
Private Function TraverseComponent(Level As Integer, Component As Object, CurrentComponentNumber As Integer)
'Recursive routine to traverse all the children of a component
Dim text As String
Dim i As Integer
Dim Children As Variant
Dim Child As Object
Dim ChildCount As Integer

If Component.Name <> "" Then
AllComponentsCurrentStateStatusMatrix(CurrentComponentNumber, 0) = Component.Name
AllComponentsCurrentStateStatusMatrix(CurrentComponentNumber, 1) = Component.IsHidden(False) 'True=Hidden, False=Shown

AllComponentsCurrentStateStatusMatrix(CurrentComponentNumber, 2) = Component.GetSuppression
'Below Needs Fixed
'Needs FixedSuppression=0=swComponentSuppressed 'Fully suppressed - nothing is loaded
'Needs FixedSuppression=1=swComponentLightweight 'Featherweight - only graphics data is loaded
'Needs FixedSuppression=2=swComponentFullyResolved 'Fully resolved - model is completly loaded

'Below Needs Fixed
'AllComponentsCurrentStateStatusMatrix(CurrentComponentNumber, 3) = Component.GetType
'swDocNONE = 0 ' Used to be TYPE_NONE
'swDocPART = 1 ' Used to be TYPE_PART
'swDocASSEMBLY = 2 ' Used to be TYPE_ASSEMBLY
'swDocDRAWING = 3 ' Used to be TYPE_DRAWING
'swDocSDM = 4 ' Solid data manager.

CurrentComponentNumber = CurrentComponentNumber + 1
AllComponentsCurrentStateStatusMatrix(0, 2) = CurrentComponentNumber - 1
'Below Needs Fixed
'If CurrentComponentNumber = AllComponentsCurrentStateStatusMatrix(0, 2) Then
'ReDim Preserve AllComponentsCurrentStateStatusMatrix(AllComponentsCurrentStateStatusMatrix(0, 2) + MatrixLengthStep, 0)
'End If
End If

'Traverse the children
Children = Component.GetChildren ' Get the list of children
'Get the # of elements in the variant safearray. UBound returns the upper element number. Since the array begins at zero, we must add 1 to get the actual number of array elements. If no elements are in the array, then UBound returns -1.
ChildCount = UBound(Children) + 1
'For each Child in this subassembly
For i = 0 To (ChildCount - 1)
'Get Child component object
Set Child = Children(i)
'Traverse the child's components
TraverseComponent Level + 1, Child, CurrentComponentNumber
Next i
End Function
 
Thanks KenBolen

Your macro seems to be working fine.

So you use Component.IsHidden(False)instead of what I was trying to do (as simple as that!).

I'm writing the macro to output in Excel instead of text doc. This way a query will be possible and will find problems in a much easier way.

Then I will run both macros for a final comparing test.

Regards
 
macPT,

There's probably a few other ways to accomplish this as well...just like most problems :)

You can open text files directly with Excel. Just make sure (at least for this example) that you choose comma delimited file. In the macro, I'd also recommend changing the output value to 0 or 1 instead of #FALSE# or #TRUE#, just to save some possible headaches in Excel.

Just my $0.02,
Ken
 
KenBolen

I just finished my Excel macro. I've tested it with a 3 level 517 component assembly, with components in different states.

Good news - your macro matched 100% the results of mine
Bad news - the visibility seems to lack consistency when derived configurations are present (SW consider then as hidden). This thing of derived configuration is getting on my nerves. I'll try to find out what's the problem.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top