Check if displayed part is assembly

Hello

My Script should only work if it's used in an assembly. What is the best way to check that?
My idea was to try and count the Items in GetChildren(), but it isn't really a clean solution.

Regards

Looking at the number of children sounds like a solid plan. What about it isn't working for you?

It works, but I don't like the solution because of the Exception. Otherwise it won't work in parts that are not an assembly, only in drawings. How can I get rid of the Exception?

Dim BG as Integer = 1
Try
If(displayPart.ComponentAssembly.RootComponent.GetChildren().Length) = 1 Then
BG = 0
End If
Catch ex as Exception
BG = 0
End Try
Dim err(0) As String
If(BG = 0) Then
err(0) = "Error"
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Information, err)
Exit Sub
End If

If the .RootComponent is a null reference ("Nothing", in VB), it is a piece part and there are no components. Code to check this might look something like below:

if isNothing(displayPart.ComponentAssemby.RootComponent) then
'no components
else
if displayPart.ComponentAssembly.RootComponent.GetChildren.Length > 1 then
'multiple components, probably assembly
else
'single component, may be a drawing
'check for drawing sheets
end if
end if

You can find a variation of this in the article about processing an assembly:
http://nxjournaling.com/content/creating-subroutine-process-all-componen...

Bear in mind that the code above is a bit simplistic in that it doesn't consider non-geometric components and drafting components. Components that are explicitly suppressed might also require special handling.