Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Import lines to NX4 1

Status
Not open for further replies.

pitertxuss

Industrial
Jun 11, 2012
3
Hello every body,
I would like to import lines to a cad model. I have in a worksheet the lines defined by points (x,y). I know how to import those points, but I can´t import a line. It´s possible? for import the points I´m using "file2points" file.

Thanks!!
 
Replies continue below

Recommended for you

Inside of NX's database lines are defined simply as two points. So if you have the points imported, that all that you need. Granted, you will now need to actually create the 'line' objects, but you already have the data needed for the job.

John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Industry Sector
Cypress, CA
Siemens PLM:
UG/NX Museum:

To an Engineer, the glass is twice as big as it needs to be.
 
Here is a quick & dirty edit that will import the pairs of points in the file as lines. This may not do exactly what you want, but should serve as an example of how to create the lines from your given points.

Code:
[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] System.IO  
[COLOR=blue]Imports[/color] System.Windows.Forms  
[COLOR=blue]Imports[/color] NXOpen  

[COLOR=blue]Module[/color] Module1  

    [COLOR=blue]Sub[/color] Main()  
        [COLOR=blue]Try[/color]  
            [COLOR=blue]Dim[/color] openFileDialog1 [COLOR=blue]As[/color] [COLOR=blue]New[/color] OpenFileDialog()  

            openFileDialog1.Filter [COLOR=blue]=[/color] "txt files (*.txt)|*.txt|All files (*.*)|*.*"  
            openFileDialog1.FilterIndex [COLOR=blue]=[/color] 1  
            openFileDialog1.RestoreDirectory [COLOR=blue]=[/color] [COLOR=blue]True[/color]  

            [COLOR=blue]If[/color] openFileDialog1.ShowDialog() [COLOR=blue]=[/color] DialogResult.OK [COLOR=blue]Then[/color]  
                [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
                [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
                [COLOR=blue]Dim[/color] sr [COLOR=blue]As[/color] StreamReader [COLOR=blue]=[/color] [COLOR=blue]New[/color] StreamReader(openFileDialog1.FileName)  
                [COLOR=blue]Dim[/color] line [COLOR=blue]As[/color] [COLOR=blue]String[/color]  
                [COLOR=blue]Dim[/color] startPoint [COLOR=blue]As[/color] Point3d  
                [COLOR=blue]Dim[/color] endPoint [COLOR=blue]As[/color] Point3d  
                [COLOR=blue]Dim[/color] i [COLOR=blue]As[/color] [COLOR=blue]Integer[/color] [COLOR=blue]=[/color] 0  

                [COLOR=blue]Try[/color]  
                    line [COLOR=blue]=[/color] sr.ReadLine()  
                    [COLOR=blue]While[/color] [COLOR=blue]Not[/color] line [COLOR=blue]Is[/color] [COLOR=blue]Nothing[/color]  
 [COLOR=green]'Dim pt As Point3d[/color]
                        [COLOR=blue]Dim[/color] delim [COLOR=blue]As[/color] Char() [COLOR=blue]=[/color] {","c}  
                        [COLOR=blue]Dim[/color] strings [COLOR=blue]As[/color] String() [COLOR=blue]=[/color] line.Split(delim)  
                        endPoint.x [COLOR=blue]=[/color] Double.Parse(strings(0))  
                        endPoint.y [COLOR=blue]=[/color] Double.Parse(strings(1))  
                        endPoint.z [COLOR=blue]=[/color] Double.Parse(strings(2))  
                        [COLOR=blue]If[/color] [COLOR=blue]Not[/color] IsNothing(startPoint) [COLOR=blue]Then[/color]  
 [COLOR=green]'create a line from startpoint to endpoint[/color]
                            workPart.Curves.CreateLine(startPoint, endPoint)  
                        End [COLOR=blue]If[/color]  
                        startPoint [COLOR=blue]=[/color] endPoint  

 [COLOR=green]'Dim p As Point[/color]
 [COLOR=green]'p = session.Parts.Work.Points.CreatePoint(pt)[/color]
 [COLOR=green]'p.SetVisibility(SmartObject.VisibilityOption.Visible)[/color]

                        line [COLOR=blue]=[/color] sr.ReadLine()  
                    End [COLOR=blue]While[/color]  
                [COLOR=blue]Finally[/color]  
                    sr.Close()  
                End [COLOR=blue]Try[/color]  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Catch[/color] E [COLOR=blue]As[/color] Exception  
            MessageBox.Show(E.Message)  
        End [COLOR=blue]Try[/color]  
    End [COLOR=blue]Sub[/color]  


    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  

 [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

    End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
thanks!! I will go to try! I´ve never used the journal before, so I have to try it.
 
In what format should be the coordinates in the file?
My file looks:

264.560,-259.448,-18.276
263.969,-259.748,-18.302
263.374,-260.040,-18.328
262.776,-260.325,-18.354
262.174,-260.602,-18.380
266.889,-258.178,-18.274

and the result: Input string was not in a correct format.

----
kukelyk
 
Your test data appears to be in the correct comma delimited format. I ran a test on your posted data and it worked. I suspect this is only a small portion of your data; does the data file have headers or blank lines? If so, they may cause problems. The journal uses Double.Parse to convert input to numbers, if there are other non-numeric characters in the file it may also cause such an error.

www.nxjournaling.com
 
After I removed all empty lines, the error is the same..[mad]

----
kukelyk
 
With the exception of an extra line from (0,0,0) to the first point in the file, it works fine for me.

What language is your computer set to use? I suspect it is a .NET setting that is interpreting your input differently than what my computer is using (reference thread561-315066 for another example). If your computer language is something other than English, perhaps we can "borrow" petulf's fix from the other thread.

Below is the updated code to get rid of that extra line.

Code:
[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]    
[COLOR=blue]Imports[/color] System    
[COLOR=blue]Imports[/color] System.IO    
[COLOR=blue]Imports[/color] System.Windows.Forms    
[COLOR=blue]Imports[/color] NXOpen    

[COLOR=blue]Module[/color] Module1    

    [COLOR=blue]Sub[/color] Main()    
        [COLOR=blue]Try[/color]    
            [COLOR=blue]Dim[/color] openFileDialog1 [COLOR=blue]As[/color] [COLOR=blue]New[/color] OpenFileDialog()    

            openFileDialog1.Filter [COLOR=blue]=[/color] "txt files (*.txt)|*.txt|All files (*.*)|*.*"    
            openFileDialog1.FilterIndex [COLOR=blue]=[/color] 1    
            openFileDialog1.RestoreDirectory [COLOR=blue]=[/color] [COLOR=blue]True[/color]    

            [COLOR=blue]If[/color] openFileDialog1.ShowDialog() [COLOR=blue]=[/color] DialogResult.OK [COLOR=blue]Then[/color]    
                [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()    
                [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work    
                [COLOR=blue]Dim[/color] sr [COLOR=blue]As[/color] StreamReader [COLOR=blue]=[/color] [COLOR=blue]New[/color] StreamReader(openFileDialog1.FileName)    
                [COLOR=blue]Dim[/color] line [COLOR=blue]As[/color] [COLOR=blue]String[/color]    
                [COLOR=blue]Dim[/color] startPoint [COLOR=blue]As[/color] Point3d  [COLOR=blue]=[/color] [COLOR=blue]nothing[/color]  
                [COLOR=blue]Dim[/color] endPoint [COLOR=blue]As[/color] Point3d    
                [COLOR=blue]Dim[/color] i [COLOR=blue]As[/color] [COLOR=blue]Integer[/color] [COLOR=blue]=[/color] 0    
				Dim firstPass [COLOR=blue]as[/color] [COLOR=blue]Boolean[/color] [COLOR=blue]=[/color] [COLOR=blue]True[/color]  
                [COLOR=blue]Try[/color]    
                    line [COLOR=blue]=[/color] sr.ReadLine()    
                    [COLOR=blue]While[/color] [COLOR=blue]Not[/color] line [COLOR=blue]Is[/color] [COLOR=blue]Nothing[/color]    
 [COLOR=green]'Dim pt As Point3d[/color]
                        [COLOR=blue]Dim[/color] delim [COLOR=blue]As[/color] Char() [COLOR=blue]=[/color] {","c}    
                        [COLOR=blue]Dim[/color] strings [COLOR=blue]As[/color] String() [COLOR=blue]=[/color] line.Split(delim)    
                        endPoint.x [COLOR=blue]=[/color] Double.Parse(strings(0))    
                        endPoint.y [COLOR=blue]=[/color] Double.Parse(strings(1))    
                        endPoint.z [COLOR=blue]=[/color] Double.Parse(strings(2))    
                        [COLOR=blue]If[/color] firstPass [COLOR=blue]Then[/color]  
							firstPass [COLOR=blue]=[/color] [COLOR=blue]False[/color]  
						Else  
 [COLOR=green]'create a line from startpoint to endpoint[/color]
                            workPart.Curves.CreateLine(startPoint, endPoint)    
                        End [COLOR=blue]If[/color]    
                        startPoint [COLOR=blue]=[/color] endPoint    

 [COLOR=green]'Dim p As Point[/color]
 [COLOR=green]'p = session.Parts.Work.Points.CreatePoint(pt)[/color]
 [COLOR=green]'p.SetVisibility(SmartObject.VisibilityOption.Visible)[/color]

                        line [COLOR=blue]=[/color] sr.ReadLine()    
                    End [COLOR=blue]While[/color]    
                [COLOR=blue]Finally[/color]    
                    sr.Close()    
                End [COLOR=blue]Try[/color]    
            End [COLOR=blue]If[/color]    
        [COLOR=blue]Catch[/color] E [COLOR=blue]As[/color] Exception    
            MessageBox.Show(E.Message)    
        End [COLOR=blue]Try[/color]    
    End [COLOR=blue]Sub[/color]    


    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]    

 [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination    

    End [COLOR=blue]Function[/color]    

End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
Thanks, but still not resolved, the same error..
Code:
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Module Test
    Sub Main
                Dim session As Session = Session.GetSession()
                Dim line As String
                    line = "264.560,-259.448,-18.276"
                        Dim pt As Point3d
                       ' Dim delim As Char () = { ","C }
                        Dim delim As Char () = { "," }
                        Dim strings As String () = line.Split(",")
session.ListingWindow.Open    
session.ListingWindow.WriteLine( strings(0) )  
session.ListingWindow.WriteLine( strings(1) )  
session.ListingWindow.WriteLine( strings(2) )  

                        pt.x = Double.Parse(strings(0))
                        pt.y = Double.Parse(strings(1))
                        pt.z = Double.Parse(strings(2))
 
                       Dim p As Point
                        p = session.Parts.Work.Points.CreatePoint(pt)
                        p.SetVisibility(SmartObject.VisibilityOption.Visible)
    End Sub
End Module
gives as a result:
in the info window:
264.560
-259.448
-18.276

error message:
System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Double.Parse(String s)
at Test.Main() in C:\Users\kkukely\AppData\Local\Temp\NXJournals3400\journal.vb:line 23

What??
the 23th row is:

pt.x = Double.Parse(strings(0))

----
kukelyk
 
What language/regional settings is your computer set to use?
In win xp this can be found in the control panel under "Regional and Language options", I'd guess it is in a similar location for more recent versions of windows as well.

www.nxjournaling.com
 
Really, the problem was in this "Regional and Language options". When I set it to en-US, the Double.Parse runs correctly.

----
kukelyk
 
Other problem: it is possible to create points in WCS instead of absolute?

----
kukelyk
 
Other problem: is it possible to create points in WCS instead of absolute?

----
kukelyk
 
This version should work regardless of your regional setting and it will output the lines relative to the WCS.

Code:
[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] System.IO  
[COLOR=blue]Imports[/color] System.Windows.Forms  
[COLOR=blue]Imports[/color] NXOpen  
[COLOR=blue]Imports[/color] NXOpen.UF  

[COLOR=blue]Module[/color] Module1  
	Dim ufs [COLOR=blue]As[/color] UFSession [COLOR=blue]=[/color] UFSession.GetUFSession  

	Sub Main()  
		Try  
			Dim openFileDialog1 [COLOR=blue]As[/color] [COLOR=blue]New[/color] OpenFileDialog()  

			openFileDialog1.Filter [COLOR=blue]=[/color] "txt files (*.txt)|*.txt|All files (*.*)|*.*"  
			openFileDialog1.FilterIndex [COLOR=blue]=[/color] 1  
			openFileDialog1.RestoreDirectory [COLOR=blue]=[/color] [COLOR=blue]True[/color]  

			If openFileDialog1.ShowDialog() [COLOR=blue]=[/color] DialogResult.OK [COLOR=blue]Then[/color]  
				Dim theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
				Dim workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
				Dim sr [COLOR=blue]As[/color] StreamReader [COLOR=blue]=[/color] [COLOR=blue]New[/color] StreamReader(openFileDialog1.FileName)  
				Dim line [COLOR=blue]As[/color] [COLOR=blue]String[/color]  
				Dim startPoint [COLOR=blue]As[/color] Point3d  [COLOR=blue]=[/color] [COLOR=blue]nothing[/color]  
				Dim endPoint [COLOR=blue]As[/color] Point3d  
				Dim i [COLOR=blue]As[/color] [COLOR=blue]Integer[/color] [COLOR=blue]=[/color] 0  
				Dim firstPass [COLOR=blue]as[/color] [COLOR=blue]Boolean[/color] [COLOR=blue]=[/color] [COLOR=blue]True[/color]  
				Dim USculture [COLOR=blue]As[/color] system.globalization.CultureInfo [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Globalization.CultureInfo("en-US")  
				Try  
					line [COLOR=blue]=[/color] sr.ReadLine()  
					While [COLOR=blue]Not[/color] line [COLOR=blue]Is[/color] [COLOR=blue]Nothing[/color]  
						Dim delim [COLOR=blue]As[/color] Char() [COLOR=blue]=[/color] {","c}  
						Dim strings [COLOR=blue]As[/color] String() [COLOR=blue]=[/color] line.Split(delim)  
						endPoint.x [COLOR=blue]=[/color] Double.Parse(strings(0), USculture)  
						endPoint.y [COLOR=blue]=[/color] Double.Parse(strings(1), USCulture)  
						endPoint.z [COLOR=blue]=[/color] Double.Parse(strings(2), USCulture)  
						endPoint [COLOR=blue]=[/color] Abs2WCS(endPoint)  
						If firstPass [COLOR=blue]Then[/color]  
							firstPass [COLOR=blue]=[/color] [COLOR=blue]False[/color]  
						Else  
 [COLOR=green]'create a line from startpoint to endpoint[/color]
							workPart.Curves.CreateLine(startPoint, endPoint)  
						End [COLOR=blue]If[/color]  
						startPoint [COLOR=blue]=[/color] endPoint  
						line [COLOR=blue]=[/color] sr.ReadLine()  
					End [COLOR=blue]While[/color]  
				Finally  
					sr.Close()  
				End [COLOR=blue]Try[/color]  
			End [COLOR=blue]If[/color]  
		Catch E [COLOR=blue]As[/color] Exception  
			MessageBox.Show(E.Message)  
		End [COLOR=blue]Try[/color]  
	End [COLOR=blue]Sub[/color]  
[COLOR=green]'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[/color]
[COLOR=green]'Date:  11/18/2010[/color]
[COLOR=green]'Subject:  Sample NX Open .NET Visual Basic routine : map point from absolute to wcs[/color]
[COLOR=green]'[/color]
[COLOR=green]'Note:  GTAC provides programming examples for illustration only, and[/color]
[COLOR=green]'assumes that you are familiar with the programming language being[/color]
[COLOR=green]'demonstrated and the tools used to create and debug procedures.  GTAC[/color]
[COLOR=green]'support professionals can help explain the functionality of a particular[/color]
[COLOR=green]'procedure, but we will not modify these examples to provide added[/color]
[COLOR=green]'functionality or construct procedures to meet your specific needs.[/color]

	Function Abs2WCS(ByVal inPt [COLOR=blue]As[/color] Point3d) [COLOR=blue]As[/color] Point3d  
		Dim pt1(2), pt2(2) [COLOR=blue]As[/color] [COLOR=blue]Double[/color]  

		pt1(0) [COLOR=blue]=[/color] inPt.X  
		pt1(1) [COLOR=blue]=[/color] inPt.Y  
		pt1(2) [COLOR=blue]=[/color] inPt.Z  

		ufs.Csys.MapPoint(UFConstants.UF_CSYS_ROOT_COORDS, pt1, _  
			UFConstants.UF_CSYS_ROOT_WCS_COORDS, pt2)  

		Abs2WCS.X [COLOR=blue]=[/color] pt2(0)  
		Abs2WCS.Y [COLOR=blue]=[/color] pt2(1)  
		Abs2WCS.Z [COLOR=blue]=[/color] pt2(2)  

    End [COLOR=blue]Function[/color]   
[COLOR=green]'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&[/color]
	Public [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  

[COLOR=green]'Unloads the image when the NX session terminates[/color]
		GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

	End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor