Cancel
Showing results for 
Search instead for 
Did you mean: 

Get coordinates of points from Point Set

Creator
Creator

Hey all, 

 

I have been through all the threads about how to export xyz coordinates from a Point Set and have given VB a shot. However, I am not having luck with exporting the xyz coordinates from the Point Set. I can export data point coordinates but not specifically from the point set that I am interested in. I am not sure how to implement this in my code. Could someone please take a look at what I have and point me in the right direction! I did see a post about Point.Set.GetPoints(), but I am not sure how to implement this in my code. Very new to VB. Mostly write with MATLAB and the syntax is very different from object oriented. Thanks in advance!

 

RVP

17 REPLIES

Re: Get coordinates of points from Point Set

Creator
Creator

Sorry, I am using NX 12

Here is the code that I have so far. Its rather simplistic but I am a new user to journaling with NX and VB:

'-----------------------'
Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.UF

Module PointDataToFile
Sub Main()
'set up code to get the nx session and defines the variables
Dim s As Session = Session.GetSession()
Dim ui As UI = ui.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim wp As Part = s.Parts.Work

'Get point set.
Dim pointcollection1 As PointCollection = wp.Points

'Open file to write contents.
Dim objStreamWriter As StreamWriter ‘defines method to write to file

'Specify directory to write file to.
objStreamWriter = New StreamWriter("U:\Point_Set.txt") ‘creates the file and writes to this directory

'Write file header.
Dim Header as string = “XYZ Coordinates of Points Defining Curve”
objstreamwriter.writeline(Header)


'Define cnt1 (counter) to start at 1.
Dim cnt1 As Integer = 1

'Write the xyz header line before the loop starts.
objstreamwriter.writeline(“Point, X, Y, Z”)

'Loop.
For Each pt As Point In pointcollection1
'write contents of header2 and the counter to file
'objStreamWriter.WriteLine(header2 & cnt1.ToString)

'Write the xyz coordinates of points to the text file.
objstreamwriter.writeline(cnt1 & “,” & Math.Round(pt.Coordinates.X, 3).ToString & “,” & Math.Round(pt.Coordinates.Y, 3).ToString & “,” & Math.Round(pt.Coordinates.Z, 3).ToString)

'Start the counter.
cnt1 += 1 ‘ same as saying cnt1 = cnt1 + 1

Next
'Close the file.
objStreamWriter.Close()
End Sub

End Module

Re: Get coordinates of points from Point Set

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

Have you tried running the journal here? It should print out all the point coordinates from each point set found in your current work part (formatting might need some work). What results did you get vs. what were you expecting?

Re: Get coordinates of points from Point Set

Creator
Creator

I have a curve (spline) that I place 2000 points along, using a point set. I just want to write the xyz coordinates of the point set data points (the 2000 pts along the curve), so I can use that data to reproduce the curve in Excel for verification of the points extracted. When I run my script, I assumed incorrectly that if I isolated the point set to a layer, that I could just have that layer visible and my code would only see the point set objects. However, what I get is for an output is 6000 points and when I graph that in excel, it is nothing close to the curve I am trying to recreate. I added a picture of the points along the curve that I am intending to get the coordinates for.

Re: Get coordinates of points from Point Set

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

No, not your code. We know that doesn't work or you wouldn't be here. Smiley Happy Run the code that I linked to and see what happens.

Re: Get coordinates of points from Point Set

Creator
Creator
Ok cool, I'll give it a try and get back to you!

Re: Get coordinates of points from Point Set

Creator
Creator

So I gave it a run and it appears to print the information in the window, but once it is done printing to the window, it crashes the program and all is frozen. I am accessing the program using Citrix Applications, and that may be a problem. If I wanted to have it export the xyz data by writing it to a file, how would I do that? In my code, I have it set to write all the coordinates to a file, which numbers them 1-2000 [points, x, y, z] in column format, which makes it very easy to copy and paste into excel. With this output I would have to go through each point and delete the "x=" "y=" and "z=". Its a great code though and appears to work great. Any thoughts about writing it to a file? I have run it several times and crashes every time once it finishes. I wonder if writing it to a file would eliminate that issue? I am also not sure that it is grabbing the data from just the point set. Something appears off with the data when I watch it printing to the information window. 

Re: Get coordinates of points from Point Set

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

"I am accessing the program using Citrix Applications, and that may be a problem."

That seems likely.

 

"If I wanted to have it export the xyz data by writing it to a file, how would I do that? In my code, I have it set to write all the coordinates to a file, which numbers them 1-2000 [points, x, y, z] in column format, which makes it very easy to copy and paste into excel."

My code shows how to access the points given a point set feature. Your code writes out the point information in your desired format. Take the bits of code you like from one of the journals and update the other to make it work for you.

 

The last hurdle is how to identify the point set that you want to export; the part file may have multiple point set features. You might want to export each point set to an individual file. Alternately, you could give each point set feature a custom name or attribute and your code could find and export the point set(s) based on that.

Re: Get coordinates of points from Point Set

Creator
Creator
Let me work with that for a bit and see if I can Frankenstein something together. It may be easiest to just export the point set to a new file. I’ve never exported something like that, but I assume you can export it as a part file. I will try that and see if I can isolate just the point set to it’s own file and grab the coordinates that way. I will update you whether it works or not. Really appreciate the help. Thank you!

Re: Get coordinates of points from Point Set

@RVP

Here is a simple addition to your program that allows the user to select a pointset feature.  If the user selects any other feature type then the program simply exits. Note you will need to change the output file location. It runs fine for me on a test part which two point sets (with 2000 points) and various other points.

 

Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.Features
Imports NXOpen.UF

Module PointDataToFile
    Dim s As Session = Session.GetSession()
    Dim ui As UI = UI.GetUI()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim wp As Part = s.Parts.Work
    Sub Main()
        'set up code to get the nx session and defines the variables
        ' select point set feature
        Dim feat As Feature = Nothing
        Dim pointSet1 As PointSet = Nothing
        Dim thePoints(-1) As Point
        Dim response1 As Selection.Response = select_feature(feat)
        If feat.GetFeatureName.ToString.Contains("Point Set") Then
            pointSet1 = DirectCast(feat, PointSet)
            thePoints = pointSet1.GetPoints
        Else
            Exit Sub
        End If

        'Open file to write contents.
        Dim objStreamWriter As StreamWriter 'defines method to write to file

        'Specify directory to write file to.
        objStreamWriter = New StreamWriter("C:\temp3\Point_Set.txt") 'creates the file and writes to this directory

        'Write file header.
        Dim Header As String = “XYZ Coordinates of Points Defining Curve”
        objStreamWriter.WriteLine(Header)


        'Define cnt1 (counter) to start at 1.
        Dim cnt1 As Integer = 1

        'Write the xyz header line before the loop starts.
        objStreamWriter.WriteLine(“Point, X, Y, Z”)

        'Loop.
        For Each pt As Point In thePoints
            'write contents of header2 and the counter to file
            'objStreamWriter.WriteLine(header2 & cnt1.ToString)

            'Write the xyz coordinates of points to the text file.
            objStreamWriter.WriteLine(cnt1 & “,” & Math.Round(pt.Coordinates.X, 3).ToString & “,” & Math.Round(pt.Coordinates.Y, 3).ToString & “,” & Math.Round(pt.Coordinates.Z, 3).ToString)

            'Start the counter.
            cnt1 += 1 ' same as saying cnt1 = cnt1 + 1

        Next
        'Close the file.
        objStreamWriter.Close()
    End Sub
    Public Function select_feature(ByRef feat As Feature) As Selection.Response
        Dim selobj As NXObject = Nothing
        Dim cursor As Point3d
        Dim typeArray() As Selection.SelectionType =
         {Selection.SelectionType.Features}

        Dim resp As Selection.Response = UI.SelectionManager.SelectTaggedObject("Select feature",
           "Select feature", Selection.SelectionScope.WorkPart, False, typeArray, selobj, cursor)
        If ((resp = Selection.Response.ObjectSelected) _
                    OrElse (resp = Selection.Response.ObjectSelectedByName)) Then
            feat = CType(selobj, Feature)
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
    End Function
End Module

Frank Swinkels