Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Auto generate model screenshots with journal

Status
Not open for further replies.

erpj

Aerospace
Jul 15, 2011
24
0
0
CA
Hi everyone,

I'm trying to generate multiple screenshots of an assembly by rotating it around the Y axis and invoking the CreateImage method. The script works, however, I cannot figure out how to update the display to redraw the model edges after each rotation. After each rotation, the image is saved, but it is as if NX doen't have enough time to redraw completly model and what gets exported is a simplified version of the geometry.

You can see below that I try multiple function to uodate the display, but nothing seems to work.

I would also like to know how define the base 3D point (origin2) dynamically instead of statically. (something like Pick the geometric center of the view)


Code:
' NX 8.5.3.3
'
Option Strict Off
Imports System
Imports System.Threading
Imports NXOpen  
Imports NXOpen.UF


Module NXJournal
Sub Main


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

    ' ---------------------------------------------------
    '   Choose the folder where to export the images
    ' ---------------------------------------------------
    Dim theFolder as String
    
	Dim defAns as String = "C:\"
        theFolder = InputBox("Where to export images? (Paste directory path)", "", defAns)
        if theFolder = "" then
        Return
        Else
		theFolder = theFolder.Replace("""", "")
		End If

    ' ---------------------------------------------------
    '   Choose the base name for the images
    ' ---------------------------------------------------
	Dim ImageName As String

	defAns = "IMG"
       ImageName = InputBox("Enter a base name for all created images", "", defAns)
       if ImageName = "" then
       Return
       Else
	   ImageName = ImageName.Replace("""", "")
	   End If

    ' ---------------------------------------------------
    '   Choose the angle increment for the images
    ' ---------------------------------------------------
	Dim strAngle As String
    Dim Angle As Integer
	
	defAns = "45"
       strAngle = InputBox("Enter an angle between each generated images", "", defAns)
       if strAngle = "" then
       Return
       Else
	   Try
        Angle = CInt(strAngle)
		Catch ex As NXException
		MsgBox("You did not enter a proper integer value for the angle")
		Return
		End Try
	   End If

	   
	' ---------------------------------------------------
    '   Create the images
    ' ---------------------------------------------------
	
	'Parameters for the view rotation
	Dim origin2 As Point3d = New Point3d(0.0, -46.1544676136208, 79.5)
    Dim vector2 As Vector3d = New Vector3d(0.0, 1.0, 0.0)
	   
	'Parameters for the exported image name
    Dim exportFileName As String = Nothing
	Dim ImageNameInt As String = Nothing
	
	'parameters for the loop
	Dim StartInt As Integer = 1
	Dim EndInt As Integer = Math.Floor(360/Angle)
	
	For index As Integer = StartInt To EndInt

	   ImageNameInt = ImageName & "_" & cstr(index)
	   exportFileName = IO.Path.Combine(theFolder, ImageNameInt & ".png")
	   
	   'if this file already exists, delete it
       If My.Computer.FileSystem.FileExists(exportFileName) Then
           My.Computer.FileSystem.DeleteFile(exportFileName)
       End If	   
       
	   workPart.ModelingViews.WorkView.Rotate(origin2, vector2, Angle)

	   'Refresh the view after the rotation
	   
	   ufs.Disp.MakeDisplayUpToDate()
	   ufs.Disp.Refresh()
	   workPart.ModelingViews.WorkView.UpdateDisplay()
	   
           'This discard all the display and redraw, but still not entirely. (and it is really slow)
           'ufs.Disp.RegenerateDisplay()
	   
	   Dim background_color As UFDisp.BackgroundColor = UFDisp.BackgroundColor.white
       displayPart.WCS.Visibility = False
       'ufs.Disp.CreateImage(exportFileName, UFDisp.ImageFormat.Png, background_color)  

	Next   
	   
End Sub
End Module

Etienne
NX8.5.3.3 + TC9.1.2.2

 
Replies continue below

Recommended for you

I had a similar issue once; I added a "DoEvents" call before attempting to export the image. It seems to have taken care of the issue.

Code:
    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

            'process other messages until system is ready to export screenshot
            [highlight #FCE94F]System.Windows.Forms.Application.DoEvents()[/highlight]
            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

    End Sub

www.nxjournaling.com
 
Hi,

We use a slightly different approach (see code below) to create a JPEG that uses the canned views to rotate (Orient()) to and a different jpeg call (BatchShadeOptions()).

Couldn't tell you what the exact differences are but we don't get your problem.

Also you may be able to get your origin from the view origin (NXOpen.View.Origin). Or you may not need the origin because all the positioning is driven from the View.


Code:
// Fit model to screen
            Session.Parts.Work.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Isometric, NXOpen.View.ScaleAdjustment.Fit);

            // Setup export options and create jpg
            UFDisp.ShadeOptions options = new UFDisp.ShadeOptions();
            options.disable_raytracing = true;
            options.distribute_excess_light = true;
            options.facet_quality = 1;
            options.resolution = 400;
            options.plot_quality = UFDisp.ShadePlot.PlotCoarse;
            options.generate_shadows = false;
            options.transparent_shadows = false;
            options.fixed_camera_viewing = false;
            options.super_sample = 3;
            options.raytrace_memory = 128;
            options.subdivision_depth = 6;
            options.radiosity_quality = 15;
            options.distribute_excess_light = false;
            options.use_midpoint_sampling = true;
            options.format = UFDisp.ShadeFormat.FormatRaster;
            options.display = UFDisp.ShadeDisplay.DisplayNearestRgb;
            UFSession.Disp.BatchShadeOptions(aJpgFileName, 1920, 1440, UFDisp.ShadeMethod.Preview, ref options);

Paul


Paul Turner
CAD & Process Engineer
Mastip Technology
 
Thank you fo your suggestion cowski, unfortunately it didn't work. I tried adding a timer to pause the execution of the script, but it seems to also stop the execution in NX..

'pause execution
Threading.Thread.Sleep(2500)

'other display update functions (that also don't work)
ufs.Disp.MakeDisplayUpToDate()
ufs.Disp.Refresh()
workPart.ModelingViews.WorkView.UpdateDisplay()
ufs.Disp.RegenerateDisplay()

Etienne
NX8.5.3.3 + TC9.1.2.2

 
Status
Not open for further replies.
Back
Top