Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

journal that moves in the xy plane 1

ppap eroni

Mechanical
Feb 22, 2023
12
When I asked AI to create a journal code that moves in the xy plane, the following code came out.
It didn't work, so I modified it a little, but it still doesn't work.
I don't know much about journals. Even if I look at the help of nx, it's not easy to learn because it's not related to practical work. So if there's someone who can modify it, please answer.
I want to study based on this.

What I want to implement is the same as doing point to point in Move Component, but discard the z point or make it 0. As a result, no matter which point you select, the part you want to move will move on the xy plane.

I'm designing a press die, and plane movement is a really necessary command.

Thank you.

--------------------------------------------------
Imports NXOpen
Module NXJournal

Sub Main ()
Dim theSession = NXOpen.Session.GetSession()


' 유틸리티 기능 객체 가져오기
Dim theUF As UFSession = UFSession.GetUFSession()

' 사용자에게 객체 선택하도록 안내
Dim workPart As WorkPart = theSession.Parts.Work
Dim selectedObjects As Selection = theSession.SelectionManager.GetSelectedObjects(True)

' 선택된 객체가 없으면 종료
If selectedObjects.Count = 0 Then
MsgBox("이동할 객체를 선택해주세요.")
Exit Sub
End If

' 이동할 거리 입력 받기
Dim xDistance As Double = CDbl(InputBox("X축 이동 거리:"))
Dim yDistance As Double = CDbl(InputBox("Y축 이동 거리:"))

' 변환 행렬 생성 (Z축 이동 무시)
Dim transformationMatrix(1 To 4, 1 To 4) As Double
' 단위 행렬로 초기화 (대각선 성분만 1)
For i As Integer = 1 To 4
For j As Integer = 1 To 4
If i = j Then
transformationMatrix(i, j) = 1
Else
transformationMatrix(i, j) = 0
End If
Next
Next
' X, Y 이동 거리 설정
transformationMatrix(1, 4) = xDistance
transformationMatrix(2, 4) = yDistance

' 선택된 각 객체에 대해 이동 적용
For Each selectedObject As NXObject In selectedObjects
Dim component As Component = TryCast(selectedObject, Component)
If component IsNot Nothing Then
theUF.Component.Move(component, transformationMatrix)
End If
Next

' 재생성
theSession.UpdateManager.DoUpdate(NXOpen.UpdateManager.UpdateOption.Regenerate)

End Sub
End Module
 
Replies continue below

Recommended for you

I tried asking GPT to fix it several times, but it didn't work. PDW has a similar function.
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Assemblies

Module MoveComponentXY
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI()
Dim workPart As Part = theSession.Parts.Work

Try
' Select component to move
Dim selectedObject As TaggedObject = Nothing
Dim cursor As Point3d
Dim response As Selection.Response = theUI.SelectionManager.SelectTaggedObject("Select the component to move", _
"Select a component", _
Selection.SelectionScope.AnyInAssembly, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, _
False, _
selectedObject, _
cursor)

' Validate selection
If response <> Selection.Response.Ok OrElse Not TypeOf selectedObject Is Component Then
theUI.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "No valid component selected.")
Return
End If

Dim selectedComponent As Component = CType(selectedObject, Component)

' Select start point
Dim startPoint As Point3d
If Not SelectPoint("Select the start point", startPoint) Then
Return
End If

' Select end point
Dim endPoint As Point3d
If Not SelectPoint("Select the end point", endPoint) Then
Return
End If

' Calculate translation vector (XY plane only)
Dim translationVector As Vector3d = New Vector3d(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y, 0.0)

' Create identity rotation matrix
Dim identityMatrix As Matrix3x3
identityMatrix.Xx = 1.0 : identityMatrix.Xy = 0.0 : identityMatrix.Xz = 0.0
identityMatrix.Yx = 0.0 : identityMatrix.Yy = 1.0 : identityMatrix.Yz = 0.0
identityMatrix.Zx = 0.0 : identityMatrix.Zy = 0.0 : identityMatrix.Zz = 1.0

' Apply transformation to the component
Dim assemblyManager As AssemblyManager = workPart.AssemblyManager
Dim components() As Component = {selectedComponent}
assemblyManager.TransformComponents(components, identityMatrix, translationVector)

theUI.NXMessageBox.Show("Success", NXMessageBox.DialogType.Information, "Component moved successfully in XY plane.")
Catch ex As Exception
theUI.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, ex.Message)
End Try
End Sub

Private Function SelectPoint(prompt As String, ByRef selectedPoint As Point3d) As Boolean
Dim theUI As UI = UI.GetUI()
Dim view As NXOpen.View = Nothing
Dim point As Point3d

Dim response As Selection.Response = theUI.SelectionManager.SelectScreenPosition(prompt, view, point)

If response = Selection.Response.Ok Then
selectedPoint = point
Return True
Else
Return False
End If
End Function
End Module
 
What is your desired workflow? Allow the user to select a component and 2 points, then move the component in the direction from point1 to point2 (ignoring the Z values)?

Below is some code to allow the user to select a component. The SelectComponent function is the important part; I included a short Sub Main to show how to call it.

Code:
Sub Main()

    Dim myComponent As Assemblies.Component
    If SelectComponent("Select a component", myComponent) = Selection.Response.Cancel Then
        Return
    End If
    'do something with myComponent
End Sub

Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

    Dim theUI As UI = UI.GetUI
    Dim title As String = "Select a component"
    Dim includeFeatures As Boolean = False
    Dim keepHighlighted As Boolean = False
    Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    Dim cursor As Point3d
    Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
    Dim selectionMask_array(0) As Selection.MaskTriple

    With selectionMask_array(0)
        .Type = UFConstants.UF_component_type
        .Subtype = UFConstants.UF_component_subtype
    End With

    Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
     title, scope, selAction, _
     includeFeatures, keepHighlighted, selectionMask_array, _
     selobj, cursor)
    If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
        Return Selection.Response.Ok
    Else
        Return Selection.Response.Cancel
    End If

End Function
 
What is your desired workflow? Allow the user to select a component and 2 points, then move the component in the direction from point1 to point2 (ignoring the Z values)?

Below is some code to allow the user to select a component. The SelectComponent function is the important part; I included a short Sub Main to show how to call it.

Code:
Sub Main()

    Dim myComponent As Assemblies.Component
    If SelectComponent("Select a component", myComponent) = Selection.Response.Cancel Then
        Return
    End If
    'do something with myComponent
End Sub

Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

    Dim theUI As UI = UI.GetUI
    Dim title As String = "Select a component"
    Dim includeFeatures As Boolean = False
    Dim keepHighlighted As Boolean = False
    Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
    Dim cursor As Point3d
    Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
    Dim selectionMask_array(0) As Selection.MaskTriple

    With selectionMask_array(0)
        .Type = UFConstants.UF_component_type
        .Subtype = UFConstants.UF_component_subtype
    End With

    Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
     title, scope, selAction, _
     includeFeatures, keepHighlighted, selectionMask_array, _
     selobj, cursor)
    If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
        Return Selection.Response.Ok
    Else
        Return Selection.Response.Cancel
    End If

End Function

What you said is exactly what I am trying to say. When moving a part or component from point to point, the z value is ignored.
I tried pasting the code you wrote here and there, but it doesn't work. If you don't mind, can I request a correction? Actually, I don't know much about journals, but I will look at this and study.
 
Below is a full example. It will prompt you to select a component and 2 point locations. It will then attempt to move the component along a vector defined by the 2 points (ignoring the Z component). The code is to illustrate the function calls involved and as such it makes some simplifying assumptions and does not do much error checking.

Code:
'Prompt user to select a component and 2 point locations;
'move component along vector, ignoring Z coordinates.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module3
    Dim theSession As Session = Session.GetSession()
    Dim theUI As UI = UI.GetUI()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        lw.Open()

        Const undoMarkName As String = "move component XY"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        Dim myComponent As Assemblies.Component
        If SelectComponent("Select a component", myComponent) = Selection.Response.Cancel Then
            Return
        End If

        Dim startPoint As Point = Nothing
        Dim endPoint As Point = Nothing

        If Not select_point("Select start point reference", startPoint) = UFConstants.UF_UI_OK Then
            Exit Sub
        End If

        If Not select_point("Select end point reference", endPoint) = UFConstants.UF_UI_OK Then
            Exit Sub
        End If

        'create translation vector for component move
        Dim moveVector As Vector3d
        moveVector.X = endPoint.Coordinates.X - startPoint.Coordinates.X
        moveVector.Y = endPoint.Coordinates.Y - startPoint.Coordinates.Y
        moveVector.Z = 0    'ignore the Z component of the picked points

        'use identity matrix so that the component does not rotate
        Dim rotationMatrix As Matrix3x3
        rotationMatrix.Xx = 1
        rotationMatrix.Xy = 0
        rotationMatrix.Xz = 0
        rotationMatrix.Yx = 0
        rotationMatrix.Yy = 1
        rotationMatrix.Yz = 0
        rotationMatrix.Zx = 0
        rotationMatrix.Zy = 0
        rotationMatrix.Zz = 1

        theSession.Parts.Display.ComponentAssembly.MoveComponent(myComponent, moveVector, rotationMatrix)


    End Sub

    Function select_point(ByVal cue As String, ByRef pickedPoint As Point) As Integer

        Dim base_pt As Double() = New Double(2) {}
        Dim pt_tag As NXOpen.Tag = NXOpen.Tag.Null
        Dim response As Integer = 0
        Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointInferred

        theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
        theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
        theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

        Dim pointLocation As Point3d
        pointLocation.X = base_pt(0)
        pointLocation.Y = base_pt(1)
        pointLocation.Z = base_pt(2)
        pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
        'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)

        'lw.WriteLine("selection method used: " & base_method.ToString)

        Return response

    End Function

    Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt,
         title, scope, selAction,
         includeFeatures, keepHighlighted, selectionMask_array,
         selObj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

End Module
 
This works fine.
Thank you very much. Actually, I was at a loss when studying nx journal, but I think it will be really useful if you study based on this code.
Thank you again.




Below is a full example. It will prompt you to select a component and 2 point locations. It will then attempt to move the component along a vector defined by the 2 points (ignoring the Z component). The code is to illustrate the function calls involved and as such it makes some simplifying assumptions and does not do much error checking.

Code:
'Prompt user to select a component and 2 point locations;
'move component along vector, ignoring Z coordinates.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module3
    Dim theSession As Session = Session.GetSession()
    Dim theUI As UI = UI.GetUI()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        lw.Open()

        Const undoMarkName As String = "move component XY"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        Dim myComponent As Assemblies.Component
        If SelectComponent("Select a component", myComponent) = Selection.Response.Cancel Then
            Return
        End If

        Dim startPoint As Point = Nothing
        Dim endPoint As Point = Nothing

        If Not select_point("Select start point reference", startPoint) = UFConstants.UF_UI_OK Then
            Exit Sub
        End If

        If Not select_point("Select end point reference", endPoint) = UFConstants.UF_UI_OK Then
            Exit Sub
        End If

        'create translation vector for component move
        Dim moveVector As Vector3d
        moveVector.X = endPoint.Coordinates.X - startPoint.Coordinates.X
        moveVector.Y = endPoint.Coordinates.Y - startPoint.Coordinates.Y
        moveVector.Z = 0    'ignore the Z component of the picked points

        'use identity matrix so that the component does not rotate
        Dim rotationMatrix As Matrix3x3
        rotationMatrix.Xx = 1
        rotationMatrix.Xy = 0
        rotationMatrix.Xz = 0
        rotationMatrix.Yx = 0
        rotationMatrix.Yy = 1
        rotationMatrix.Yz = 0
        rotationMatrix.Zx = 0
        rotationMatrix.Zy = 0
        rotationMatrix.Zz = 1

        theSession.Parts.Display.ComponentAssembly.MoveComponent(myComponent, moveVector, rotationMatrix)


    End Sub

    Function select_point(ByVal cue As String, ByRef pickedPoint As Point) As Integer

        Dim base_pt As Double() = New Double(2) {}
        Dim pt_tag As NXOpen.Tag = NXOpen.Tag.Null
        Dim response As Integer = 0
        Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointInferred

        theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
        theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
        theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

        Dim pointLocation As Point3d
        pointLocation.X = base_pt(0)
        pointLocation.Y = base_pt(1)
        pointLocation.Z = base_pt(2)
        pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
        'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)

        'lw.WriteLine("selection method used: " & base_method.ToString)

        Return response

    End Function

    Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt,
         title, scope, selAction,
         includeFeatures, keepHighlighted, selectionMask_array,
         selObj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

End Module
 

Part and Inventory Search

Sponsor