Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Export Image Macro

Status
Not open for further replies.

DHuskic

Computer
Dec 18, 2012
114
I would like a macro that exports an image(jpeg or bitmap), and saves it as the file name on a folder on my desktop in the C: drive.
I am clueless on where I would start to do this and an im need of assistance.
 
Replies continue below

Recommended for you

that is perfect! some minor modifications and I will have myself the macro I have been looking for. Thanks cowski!
 
can anyone help me debug this?
I attepted to make a version of the jpeg macro with an output directory that could be chosen every time. If you click cancel it uses the last directory.

Or better yet, is there a way to go up a level in the folder path from where the root part it, and then back down a level to a named folder?
Here is what I have so far, the line below is giving me the issue.
ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.original)

Code:
' NX 7.5.5.4
'[URL unfurl="true"]http://www.eng-tips.com/viewthread.cfm?qid=327914&tmac=fav&val=1,327914[/URL]

Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpen.Utilities
Imports NXOpen.Layer  

Module Module1  

Sub Main()  

        Dim theSession As Session = Session.GetSession()  
        Dim ufs As UFSession = UFSession.GetUFSession()  
        Dim workPart As Part = theSession.Parts.Work  
        Dim partName As String = Path.GetFileNameWithoutExtension(workPart.FullPath)    
      'turn triad off
        theSession.Preferences.ScreenVisualization.TriadVisibility = 0 
      'directory to output jpegs, change as needed
      'Change the directory to where you would like the picture for now
      'the next update will feature automatic storing into Vantage folders for that job
        Dim folderName As String = "C:"        
        Dim outputDirectory As String = folderName   
        Dim strPartJpg As String = ""  
        Dim wpModelingView As ModelingView
        Dim strCurrentDate as String
        Dim s As Session = Session.GetSession()
        Dim ui As UI = UI.GetUI()
        Dim lw As ListingWindow = s.ListingWindow
        Dim root As Component = s.Parts.Work.ComponentAssembly.RootComponent
        Dim folderBrowserDialog1 As New FolderBrowserDialog
        folderBrowserDialog1.ShowNewFolderButton = False
        Dim result As DialogResult = folderBrowserDialog1.ShowDialog()
        
        If (result = DialogResult.OK) Then
            folderName = folderBrowserDialog1.SelectedPath
        End If
        
        If Not Directory.Exists(outputDirectory) Then  
            MsgBox("The specified directory does not exist, journal will now exit", MsgBoxStyle.Exclamation, outputDirectory & " not found")  
        Exit Sub
        End If   
            strCurrentDate = format(Today, "dd.MM.y")
            strPartJpg = Path.GetFileNameWithoutExtension(workPart.FullPath) & "_" & strCurrentDate & ".jpg"  
            strPartJpg = Path.Combine(outputDirectory, strPartJpg)  
            ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.original) 
      'turn triad on
        theSession.Preferences.ScreenVisualization.TriadVisibility = 1
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
 
You save the user's selection as folderName, but then output the jpg to outputDirectory.

The following code fixes that and uses the work part's parent folder as the default directory choice in the folder browser.

Code:
[COLOR=green]' NX 7.5.5.4[/color]
[COLOR=green]'[URL unfurl="true"]http://www.eng-tips.com/viewthread.cfm?qid=327914&tmac=fav&val=1,327914[/URL][/color]

[COLOR=blue]Option Strict Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] System.IO  
[COLOR=blue]Imports[/color] System.Windows.Forms  
[COLOR=blue]Imports[/color] NXOpen  
[COLOR=blue]Imports[/color] NXOpen.UF  
[COLOR=blue]Imports[/color] NXOpen.Assemblies  
[COLOR=blue]Imports[/color] NXOpen.Utilities  
[COLOR=blue]Imports[/color] NXOpen.Layer  

[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] partName [COLOR=blue]As String =[/color] Path.GetFileNameWithoutExtension(workPart.FullPath)  
 [COLOR=green]'turn triad off[/color]
        theSession.Preferences.ScreenVisualization.TriadVisibility [COLOR=blue]=[/color] 0  
 [COLOR=green]'directory to output jpegs, change as needed[/color]
 [COLOR=green]'Change the directory to where you would like the picture for now[/color]
 [COLOR=green]'the next update will feature automatic storing into Vantage folders for that job[/color]
        [COLOR=blue]Dim[/color] folderName [COLOR=blue]As String =[/color] "C:"  
 [COLOR=green]'assign output folder to workpart folder[/color]
        folderName [COLOR=blue]=[/color] Path.GetDirectoryName(workPart.FullPath)  
 [COLOR=green]'get parent folder (up a level)[/color]
        [COLOR=blue]Dim[/color] directoryInfo [COLOR=blue]As[/color] DirectoryInfo  
        directoryInfo [COLOR=blue]=[/color] Directory.GetParent(folderName)  
 [COLOR=green]'assign the parent folder to folderName[/color]
        folderName [COLOR=blue]=[/color] directoryInfo.FullName  

        [COLOR=blue]Dim[/color] strPartJpg [COLOR=blue]As String =[/color] ""  
        [COLOR=blue]Dim[/color] strCurrentDate [COLOR=blue]As String =[/color] Format(Today, "dd.MM.y")  
        [COLOR=blue]Dim[/color] s [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] ui [COLOR=blue]As[/color] UI [COLOR=blue]=[/color] UI.GetUI()  
        [COLOR=blue]Dim[/color] lw [COLOR=blue]As[/color] ListingWindow [COLOR=blue]=[/color] s.ListingWindow  
        [COLOR=blue]Dim[/color] root [COLOR=blue]As[/color] Component [COLOR=blue]=[/color] s.Parts.Work.ComponentAssembly.RootComponent  
        [COLOR=blue]Dim[/color] folderBrowserDialog1 [COLOR=blue]As New[/color] FolderBrowserDialog  
        [COLOR=blue]With[/color] folderBrowserDialog1  
            .Description [COLOR=blue]=[/color] "Specify folder for screenshot output"  
            .ShowNewFolderButton [COLOR=blue]= False[/color]  
            .RootFolder [COLOR=blue]=[/color] Environment.SpecialFolder.Desktop  
 [COLOR=green]'use folderName as default directory[/color]
            .SelectedPath [COLOR=blue]=[/color] folderName  
        End [COLOR=blue]With[/color]  

        [COLOR=blue]Dim[/color] result [COLOR=blue]As[/color] DialogResult [COLOR=blue]=[/color] folderBrowserDialog1.ShowDialog()  

        [COLOR=blue]If[/color] (result [COLOR=blue]=[/color] DialogResult.OK) [COLOR=blue]Then[/color]  
            folderName [COLOR=blue]=[/color] folderBrowserDialog1.SelectedPath  
        [COLOR=blue]Else[/color]  
 [COLOR=green]'user pressed cancel, exit the journal[/color]
            [COLOR=blue]Exit Sub[/color]  
        End [COLOR=blue]If[/color]  

        [COLOR=blue]If Not[/color] Directory.Exists(folderName) [COLOR=blue]Then[/color]  
            MsgBox("The specified directory does [COLOR=blue]not[/color] exist, journal will now exit", MsgBoxStyle.Exclamation, folderName [COLOR=blue]&[/color] " [COLOR=blue]not[/color] found")  
            [COLOR=blue]Exit Sub[/color]  
        End [COLOR=blue]If[/color]  
        strPartJpg [COLOR=blue]=[/color] Path.GetFileNameWithoutExtension(workPart.FullPath) [COLOR=blue]&[/color] "_" [COLOR=blue]&[/color] strCurrentDate [COLOR=blue]&[/color] ".jpg"  
        strPartJpg [COLOR=blue]=[/color] Path.Combine(folderName, strPartJpg)  
        ufs.Disp.CreateImage(strPartJpg, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.Original)  
 [COLOR=green]'turn triad on[/color]
        theSession.Preferences.ScreenVisualization.TriadVisibility [COLOR=blue]=[/color] 1  
    End [COLOR=blue]Sub[/color]  
    [COLOR=blue]Public Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As String[/color]) [COLOR=blue]As 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
 
And a photo (JPEG) on NX like Snagit? Custom view and use the mouse for capture the image?

Take out as PDF and JPEG images, to a specific folder.
 
There is a folder called "Job_Pictures" under ever every parent. How would I make the pictures automatically go to this folder?
 
Implement the following code (or equivalent) into the journal:

Code:
[COLOR=blue]Dim[/color] folderName [COLOR=blue]As String =[/color] "C:"  
[COLOR=green]'assign output folder to workpart folder[/color]
folderName [COLOR=blue]=[/color] Path.GetDirectoryName(workPart.FullPath)  
[COLOR=green]'get parent folder (up a level)[/color]
[COLOR=blue]Dim[/color] directoryInfo [COLOR=blue]As[/color] DirectoryInfo  
directoryInfo [COLOR=blue]=[/color] Directory.GetParent(folderName)  
[COLOR=green]'assign the parent folder to folderName[/color]
folderName [COLOR=blue]=[/color] directoryInfo.FullName  
[highlight #FCE94F][COLOR=blue]If[/color] IO.Directory.Exists(IO.Path.Combine(directoryInfo.FullName, "Job_Pictures")) Then  
    folderName [COLOR=blue]=[/color] IO.Path.Combine(directoryInfo.FullName, "Job_Pictures")  
End [COLOR=blue]If[/color][/highlight]


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

Part and Inventory Search

Sponsor