Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Step File Export Automation 1

Status
Not open for further replies.

kfraysur

Bioengineer
Feb 4, 2010
42
I am trying to use journaling to automate the export of a step file from a part. Our company produces one-off products that are exported to a step file with objects on specific layers broken down like this:

Layer 1 - Solids
Layer 4 - Solids
Layer 5 - Solids
Layer 41 - Lines
Layer 44 - Points
Layer 63 - Solids

The objects on layers 5, 41, 44, and 63 all come from imported part files. These imported files also have components on other layer (21, 42, etc.)

Utilizing cowski's excellent nxjournaling.com resource I was able to write a journal that will prompt the user to select the objects with the necessary layers visible, and export a stp file. The problem I am having is that the stp file I create contains the objects on layers 21, 42, etc... even though they weren't selected. I assume this is because they are associated with the other components from the imported part files.

I thought I had solved this by exporting copies of the objects on layers 5, 41, 44, and 63 instead of the objects themselves. Unfortunately, I cannot make a copy of the points. When executing a journal that makes a copy of the points to layer 44, it returns an error "Smart Objects cannot be copied by layer copy".

So I'm a bit stuck and was hoping someone could help. I've included a sample of the code I am using

Code:
Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
 

Module NXJournal

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

Sub Main
 

Dim mySelectedObjects as NXObject()
 
 
If SelectObjects("Hey, select multiple somethings", mySelectedObjects) = Selection.Response.Ok Then

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
      
	
Dim step214Creator1 As Step214Creator
	step214Creator1 = theSession.DexManager.CreateStep214Creator()

	Dim added1 As Boolean
	added1 = step214Creator1.ExportSelectionBlock.SelectionComp.Add(mySelectedObjects)
	step214Creator1.OutputFile = "Random Directory Here"

	Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Export to STEP214 Options")

step214Creator1.FileSaveFlag = False

step214Creator1.LayerMask = "*"

Dim nXObject1 As NXObject
nXObject1 = step214Creator1.Commit()

theSession.DeleteUndoMark(markId2, Nothing)

theSession.SetUndoMarkName(markId1, "Export to STEP214 Options")

step214Creator1.Destroy()


	 
End if
 
 
End Sub
 
    Function SelectObjects(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

Again, thanks to cowski for the SelectObjects function info on nxjournaling.com
 
Replies continue below

Recommended for you

I'm not entirely clear if you are working in a single monolithic part file or an assembly file. You mention "imported parts", which makes me think of File -> Import Part... (single part file), but later mention components (assembly file).

If you are working in an assembly file, do you want to export just those objects from the assembly file or from each component file? For example, do you want to export all the solid bodies from layer 1 from just the assembly file or do you want to export all the solid bodies on layer 1 from the assembly file and each of the component files? Depending on how the component was added to the assembly, these options may have different results.

 
Sorry for the confusion. We work with single part files and use the File -> Import Part function. There will never be any assembly file or components.

Again, sorry for the confusion.
 
Here is a journal that selects the objects according to your list. The SelectObjects function isn't used in this version, but I left it in the file in case you want to add some functionality to allow the user to pick additional objects. You'll probably want to change the output location. It currently outputs the STEP file based on the name and location of the work part.

Code:
[COLOR=green]'Purpose: automate STEP export of the following entities:[/color]
[COLOR=green]'Layer 1 - Solids[/color]
[COLOR=green]'Layer 4 - Solids[/color]
[COLOR=green]'Layer 5 - Solids[/color]
[COLOR=green]'Layer 41 - Lines[/color]
[COLOR=green]'Layer 44 - Points[/color]
[COLOR=green]'Layer 63 - Solids[/color]

[COLOR=green]'eng-tips thread561-321704[/color]

[COLOR=green]'May 11, 2012[/color]

[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] System.Collections.Generic  
[COLOR=blue]Imports[/color] NXOpen  

[COLOR=blue]Module[/color] Module1  

    [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
    [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
    [COLOR=blue]Dim[/color] displayPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Display  
    [COLOR=blue]Dim[/color] lw [COLOR=blue]As[/color] ListingWindow [COLOR=blue]=[/color] theSession.ListingWindow  

    [COLOR=blue]Sub[/color] Main()  

        lw.Open()  

        [COLOR=blue]Dim[/color] mySelectedObjects [COLOR=blue]As[/color] [COLOR=blue]New[/color] List(Of NXObject)  
        [COLOR=blue]Dim[/color] tempSelectedObjects() [COLOR=blue]As[/color] NXObject  
        [COLOR=blue]Dim[/color] step214File [COLOR=blue]As[/color] [COLOR=blue]String[/color]  
        [COLOR=blue]Dim[/color] outputPath [COLOR=blue]As[/color] [COLOR=blue]String[/color] [COLOR=blue]=[/color] IO.Path.GetDirectoryName(workPart.FullPath)  
        [COLOR=blue]Dim[/color] outputFile [COLOR=blue]As[/color] [COLOR=blue]String[/color] [COLOR=blue]=[/color] IO.Path.GetFileNameWithoutExtension(workPart.FullPath)  
        outputFile [COLOR=blue]=[/color] IO.Path.Combine(outputPath, outputFile [COLOR=blue]&[/color] ".stp")  

        [COLOR=blue]Dim[/color] STEP214UG_DIR [COLOR=blue]As[/color] [COLOR=blue]String[/color] [COLOR=blue]=[/color] theSession.GetEnvironmentVariableValue("STEP214UG_DIR")  
        step214File [COLOR=blue]=[/color] IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")  

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

        AddSolidsFromLayer(1, mySelectedObjects)  
        AddSolidsFromLayer(4, mySelectedObjects)  
        AddSolidsFromLayer(5, mySelectedObjects)  
        AddSolidsFromLayer(63, mySelectedObjects)  

 [COLOR=green]'add lines from layer 41[/color]
        tempSelectedObjects [COLOR=blue]=[/color] workPart.Layers.GetAllObjectsOnLayer(41)  
        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] obj [COLOR=blue]As[/color] NXObject [COLOR=blue]In[/color] tempSelectedObjects  
            [COLOR=blue]If[/color] [COLOR=blue]TypeOf[/color] obj [COLOR=blue]Is[/color] Line [COLOR=blue]Then[/color]  
                mySelectedObjects.Add(obj)  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

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

 [COLOR=green]'export to STEP214 file[/color]
        [COLOR=blue]Dim[/color] step214Creator1 [COLOR=blue]As[/color] Step214Creator  
        step214Creator1 [COLOR=blue]=[/color] theSession.DexManager.CreateStep214Creator()  

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

        [COLOR=blue]Dim[/color] added1 [COLOR=blue]As[/color] [COLOR=blue]Boolean[/color]  
        added1 [COLOR=blue]=[/color] step214Creator1.ExportSelectionBlock.SelectionComp.Add(mySelectedObjects.ToArray)  

        [COLOR=blue]Dim[/color] nXObject1 [COLOR=blue]As[/color] NXObject  
        nXObject1 [COLOR=blue]=[/color] step214Creator1.Commit()  

        step214Creator1.Destroy()  
        lw.Close()  

    End [COLOR=blue]Sub[/color]  

    [COLOR=blue]Sub[/color] AddSolidsFromLayer(ByVal layer [COLOR=blue]As[/color] Integer, [COLOR=blue]ByRef[/color] objList [COLOR=blue]As[/color] List(Of NXObject))  

        [COLOR=blue]Dim[/color] tempselectedobjects() [COLOR=blue]As[/color] NXObject  
        [COLOR=blue]Dim[/color] tempSolidObj [COLOR=blue]As[/color] Body  

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


    End [COLOR=blue]Sub[/color]  

    [COLOR=blue]Function[/color] SelectObjects(ByVal prompt [COLOR=blue]As[/color] String, _  
           [COLOR=blue]ByRef[/color] selObj [COLOR=blue]As[/color] NXObject()) [COLOR=blue]As[/color] Selection.Response  

        [COLOR=blue]Dim[/color] theUI [COLOR=blue]As[/color] UI [COLOR=blue]=[/color] UI.GetUI  
        [COLOR=blue]Dim[/color] typeArray() [COLOR=blue]As[/color] Selection.SelectionType [COLOR=blue]=[/color] _  
            {Selection.SelectionType.All}  

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

        [COLOR=blue]If[/color] resp [COLOR=blue]=[/color] Selection.Response.ObjectSelected [COLOR=blue]Or[/color] _  
                resp [COLOR=blue]=[/color] Selection.Response.ObjectSelectedByName [COLOR=blue]Or[/color] _  
                resp [COLOR=blue]=[/color] Selection.Response.Ok [COLOR=blue]Then[/color]  
            [COLOR=blue]Return[/color] Selection.Response.Ok  
        [COLOR=blue]Else[/color]  
            [COLOR=blue]Return[/color] Selection.Response.Cancel  
        End [COLOR=blue]If[/color]  

    End [COLOR=blue]Function[/color]  

    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  

 [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

    End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
Once again, cowski, you have helped me a great deal. Thank you so much.

I am still confused why the objects on layers 21 and 42 were being exported in my original code. It seems that the SelectObjects function works differently from the AddSolidsFromLayer function. Do you have any idea why?

Again, thank you so much. Your site is excellent. Perhaps if I get enough experience with this journaling I can one day submit something to it.
 
kfraysur said:
I am still confused why the objects on layers 21 and 42 were being exported in my original code.
I don't have a good answer for you on this one. I did add the line:
Code:
step214Creator1.ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.SelectedObjects
before adding the objects to the selection list; but I can't say for sure that this is the 'magic bullet' in this case.

Thanks for the kind words, I'm glad you have found the website useful. I'm looking forward to getting some submitted journals to share!

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

Part and Inventory Search

Sponsor