Showing results for 
Search instead for 
Did you mean: 

Obtaining point coordinates relative to a datum csys

Solution Partner Legend Solution Partner Legend
Solution Partner Legend

Hello everyone!

I need to programmatically obtain point coordinates relative to a given Datum CSYS. As in the example: the point has absolute X=40 mm, but I need to obtain X=-10, which is what it is relative to a displayed Datum CSYS.

I understand that I can put WCS to a datum CSYS and use it to obtain coordinates in question. Is it how it is supposed to be done? Or maybe there is a smarter way?



Re: Obtaining point coordinates relative to a datum csys

Siemens Phenom Siemens Phenom
Siemens Phenom

You can use UFCsys.SetWcs(NXOpen.Tag) to set the WCS to a specific csys, then use NXOpen.UF.UFCsys.MapPoint() to map the coordinates.

Re: Obtaining point coordinates relative to a datum csys

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

As an alternative, you could use the .MapPosition method with the .CsysToCsys method. I don't claim it to be "easier" or "smarter", but it could be useful, depending on your needs.


The code below illustrates the usage; it reports all the points in the file with respect to the first datum csys feature found in the part. Often, the first datum csys feature is positioned at absolute, so you may need to move the datum csys away from absolute before running the journal to see any significant result. The code was tested in a single part file, it may need to be tweaked a bit if you are working in the context of an assembly.


Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module convert_coordinates

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = ufsession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow()

    Sub Main()


        'csys of the absolute coordinate system
        Dim absOrigin() As Double = {0.0, 0.0, 0.0}
        Dim absXaxis() As Double = {1.0, 0.0, 0.0}
        Dim absYaxis() As Double = {0.0, 1.0, 0.0}

        'csys of the datum csys
        Dim datumOrigin(2) As Double
        Dim datumXaxis(2) As Double
        Dim datumYaxis(2) As Double

        'for testing, use the first datum csys found
        Dim dCsysFound As Boolean = False
        For Each temp As Features.Feature In theSession.Parts.Work.Features
            If TypeOf (temp) Is NXOpen.Features.DatumCsys Then
                dCsysFound = True
                Dim myDatumCsysBuilder As Features.DatumCsysBuilder = theSession.Parts.Work.Features.CreateDatumCsysBuilder(temp)
                datumOrigin(0) = myDatumCsysBuilder.Csys.Origin.X
                datumOrigin(1) = myDatumCsysBuilder.Csys.Origin.Y
                datumOrigin(2) = myDatumCsysBuilder.Csys.Origin.Z

                datumXaxis(0) = myDatumCsysBuilder.Csys.Orientation.Element.Xx
                datumXaxis(1) = myDatumCsysBuilder.Csys.Orientation.Element.Xy
                datumXaxis(2) = myDatumCsysBuilder.Csys.Orientation.Element.Xz

                datumYaxis(0) = myDatumCsysBuilder.Csys.Orientation.Element.Yx
                datumYaxis(1) = myDatumCsysBuilder.Csys.Orientation.Element.Yy
                datumYaxis(2) = myDatumCsysBuilder.Csys.Orientation.Element.Yz

                Exit For
            End If

        If Not dCsysFound Then
            lw.WriteLine("no datum csys feature found, exiting")
        End If

        Dim transformMatrix(15) As Double

        theUfSession.Mtx4.CsysToCsys(datumOrigin, datumXaxis, datumYaxis, absOrigin, absXaxis, absYaxis, transformMatrix)

        'report coordinates of points in file w.r.t. the datum csys
        Dim newCoords(2) As Double
        For Each temp As Point In theSession.Parts.Work.Points
            newCoords(0) = temp.Coordinates.X
            newCoords(1) = temp.Coordinates.Y
            newCoords(2) = temp.Coordinates.Z

            lw.WriteLine("coordinate in absolute:")
            lw.WriteLine(newCoords(0).ToString & ", " & newCoords(1).ToString & ", " & newCoords(2).ToString)

            'MapPosition takes a 12 element array (transformation matrix) as input
            'we can use the 16 element array from the .CsysToCsys method, .MapPosition will ignore the extra elements
            theUfSession.Trns.MapPosition(newCoords, transformMatrix)

            lw.WriteLine("coordinates w.r.t. datum csys:")
            lw.WriteLine(newCoords(0).ToString & ", " & newCoords(1).ToString & ", " & newCoords(2).ToString)


    End Sub

    Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module