Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Journal Component position as expressions 1

Status
Not open for further replies.

OlafU

Automotive
Jan 31, 2012
19
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.
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
 
Replies continue below

Recommended for you

The "Try" block, as it is currently written, will only warn you if it runs into the "not found" condition. Any other errors will be ignored. So, it may be that there is an error occurring, but you don't know about it. The first thing that I'd try is to modify the Try block to report any error it encounters:

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 try

If 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
 
The error code is: Error 3270008, The specified string contains a syntax error

NX11.0.0
 
I'm referring to the NX 9 help files, so this may be different in NX 11; but the expression.Edit method takes an expression and a string value. Try changing the edit lines to something like:

Code:
workPart.Expressions.Edit(x_pos, compPt.X.ToString)

www.nxjournaling.com
 
Hello cowski.
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 Module

For 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
 
A copy-Paste-Mistake:
Code-Lines for second Way should be:
Code:
'Second Way: Create String expressions and make conversion in NX
			workPart.Expressions.Edit(x_pos, """" & compPt.X.ToString & """")
			workPart.Expressions.Edit(y_pos, """" & compPt.Y.ToString & """")
			workPart.Expressions.Edit(z_pos, """" & compPt.Z.ToString & """")

Olaf

NX11.0.0
 
I was assuming that you were using numeric expressions instead of string expressions. I guess that I should back up and ask a fundamental question: What do you intend to do with these component position expressions once you have them?

www.nxjournaling.com
 
Your assumption was wright: I need the component postions as numerical expressions in NX as Inputs for a sketch.
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
 
Below is a small modification of your original code, it should fix the radix issue.

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 Module

www.nxjournaling.com
 
Thank you very much. It works perfectly. I added a few lines to round the coordinates to 3 decimals:
Code:
Dim X_Pos_N As Double = Math.Round(compPt.X,3)
		Dim Y_Pos_N As Double = Math.Round(compPt.Y,3)
		Dim Z_Pos_N As Double = Math.Round(compPt.Z,3)

and then

Code:
workPart.Expressions.Edit(x_pos, X_Pos_N.ToString(Globalization.CultureInfo.InvariantCulture))
		    workPart.Expressions.Edit(y_pos, Y_Pos_N.ToString(Globalization.CultureInfo.InvariantCulture))
		    workPart.Expressions.Edit(z_pos, Z_Pos_N.ToString(Globalization.CultureInfo.InvariantCulture))

Merry XMAS and a Happy New Year :)

Olaf

NX11.0.0
 
Alternatively, you could use the string format specifier "F3" where the "F" stands for "fixed point" and the "3" controls the number of decimal places.

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([highlight #FCE94F]"F3"[/highlight], Globalization.CultureInfo.InvariantCulture))
		    workPart.Expressions.Edit(y_pos, compPt.Y.ToString([highlight #FCE94F]"F3"[/highlight], Globalization.CultureInfo.InvariantCulture))
		    workPart.Expressions.Edit(z_pos, compPt.Z.ToString([highlight #FCE94F]"F3"[/highlight], 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

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

Part and Inventory Search

Sponsor