Showing results for 
Search instead for 
Do you mean 
Reply

Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Hello, I have been trying to use a script made by GTAC for the simple purpose of populating a list of the objects in the model. The Model itself is quite large and a list of the parts would be nice.

 

I have been trying to use the AskChildrenOrder script made by GTAC to achieve this. I like the script as it gives me the parts in Chronological order in the same way they are listed in my assembly navigator, plus it is fast and efficient.

 

However I am now getting an error.

 

Please take a look at the picture. Also below is the code I used.

 

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports System.Collections.Generic

Module IR_7571150

    Dim theSession As Session = Session.GetSession()
    Dim theUFSession As UFSession = UFSession.GetUFSession()
    Dim workPart As Part = theSession.Parts.Work

    Public Sub Main(ByVal args As String())
        Dim displayPart As Part = theSession.Parts.Display

        Dim markId1 As NXOpen.Session.UndoMarkId = theSession.SetUndoMark( _
            NXOpen.Session.MarkVisibility.Visible, "Reorder Components")

        Dim theOrders As Assemblies.ComponentOrder() = Nothing
        displayPart.ComponentAssembly.GetComponentOrders(theOrders)

        Echo(displayPart.Leaf & " has " & theOrders.Length & " ComponentOrder objects")

        For Each anOrder As Assemblies.ComponentOrder In theOrders

            anOrder.Activate()
            theSession.UpdateManager.DoUpdate(markId1)

            Echo(anOrder.Name & ":")
            WalkAssembyTree(displayPart.ComponentAssembly.RootComponent, anOrder, "")

            theUFSession.Ui.DisplayMessage(anOrder.Name, 1)

            theSession.UndoToMark(markId1, "")
        Next

        theSession.DeleteUndoMarksUpToMark(markId1, "", False)

    End Sub

    Sub WalkAssembyTree(ByVal theComponent As NXOpen.Assemblies.Component,
                        ByVal theOrder As NXOpen.Assemblies.ComponentOrder,
                        ByVal indent As String)

        Dim kids As Assemblies.Component() = theComponent.GetChildren()
        If kids.Length = 0 Then Return ' Not an assembly or sub-assembly 

        indent = indent + "  "

        ' This does not always work - see PR 7421390 
        ' Testing in the Toycar assembly only Chronological returns any Children 

        kids = theOrder.AskChildrenOrder(theComponent)

        For ii As Integer = 0 To kids.Length - 1
            Echo(indent & kids(ii).DisplayName)

            '========================================================

            ' Process parts here

            Dim thisPart As Part = kids(ii).Prototype()

            processThisPart(thisPart, indent)


            '========================================================



            WalkAssembyTree(kids(ii), theOrder, indent)
        Next
    End Sub

    Public Sub processThisPart(ByVal p As Part, ByVal ind As String)

        Dim theBodies() As Body = p.Bodies.ToArray()
        Echo(ind & "Bodies in part: " & theBodies.GetLength(0).ToString())

        For Each thisBody As Body In theBodies 

            Echo(ind & thisBody.ToString())

            '======================== Other desired processing here


        Next

    End Sub

    Sub Echo(ByVal output As String)
        theSession.ListingWindow.Open()
        theSession.ListingWindow.WriteLine(output)
        theSession.LogFile.WriteLine(output)
    End Sub

    Public Function GetUnloadOption(ByVal arg As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module

 

 

Appearantly the problem is with this line:

 

Dim thisPart As Part = kids(ii).Prototype()

 

Any help is appreciated. Thank you!

9 REPLIES

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Instead of:

Dim thisPart As Part = kids(ii).Prototype()

 

Try this:

Dim thisPart As Part = kids(ii).Prototype.OwningPart

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Hi

I getting the same error in the below code.

 

Please suggest

Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module replace_underscores_with_hyphens_in_assembly_part_names

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow

    Sub Main()

        Dim dispPart As Part = s.Parts.Display()
        Dim allParts() As Part = s.Parts.ToArray()
        Dim pieceParts As New ArrayList
        Dim assyParts As New ArrayList

        For Each thisPart As Part In allParts
            Dim thisTag As NXOpen.Tag = ufs.Assem.AskRootPartOcc(thisPart.Tag)

            If thisTag = NXOpen.Tag.Null Then
                pieceParts.Add(thisPart)
            Else
                assyParts.Add(thisPart)
            End If
            
        Next

        '========== Piece parts must be processed first.
 lw.Open()
        lw.WriteLine("Piece parts found: " & pieceParts.Count.ToString())

        For inx As Integer = 0 To pieceParts.Count - 1
            Dim aPart As NXOpen.Part = pieceParts(inx)
            Dim oldPath As String = aPart.FullPath()
            Dim lastSlash As Integer = oldPath.LastIndexOf("\")
            Dim oldShortName As String = oldPath.Substring(lastSlash)

            lw.WriteLine(inx.ToString() & ".  Original : " & oldPath)
            Dim newShortName As String = oldShortName.Replace("_", "_")
            Dim newName As String = oldPath.Replace(oldShortName, newShortName)

            If oldShortName <> newShortName Then

                lw.WriteLine("    Saving As: " & newName)

                Dim saveStatus As PartSaveStatus = Nothing
                saveStatus = aPart.SaveAs(newName)
            Else
                lw.WriteLine("   Not Saving: " & newName)
            End If
        Next


        lw.WriteLine("======================================================")
        lw.WriteLine(" ")

        '========== Then the assembly parts can be processed.

        lw.WriteLine("Assembly parts found:" & assyParts.Count.ToString())

        For inx As Integer = 0 To assyParts.Count - 1
            Dim aPart As NXOpen.Part = assyParts(inx)
            Dim oldPath As String = aPart.FullPath()
            Dim lastSlash As Integer = oldPath.LastIndexOf("\")
            Dim oldShortName As String = oldPath.Substring(lastSlash)

Dim old_1 As Double
Dim new_1 As Double 
Dim EXCEL = CreateObject("Excel.Application") 
EXCEL.Visible = False 
Dim Doc = EXCEL.Workbooks.Open("D:\rename.xlsx", ReadOnly:=True) 
Dim Sheets = EXCEL.Sheets 
For i As Integer = 1 To Doc.Sheets.Count 
Dim Sheet = Doc.Sheets.Item(i) 
Dim pointCounter As Integer
 
For pointCounter = 0 To Sheet.UsedRange.Rows.Count - 1 
old_1 = Sheet.Cells((pointCounter + 1), 1).Value 
new_1= Sheet.Cells((pointCounter + 1), 2).Value 

            lw.WriteLine(inx.ToString() & ".  Original : " & oldPath)
            Dim newShortName As String = oldShortName.Replace(old_1, new_1)
            Dim newName As String = oldPath.Replace(oldShortName, newShortName)

            If oldShortName <> newShortName Then

                lw.WriteLine("    Saving As: " & newName)

                Dim saveStatus As PartSaveStatus = Nothing
                saveStatus = aPart.SaveAs(newName)
            Else
                lw.WriteLine("   Not Saving: " & newName)
            End If
Next
        Next
Next

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module

at this particular line

Dim allParts() As Part = s.Parts.ToArray()

 

 

Error.PNG

 

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

The problem is solved.

I replace the following line of code:

 

Dim allParts() As Part = s.Parts.ToArray()

 

with:

Dim allParts() As BasePart = s.Parts.ToArray()

 

There is a new proble at line 83

old_1 = Sheet.Cells((pointCounter + 1), 1)).Value

I am trying to read from excel sheet and the error i get is

Error-2.PNG

Please suggest

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Perhaps consider changing the variable old_1 to type String. I don't think you can use a type Double in the .Replace() method anyway. Without testing though I'm not sure. Give that try and see if it works.

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Hi

I have a part number inside that particular excel cell.It is Alpha Numeric as XY123.

Please Suggest

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Did you try what I suggested? If not let me know. It should work.

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

You stated your own problem. You have a part "number" that is not a number at all (in a programming sense). Both examples given (ML291903 and XY123) are not in a numeric format since they contain leading alpha characters. As suggested earlier, you need to store these in a String, not a Double.

 

Change the following:

 

Dim old_1 as Double

Dim new_1 as Double

 

to:

 

Dim old_1 as String

Dim new_1 as String

 

You are doing string replace operations on these later. Even if you were able to get the values from Excel, the oldShortName.Replace() method would have failed as it requires arguments of type String.

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Hi junfan 

It works .

AmazeBalls.

Re: Unable to cast object of type NXOpen.NXObject to type NXOpen.Part

Hi Jim

Thanks .Yes you are right .

I haven't looked at that variable.

Amazeballs!!!