Quantcast
Channel: NX Journaling - Journaling / NXOpen API
Viewing all articles
Browse latest Browse all 783

Exporting Screenshot to PowerPoint

$
0
0

Hi I am taking NX standard views and export to them to a PPT in standard format, I am facing problem with the scale. The view scale is not aligned to each other due to the capture size and fit view (Width and Height is not same when the image is compressed to standard size, View fit is also causing the issue). Is that possible to Make the capture area square and Properly fit, so the views will look aligned. Will this one will help me to define the frame "ufs.Disp.CreateFramedImage"? for this what has to be the upleftcorner value

Option Strict Off
Imports System
Imports System.IOImports System.Windows.FormsImports System.DrawingImports System.Drawing.ImagingImports System.CollectionsImports System.Runtime.InteropServicesImports NXOpen
Imports NXOpen.GeometricUtilitiesImports NXOpen.UFImports NXOpen.AssembliesImports NXOpenUI
Imports System.Collections.GenericImports System.ThreadingImports Microsoft.OfficeImports Microsoft.Office.Interop.PowerPointImports Microsoft.Office.InteropImports Microsoft.Office.Interop.Excel 
 
Module OnePagerToolNX
 
    Dim theSession As Session = Session.GetSession()Dim workPart As Part = theSession.Parts.WorkDim displayPart As Part = theSession.Parts.DisplayDim theUI As UI = UI.GetUIDim ufs As UFSession = UFSession.GetUFSessionDim lw As ListingWindow = theSession.ListingWindow'Dim AppPowerPoint As PowerPoint.Application = Nothing'Dim ActiveSlide As PowerPoint.Slide = Nothing'Dim ActivePresentation As PowerPoint.Presentation = NothingDim AppPowerPoint AsObject=Nothing' NX Doesnot support Powerpoint or Office ObjectsDim ActiveSlide AsObject=NothingDim ActivePresentation AsObject=NothingDim RefFileName AsString=NothingDim RefPresentation AsObject=NothingDim RefSlide AsObject=NothingDim ModelParts AsNew ArrayList
    Dim AssyParts AsNew ArrayList
    Dim MyPart As Part =NothingDim PowerpointFileName AsString=NothingDim PowerpointFileExists AsBoolean=FalseDim basePart1 As BasePart =NothingDim allParts()As Part =Nothing 
 
    Sub Main() 
        lw.Open() 
        Dim ParentPartNo AsString=NothingDim ParentPartRev AsString=NothingDim ParentPartName AsString=NothingDim CurrentDate AsString= System.DateTime.Today
        ParentPartNo ="X"'workPart.GetStringAttribute("DB_PART_NO")
        ParentPartRev ="Y"'workPart.GetStringAttribute("DB_PART_REV")
        ParentPartName ="Z"'workPart.GetStringAttribute("DB_PART_NAME") 
        'Create Powerpoint File and Save it in user defined locationDim SaveFileDialog1 AsNew SaveFileDialog
        With SaveFileDialog1
            .Title="Save OnePager to Presentation File".InitialDirectory="C:\".Filter="PowerpointFiles (*.pptx)|*.pptx|Macro enabled Powerpoint files (*.pptm)|*.pptm|All files (*.*)|*.*".FilterIndex=1.RestoreDirectory=True.OverwritePrompt=False.FileName= ParentPartNo &"- One Pager Presentation"If.ShowDialog()= DialogResult.OKThen
                PowerpointFileName =.FileNameElseExitSubEndIfEndWith 
        If AppPowerPoint IsNothingThen
            AppPowerPoint =CreateObject("PowerPoint.Application") 
            If AppPowerPoint IsNothingThen
                theUI.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Powerpoint, journal exiting")ExitSubEndIfEndIf 
        AppPowerPoint.Visible=True 
        If File.Exists(PowerpointFileName)Then
            PowerpointFileExists =True
            ActiveSlide = AppPowerPoint.Presentations.Open(PowerpointFileName)Else
            ActivePresentation = AppPowerPoint.Presentations.Add()
            ActivePresentation.SaveAs(PowerpointFileName)EndIf 
        If ActivePresentation IsNothingThen
            theUI.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not open Powerpoint file: "& PowerpointFileName & ControlChars.NewLine&"journal exiting.")ExitSubEndIf 
        RefPresentation = AppPowerPoint.Presentations.Open("C:\MyTemplate.pptx") 
        For i AsInteger=1To RefPresentation.Slides.Count
            RefPresentation.Slides.Item(i).Copy()
            ActivePresentation.Slides.Paste()'ActivePresentation.Slides.Item(i).Design = RefPresentation.Slides.Item(i).Design()Next i
        RefPresentation.Close() 
        Dim Titlebox AsObject=NothingDim DateBox AsObject=Nothing 
        Titlebox = ActivePresentation.slides(1).Shapes.AddTextbox(1, 20, 150, 400, 30)With Titlebox.TextFrame.TextRange.Text= ParentPartNo &"/"& ParentPartRev &" : "& ParentPartName
            .Font.Bold=True.Font.Name="Times New Roman".Font.Size=15EndWith 
        DateBox = ActivePresentation.slides(1).Shapes.AddTextbox(1, 20, 260, 150, 15)With DateBox.TextFrame.TextRange.Text= CurrentDate.ToString.Font.Bold=False.Font.Name="Times New Roman".Font.Size=11EndWith 
 
        'Categorize Assembly and Parts in the current session
        allParts = theSession.Parts.ToArray()ForEach Part1 As Part In allParts
            Dim thisTag As NXOpen.Tag= ufs.Assem.AskRootPartOcc(Part1.Tag)If thisTag = NXOpen.Tag.NullThen
                ModelParts.Add(Part1)Else
                AssyParts.Add(Part1)EndIfNext 
        'Export all Parts Detail to Powerpoint 
        ForEach Part2 As Part In AssyParts
            MyPart = Part2
            Call CreatePPT()Next 
        ForEach Part3 As Part In ModelParts
            MyPart = Part3
            Call CreatePPT()Next 
        'ActivePresentation.Slides(2).Delete()
        ActivePresentation.Save()
        ActivePresentation.Close()
        AppPowerPoint.Quit()
        AppPowerPoint =Nothing
        ActivePresentation =Nothing
        ActiveSlide =Nothing 
 
    EndSubSub CreatePPT() 
        Dim Partno AsString=Nothing 
        Dim MyPartLoadStatus As PartLoadStatus =NothingDim SDPSstatus As PartCollection.SdpsStatus=Nothing
        SDPSstatus = theSession.Parts.SetDisplay(MyPart, False, False, MyPartLoadStatus)Dim tempScreenshot AsStringDim tempLocation AsString="C:\Temp" 
        'Getting Part AttributesTry
            Partno = MyPart.GetStringAttribute("DB_PART_NO")Catch ex As NXException
            lw.WriteLine("Partno Not found"& ex.ErrorCode&":"& ex.Message.ToString)
            Partno ="Not Found"EndTry 
 
        'Capture ViewsDim Mylayout As Layout =Nothing'Hide unwanted Items in ViewDim visibleObjects()As DisplayableObject =NothingTry
            Mylayout =CType(MyPart.Layouts.FindObject("L1"), Layout)Const OtheritemLayer AsInteger=6'Const Solidlayer As Integer = 1Dim Isoview As ModelingView =CType(MyPart.ModelingViews.FindObject("Isometric"), ModelingView)
            Mylayout.ReplaceView(MyPart.ModelingViews.WorkView, Isoview, True)
            visibleObjects = MyPart.ModelingViews.WorkView.AskVisibleObjects()ForEach Obj As DisplayableObject In visibleObjects
                IfTypeOf(Obj)Is DatumPlane Then
                    Obj.Layer= OtheritemLayer
                    Obj.RedisplayObject()EndIfIfTypeOf(Obj)Is CoordinateSystem Then
                    Obj.Layer= OtheritemLayer
                    Obj.RedisplayObject()EndIfIfTypeOf(Obj)Is DatumAxis Then
                    Obj.Layer= OtheritemLayer
                    Obj.RedisplayObject()EndIfIfTypeOf(Obj)Is Curve Then
                    Obj.Layer= OtheritemLayer
                    Obj.RedisplayObject()EndIf 
                IfTypeOf(Obj)Is Sketch Then
                    Obj.Layer= OtheritemLayer
                    Obj.RedisplayObject()EndIf 
            Next
            MyPart.Layers.SetState(1, Layer.State.WorkLayer)
            MyPart.Layers.SetState(6, Layer.State.Hidden)Catch ex As NXException
            lw.WriteLine(Partno &": Layers are not correct Please Check Image-"& ex.ErrorCode&":"& ex.Message.ToString)EndTry 
 
        TryDim currentScale AsDouble= workPart.ModelingViews.WorkView.Scale
            workPart.ModelingViews.WorkView.Fit()
            workPart.ModelingViews.WorkView.SetScale(currentScale)Catch ex As Exception
            lw.WriteLine(Partno & ex.Message.ToString&"View Scale failed")EndTryTryDim Isoview As ModelingView =CType(MyPart.ModelingViews.FindObject("Isometric"), ModelingView)
            Mylayout.ReplaceView(MyPart.ModelingViews.WorkView, Isoview, True)
            MyPart.ModelingViews.WorkView.RenderingStyle= NXOpen.View.RenderingStyleType.ShadedWithEdges
            tempScreenshot = IO.Path.Combine(tempLocation, Partno &"- ISO.jpg")
            ExportScreenshot(tempScreenshot) 
        Catch ex As NXException
            lw.WriteLine(Partno &": ISO View Not Generated-"& ex.ErrorCode&":"& ex.Message.ToString)EndTry'Capture Front ViewTryDim Frontview As ModelingView =CType(MyPart.ModelingViews.FindObject("Front"), ModelingView)
            Mylayout.ReplaceView(MyPart.ModelingViews.WorkView, Frontview, True)
            MyPart.ModelingViews.WorkView.RenderingStyle= NXOpen.View.RenderingStyleType.ShadedWithEdges
            tempScreenshot = IO.Path.Combine(tempLocation, Partno &"- Front.jpg")
            ExportScreenshot(tempScreenshot) 
        Catch ex As NXException
            lw.WriteLine(Partno &": Front View Not Generated-"& ex.ErrorCode&":"& ex.Message.ToString)EndTry 
        'Capture Left ViewTryDim Leftview As ModelingView =CType(MyPart.ModelingViews.FindObject("Left"), ModelingView)
            Mylayout.ReplaceView(MyPart.ModelingViews.WorkView, Leftview, True)
            MyPart.ModelingViews.WorkView.RenderingStyle= NXOpen.View.RenderingStyleType.ShadedWithEdges
            tempScreenshot = IO.Path.Combine(tempLocation, Partno &"- Left.jpg")
            ExportScreenshot(tempScreenshot) 
        Catch ex As NXException
            lw.WriteLine(Partno &": Left View Not Generated-"& ex.ErrorCode&":"& ex.Message.ToString)EndTry 
        'Capture Top ViewTryDim Topview As ModelingView =CType(MyPart.ModelingViews.FindObject("Top"), ModelingView)
            Mylayout.ReplaceView(MyPart.ModelingViews.WorkView, Topview, True)
            MyPart.ModelingViews.WorkView.RenderingStyle= NXOpen.View.RenderingStyleType.ShadedWithEdges
            tempScreenshot = IO.Path.Combine(tempLocation, Partno &"- Top.jpg")
            ExportScreenshot(tempScreenshot) 
        Catch ex As NXException
            lw.WriteLine(Partno &": Top View Not Generated-"& ex.ErrorCode&":"& ex.Message.ToString)EndTry 
 
        AppPowerPoint.ActivePresentation.Slides.AddSlide(AppPowerPoint.ActivePresentation.Slides.Count+1, AppPowerPoint.ActivePresentation.SlideMaster.CustomLayouts(7))
        AppPowerPoint.ActiveWindow.View.GotoSlide(AppPowerPoint.ActivePresentation.Slides.Count)
        ActiveSlide = AppPowerPoint.ActivePresentation.Slides(AppPowerPoint.ActivePresentation.Slides.Count)'Adding Images from Temporary LocationTry
            ActiveSlide.Shapes.AddPicture(tempLocation &"\"& Partno &"- ISO.jpg", False, True, 40, 60, 200, 200)
            ActiveSlide.Shapes.AddPicture(tempLocation &"\"& Partno &"- Left.jpg", False, True, 40, 280, 200, 200)
            ActiveSlide.Shapes.AddPicture(tempLocation &"\"& Partno &"- Top.jpg", False, True, 280, 60, 200, 200)
            ActiveSlide.Shapes.AddPicture(tempLocation &"\"& Partno &"- Front.jpg", False, True, 280, 280, 200, 200)Catch ex As Exception
            lw.WriteLine(Partno &": Image Not added-"& ex.Message.ToString)EndTry 
        ForEach Myshape AsObjectIn ActiveSlide.Shapes
            Myshape.Select()
            Myshape.Line.Transparency=1
            Myshape.Line.Weight=1
            Myshape.Line.Visible=1Next 
 
    EndSub 
 
    Sub ExportScreenshot(ByVal filename AsString) 
        'save user preference for visibility of WCS, triad, view name, and view borderDim wcsVisible AsBoolean= theSession.Parts.BaseDisplay.WCS.VisibilityDim triadVisible AsInteger= theSession.Preferences.ScreenVisualization.TriadVisibilityDim displayModelViewNames AsBoolean= theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNamesDim displayModelViewBorders AsBoolean= 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=True
        theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders=False
        theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames=False 
        Try
            ufs.Disp.CreateImage(filename, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.White)'Dim Upleftcorner() As Integer = Nothing'Upleftcorner(0) = 0'Upleftcorner(1) = 0'ufs.Disp.CreateFramedImage(filename, UFDisp.ImageFormat.Jpeg, UFDisp.BackgroundColor.White, Upleftcorner, 200, 200)Catch ex As Exception
            MsgBox(ex.Message& ControlChars.NewLine& _
                   "'"& filename &"' could not be created")ThrowNew 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
        EndTry 
    EndSub 
    PublicFunction GetUnloadOption(ByVal dummy AsString)AsInteger'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately 
    EndFunction 
EndModule 
 
 

Viewing all articles
Browse latest Browse all 783

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>