Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

I learned how to import external expression from excel CSV file to NX!!!! BUT have some questions

Status
Not open for further replies.

godpaul

Automotive
Joined
May 4, 2014
Messages
119
Location
US
nx 8.5

first, thanks to NXjournaling.com. I spent whole day reading codes......seriously..

here is my code:

Option Strict Off
Imports System
Imports NXOpen
Imports System.Text.RegularExpressions
Imports System.Collections.Generic
Imports System.IO
Imports System.Windows.Forms


Module Module1

Sub Main()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

'get number of expressions in the part
'Dim NXexp() As Expression
'NXexp = workPart.Expressions.ToArray
'Dim noOfNXExp As Integer = NXexp.Length

Dim xlsData() As String

Using xlsFile As New Microsoft.VisualBasic.FileIO.TextFieldParser("E:\book.csv")
xlsFile.SetDelimiters(",")
xlsFile.TextFieldType = FileIO.FieldType.Delimited
xlsFile.HasFieldsEnclosedInQuotes = True

While Not xlsFile.EndOfData
xlsData = xlsFile.ReadFields()
For Each NXexp As Expression In workPart.Expressions
If NXexp.Name = xlsData(0) Then
NXexp.RightHandSide = xlsData(1)
Exit For
End If
Next

End While
End Using

End Sub

End Module



the excel book.csv looks like this:
a, 1
diameter, "if a<1 then 10else 15"


the main thing i did is to use TextFieldParser which can detect comma and quotation mark

interresting thing is after this line of code
xlsData = xlsFile.ReadFields()

i tried to use lw.writeline(xlsData) to see what happen, BUT the result is:
System. String[]
which i dont understand


i suspect that did VS divide xlsdata into two strings because the original lne is seperated by a comma
then i try
lw.writeline(xlsData (0))

it return a.

then, i keeping trying
lw.writeline(xlsData (1))

it returns 1



As you see, the code is EXTREMELY INEFFICIENT as it has to go through all expressions in NX, but it works....


question:
1) any better way to improve the code? I saw IsMatch sort of NX built in function but really dont know how to use it
2) to succeed, i use
For Each NXexp As Expression In workPart.Expressions
because initially i use
for i as integer = 0 to 999
If NXexp.
after i type If NXexp.
the VS inteliSense doesnt show up the name....

i still dont know how the expressions work exactly internally










 
For starters, are you aware that NX will allow you to export expressions to a spreadsheet to manipulate them there? Also, you can export expressions to a text file and import them from the same (as long as the format is correct). There may be functionality in NX already that does what you are trying to do.

However, if you plan to write more journals in the future, working through a problem such as this is a good way to learn more about NX and the API.

godpaul said:
i tried to use lw.writeline(xlsData) to see what happen, BUT the result is:
System. String[]
which i don't understand

Your variable xlsData is a string array, the .writeline method only accepts single strings as input (not arrays).

godpaul said:
If NXexp.
after i type If NXexp.
the VS inteliSense doesnt show up the name....

Intellisense should work in this case, there may be an error in your code or VS/NX setup.

godpaul said:
any better way to improve the code?

You can use .FindObject to see if the expression exists in the part file rather than iterating through the entire collection each time. Be aware though, the help file specifically states that .FindObject should not be used in hand written code; but it seems to work fine with expressions in NX 8.5, you will need to test this with future NX versions if/when you upgrade.

The code below illustrates the use of .FindObject:

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    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 expToFind As String = "NomWallThk"

        Try
            Dim myExp As Expression
            myExp = workPart.Expressions.FindObject(expToFind)
            lw.WriteLine(myExp.Name)
            lw.WriteLine(myExp.RightHandSide)

        Catch ex As NXException
            'MsgBox(ex.Message)
            If ex.ErrorCode = 3520016 Then
                'no object found with this name
                'code to handle this error
                lw.WriteLine("expression not found with name: " & expToFind)
            Else
                'code to handle other errors
                lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
            End If
        Finally
            lw.WriteLine("done processing")
            lw.WriteLine("")
        End Try

        lw.Close()

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

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

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

End Module

www.nxjournaling.com
 
Hi, cowski

Oh, I never notice you are the author of the nxjournaling website...thanks again, a wondersul learning resource!!

I will go through the FindObject this weekend :)
 
Hi cowski,

i tried your method by using FindObject
using this method , i can eliminate the for loop :0

NXexp = workPart.Expressions.FindObject(xlsData(0))
NXexp.RightHandSide = xlsData(1)

question is NXexp has lots of perperties like Nxexp.Name, Nxexp.righthandside, etc, etc

how does NX figure out I am trying to find the NAME of the expression BUT not anything else? after the name of the expression matches with xleData(0), then the code proceed to assign expression to it
 
The .FindObject method is programmed to look for the "name" or journal identifier, we are just using this to our advantage. You can give a custom name to pretty much any object in NX, most do not require a name - expressions do.

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

Part and Inventory Search

Sponsor

Back
Top