Part Getting corrupted

Running this code more than onetime makes something corrupt in the assembly. After this I am unable to create arrangements, Not able to save or not able to part cleanup (Actually this is part of big script which I shared earlier for creating arrangement from sketch and table, But this causes issue in the file). First time it is running perfectly on new assembly without any issues, but whenever I am running this next the assembly is getting corrupt, throws "A dimension subscript for an array attribute was out of range or invalid" message.

Edit: Updated the Code

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.GeometricUtilities
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpenUI
Imports System.Collections.Generic

Module PositionOverRide

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim theUI As UI = UI.GetUI
Dim ufs As UFSession = UFSession.GetUFSession
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

'ArrangementOptions.IndividuallyPositioned and Position override for each assemblies
Try
Dim Mycomponent As ComponentAssembly = workPart.ComponentAssembly
UpdateArrangementPosition(Mycomponent.RootComponent, 0)
Catch e As Exception
lw.WriteLine("Assembly component Individually Postitioned Option failed : " & e.ToString)
End Try

End Sub

Sub UpdateArrangementPosition(ByVal comp As Component, ByVal indent As Integer)

For Each child As Component In comp.GetChildren()
If child.GetChildren.Length <> 0 Then
' lw.WriteLine(child.Name)
Try
Dim nullAssemblies_Component As Assemblies.Component = Nothing
child.EstablishPositionOverride(nullAssemblies_Component)
Dim MyObj(0) As NXObject
MyObj(0) = child
Dim assembliesParameterPropertiesBuilder1 As AssembliesParameterPropertiesBuilder = workPart.PropertiesManager.CreateAssembliesParameterPropertiesBuilder(MyObj)
assembliesParameterPropertiesBuilder1.Arrangements = Assemblies.AssembliesParameterPropertiesBuilder.ArrangementOptions.IndividuallyPositioned
Dim NxObject1 As NXObject
NxObject1 = assembliesParameterPropertiesBuilder1.Commit()
assembliesParameterPropertiesBuilder1.Destroy()
Catch ex As NXException
lw.WriteLine("Failed To Position Override")
End Try
Else
Try
Dim MyObj(0) As NXObject
MyObj(0) = child
Dim nullAssemblies_Component As Assemblies.Component = Nothing
child.EstablishPositionOverride(nullAssemblies_Component)
Dim assembliesParameterPropertiesBuilder1 As AssembliesParameterPropertiesBuilder = workPart.PropertiesManager.CreateAssembliesParameterPropertiesBuilder(MyObj)
assembliesParameterPropertiesBuilder1.Arrangements = Assemblies.AssembliesParameterPropertiesBuilder.ArrangementOptions.IndividuallyPositioned
Dim NxObject1 As NXObject
NxObject1 = assembliesParameterPropertiesBuilder1.Commit()
assembliesParameterPropertiesBuilder1.Destroy()
Catch ex As NXException
lw.WriteLine("Failed To Position Override")
End Try
End If
UpdateArrangementPosition(child, indent + 1)
Next
End Sub
End Module

I tested the posted code on a small assembly, but did not experience the errors that you listed. After running the code, I could create arrangements and run part cleanup as usual.

I only tested on one small assembly (only top level components, no subassemblies), so the testing was by no means extensive. Do these errors affect every assembly that you run the code on? How many levels deep do your assemblies run? Do any of your assemblies include promoted, deformed, or wave linked bodies?

I don't have any promoted,Deformed or linked bodies. The issue is causes by "establish position override". But for level one assemblies position override will not be there. May be that's why it was running good for you. I have tested 4 assemblies and ended with the same error whenever I run it second time.

Regards,

Joe

Adding Part cleanup after each time solved the issue.

Regards,

Joe