cancel
Showing results for 
Search instead for 
Did you mean: 

NXOpen - Reposition component till collision

Experimenter
Experimenter

Hi All,

 

I am writing a NXOpen application to reposition a component until it makes touch interference with another component.
In interactive NX there is an option called "Stop Before Collision" under "Collision Detection" in reposition component dialog.
Kindly suggest me a way to ahieve this in NXOpen.

 

Regards,
basenxuser

1 REPLY

Re: NXOpen - Reposition component till collision

Genius
Genius

Hi,

 

Here is some code how i did it in NX9 to get you started. I move my component and check for interference.

The collision detection option is not working, or i could not get it to work (I don't remember).

 

br

Fred

 

 

    Sub MoveComponentWithCollisionDetection(comp() As Component, copyVector As Vector3d)

        Dim intStepSize As Integer = 2       'step size in millimeter (accuracy)
        Dim iiMax As Integer = 20

        'detect direction and calc number of steps
        Dim chrDir As Char = ""
        Dim translation1 As Vector3d

        If copyVector.X <> 0 Then
            chrDir = "X"
            iiMax = Math.Round((Math.Abs(copyVector.X) + 20) / intStepSize, 0)
            translation1 = New Vector3d(-intStepSize, 0.0, 0.0)
        ElseIf copyVector.Y <> 0 Then
            chrDir = "Y"
            iiMax = Math.Round((Math.Abs(copyVector.Y) + 20) / intStepSize, 0)
            translation1 = New Vector3d(0.0, -intStepSize, 0.0)
        ElseIf copyVector.Z <> 0 Then
            chrDir = "Z"
            iiMax = Math.Round((Math.Abs(copyVector.Z) + 20) / intStepSize, 0)
            translation1 = New Vector3d(0.0, 0.0, -intStepSize)
        Else
            Echo("!!!ERROR!!! Can not detect axis!!! using X")
            chrDir = "X"
        End If

        'create clearenceSet
        Dim nxObj1 As NXObject = CreateClearenceSet(cItemComps.ToArray, comp, "SET_" & chrDir)
        Dim clearanceSet1 As Assemblies.ClearanceSet = CType(nxObj1, Assemblies.ClearanceSet)

        'move component and check if collision
        For ii As Integer = 1 To iiMax

            'move component
            MoveComponent(comp, translation1, False)

            'check collision
            Try
                'One or more objects were unavailable (unloaded) during clearance analysis
                clearanceSet1.PerformAnalysis(Assemblies.ClearanceSet.ReanalyzeOutOfDateExcludedPairs.True)
            Catch ex As NXException
                ex.AssertErrorCode(1185014)
                Dim results As Assemblies.ClearanceSet.Summary = clearanceSet1.GetResults
                If results.NumHard <> 0 Then
                    'Echo("")
                    'Echo("Collision detected")
                    Echo("Optimal Nesting " & chrDir & " = +/- " & (ii - 1) * intStepSize)
                    Select Case chrDir
                        Case "X"
                            pntNestingCalc.X = (ii - 1) * intStepSize
                        Case "Y"
                            pntNestingCalc.Y = (ii - 1) * intStepSize
                        Case "Z"
                            pntNestingCalc.Z = (ii - 1) * intStepSize
                    End Select
                    Exit For 'exit when collision
                End If
            End Try
        Next


    End Sub


    Function CreateClearenceSet(cmp1() As Component, cmp2() As Component, strSetName As String) As NXObject

        CreateClearenceSet = Nothing
        ' ----------------------------------------------
        '   Menu: Analysis->Assembly Clearance->Clearance Set->New...
        ' ----------------------------------------------

        Dim nullAssemblies_ClearanceSet As Assemblies.ClearanceSet = Nothing

        Dim clearanceAnalysisBuilder1 As Assemblies.ClearanceAnalysisBuilder
        clearanceAnalysisBuilder1 = s.Parts.Work.AssemblyManager.CreateClearanceAnalysisBuilder(nullAssemblies_ClearanceSet)

        Dim expression1 As Expression
        expression1 = clearanceAnalysisBuilder1.CreateClearanceZoneExpression("0.0")
        Dim unit1 As Unit = CType(s.Parts.Work.UnitCollection.FindObject("MilliMeter"), Unit)
        expression1.Units = unit1
        expression1.RightHandSide = "0"

        clearanceAnalysisBuilder1.SetDefaultClearanceZone(expression1)
        clearanceAnalysisBuilder1.CalculationMethod = Assemblies.ClearanceAnalysisBuilder.CalculationMethodType.ExactifLoaded
        clearanceAnalysisBuilder1.ClearanceSetName = strSetName
        clearanceAnalysisBuilder1.ClearanceBetween = Assemblies.ClearanceAnalysisBuilder.ClearanceBetweenEntity.Components
        clearanceAnalysisBuilder1.TotalCollectionCount = Assemblies.ClearanceAnalysisBuilder.NumberOfCollections.Two
        clearanceAnalysisBuilder1.CollectionOneRange = Assemblies.ClearanceAnalysisBuilder.CollectionRange.SelectedObjects 'specific objects for collection 1
        clearanceAnalysisBuilder1.CollectionTwoRange = Assemblies.ClearanceAnalysisBuilder.CollectionRange.SelectedObjects 'specific objects for collection 2

        'add the components
        Dim added1 As Boolean = clearanceAnalysisBuilder1.CollectionOneObjects.Add(cmp1)
        Dim added2 As Boolean = clearanceAnalysisBuilder1.CollectionTwoObjects.Add(cmp2)

        'set te clearenceSet 
        CreateClearenceSet = clearanceAnalysisBuilder1.Commit()

        clearanceAnalysisBuilder1.Destroy()

    End Function


    Function MoveComponent(components1() As Component, MoveVector As Vector3d, blnCopy As Boolean) As Component()

        MoveComponent = Nothing
        Dim componentPositioner1 As Positioning.ComponentPositioner = s.Parts.Work.ComponentAssembly.Positioner

        Dim network1 As Positioning.Network
        network1 = componentPositioner1.EstablishNetwork()

        Dim componentNetwork1 As Positioning.ComponentNetwork = CType(network1, Positioning.ComponentNetwork)

        Dim markId_MoveComponent As Session.UndoMarkId = s.SetUndoMark(Session.MarkVisibility.Invisible, "Move Component, copy = " & blnCopy)

        'copy component
        Dim movableObjects(0) As NXObject
        If blnCopy Then
            'Dim components1(0) As Assemblies.Component
            'components1(0) = cmp1
            Dim newComponents1() As Assemblies.Component
            newComponents1 = s.Parts.Work.ComponentAssembly.CopyComponents(components1)
            MoveComponent = newComponents1 'store new components for function output
            movableObjects = newComponents1
        Else
            movableObjects = components1
        End If

        componentNetwork1.SetMovingGroup(movableObjects)
        componentNetwork1.BeginDrag()

        Dim translation1 As Vector3d = MoveVector
        componentNetwork1.DragByTranslation(translation1)

        componentNetwork1.EndDrag()
        componentNetwork1.ResetDisplay()
        componentNetwork1.ApplyToModel()
        componentNetwork1.Solve()

        s.UpdateManager.AddToDeleteList(componentNetwork1)
        s.UpdateManager.DoUpdate(markId_MoveComponent)

    End Function