Extracting "Coordinate System" type from "Datum Coordinate System" type

Hello,

I am new to these forums, so please forgive any mistakes I make in formatting.

I am building a journal file that can replace or clone a Datum Coordinate System and have its parent be a "DEFAULT" Datum Coordinate System based on my company's customer standards.

The problem comes where I need to retrieve both the coordinate system of the feature AND the feature by having the user select a single target Datum.

What I would want to happen, ideally, would be for the user to select the Datum FEATURE and be able to have the journal extract a coordinate system type from the feature. Is this possible?

Thank you,

There are a few ways to get the csys from a datum csys feature. The UF function .AskDatumCsysComponents will work, but I prefer to use the DatumCsysBuilder object. Both methods are illustrated in this eng-tips thread:
https://www.eng-tips.com/viewthread.cfm?qid=363348

I wrote a small helper class based on the DatumCsysBuilder approach. The "Sub Main" code below shows how to use the class.

'NXJournaling.com
'June 11, 2014
'
'Demonstration of a class that allows easy access to the components of a DatumCsys object.

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

Module Module2

Sub Main()

Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim myDatumCsys As New NXJ_DatumCsys
For Each myFeature As Features.Feature In workPart.Features
If TypeOf (myFeature) Is Features.DatumCsys Then

lw.WriteLine(myFeature.GetFeatureName)
myDatumCsys.TheDatumCsys = myFeature

lw.WriteLine("Datum Csys origin: " & myDatumCsys.Origin.Coordinates.ToString)
'lw.WriteLine(" journal identifier: " & myDatumCsys.Origin.JournalIdentifier)
lw.WriteLine("")

lw.WriteLine("X axis direction: " & myDatumCsys.XAxis.Direction.ToString)
lw.WriteLine("Y axis direction: " & myDatumCsys.YAxis.Direction.ToString)
lw.WriteLine("Z axis direction: " & myDatumCsys.ZAxis.Direction.ToString)
lw.WriteLine("")

lw.WriteLine("XY plane normal: " & myDatumCsys.XYPlane.Normal.ToString)
lw.WriteLine("YZ plane normal: " & myDatumCsys.YZPlane.Normal.ToString)
lw.WriteLine("XZ plane normal: " & myDatumCsys.XZPlane.Normal.ToString)
lw.WriteLine("")

End If
Next

lw.Close()

End Sub

Public 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

'The class requires the following imports:
'Imports System
'Imports System.Collections.Generic
'Imports NXOpen

Public Class NXJ_DatumCsys

#Region "Properties"

Private _theDatumCsys As Features.DatumCsys = Nothing
Public Property TheDatumCsys() As Features.DatumCsys
Get
Return _theDatumCsys
End Get
Set(ByVal value As Features.DatumCsys)
_theDatumCsys = value
Me.GetComponents()
End Set
End Property

Private _XYplane As DatumPlane = Nothing
Public ReadOnly Property XYPlane() As DatumPlane
Get
Return _XYplane
End Get
End Property

Private _YZplane As DatumPlane = Nothing
Public ReadOnly Property YZPlane() As DatumPlane
Get
Return _YZplane
End Get
End Property

Private _XZplane As DatumPlane = Nothing
Public ReadOnly Property XZPlane() As DatumPlane
Get
Return _XZplane
End Get
End Property

Private _Xaxis As DatumAxis = Nothing
Public ReadOnly Property XAxis() As DatumAxis
Get
Return _Xaxis
End Get
End Property

Private _Yaxis As DatumAxis = Nothing
Public ReadOnly Property YAxis() As DatumAxis
Get
Return _Yaxis
End Get
End Property

Private _Zaxis As DatumAxis = Nothing
Public ReadOnly Property ZAxis() As DatumAxis
Get
Return _Zaxis
End Get
End Property

Private _origin As Point = Nothing
Public ReadOnly Property Origin() As Point
Get
Return _origin
End Get
End Property

Private _csys As CoordinateSystem = Nothing
Public ReadOnly Property Csys() As CoordinateSystem
Get
Return _csys
End Get
End Property

Private _dCsysComponents As New List(Of DisplayableObject)
Private ReadOnly Property Components As List(Of DisplayableObject)
Get
Return _dCsysComponents
End Get
End Property

Private _layer As Integer
Public Property Layer() As Integer
Get
Return _layer
End Get
Set(ByVal value As Integer)
If Not (value < 1 Or value > 256) Then
Me.ChangeLayer(value)
_layer = value
End If
End Set
End Property

Private _color As Integer
Public Property Color() As Integer
Get
Return _color
End Get
Set(ByVal value As Integer)
If Not (value < 1 Or value > 216) Then
Me.ChangeColor(value)
_color = value
End If
End Set
End Property

Private _scaleFactor As Double
Public Property ScaleFactor() As Double
Get
Return _scaleFactor
End Get
Set(ByVal value As Double)
_scaleFactor = value
Me.SetScaleFactor(value)
End Set
End Property

#End Region

#Region "Private Variables"

Private Const mathTolerance As Double = 0.0001
Private theSession As Session = Session.GetSession

Private thePlanes As New List(Of DatumPlane)
Private theAxes As New List(Of DatumAxis)

Private tempXdir As Vector3d
Private tempYdir As Vector3d
Private tempZdir As Vector3d

#End Region

#Region "Public Methods"

Public Sub New()

End Sub

Public Sub New(ByVal aDatumCsys As Features.DatumCsys)

Me.TheDatumCsys = aDatumCsys

End Sub

#End Region

#Region "Private Methods"

Private Sub GetComponents()

Dim DBuilder As Features.DatumCsysBuilder
DBuilder = theSession.Parts.Work.Features.CreateDatumCsysBuilder(_theDatumCsys)
_scaleFactor = DBuilder.DisplayScaleFactor
Dim DCObj() As TaggedObject = DBuilder.GetCommittedObjects

Dim tempCsys As CartesianCoordinateSystem

'grab the csys
For Each temp As TaggedObject In DCObj
_dCsysComponents.Add(temp)
If TypeOf (temp) Is CartesianCoordinateSystem Then
tempCsys = temp

tempCsys.GetDirections(tempXdir, tempYdir)

tempZdir.X = tempCsys.Orientation.Element.Zx
tempZdir.Y = tempCsys.Orientation.Element.Zy
tempZdir.Z = tempCsys.Orientation.Element.Zz

End If

If TypeOf (temp) Is Point Then
_origin = temp
Dim tempPoint As Point = temp
_layer = tempPoint.Layer
End If

If TypeOf (temp) Is CoordinateSystem Then
_csys = temp
End If

'axes seem to be reported in X, Y, Z order
If TypeOf (temp) Is DatumAxis Then
theAxes.Add(temp)
End If

'planes seem to be reported in XY, YZ, XZ order
If TypeOf (temp) Is DatumPlane Then
thePlanes.Add(temp)
End If

Next
DBuilder.Destroy()

Me.GetAxes()
Me.GetPlanes()

End Sub

Private Sub GetAxes()

'find the X axis
For Each tempAxis As DatumAxis In theAxes
If Math.Abs(tempAxis.Direction.X - tempXdir.X) < mathTolerance AndAlso
Math.Abs(tempAxis.Direction.Y - tempXdir.Y) < mathTolerance Then
_Xaxis = tempAxis
theAxes.Remove(tempAxis)
Exit For
End If
Next

'find the Y axis
For Each tempAxis As DatumAxis In theAxes
If Math.Abs(tempAxis.Direction.X - tempYdir.X) < mathTolerance AndAlso
Math.Abs(tempAxis.Direction.Y - tempYdir.Y) < mathTolerance Then
_Yaxis = tempAxis
theAxes.Remove(tempAxis)
Exit For
End If
Next

'last axis = Z axis
_Zaxis = theAxes.Item(0)

End Sub

Private Sub GetPlanes()

'find the XY plane
For Each tempPlane As DatumPlane In thePlanes
If Math.Abs(tempPlane.Normal.Z - tempZdir.Z) < mathTolerance AndAlso
Math.Abs(tempPlane.Normal.X - tempZdir.X) < mathTolerance Then
_XYplane = tempPlane
thePlanes.Remove(tempPlane)
Exit For
End If
Next

'find the YZ plane
For Each tempPlane As DatumPlane In thePlanes
If Math.Abs(tempPlane.Normal.Z - tempXdir.Z) < mathTolerance AndAlso
Math.Abs(tempPlane.Normal.X - tempXdir.X) < mathTolerance Then
_YZplane = tempPlane
thePlanes.Remove(tempPlane)
Exit For
End If
Next

'last plane = XZ plane
_XZplane = thePlanes.Item(0)

End Sub

Private Sub ChangeLayer(ByVal newLayer As Integer)

If newLayer < 1 Or newLayer > 256 Then
'layer out of bounds
Return
End If

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.NewLayer = newLayer
displayModification1.Apply(_dCsysComponents.ToArray)
displayModification1.Dispose()

End Sub

Private Sub ChangeColor(ByVal newColorNum As Integer)

If newColorNum < 1 Or newColorNum > 216 Then
'color number out of bounds
Return
End If

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.NewColor = newColorNum
displayModification1.Apply(_dCsysComponents.ToArray)
displayModification1.Dispose()

End Sub

Private Sub SetScaleFactor(ByVal newScaleFactor As Double)

Dim DBuilder As Features.DatumCsysBuilder
DBuilder = theSession.Parts.Work.Features.CreateDatumCsysBuilder(_theDatumCsys)
DBuilder.DisplayScaleFactor = newScaleFactor
DBuilder.Commit()
DBuilder.Destroy()

End Sub

#End Region

End Class

Thank you! I brought in the class and used it to extract the Csys. It worked perfectly.

Another question, when using datumCsysBuilder, is there a way to have the created datum be a certain "type" of datum (I.E. Dynamic, Inferred, Offset CSYS...). I'd like my newly created datum to be of the type "Offset CSYS" so when the USER tries to edit the datum, they'll be met with the "Offset CSYS" dialog window and not the "Dynamic" dialog window.

If you want an offset datum csys, it will need to be offset from something when it is created. Start a new part file, create a datum csys, and now record a journal while you create a new datum csys offset from the first one. The resulting code will show you how to create an offset datum csys.