×
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

Import_run_export script

Import_run_export script

Import_run_export script

(OP)
I want to import a value from an excell sheet (lets say from column A row one)to a predefined expression (called "SmStroke") update the model according to this newly imported value (a constraint in the model is defined by "SmStroke"), export a number of measurements also defined as expressions back to the excell sheet to row 1 columns B,C,D... and so on. Then import the value from Column A row 2, update model, export values to row 2 columns B,C,D.. and so on, then do the same for column A row 3....

Can this be done?

RE: Import_run_export script

(OP)
Thank you! I will give this a try and come back if (when) i run into problems :)

RE: Import_run_export script

(OP)
The code you linked to works like a charm, thank you! I have managed to get the model to take a value from the excel sheet, put it into an expression, update the model according to the new expression, get the next value from the spreadsheet, update end so on. I have spent half a day looking for a way to export a measurement but can't seem to find a way. All i need now is a subroutine at the end of each loop that exports measurements to row "i" in the spreadsheet.
I don't expect someone to do all the work for me just to be pointed in the right direction and i will try to cut it together myself :)

RE: Import_run_export script

(OP)
Got it working! Running a test tonight as i expect it to take 4-5hours..
I hope it's ok to post altered code from NXJournaling.com?

CODE -->

'spreadsheet_update

'NXJournaling.com
'April 13, 2015
'
'Update a model from a range of values in a spreadsheet.
'The specified expression in the part file must make use of the
'ug_cell_read or ug_excel_read function.
'The journal will loop through the specified rows in the specified column
'and attempt to update the model.
'If any update error occurs, the corresponding cell reference is written to the listing window.

Option Strict Off
Imports System
Imports System.Text.RegularExpressions
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUfSession As UFSession = UFSession.GetUFSession()
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim objExcel = CreateObject("Excel.Application")  
        If objExcel Is Nothing Then  
            MsgBox("Could not start Excel, this journal will now exit.", MsgBoxStyle.Critical, "Error")  
            Exit Sub  
        End If  

        Dim excelFile As String = "c:\my excel file location"  
        If Not IO.File.Exists(excelFile) Then  
            MsgBox("Specified file not found, journal will now exit.", MsgBoxStyle.Critical, "File not found.")  
            Exit Sub  
        End If  

        Dim objWorkbook = objExcel.Workbooks.Open(excelFile)  
        If objWorkbook Is Nothing Then  
            MsgBox("Could not open Excel file, journal will now exit.", MsgBoxStyle.Critical, "Error")  
            Exit Sub  
        End If  

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

        Const undoMarkName As String = "update expression from spreadsheet"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        'Change the values of the following constants to suit your needs
        '  specify spreadsheet column
        Const spreadsheetColumn As String = "A"
        '  specify spreadsheet rows
        Const spreadsheetRowStart As Integer = 4
        Const spreadsheetRowEnd As Integer = 6
        '  specify expression name in .prt file that references
        '  spreadsheet value
        Const expressionName As String = "my expression to vary"
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

        Dim cellRef As String = spreadsheetColumn & spreadsheetRowStart.ToString

        Dim theExpression As Expression
        Try
            theExpression = workPart.Expressions.FindObject(expressionName)
        Catch ex As NXException
            If ex.ErrorCode = 3520016 Then
                'no expression found with given name
                lw.WriteLine("expression '" & expressionName & "' not found, journal exiting")
                Return
            Else
                lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
            End If
        End Try

        If (theExpression.RightHandSide.ToUpper.Contains("UG_CELL_READ")) Or _
            (theExpression.RightHandSide.ToUpper.Contains("UG_EXCEL_READ")) Then
            'expression references a spreadsheet
        Else
            'cannot update cell
            lw.WriteLine("expression does not reference a spreadsheet cell, journal exiting")
            Return
        End If

        Dim strRegex As String = "(.*?ug_(?:cell|excel)_read\s*\(\s*(?<sheet>"".*?"")\s*,\s*"")(?<cell>.*?)(""\s*\)(.*))"
        Dim regexOptions As RegexOptions = regexOptions.IgnoreCase Or regexOptions.Multiline

        For i As Integer = spreadsheetRowStart To spreadsheetRowEnd

            cellRef = spreadsheetColumn & i.ToString

            Dim theMatches As MatchCollection = Regex.Matches(theExpression.RightHandSide, strRegex, regexOptions)
            Dim newFormula As String = theMatches(0).Groups(1).Value & cellRef & theMatches(0).Groups(2).Value

            theSession.UpdateManager.ClearErrorList()
            Dim markId2 As Session.UndoMarkId
            markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Expression edit")

            theExpression.RightHandSide = newFormula

            Dim nErrs1 As Integer

            Try
                nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

            Catch ex As NXException
                lw.WriteLine("** Update Error with value in cell: " & cellRef)
                lw.WriteLine("** " & ex.ErrorCode & ": " & ex.Message)
                lw.WriteLine("")

            End Try
        Dim output1 As Expression = Nothing
        output1 = workPart.Expressions.FindObject("output1")   

        Dim output2 As Expression = Nothing
        output2 = workPart.Expressions.FindObject("output2") 

        Dim output3 As Expression = Nothing
        output3 = workPart.Expressions.FindObject("output3") 

        Dim output4 As Expression = Nothing
        output4 = workPart.Expressions.FindObject("output4")  
 		
        objExcel.visible = True  
        objExcel.cells(i, 2).value = (output1.Value)  
	objExcel.cells(i, 3).value = (output2.Value) 
        objExcel.cells(i, 4).value = (output3.Value) 
        objExcel.cells(i, 5).value = (output4.Value) 

        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 

Thanks ALOT for your help :)

RE: Import_run_export script

Are these measurements existing measure features that you need to export the updated values, or do you need to create a new measurement each time?

www.nxjournaling.com

RE: Import_run_export script

(OP)
These are expressions that have their value defined as a measurement, for instance the distance between 2 surfaces or an angle between 2 objects that change for each iteration, the measurements update and i do not need to create new measurements each time.

RE: Import_run_export script

The code below shows how to get the value of a particular expression. Replace p2421 with the expression name that you are interested in.

CODE

Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

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

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

        Dim myExp As Expression
        Try
            myExp = workPart.Expressions.FindObject("p2421")
        Catch ex As NXException
            lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
            Return
        End Try

        Dim expValue As String
        expValue = myExp.Value.ToString & " " & myExp.Units.Abbreviation

        lw.WriteLine("expression value: " & expValue)

        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

    End Function

End Module 

www.nxjournaling.com

RE: Import_run_export script

(OP)
I believe that is the code i cut some of the

" Dim output1 As Expression = Nothing
output1 = workPart.Expressions.FindObject("output1")

Dim output2 As Expression = Nothing
output2 = workPart.Expressions.FindObject("output2")

Dim output3 As Expression = Nothing
output3 = workPart.Expressions.FindObject("output3")

Dim output4 As Expression = Nothing
output4 = workPart.Expressions.FindObject("output4")

objExcel.visible = True
objExcel.cells(i, 2).value = (output1.Value)
objExcel.cells(i, 3).value = (output2.Value)
objExcel.cells(i, 4).value = (output3.Value)
objExcel.cells(i, 5).value = (output4.Value) "

from :)

Thank you so much for your help!

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