×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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










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

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.

Quote (godpaul)

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).

Quote (godpaul)

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.

Quote (godpaul)

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

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

(OP)
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 :)

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

Thanks for the kind words, glad to help out.

www.nxjournaling.com

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

(OP)
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

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources