Problem with View.SaveAsBitmap

Problem with View.SaveAsBitmap

Boorda
Advocate Advocate
1,452 Views
7 Replies
Message 1 of 8

Problem with View.SaveAsBitmap

Boorda
Advocate
Advocate

I have an interesting dilemma with the "SaveAsBitmap" method in the Inventor.View object.

 

The actual method works as far as saving an image but it is written as a sub instead of a function.

This means it does not return the image stream so that I can continue to make edits and it does not close the stream so I can reopen the file. The image remains locked by Inventor to where I can't continue to modify the image.

  

 

It seems to me that this method should either close the stream or return it.

The file actually remains open by Inventor even if I close all Inventor documents and as of right now the only way I can get rid of the "used by another process" or "file in use" lock is to close Inventor which would kill my add-in as well.

 

File In Use

 

 

Am I missing something?

How can I close or continue to make edits to the newly created bitmap?

 

Also what is the View._GraphicsCollectionEndDump method do and what are the available action parameters? I cannot find any documentation on the method and wondering if this is how I can close the bitmap stream.

 

 

In case there are any question as far as why I want to continue to edit the image it's because I am using WPF to layer the image with a header and caption box that the user can edit. Upon saving the original image is replaced by rendering the WPF visual.

 

Edit View Rep Image

 

 

 

Thanks in advance, 

    Addam


Automation is key!
0 Likes
Accepted solutions (1)
1,453 Views
7 Replies
Replies (7)
Message 2 of 8

ekinsb
Alumni
Alumni

I'm not able to reproduce the problem you've described.  I wrote the small VBA test program below and after running the program and while the same session of Inventor is still running I'm able to delete the two files.

 

Public Sub SaveViewTest()
    Dim v As View
    Set v = ThisApplication.ActiveView
    
    Call v.SaveAsBitmap("C:\Temp\TestImageView.png", 0, 0)
    Call v.Camera.SaveAsBitmap("C:\Temp\TestImageCamera.png", 0, 0)
End Sub

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 8

Boorda
Advocate
Advocate

Hey Brian, thanks for the quick reply.

 

Your example does work in VBA and it runs successfully on my machine as well, but when I try the same method in my add-in which is written in VB.NET I get the issues I discussed above. Could there be some app domain issue going on here making it act differently in VB than in VBA? Below is the function I am having issues with.

    Public Function BuildViewRepTables() As String
        Try
            Dim cDef As ComponentDefinition = InvApp.ActiveDocument.ComponentDefinition
            
'Capture the current activated view rep so
CurrentViewRep = cDef.RepresentationsManager.ActiveDesignViewRepresentation InvApp.StatusBarText = "Generating Image..." InvViewReps = cDef.RepresentationsManager.DesignViewRepresentations 'Create new instance of the required dictionaries. ViewRepTables = New Dictionary(Of String, String) ViewRepImages = New Dictionary(Of String, ImageBox) For i As Integer = 2 To InvViewReps.Count 'Get the Inventor View Rep. InvViewRep = InvViewReps.Item(i)
'Set up a generic view for output. Dim OutputView As View = InvApp.ActiveDocument.Views.Add() OutputView.WindowState = WindowsSizeEnum.kMaximize OutputView.Visible = False OutputView.Caption = InvViewRep.Name OutputView.ShowAmbientShadows = True OutputView.ShowObjectShadows = True OutputView.ShowGroundReflections = False OutputView.ShowGroundShadows = False OutputView.ShowGroundPlane = False OutputView.DisplayMode = DisplayModeEnum.kShadedWithEdgesRendering 'Activate the view rep and restore the save camera view. InvViewRep.Activate() InvViewRep.RestoreCamera() Dim ViewRepCaption As String = Strings.Split(InvApp.ActiveDocument.DisplayName, ".")(0) + _
" [" + Strings.Replace(InvViewRep.Name, "/", " - ") + " View Rep]"
Dim ViewRepTempImgPath As String = DesignIntentDocumentFolder + ViewRepCaption + " [TEMP].png" Dim ViewRepImgPath As String = DesignIntentDocumentFolder + ViewRepCaption + ".png" Select Case InvViewRep.Camera.ViewOrientationType Case ViewOrientationTypeEnum.kArbitraryViewOrientation OutputView.Camera.Perspective = True Case Else OutputView.Camera.Perspective = False End Select 'Save the temp ViewRepImage from Inventor. '(This is the file that Inventor for whatever reason hangs on to and we cant close.) OutputView.SaveAsBitmap(ViewRepTempImgPath, OutputView.Width, OutputView.Height) 'Crate the New ViewRep Image File. Dim TempImage As New BitmapImage(New Uri(ViewRepTempImgPath)) 'Create the ImageBox Dim IB As New ImageBox(ViewRepImgPath, TempImage, ViewRepCaption) 'Add the new ViewRepImage to the dictionary. ViewRepImages.Add(ViewRepCaption, IB) OutputView.Update() 'OutputView._GraphicsCollectionEndDump("Delete", ViewRepTempImgPath) OutputView.Close() OutputView = Nothing Next InvApp.StatusBarText = "Completed Generating Images..." 'Reset the view rep back to the original one. CurrentViewRep.Activate() CurrentViewRep.RestoreCamera()
Catch ex As Exception MsgBox("Error generating the Design View Rep Images!" & vbnewline & ex.Message) End Try Return "" End Function

 

 Also what does the "_GraphicsCollectionEndDump" method do and what are the params?

 

Thanks again.


Automation is key!
0 Likes
Message 4 of 8

adam.nagy
Autodesk Support
Autodesk Support

On which line of the code does the dialog pop up?

I'm wondering if it's one of the other objects that has the image file as an input param that is locking the file and not Inventor itself.  



Adam Nagy
Autodesk Platform Services
0 Likes
Message 5 of 8

Boorda
Advocate
Advocate

Hi Adam, 

      

   It happens on this line:

OutputView.SaveAsBitmap(ViewRepTempImgPath, OutputView.Width, OutputView.Height)

 

 

If the image does not previously exist then the image is created successfully and there are no messages indicating that the file is in use by another process. If I pass the path of a previously created image to overwrite it with a new one it works only if I shut down and restart Inventor.

Now, if I try to pass the path of a previously existing image that was created without shutting down Inventor then I can't overwrite the file with my modified Image or delete it form disk and it gives me the error indicating the there was an error saving the file.

The ultimate goal here is to grab the saved camera of each Design View Representation and load it into my ImageBox object. My ImageBox is simply a custom WPF control that overlays two semitransparent textboxes that allow the user to add a header and caption. When the ImageBox image is saved it renders the visual of the WPF object and attempts to overwrite the original image created by Inventor using the generated file path. The function fails as described above before I can even load the image into my ImageBox object.

I apologize as the function looks kind of hacked up due to trying many different things in desperation but here is the general idea of what this function is doing....

  • Grab the ComponentDefinition of the active document
  • Save the current view rep in the CurrentViewRep so we can reactivate it when we are done with our process
  • Grab the DesignViewRepresentations from the active document's ComponentDefinition
  • Create a Dictionary(Of String, ImageBox) which holds all of the completed Images identified by their View Rep name


Now Iterate through the Design View Representations and preform the following...

  • Add a new View to the Views collection so that we can change the display types and shading
  • Activate the new view with the new display settings
  • Automatically generate the default header by using the view rep name
  • Generate the full file name of the image to be saved
  • Try to determine if the view should be Ortho or Perspective based on the views orientation
  • Check to see if the image already exists on disk then decide to keep or overwrite
  • If overwrite chosen then save new view image via the SaveAsBitmap sub
  • Load the newly created image into ImageBox object
  • Add the ImageBox to the ImageBox Dictionary

Finish up...

  • Close the temporary view we created earlier
  • Activate the View Rep that was current when we first started using the CurrentViewRep object

 

And finally later in the user interface once the user has entered the header and or caption and calls the save method we overwrite the original image created by Inventor using the generated file path.


Also.....
I noticed that in Brian’s example he is using the main document view window and activating the view rep inside of it but I’m creating a new view first which shows up in another tab. I wanted to do it in a new tab/view so I could hide the view from the user as the add-in was creating the initial images. Which by the way hiding the view and changing its caption didn’t work for me either.

Should I maybe try to run this process on a separate thread using ApprenticeServer?

Thanks for your interest in this matter.

-Addam B.


Automation is key!
0 Likes
Message 6 of 8

ekinsb
Alumni
Alumni
Accepted solution

Could it be that adding the image to the ImageBox and/or the ImageBox dictionary is locking the file?


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 7 of 8

Boorda
Advocate
Advocate

Brian, Addam thanks for your help.

 

I think you are right Brian about my object possibly locking the file. I broke away the function into a separate add-in by itself and started dwindling down the code until it worked. Without my ImageBox object the function ran without error. Looks like I will need to rethink how I'm referencing the image originally created by Inventor. Thanks again for the time looking into my issue.

 

-Addam


Automation is key!
0 Likes
Message 8 of 8

Boorda
Advocate
Advocate

To follow up and elaborate more in depth about the problems I was having with saving then modifiying images here's what I've figured out.

Based on my reading from MSDN and StackOverflow Brian was right in the fact that my own image object was locking the file.

At first it my assumption was that it was Inventor causing the issue since Inventor's name shows up in the locked file message, but that is simply because my add-in runs in Inventors thread. Basically if you want to open an image and keep it unlocked for edit then you need to cache the file in memory.

You can read more about it here View Image file without locking it. (Copy to memory?)

 


Automation is key!
0 Likes