Showing results for 
Search instead for 
Do you mean 

Problem with System.InvalidCastExceptio NX 11 Journal

[ Edited ]

Journal worked perfectly in NX 10
When I run the Journal in  NX 11 I get the following message:


"System.InvalidCastExceptio: Das Objekt Des Typs "NXOpen.BasePart[]" kann nicht in Typ "NXOpen.PArt[]" umgewendelt werden."


Journal reads atribude of the Parts, and lists them in the Info window.


Does anyone have a suggestion what to change in the code?


In the attachment you Have picture of error.



Option Strict Off
Imports System
Imports System.Collections  'This line is required to run this as a Journal
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Module save_assembly_in_different_directory
    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow
    Sub Main()
        Dim allParts() As Part = s.Parts.ToArray()
        Dim pieceParts As New ArrayList
        Dim assyParts As New ArrayList

Dim slovoM as Integer=0
Dim slovoS as Integer=0
Dim slovoB as Integer=0
Dim slovoX as Integer=0
Dim slovoK as Integer=0
Dim slovoE as Integer=0

        For Each thisPart As Part In allParts
            Dim thisTag As NXOpen.Tag = ufs.Assem.AskRootPartOcc(thisPart.Tag)
            If thisTag = NXOpen.Tag.Null Then
            End If
        lw.WriteLine("Piece parts found: " & pieceParts.Count.ToString())

Dim noviNazivPart as String
Dim noviNazivAssembly as String

Dim prefix As String = ""
        For inx As Integer = 0 To pieceParts.Count - 1
            Dim aPart As NXOpen.Part = pieceParts(inx)


 Dim words As String() = pieceParts(inx).FullPath.Split("\")

Dim words01 As String() = words(words.Length-1).Split(".")

Dim saveStatus As PartSaveStatus = Nothing

Dim a1 as String = aPart.getstringattribute("NOMENCLATURE")

        lw.WriteLine(words(words.Length-1) & " ==> " & a1  )
'end if

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

        For inx As Integer = 0 To assyParts.Count - 1
            Dim aPart As NXOpen.Part = assyParts(inx)
            'lw.WriteLine(inx.ToString() & ".  Original: " & aPart.FullPath())
            Dim newPath As String = _
                aPart.FullPath.Insert(aPart.FullPath.LastIndexOf("\"), _
            'lw.WriteLine("    Saving As: " & newPath)
            Dim pathOnly As String = Microsoft.VisualBasic.Left(newPath, _
            'If My.Computer.FileSystem.DirectoryExists(pathOnly) = False Then
             '   My.Computer.FileSystem.CreateDirectory(pathOnly)
            'End If

Dim words As String() = assyParts(inx).FullPath.Split("\")

Dim words01 As String() = words(words.Length-1).Split(".")

Dim assemblyNom as String =aPart.GetStringAttribute("NOMENCLATURE")
            Dim saveStatus As PartSaveStatus = Nothing
          '  saveStatus = aPart.SaveAs("D:\Pregled_NX\assemblyy" & inx.ToString)


lw.WriteLine(words(words.Length-1) & " ==> " & assemblyNom  )

       ' lw.WriteLine("partoviStari(" & inx & ")=(*" & words01(0) & "*)" )
 ' lw.WriteLine("partoviNovi(" & inx & ")=(*" & assemblyNom & "*)")

If words(words.Length-1).Contains("M") Then


End if

If words(words.Length-1).Contains("S") Then


End if

If words(words.Length-1).Contains("B") Then


End if

If words(words.Length-1).Contains("X") Then


End if

If words(words.Length-1).Contains("K") Then


End if

If words(words.Length-1).Contains("E") Then


End if

lw.WriteLine("M= " & slovoM.ToString)
lw.WriteLine("S= " & slovoS.ToString)
lw.WriteLine("B= " & slovoB.ToString)
lw.WriteLine("X= " & slovoX.ToString)
lw.WriteLine("K= " & slovoK.ToString)
lw.WriteLine("E= " & slovoE.ToString)
lw.WriteLine("Dodati +1 za citav product)

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




Thanks in advance



Re: Problem with System.InvalidCastExceptio NX 11 Journal

Did you have any .fem, .afm, or .sim parts open when running the journal? These NX file types are not handled by the "part" class and could potentially cause the error that you saw.

Re: Problem with System.InvalidCastExceptio NX 11 Journal

I only open the NX file type .prt

Re: Problem with System.InvalidCastExceptio NX 11 Journal

You need to double check the part types that were open. All parts inherit from BasePart, but only design parts can be cast to Part. FemPart, SimPart, AfemPart, etc. cannot.


This message is explicitly telling you that the part your are trying to cast to Part is not of that type. You may think you don't have any other type of parts open, but the message is telling you otherwise.

Re: Problem with System.InvalidCastExceptio NX 11 Journal

The problem is solved.

I replace the following line of code:


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



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


Thanks anyway.