Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Journal exporting part files 4

Status
Not open for further replies.

DHuskic

Computer
Dec 18, 2012
114
I am in search of vb code to finish a journal I am creating to seek out and export certain parts in an assembly. I can figure out how to find said parts but am stumped on to to actually export them out. I would like to specify the part be placed in given folder for all assemblies and would name it after the assembly, without parameters as well. Any ideas? All input is well appreciated.
 
Replies continue below

Recommended for you

I have had luck calling the SaveAs() function of a Part instance. The argument it takes is the full path of the file you want to save.
You can also specify different extensions (.stp, .igs, etc) and NX will translate them. As far as removing the parameters... don't know. I was working on a similar function before to export certain components of an assembly as step files but couldn't get the step exporter properties to apply correctly.
 
I would like to export unparamaterized .prt files of the given files individually
 
Here's a simplified example of exporting an unparameterized solid to a new part file.

Code:
[COLOR=blue]Option Strict Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] NXOpen  
[COLOR=blue]Imports[/color] NXOpen.UF  

[COLOR=blue]Module[/color] Module1  

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

        [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] ufs [COLOR=blue]As[/color] UFSession [COLOR=blue]=[/color] UFSession.GetUFSession  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  

        [COLOR=blue]Dim[/color] myOptions [COLOR=blue]As[/color] UFPart.ExportOptions  
        myOptions.params_mode [COLOR=blue]=[/color] UFPart.ExportParamsMode.RemoveParams  
        myOptions.new_part [COLOR=blue]= True[/color]  

        [COLOR=blue]Dim[/color] partName [COLOR=blue]As String =[/color] "C:\temp\export_test.prt"  
        [COLOR=blue]Dim[/color] bodyTag(0) [COLOR=blue]As[/color] Tag  

        [COLOR=blue]Dim[/color] mySolid [COLOR=blue]As[/color] NXObject  
        [COLOR=blue]If[/color] SelectSolid("Select a solid", mySolid) [COLOR=blue]=[/color] Selection.Response.Cancel [COLOR=blue]Then[/color]  
            [COLOR=blue]Exit Sub[/color]  
        End [COLOR=blue]If[/color]  

        bodyTag(0) [COLOR=blue]=[/color] mySolid.Tag  

        ufs.Part.ExportWithOptions(partName, 1, bodyTag, myOptions)  

    End [COLOR=blue]Sub[/color]  

    [COLOR=blue]Function[/color] SelectSolid(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] title [COLOR=blue]As String =[/color] "Select a solid"  
        [COLOR=blue]Dim[/color] includeFeatures [COLOR=blue]As Boolean = False[/color]  
        [COLOR=blue]Dim[/color] keepHighlighted [COLOR=blue]As Boolean = False[/color]  
        [COLOR=blue]Dim[/color] selAction [COLOR=blue]As[/color] Selection.SelectionAction [COLOR=blue]=[/color] Selection.SelectionAction.ClearAndEnableSpecific  
        [COLOR=blue]Dim[/color] cursor [COLOR=blue]As[/color] Point3d  
        [COLOR=blue]Dim[/color] scope [COLOR=blue]As[/color] Selection.SelectionScope [COLOR=blue]=[/color] Selection.SelectionScope.AnyInAssembly  
        [COLOR=blue]Dim[/color] selectionMask_array(0) [COLOR=blue]As[/color] Selection.MaskTriple  

        [COLOR=blue]With[/color] selectionMask_array(0)  
            .Type [COLOR=blue]=[/color] UFConstants.UF_solid_type  
            .SolidBodySubtype [COLOR=blue]=[/color] UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY  
        End [COLOR=blue]With[/color]  

        [COLOR=blue]Dim[/color] resp [COLOR=blue]As[/color] Selection.Response [COLOR=blue]=[/color] theUI.SelectionManager.SelectObject(prompt, _  
         title, scope, selAction, _  
         includeFeatures, keepHighlighted, selectionMask_array, _  
         selobj, cursor)  
        [COLOR=blue]If[/color] resp [COLOR=blue]=[/color] Selection.Response.ObjectSelected [COLOR=blue]OrElse[/color] resp [COLOR=blue]=[/color] Selection.Response.ObjectSelectedByName [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]  


End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
Would it be possible to export all solids and surfaces on layer 1 and all points and curves on layers 230-240?
 
UGperson said:
Would it be possible to export all solids and surfaces on layer 1 and all points and curves on layers 230-240?

Yes.
Below is a bare bones journal to do just that. You'll want to add some error checking or possibly a file select dialog as needed, but this should get you started.

Code:
[COLOR=green]'eng-tips thread561-336277[/color]
[COLOR=green]'code to export unparameterized objects to a new file[/color]
[COLOR=green]'export all solids and surfaces from layer 1[/color]
[COLOR=green]'export all curves and points from layers 230-240[/color]

[COLOR=blue]Imports[/color] NXOpen  
[COLOR=blue]Imports[/color] NXOpen.UF  
[COLOR=blue]Imports[/color] System.Collections.Generic  

[COLOR=blue]Module[/color] Module2  

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

        [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] ufs [COLOR=blue]As[/color] UFSession [COLOR=blue]=[/color] UFSession.GetUFSession  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  

        [COLOR=blue]Const[/color] bodyLayer [COLOR=blue]As Integer =[/color] 1  
        [COLOR=blue]Const[/color] curveLayerStart [COLOR=blue]As Integer =[/color] 230  
        [COLOR=blue]Const[/color] curveLayerEnd [COLOR=blue]As Integer =[/color] 240  

        [COLOR=blue]Dim[/color] myOptions [COLOR=blue]As[/color] UFPart.ExportOptions  
        myOptions.params_mode [COLOR=blue]=[/color] UFPart.ExportParamsMode.RemoveParams  
        myOptions.new_part [COLOR=blue]= True[/color]  

        [COLOR=blue]Dim[/color] partName [COLOR=blue]As String =[/color] "C:\temp\export_test2.prt"  
        [COLOR=blue]Dim[/color] objectTags [COLOR=blue]As New[/color] List(Of Tag)  

 [COLOR=green]'collect bodies from layer 1[/color]
        [COLOR=blue]For Each[/color] myObj [COLOR=blue]As[/color] Body [COLOR=blue]In[/color] workPart.Bodies  
            [COLOR=blue]If[/color] myObj.Layer [COLOR=blue]=[/color] bodyLayer [COLOR=blue]Then[/color]  
                objectTags.Add(myObj.Tag)  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

 [COLOR=green]'collect curves[/color]
        [COLOR=blue]For Each[/color] myObj [COLOR=blue]As[/color] Curve [COLOR=blue]In[/color] workPart.Curves  
            [COLOR=blue]If[/color] myObj.Layer >= curveLayerStart [COLOR=blue]And[/color] myObj.Layer <= curveLayerEnd [COLOR=blue]Then[/color]  
                objectTags.Add(myObj.Tag)  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

 [COLOR=green]'collect points[/color]
        [COLOR=blue]For Each[/color] myObj [COLOR=blue]As[/color] Point [COLOR=blue]In[/color] workPart.Points  
            [COLOR=blue]If[/color] myObj.Layer >= curveLayerStart [COLOR=blue]And[/color] myObj.Layer <= curveLayerEnd [COLOR=blue]Then[/color]  
                objectTags.Add(myObj.Tag)  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

        ufs.Part.ExportWithOptions(partName, objectTags.Count, objectTags.ToArray, myOptions)  

    End [COLOR=blue]Sub[/color]  

End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
Thanks!

Is there a better way to get the folder name that the file is in than what I have here?

'***********************************************************************

Function GetFilePath2()
Dim strPath as String
Dim strPart as String
Dim strPart2 as String
Dim pos as Integer
Dim pos2 as Integer

'get the full file path
strPart2 = displayPart.fullpath
pos = InStrRev(strPart2, "\")
strPart2 = Left(strPart2, pos -1)

strPath = strPart2

pos2 = InStrRev(strPath, "\")
strPart = Mid(strPath, pos2 + 1)

strPath = Left(strPath, pos)
strPart = Left(strPart, Len(strPart))

GetFilePath2 = strPart
End Function

'***********************************************************************
 
The .NET framework has you covered:

Code:
[COLOR=blue]Option Strict Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] NXOpen  

[COLOR=blue]Module[/color] Module1  

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

        [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] lw [COLOR=blue]As[/color] ListingWindow [COLOR=blue]=[/color] theSession.ListingWindow  
        lw.Open()  

        [COLOR=blue]Dim[/color] fileName [COLOR=blue]As String =[/color] IO.Path.GetFileName(workPart.FullPath)  
        [COLOR=blue]Dim[/color] fileNameNoExt [COLOR=blue]As String =[/color] IO.Path.GetFileNameWithoutExtension(workPart.FullPath)  
        [COLOR=blue]Dim[/color] parentFolder [COLOR=blue]As String =[/color] IO.Path.GetDirectoryName(workPart.FullPath)  
        [COLOR=blue]Dim[/color] root [COLOR=blue]As String =[/color] IO.Path.GetPathRoot(workPart.FullPath)  

        lw.WriteLine("full path: " [COLOR=blue]&[/color] workPart.FullPath)  
        lw.WriteLine(New String("-", ("full path: " [COLOR=blue]&[/color] workPart.FullPath).Length))  
        lw.WriteLine("file name: " [COLOR=blue]&[/color] fileName)  
        lw.WriteLine("file name w/o extension: " [COLOR=blue]&[/color] fileNameNoExt)  
        lw.WriteLine("parent folder: " [COLOR=blue]&[/color] parentFolder)  
        lw.WriteLine("root folder: " [COLOR=blue]&[/color] root)  

        lw.Close()  

    End [COLOR=blue]Sub[/color]  

End [COLOR=blue]Module[/color]


www.nxjournaling.com
 
cowski,
is there a way to process an assembly file and export all parts that have an attribute that equals a defined string.
Say, export out a .prt with all components that have a catalog attribute that equals "Outside_Work"?
My best swing at it was attempting to compile a list of objects where the attributed equaled "Outside_Work" but I am stuck on how to call out the attribute of each part individually.

Denis Huskic
Data Prep
Kettering University
Class of '17
 
Component level I believe. It is an attribute we have in the assembly navigator.

Denis Huskic
Data Prep
Kettering University
Class of '17
 
I still have yet to get the code to succesfully run. I am having issues with the two following lines. I don't know for sure if this is in the right direction to accomplish the goal, but this is all I have so far.

Code:
For Each child As Component In comp.GetChildren()
...
Dim mySolid As NXObject() = complist

Here is what I have been trying.

Code:
'NXJournal to recursively walk through the assembly structure
'eng-tips thread561-335918: 
'eng-tips thread561-336277: 
'NX 7.5

Option Strict Off  
Imports System  
Imports NXOpen  
Imports NXOpen.UF  
Imports NXOpen.Utilities  
Imports NXOpen.Assemblies  
Imports System.Windows.Forms  
Imports System.IO  
Imports System.Collections  
Imports System.Collections.Generic  
Imports System.Environment  
Imports NXOpenUI  
 
Module NXJournal
 
    Public theSession As Session = Session.GetSession()
    Public ufs As UFSession = UFSession.GetUFSession()
    Public lw As ListingWindow = theSession.ListingWindow
    
    Sub Main()
    Dim workPart As Part = theSession.Parts.Work
    Dim dispPart As Part = theSession.Parts.Display
    Dim c As ComponentAssembly = dispPart.ComponentAssembly
    Dim Components As NXobject
    Dim complist As New List(Of NXobject)
    Dim comp As Component
        For Each child As Component In comp.GetChildren()
            if child.GetChildren.Length <> 0 then
            If child.GetStringAttribute("Catalog") = "Outside-Work" Then
               compList.Add(child)      
            end if
            end if
        Next
        'MsgBox(complist)
        Dim partname As String = Path.GetFileNameWithoutExtension(workPart.FullPath) 

        Dim folderName As String = "C:"  
        'assign output folder to workpart folder
        folderName = Path.GetDirectoryName(workPart.FullPath)  
        Dim outputDirectory As String = foldername  
        Dim strPartJpg As String
        partname = partname & "_RevComponents"
        strPartJpg = Path.Combine(outputDirectory, partname)
 
        Dim myOptions As UFPart.ExportOptions  
        myOptions.params_mode = UFPart.ExportParamsMode.MaintainAllParams  
        myOptions.new_part = True  
        Dim bodyTag(0) As Tag  

        Dim mySolid As NXObject() = complist
        
        bodyTag(0) = mySolid.Tag  
        'MsgBox(strPartJpg)
        ufs.Part.ExportWithOptions(strPartJpg, 1, bodyTag, myOptions)  
    End Sub
'**********************************************************
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function
'**********************************************************
 
End Module

Denis Huskic
Data Prep
Kettering University
Class of '17
 
The first problem I see is you are trying to use an empty variable.
Code:
Dim [highlight #EDD400]comp[/highlight] As Component
    For Each child As Component In [highlight #EDD400]comp.GetChildren()[/highlight]

comp.GetChildren() will return nothing because the comp variable has been created but doesn't yet contain anything.

For some working code to walk through an assembly along with explanation, see:
www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor