Cancel
Showing results for 
Search instead for 
Did you mean: 

Journal for print screen with white background

Creator
Creator

Does anybody know how to record the print screen key stroke in journal to get a screenshot of current graphic window with white background. And then the screenshot picture can be pasted in other file or email. Thank you

8 REPLIES

Re: Journal for print screen with white background

Esteemed Contributor
Esteemed Contributor

Might be easier to

File -> Export -> (format of your choice - JPEG/PNG/whatever)

where you have control over it.

Then open that file in Paint or whatever for user to copy/paste

 

Ken

Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP5 + patch/TC11.2
I'd rather be e-steemed than e-diseaseled


Re: Journal for print screen with white background

Creator
Creator

Hi,

Thank you for your response.

The reason why I want to develop this journal is that sometimes when I pressscreen key, I just forgot to change the black background to white.  The ideal way is like by clicking one key, the system changes the background color to white and then takes a screenshot. Then I can past it to email directly. I don't need to save the picture as file.

But when I press the printscreen key and set the preference>>background>>white. The journal can't record these actions.

Is there any code for these two actions?

 

 

Re: Journal for print screen with white background

Honored Contributor
Honored Contributor

The code below will take a screenshot and put the image data on your clipboard.

 

Option Strict Off
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()

    Sub Main()

        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()

        'change export location as needed
        ExportScreenshot("C:\Temp\NX_screenshot.png")

        'optional, delete the image file
        'DeleteFile("C:\Temp\NX_screenshot.png")

        lw.Close()

    End Sub

    Sub ExportScreenshot(ByVal filename As String)

        Dim wcsVisible As Boolean = theSession.Parts.Display.WCS.Visibility
        Dim triadVisible As Integer = theSession.Preferences.ScreenVisualization.TriadVisibility


        Try
            DeleteFile(filename)

            theSession.Parts.Display.WCS.Visibility = False
            theSession.Preferences.ScreenVisualization.TriadVisibility = 0

            theUfSession.Disp.CreateImage(filename, UFDisp.ImageFormat.Png, UFDisp.BackgroundColor.White)
        Catch ex As Exception
            MsgBox(ex.Message & ControlChars.NewLine & _
                   "'" & filename & "' could not be created")
            'Return

        Finally
            theSession.Parts.Display.WCS.Visibility = wcsVisible
            theSession.Preferences.ScreenVisualization.TriadVisibility = triadVisible

        End Try

        'copy image to clipboard
        Dim theImage As Bitmap = CType(Image.FromFile(filename), Bitmap)
        Clipboard.SetImage(theImage)

    End Sub

    Sub DeleteFile(ByVal filePath As String)

        'does file exist? if so, try to delete it (overwrite)
        If IO.File.Exists(filePath) Then
            IO.File.Delete(filePath)
        End If

    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

Re: Journal for print screen with white background

Creator
Creator

Hi,

Thank you for the code. It works and the result is exactly what I want.

 

Re: Journal for print screen with white background

Experimenter
Experimenter

Hi,

 

if I try to use the code in Advanced Simulation, I get the Journal execution error:

NXOpen.NXException: The part file is not a .prt part

 

Is there any way to get the code works for *.fem and *.sim files?

 

Thank you very much!

Re: Journal for print screen with white background

Honored Contributor
Honored Contributor

Comment out the following line:

 

Dim workPart As Part = theSession.Parts.Work

The 'workPart' variable isn't needed for this journal; it is a holdover from the code in the template that I use.

Re: Journal for print screen with white background

Siemens Phenom Siemens Phenom
Siemens Phenom

To make the journal work for any type of part, any usage of theSession.Parts.Work should be replaced with theSession.Parts.BaseWork. Similarly, any usage of theSession.Parts.Display should be replaced with theSession.Parts.BaseDisplay. These both return a NXOpen.BasePart, which all part types inherit from.

 

Option Strict Off
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Imports NXOpen
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As BasePart = theSession.Parts.BaseWork
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        'change export location as needed
        ExportScreenshot("D:\workdir\NX_screenshot.png")

        'optional, delete the image file
        'DeleteFile("D:\workdir\NX_screenshot.png")

        lw.Close()

    End Sub

    Sub ExportScreenshot(ByVal filename As String)

        Dim wcsVisible As Boolean = theSession.Parts.BaseDisplay.WCS.Visibility
        Dim triadVisible As Integer = theSession.Preferences.ScreenVisualization.TriadVisibility


        Try
            DeleteFile(filename)

            theSession.Parts.BaseDisplay.WCS.Visibility = False
            theSession.Preferences.ScreenVisualization.TriadVisibility = 0

            theUfSession.Disp.CreateImage(filename, UFDisp.ImageFormat.Png, UFDisp.BackgroundColor.White)
        Catch ex As Exception
            MsgBox(ex.Message & ControlChars.NewLine & _
                   "'" & filename & "' could not be created")
            'Return

        Finally
            theSession.Parts.BaseDisplay.WCS.Visibility = wcsVisible
            theSession.Preferences.ScreenVisualization.TriadVisibility = triadVisible

        End Try

        'copy image to clipboard
        Dim theImage As Bitmap = CType(Image.FromFile(filename), Bitmap)
        Clipboard.SetImage(theImage)

    End Sub

    Sub DeleteFile(ByVal filePath As String)

        'does file exist? if so, try to delete it (overwrite)
        If IO.File.Exists(filePath) Then
            IO.File.Delete(filePath)
        End If

    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

Re: Journal for print screen with white background

Experimenter
Experimenter

Thank you very much to all!

This code, now works!

 

'NXJournaling.com
'August 11, 2015
 
'Export a screenshot from NX with a white background
'place the image data on the Windows clipboard so you can immediately
'"paste" the image to a new location (email, powerpoint, etc).
'This version will crop the image and provide a user supplied amount of "whitespace" on each side of the image.
 
Option Strict Off
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
 
Module Module2
 
    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
 
    Sub Main()
 
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If
 
'        Dim workPart As Part = theSession.Parts.BaseWork
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()
 
        Dim tempScreenshot As String
        Dim tempLocation As String = IO.Path.GetTempPath
 
        tempScreenshot = IO.Path.Combine(tempLocation, "NXscreenshot.png")
 
        Try
            ExportScreenshot(tempScreenshot)
        Catch ex As Exception
            lw.WriteLine("Error: " & ex.Message)
            Return
        End Try
 
        'crop the screenshot (filename, amount of whitespace on each side of image [integer value, number of pixels])
        CropScreenshot(tempScreenshot, 5)
 
        'copy image to clipboard
        Dim theImage As Bitmap = CType(Image.FromFile(tempScreenshot), Bitmap)
        Clipboard.SetImage(theImage)
 
 
        lw.Close()
 
    End Sub
 
    Sub ExportScreenshot(ByVal filename As String)
 
        'save user preference for visibility of WCS, triad, view name, and view border
        Dim wcsVisible As Boolean = theSession.Parts.BaseDisplay.WCS.Visibility
        Dim triadVisible As Integer = theSession.Preferences.ScreenVisualization.TriadVisibility
        Dim displayModelViewNames As Boolean = theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames
        Dim displayModelViewBorders As Boolean = theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders
 
        'turn off the WCS, triad, view name, and view border
        theSession.Parts.BaseDisplay.WCS.Visibility = False
        theSession.Preferences.ScreenVisualization.TriadVisibility = 0
        theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders = False
        theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames = False
 
        Try
            theUfSession.Disp.CreateImage(filename, UFDisp.ImageFormat.Png, UFDisp.BackgroundColor.White)
        Catch ex As Exception
            MsgBox(ex.Message & ControlChars.NewLine & _
                   "'" & filename & "' could not be created")
            Throw New Exception("Screenshot could not be created")
        Finally
            'reset visibility of WCS, triad, view name, and view border to user's preference
            theSession.Parts.BaseDisplay.WCS.Visibility = wcsVisible
            theSession.Preferences.ScreenVisualization.TriadVisibility = triadVisible
            theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders = displayModelViewBorders
            theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames = displayModelViewNames
        End Try
 
    End Sub
 
    Sub CropScreenshot(ByVal theImageFile As String, ByVal borderWidth As Integer)
 
        'borderWidth = whitespace to leave around the border of the image
 
        Dim original As New Bitmap(theImageFile)
 
        Dim JPGminX As Integer = original.Width
        Dim JPGminY As Integer = original.Height
        Dim JPGmaxX As Integer = 0
        Dim JPGmaxY As Integer = 0
 
        Dim bckgrndColor As Color = Color.White
 
        For y As Integer = 0 To original.Height - 1
            For x As Integer = 0 To original.Width - 1
                If original.GetPixel(x, y).ToArgb <> bckgrndColor.ToArgb Then
                    If x < JPGminX Then
                        JPGminX = x
                    ElseIf x > JPGmaxX Then
                        JPGmaxX = x
                    End If
                    If y < JPGminY Then
                        JPGminY = y
                    ElseIf y > JPGmaxY Then
                        JPGmaxY = y
                    End If
                End If
            Next
        Next
        Dim rect As New Rectangle(JPGminX, JPGminY, JPGmaxX - JPGminX, JPGmaxY - JPGminY)
 
        Dim cropped As Bitmap = original.Clone(rect, original.PixelFormat)
 
        'create new image sized to add border around cropped image
        Dim border As New Bitmap(cropped.Width + (2 * borderWidth), cropped.Height + (2 * borderWidth), cropped.PixelFormat)
 
        'create a new graphics object
        Dim g As Graphics = Graphics.FromImage(border)
 
        'fill the graphics object with white color (for background)
        Using myBrush As Brush = New SolidBrush(Color.White)
            g.FillRectangle(myBrush, 0, 0, border.Width, border.Height)
        End Using
 
        'calculate position to place cropped image onto background image
        Dim xImage As Integer = border.Width - cropped.Width - borderWidth
        Dim yImage As Integer = border.Height - cropped.Height - borderWidth
 
        'draw cropped image onto white background
        g.CompositingMode = Drawing2D.CompositingMode.SourceOver
        g.DrawImage(cropped, New Drawing.Point(xImage, yImage))
 
        'dispose of unneeded graphics objects
        original.Dispose()
        cropped.Dispose()
        g.Dispose()
 
        'save our finished graphics file
        border.Save(theImageFile, Imaging.ImageFormat.Png)
 
    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

I commented the line as suggested by cowski1 and changed "theSession.Parts." to "theSession.PartsBase".