cancel
Showing results for 
Search instead for 
Did you mean: 

More a question of programming than NX [VB]

Legend
Legend

Hello,

 

With this code...\UGOPEN\SampleNXOpenApplications\.NET\File2Points\file2points.vb & this example nxjournaling.com : read-text-file

I have a code who allow me from a file txt to replace a component

My question :

I guess that it is the "catch command" which do that my code stop

I  want when I do not find components (follow my txt file) that my code don't stop

example :

I want to replace "cube" with "shaft" & etc...

and if "cyl" don't exist in Assembly Navigator > continue

Thanks for your help

Txt file :

cube>C:\Users\nicolas\replace\shaft.prt
cyl>C:\Users\nicolas\replace\cylindre_2.prt

 

Capture.JPG

 

 

My code  below

Code :

                Try
                    line = sr.ReadLine()
                    While Not line Is Nothing
                        Dim strings As String() = line.Split(delim)	
                        Dim FindObject As String()	
						
						partUG = strings(0)	
						
						Dim composant As NXOpen.Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT " & strings(0) & " 1"), NXOpen.Assemblies.Component)
						Dim added1 As Boolean
				
						added1 = replaceComponentBuilder1.ComponentsToReplace.Add(composant)
						
						replaceComponentBuilder1.ReplaceAllOccurrences = True
						replaceComponentBuilder1.ReplacementPart = strings(1)
						replaceComponentBuilder1.SetComponentReferenceSetType(NXOpen.Assemblies.ReplaceComponentBuilder.ComponentReferenceSet.Maintain, Nothing)
						
						Dim partLoadStatus1 As NXOpen.PartLoadStatus
						partLoadStatus1 = replaceComponentBuilder1.RegisterReplacePartLoadStatus()
						
						Dim nXObject1 As NXOpen.NXObject
						nXObject1 = replaceComponentBuilder1.Commit()
						
						partLoadStatus1.Dispose()
						replaceComponentBuilder1.Destroy()
						theSession.ListingWindow().WriteLine("Ok >" & strings(0))	

			
                            line = sr.ReadLine()
                    End While
					
                Catch E As Exception
				theSession.ListingWindow().WriteLine("Impossible de trouver '" & partUG & "' dans ton navigateur d'assemblage NX. Stop")
				
				Finally
				sr.Close()	
				
                End Try

 

@mkn
Production: NX11.0.2.7,VERICUT 7.3.3
Development: Tcl/Tk, Gnuplot
www.deshors-tiremold.com
2 REPLIES

Re: More a question of programming than NX [VB]

Genius
Genius

I haven't tested it, but you could try the following:

 


Do While sr.EndOfStream = False Try
line = sr.ReadLine()
Dim strings As String() = line.Split(delim) Dim FindObject As String() partUG = strings(0) Dim composant As NXOpen.Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT " & strings(0) & " 1"), NXOpen.Assemblies.Component) Dim added1 As Boolean added1 = replaceComponentBuilder1.ComponentsToReplace.Add(composant) replaceComponentBuilder1.ReplaceAllOccurrences = True replaceComponentBuilder1.ReplacementPart = strings(1) replaceComponentBuilder1.SetComponentReferenceSetType(NXOpen.Assemblies.ReplaceComponentBuilder.ComponentReferenceSet.Maintain, Nothing) Dim partLoadStatus1 As NXOpen.PartLoadStatus partLoadStatus1 = replaceComponentBuilder1.RegisterReplacePartLoadStatus() Dim nXObject1 As NXOpen.NXObject nXObject1 = replaceComponentBuilder1.Commit() partLoadStatus1.Dispose() replaceComponentBuilder1.Destroy() theSession.ListingWindow().WriteLine("Ok >" & strings(0)) Catch E As Exception theSession.ListingWindow().WriteLine("Impossible de trouver '" & partUG & "' dans ton navigateur d'assemblage NX. Stop") End Try End While sr.Close()

Your code will then continue to cycle in the while loop even if there is an exception thrown during the cycling.

 

regards,

MGeyer

Re: More a question of programming than NX [VB]

Legend
Legend

I only got a problem with 'Do While' because he must to end by 'Loop' , but he works Smiley Happy

Problem solved up to the next one !

Thanks @MGeyer

@mkn
Production: NX11.0.2.7,VERICUT 7.3.3
Development: Tcl/Tk, Gnuplot
www.deshors-tiremold.com