Journal Component position as expressions
Journal Component position as expressions
(OP)
Hello all.
With the Journal below I can select a Component and the Journal gives out the Position in an Information Windows and is also editing existing expressions in the work part.
It works fine for coordinates as whole numbers. But if one coordinate is not a whole number the Journal do not change the expressions.
E.g. if the first coordinate is a whole number, the second not and the third coordinate again a whole number, the Journal only modifies the Expression for the first coordinate, the Output to the Information Windows still works perfectly.
Has someone a hint?
Thanks in Advance
Olaf
With the Journal below I can select a Component and the Journal gives out the Position in an Information Windows and is also editing existing expressions in the work part.
It works fine for coordinates as whole numbers. But if one coordinate is not a whole number the Journal do not change the expressions.
E.g. if the first coordinate is a whole number, the second not and the third coordinate again a whole number, the Journal only modifies the Expression for the first coordinate, the Output to the Information Windows still works perfectly.
CODE -->
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module Module1
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Dim myComponent As NXObject
lw.Open()
If SelectComponent("Select component", myComponent) = Selection.Response.Cancel Then
Return
End If
lw.WriteLine(myComponent.Name)
Dim compPt As Point3d = CompPos(myComponent)
lw.WriteLine(" X" & compPt.X.ToString & " Y" & compPt.Y.ToString & " Z" & compPt.Z.ToString)
lw.WriteLine("")
Try
Dim x_pos As Expression = CType(workPart.Expressions.FindObject("X_Pos"), Expression)
Dim y_pos As Expression = CType(workPart.Expressions.FindObject("Y_Pos"), Expression)
Dim z_pos As Expression = CType(workPart.Expressions.FindObject("Z_Pos"), Expression)
workPart.Expressions.Edit(x_pos, compPt.X)
workPart.Expressions.Edit(y_pos, compPt.Y)
workPart.Expressions.Edit(z_pos, compPt.Z)
Catch ex as NXException
if ex.ErrorCode = 3520016 Then
lw.WriteLine("Expression not found!")
end if
end try
End Sub
Function SelectComponent(ByVal prompt As String, ByRef selObj As NXObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim title As String = "Select component"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
Dim cursor As Point3d
With selectionMask_array(0)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_component_subtype
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj,cursor)
If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
Function CompPos(ByVal someComponent As Component) As Point3d
Dim pt As Point3d
Dim RotMat As Matrix3x3
someComponent.GetPosition(pt, RotMat)
Return pt
End Function
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
End Function
End Module Has someone a hint?
Thanks in Advance
Olaf
NX11.0.0





RE: Journal Component position as expressions
CODE
Try Dim x_pos As Expression = CType(workPart.Expressions.FindObject("X_Pos"), Expression) Dim y_pos As Expression = CType(workPart.Expressions.FindObject("Y_Pos"), Expression) Dim z_pos As Expression = CType(workPart.Expressions.FindObject("Z_Pos"), Expression) workPart.Expressions.Edit(x_pos, compPt.X) workPart.Expressions.Edit(y_pos, compPt.Y) workPart.Expressions.Edit(z_pos, compPt.Z) Catch ex as NXException if ex.ErrorCode = 3520016 Then lw.WriteLine("Expression not found!") else 'report any other error as well lw.WriteLine("Error " & ex.ErrorCode & ", " & ex.message) end if end tryIf an error is reported, we'll have a better idea what is going wrong. If no error is reported, we'll need to look elsewhere...
www.nxjournaling.com
RE: Journal Component position as expressions
NX11.0.0
RE: Journal Component position as expressions
CODE
www.nxjournaling.com
RE: Journal Component position as expressions
I figured out two Ways: First one is to truncate the Positions to whole numbers, which will be enough for one of my UseCases.
Second Way is to bring these values as strings to NX and make the conversion there:
CODE -->
Imports System Imports NXOpen Imports NXOpen.UF Imports NXOpen.Assemblies Module Module1 Sub Main() Dim theSession As Session = Session.GetSession() Dim workPart As Part = theSession.Parts.Work Dim lw As ListingWindow = theSession.ListingWindow Dim myComponent As NXObject lw.Open() If SelectComponent("Select component", myComponent) = Selection.Response.Cancel Then Return End If lw.WriteLine(myComponent.Name) Dim compPt As Point3d = CompPos(myComponent) lw.WriteLine(" X" & compPt.X.ToString & " Y" & compPt.Y.ToString & " Z" & compPt.Z.ToString) lw.WriteLine("") Try Dim x_pos As Expression = CType(workPart.Expressions.FindObject("X_Pos"), Expression) Dim y_pos As Expression = CType(workPart.Expressions.FindObject("Y_Pos"), Expression) Dim z_pos As Expression = CType(workPart.Expressions.FindObject("Z_Pos"), Expression) 'First Way: Only Whole numbers 'workPart.Expressions.Edit(x_pos, compPt.X.ToString("0")) 'workPart.Expressions.Edit(y_pos, compPt.Y.ToString("0")) 'workPart.Expressions.Edit(z_pos, compPt.Z.ToString("0")) 'Second Way: Create String expressions and make conversion in NX workPart.Expressions.Edit(x_pos, """" & compPt.X.ToString & """") workPart.Expressions.Edit(y_pos, """" & compPt.X.ToString & """") workPart.Expressions.Edit(z_pos, """" & compPt.X.ToString & """") Catch ex as NXException if ex.ErrorCode = 3520016 Then lw.WriteLine("Expression not found!") else lw.WriteLine("Error " & ex.ErrorCode & ", " & ex.message) end if end try End Sub Function SelectComponent(ByVal prompt As String, ByRef selObj As NXObject) As Selection.Response Dim theUI As UI = UI.GetUI Dim title As String = "Select component" Dim includeFeatures As Boolean = False Dim keepHighlighted As Boolean = False Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly Dim selectionMask_array(0) As Selection.MaskTriple Dim cursor As Point3d With selectionMask_array(0) .Type = UFConstants.UF_component_type .Subtype = UFConstants.UF_component_subtype End With Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _ title, scope, selAction, _ includeFeatures, keepHighlighted, selectionMask_array, _ selobj,cursor) If resp = Selection.Response.ObjectSelected Or _ resp = Selection.Response.ObjectSelectedByName Then Return Selection.Response.Ok Else Return Selection.Response.Cancel End If End Function Function CompPos(ByVal someComponent As Component) As Point3d Dim pt As Point3d Dim RotMat As Matrix3x3 someComponent.GetPosition(pt, RotMat) Return pt End Function Public Function GetUnloadOption(ByVal dummy As String) As Integer 'Unloads the image when the NX session terminates GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination End Function End ModuleFor the second Way I created several expressions in NX to get a number out of the string:
X_Pos =>the expression with the value from the Journal, Type String
X_Pos_Char_Replace = charReplace(X_Pos_String, ",", ".") Type String => replaces the comma in the value of the string by a point
X_Pos_Make_Number = MakeNumber(Char_Replace) Type Number => Give me the number from the string
Next function would be to round or truncate the Number to 3 decimals.
Maybe there is a more elegant way for the second Way, e.g. bring everything to the Journal so that there is no need for the steps in NX.
Olaf
NX11.0.0
RE: Journal Component position as expressions
Code-Lines for second Way should be:
CODE -->
Olaf
NX11.0.0
RE: Journal Component position as expressions
www.nxjournaling.com
RE: Journal Component position as expressions
I can use the truncated whole numbers of my first way to use these values directly as numerical expressions. But when there are decimals I receive this Syntax error message when I try to use these values as numerical expressions. I assume, the cause is, that NX requires a Point (like 0.01) for numerical expressions, but my Journal delivers a comma (like 0,01). Therefore I came up with the detour of using string values and convert these string values to numerical expressions in NX.
NX11.0.0
RE: Journal Component position as expressions
CODE
Option Strict Off Imports System Imports NXOpen Imports NXOpen.UF Imports NXOpen.Assemblies Module Module1 Sub Main() Dim theSession As Session = Session.GetSession() Dim workPart As Part = theSession.Parts.Work Dim lw As ListingWindow = theSession.ListingWindow Dim myComponent As NXObject lw.Open() If SelectComponent("Select component", myComponent) = Selection.Response.Cancel Then Return End If lw.WriteLine(myComponent.Name) Dim compPt As Point3d = CompPos(myComponent) lw.WriteLine(" X" & compPt.X.ToString & " Y" & compPt.Y.ToString & " Z" & compPt.Z.ToString) lw.WriteLine("") Try Dim x_pos As Expression = CType(workPart.Expressions.FindObject("X_Pos"), Expression) Dim y_pos As Expression = CType(workPart.Expressions.FindObject("Y_Pos"), Expression) Dim z_pos As Expression = CType(workPart.Expressions.FindObject("Z_Pos"), Expression) workPart.Expressions.Edit(x_pos, compPt.X.ToString(Globalization.CultureInfo.InvariantCulture)) workPart.Expressions.Edit(y_pos, compPt.Y.ToString(Globalization.CultureInfo.InvariantCulture)) workPart.Expressions.Edit(z_pos, compPt.Z.ToString(Globalization.CultureInfo.InvariantCulture)) Catch ex as NXException if ex.ErrorCode = 3520016 Then lw.WriteLine("Expression not found!") else lw.WriteLine("Error [" & ex.ErrorCode & "]: " & ex.Message) end if end try End Sub Function SelectComponent(ByVal prompt As String, ByRef selObj As NXObject) As Selection.Response Dim theUI As UI = UI.GetUI Dim title As String = "Select component" Dim includeFeatures As Boolean = False Dim keepHighlighted As Boolean = False Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly Dim selectionMask_array(0) As Selection.MaskTriple Dim cursor As Point3d With selectionMask_array(0) .Type = UFConstants.UF_component_type .Subtype = UFConstants.UF_component_subtype End With Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _ title, scope, selAction, _ includeFeatures, keepHighlighted, selectionMask_array, _ selobj,cursor) If resp = Selection.Response.ObjectSelected Or _ resp = Selection.Response.ObjectSelectedByName Then Return Selection.Response.Ok Else Return Selection.Response.Cancel End If End Function Function CompPos(ByVal someComponent As Component) As Point3d Dim pt As Point3d Dim RotMat As Matrix3x3 someComponent.GetPosition(pt, RotMat) Return pt End Function Public Function GetUnloadOption(ByVal dummy As String) As Integer 'Unloads the image when the NX session terminates GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination End Function End Modulewww.nxjournaling.com
RE: Journal Component position as expressions
CODE -->
and then
CODE -->
Merry XMAS and a Happy New Year
Olaf
NX11.0.0
RE: Journal Component position as expressions
CODE
Try Dim x_pos As Expression = CType(workPart.Expressions.FindObject("X_Pos"), Expression) Dim y_pos As Expression = CType(workPart.Expressions.FindObject("Y_Pos"), Expression) Dim z_pos As Expression = CType(workPart.Expressions.FindObject("Z_Pos"), Expression) workPart.Expressions.Edit(x_pos, compPt.X.ToString("F3", Globalization.CultureInfo.InvariantCulture)) workPart.Expressions.Edit(y_pos, compPt.Y.ToString("F3", Globalization.CultureInfo.InvariantCulture)) workPart.Expressions.Edit(z_pos, compPt.Z.ToString("F3", Globalization.CultureInfo.InvariantCulture)) Catch ex as NXException if ex.ErrorCode = 3520016 Then lw.WriteLine("Expression not found!") else lw.WriteLine("Error [" & ex.ErrorCode & "]: " & ex.Message) end if end trywww.nxjournaling.com