cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with System.InvalidCastExceptio NX 11 Journal

Experimenter
Experimenter

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
                pieceParts.Add(thisPart)
            Else
                assyParts.Add(thisPart)
            End If
        Next
 
        lw.Open()
        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)

'Msgbox(pieceParts(inx).FullPath)

 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.Open
        lw.WriteLine(words(words.Length-1) & " ==> " & a1  )
'end if
 
            
        Next
lw.Open
        lw.WriteLine("=====================================================================================")
       



 
        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("\"), _
                "\NewDirectory")
            'lw.WriteLine("    Saving As: " & newPath)
            Dim pathOnly As String = Microsoft.VisualBasic.Left(newPath, _
                                                  newPath.LastIndexOf("\"))
 
            '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.Open

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

slovoM=slovoM+1

End if


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

slovoS=slovoS+1

End if

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

slovoB=slovoB+1

End if

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

slovoX=slovoX+1

End if

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

slovoK=slovoK+1

End if

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

slovoE=slovoE+1

End if






        Next
lw.Open
     lw.WriteLine("======================================================")
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

 

4 REPLIES

Re: Problem with System.InvalidCastExceptio NX 11 Journal

Honored Contributor
Honored Contributor

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

Experimenter
Experimenter

I only open the NX file type .prt

Re: Problem with System.InvalidCastExceptio NX 11 Journal

Siemens Phenom Siemens Phenom
Siemens Phenom

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

Experimenter
Experimenter

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()

 

Thanks anyway.