Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Exporting step files for animations (journal)

Status
Not open for further replies.

ChaseWichert

Mining
Jan 4, 2012
147
I am trying to find a way to create 100 step files that will be used in a rendering. I found code by Cowski, and modified it to make it loop through the number of "Frames" which will update an expression, and then export the resulting body as a step. However on the second loop I get a Memory Access Violation on Line 93 or the step214Creator1.Destroy().
Code:
'Purpose: automate STEP export of the following entities:
'Layer 1 - Solids
'Layer 4 - Solids
'Layer 5 - Solids
'Layer 41 - Lines
'Layer 44 - Points
'Layer 63 - Solids
'eng-tips thread561-321704

'May 11, 2012

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports System.Threading

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work
    Dim displayPart As Part = theSession.Parts.Display
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        lw.Open()

        Dim mySelectedObjects As New List(Of NXObject)
        Dim tempSelectedObjects() As NXObject
        Dim step214File As String
        Dim outputPath As String = IO.Path.GetDirectoryName(workPart.FullPath)

        Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
        step214File = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")

        If Not IO.File.Exists(step214File) Then
            MsgBox("The step214 settings file (ugstep214.def) was not found." & vbCrLf & _
                "This journal will now exit.", vbOKOnly + vbCritical)
            Exit Sub
        Else
            'lw.WriteLine("STEP214 definition file found at: " & step214File)
        End If

        Dim intFrameCount As Integer = 0
        Dim intFrameTotal As Integer = InputBox("How many frames do you want?")
        Dim a As Integer = 0
        Dim filename As String = InputBox("Input .stp Filename")
        Dim outputfile As String
        Dim step214Creator1 As Step214Creator
        Do Until intFrameCount = intFrameTotal
            outputFile = IO.Path.Combine(outputPath, filename & intFrameCount & ".stp")


            Dim FrameNumber As Expression = CType(workPart.Expressions.FindObject("FrameNumber"), Expression)
            'MsgBox(workPart.Expressions.GetAttributeExpression(CType(workPart.Expressions.FindObject("FrameNumber"), NXObject),,NXObject.AttributeType.String)
            workPart.Expressions.EditWithUnits(FrameNumber, Nothing, intFrameCount)

            AddSolidsFromLayer(1, mySelectedObjects)
            'add lines from layer 41
            'tempSelectedObjects = workPart.Layers.GetAllObjectsOnLayer(41)
            'For Each obj As NXObject In tempSelectedObjects
            '    If TypeOf obj Is Line Then
            '        mySelectedObjects.Add(obj)
            '    End If
            'Next

            '   'add points from layer 44
            '    tempSelectedObjects = workPart.Layers.GetAllObjectsOnLayer(44)
            '     For Each obj As NXObject In tempSelectedObjects
            '       If TypeOf obj Is Point Then
            '       mySelectedObjects.Add(obj)
            '    End If
            'Next

            'export to STEP214 file

            step214Creator1 = theSession.DexManager.CreateStep214Creator()

            step214Creator1.SettingsFile = step214File
            step214Creator1.ObjectTypes.Solids = True
            step214Creator1.LayerMask = "1-256"
            step214Creator1.InputFile = workPart.FullPath
            step214Creator1.OutputFile = outputFile
            step214Creator1.FileSaveFlag = False
            step214Creator1.ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.SelectedObjects

            Dim added1 As Boolean
            added1 = step214Creator1.ExportSelectionBlock.SelectionComp.Add(mySelectedObjects.ToArray)

            Dim nXObject1 As NXObject
            nXObject1 = step214Creator1.Commit()
            step214Creator1.Destroy()

            intFrameCount = intFrameCount + 1
            'Thread.Sleep(5000)


        Loop

    End Sub

    Sub AddSolidsFromLayer(ByVal layer As Integer, ByRef objList As List(Of NXObject))

        Dim tempselectedobjects() As NXObject
        Dim tempSolidObj As Body

        tempselectedobjects = workPart.Layers.GetAllObjectsOnLayer(layer)
        'filter out the solid bodies on the layer and add them to the export list
        For Each obj As NXObject In tempselectedobjects
            If TypeOf obj Is Body Then
                tempSolidObj = CType(obj, Body)
                If tempSolidObj.IsSolidBody Then
                    objList.Add(tempSolidObj)
                End If
            End If
        Next


    End Sub

    Function SelectObjects(ByVal prompt As String, _
           ByRef selObj As NXObject()) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim typeArray() As Selection.SelectionType = _
            {Selection.SelectionType.All}

        Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects( _
                prompt, "Select Objects for STEP Export", _
                Selection.SelectionScope.WorkPart, _
                False, typeArray, selObj)

        If resp = Selection.Response.ObjectSelected Or _
                resp = Selection.Response.ObjectSelectedByName Or _
                resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    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

NX 8.5.2.3
 
Replies continue below

Recommended for you

The StepCreator object is created before the loop begins and is destroyed in the loop; on the 2nd time through the loop, there is no StepCreator object to use. I'd suggest waiting to call the .Destroy method until after the loop terminates.

Code:
Dim StepCreator

do until...
  do stuff
  export
loop

StepCreator.Destroy

www.nxjournaling.com
 
Well okay that works, however I am not getting a Memory Access Violation Error on the 3rd loops so Frame 2 of NXOpen.ExpressionCollection.EditWithUnits(Expression expression, Unit unitType, String newRightHandSide)
Line 57

NX 8.5.2.3
 
Assuming FrameNumber is defined as a constant (unitless) expression, I'd try:

Code:
workPart.Expressions.Edit(FrameNumber, intFrameCount.ToString)

Also, if you are exporting the same objects repeatedly, you don't have to reselect them each time through the loop. Move the following line outside of the loop (before it begins).

Code:
AddSolidsFromLayer(1, mySelectedObjects)

www.nxjournaling.com
 
It is still failing.. It starts to update but when it is updating a circular pattern is when it seems to fail.

NX 8.5.2.3
 
In the Log file i see things like

Error 11 while calling callback with reason 46 (UGUI: rename object)

+++ Failed to copy expression.

STEP214 Export: Export Translation Job submitted+++ Exception c0000005 occurred in unmanaged code

Journal execution results...
Runtime error:
NXOpen.NXException: Internal error: memory access violation
at NXOpen.ExpressionCollection.Edit(Expression expression, String newRightHandSide)
at Module1.Main() in C:\Users\wichcha\AppData\Local\Temp\NXJournals9048\journal.vb:line 59

NX 8.5.2.3
 
I kind of wonder if it is trying to go to fast and it hasn't updated before it starts the loop again, it to Frame 2 however it only exported 1 step file, Frame 0

NX 8.5.2.3
 
Good point. You'll want to pause and wait for the model update and step file creation before updating and exporting the next one.

Something like the following should work to pause for the step file creation (right before the Loop command):

Code:
Do Until File.Exists(outputFile)
  Thread.Sleep(3000)
Loop

Change the 3000 value as you see fit.

After the expression value update, you might want to try adding an update command. This may force the journal to wait until the model is done updating before trying to export a step file.

www.nxjournaling.com
 
Here's an example of calling the .DoUpdate method:

Code:
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

'do stuff, such as editing an expression value

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)

If an error occurs, it will roll back to the undo mark.

www.nxjournaling.com
 
Yup, that is what I put in, I am running it as we speak with the update and a 30 second pause. Will let you know what happens.

NX 8.5.2.3
 
Well it seems to be working. Thanks so much for the help!

NX 8.5.2.3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor