Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Copy a component from single csys to multiple csys- Nx Journal

Status
Not open for further replies.

Kumaresh_07

Automotive
Jul 21, 2019
14
Hello Cowski and Fellow Members,

I would like to copy a component from single csys to multiple csys in Nx Assembly part.

MOVE_CSYS_TO_CSYS_mmcgil.jpg


I got most of the code from cowski's replies. He wrote code for move a component from one csys to another csys's.
But my requirement is I need to copy component from one csys to n number of csys's.
so I had modified that code with "SelectTaggedObjects" method. but I missed something else. so It's shows
"System.InvalidCastException" error. I tried in all ways. But I can't able to figure out the issue.

Modified code and Error image attached below.

While run the below Journal,
Step 1)user going to select csys of component(From csys).
Step 2)user going to select destination csys's (Here user can select multiple Csys's of Nx Assembly)
Result : selected component placed in all csys's ,which I selected in step 2.
Above step 1 & 2 are worked But After journal Execution the component not placed in csys's. Journal window shows "System.InvalidCastException" error)

Error image Attached below vb code.

It would be great , if someone help with this.

Code:
'NXJournaling.com
'January 5, 2015
'Move a component from Csys to Csys
'Journal will prompt user to select a "from" csys and a "to" csys; the "from" csys should belong to the
'component that you want to move.


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1


    Dim nxopenSession As NXOpen.UF.UFSession

    Sub Main()

        Dim ui As UI = ui.GetUI
        Dim theUI As UI = ui.GetUI()

        Dim theSession As Session = Session.GetSession()
        Dim theUfSession As UFSession = UFSession.GetUFSession()
        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

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

        Dim fromCsys As CoordinateSystem = Nothing
        Dim toCsys As CoordinateSystem = Nothing

        If SelectCsys("Select 'From' coordinate system", fromCsys) = Selection.Response.Cancel Then
            Return
        End If

        'If SelectmultipleCsys("Select 'To' coordinate system", toCsys) = Selection.Response.Cancel Then
        '    Return
        'End If

        'calculate Csys to Csys transformation matrix
        Dim fromOrigin() As Double = {fromCsys.Origin.X, fromCsys.Origin.Y, fromCsys.Origin.Z}
        Dim fromXAxis() As Double = {fromCsys.Orientation.Element.Xx, fromCsys.Orientation.Element.Xy, fromCsys.Orientation.Element.Xz}
        Dim fromYAxis() As Double = {fromCsys.Orientation.Element.Yx, fromCsys.Orientation.Element.Yy, fromCsys.Orientation.Element.Yz}

        'Dim csyslist As NXObject = Nothing
        'lw.Open()
        'Dim obj As NXOpen.TaggedObject
        'Dim selectedObjects1 As TaggedObject() = SelectmultipleCsys("", csyslist)

        'If SelectmultipleCsys("Select 'To' coordinate system", toCsys) = Selection.Response.Cancel Then
        '    Return
        'End If

        'Dim tocsys As NXObject = Nothing
        Dim obj As NXOpen.TaggedObject
        'Dim selectedObjects1 As TaggedObject() = SelectmultipleCsys(toCsys)

        For Each obj In SelectmultipleCsys(toCsys)

            Dim toOrigin() As Double = {toCsys.Origin.X, toCsys.Origin.Y, toCsys.Origin.Z}
            Dim toXAxis() As Double = {toCsys.Orientation.Element.Xx, toCsys.Orientation.Element.Xy, toCsys.Orientation.Element.Xz}
            Dim toYAxis() As Double = {toCsys.Orientation.Element.Yx, toCsys.Orientation.Element.Yy, toCsys.Orientation.Element.Yz}

            Dim mtx4Transform(15) As Double

            theUfSession.Mtx4.CsysToCsys(fromOrigin, fromXAxis, fromYAxis, toOrigin, toXAxis, toYAxis, mtx4Transform)

            'extract the rotation matrix and the tranlsation vector
            Dim rotMatrix(8) As Double
            theUfSession.Mtx4.AskRotation(mtx4Transform, rotMatrix)
            Dim transVec(2) As Double
            theUfSession.Mtx4.AskTranslation(mtx4Transform, transVec)

            'convert array of doubles to vector 3d
            Dim translateVector As Vector3d = New Vector3d(transVec(0), transVec(1), transVec(2))
            'convert array of doubles to Matrix3x3
            Dim rotationMatrix As Matrix3x3 = convertToMatrix3x3(rotMatrix)

            'determine component to move based on the chosen "from" csys
            Dim comp As Assemblies.Component = fromCsys.OwningComponent
            Dim componentPositioner1 As Positioning.ComponentPositioner = theSession.Parts.Work.ComponentAssembly.Positioner

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

            Dim componentNetwork1 As Positioning.ComponentNetwork = CType(network1, Positioning.ComponentNetwork)
            Dim markId2 As Session.UndoMarkId = Nothing

            If comp IsNot Nothing Then
                markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Move Component")

                Dim movableObjects() As NXObject = {comp}
                componentNetwork1.SetMovingGroup(movableObjects)
                componentNetwork1.BeginDrag()
                'reposition component from the rotation matrix and translation vector
                componentNetwork1.DragByTransform(translateVector, rotationMatrix)
                componentNetwork1.EndDrag()
                componentNetwork1.ResetDisplay()
                componentNetwork1.ApplyToModel()

                componentNetwork1.Solve()

            End If

            theSession.UpdateManager.AddToDeleteList(componentNetwork1)
            theSession.UpdateManager.DoUpdate(markId2)

            lw.Close()
        Next obj

    End Sub

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

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a Coordinate system"
        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.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_coordinate_system_type
            .Subtype = UFConstants.UF_all_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

    Function SelectmultipleCsys(ByRef selObj As TaggedObject)


        Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
        Dim workPart As NXOpen.Part = theSession.Parts.Work
        Dim displayPart As NXOpen.Part = theSession.Parts.Display

        Dim ui As UI = ui.GetUI
        Dim message As String = "Select the faces to assign the surface finish"
        Dim title As String = "Select a Coordinate systems"
        Dim resp As Selection.Response
        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.AnyInAssembly
        Dim selectedObjects As TaggedObject() = Nothing
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_coordinate_system_type
            .Subtype = UFConstants.UF_all_subtype
        End With

        ''Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
        ' title, scope, selAction, _
        ' includeFeatures, keepHighlighted, selectionMask_array, _
        ' selObj, cursor)

        resp = ui.SelectionManager.SelectTaggedObjects(message, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selectedObjects)

        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
        'Return selectedObjects

    End Function

    Function convertToMatrix3x3(ByVal mtx As Double()) As Matrix3x3

        Dim mx As Matrix3x3
        With mx
            .Xx = mtx(0)
            .Xy = mtx(1)
            .Xz = mtx(2)
            .Yx = mtx(3)
            .Yy = mtx(4)
            .Yz = mtx(5)
            .Zx = mtx(6)
            .Zy = mtx(7)
            .Zz = mtx(8)
        End With

        Return mx

    End Function

End Module

Journal Error Image:

Error_Image_ggplat.jpg


Thanks in Advance,
Kumaresh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor