Use a camera object to set the view of a document before saving it to a bitmap

By Wayne Brill

If you are using Fit method of a view before calling the SaveAsBitmap method of the view you may have noticed that the Fit does not occur until the code has exited and so you do not get the image you expect. To resolve this you can use the Camera object.

VBA Example:

Sub cameraAndSaveAsBitmap()
  If ThisApplication.Documents.count = 0 _
                               Then Exit Sub
 
  Dim oView As view
  Set oView = ThisApplication.ActiveView
 
  Dim oCamera As Camera
  Set oCamera = oView.Camera
 
  oCamera.Fit
 
  oCamera.Apply
‘ OR oCamera.ApplyWithoutTransition
 
  Call oView.SaveAsBitmap _
     ("C:tempassembly.bmp", 400, 400)

End Sub

VB.NET

Public Class Form1             Dim m_inventorApp As Inventor.Application _                                          = Nothing             Private Sub Button1_Click(ByVal sender As  _                                    System.Object, _                      ByVal e As System.EventArgs) _                            Handles Button1.Click                 ' Get an active instance of Inventor            Try                m_inventorApp = System.Runtime. _                       InteropServices.Marshal. _           GetActiveObject("Inventor.Application")                 Catch 'Inventor not started                System.Windows.Forms.MessageBox. _                 Show("Start an Inventor session")                Exit Sub            End Try                 'Call the Sub            cameraAndSaveAsBitmap()        End Sub             Sub cameraAndSaveAsBitmap()            If m_inventorApp.Documents.Count = 0 _                                         Then Exit Sub                 Dim oView As view            oView = m_inventorApp.ActiveView                 Dim oCamera As Camera            oCamera = oView.Camera                 oCamera.Fit()                 oCamera.Apply()            ' OR oCamera.ApplyWithoutTransition                 Call oView.SaveAsBitmap _               ("C:tempassembly.bmp", 400, 400)             End Sub    End Class

Comments

3 responses to “Use a camera object to set the view of a document before saving it to a bitmap”

  1. Hi,
    i tried your code in VBNEt inside my application, but it doesn’t work! I want to take a bitmap of the Inventor Active Document when the user put the ipt file inside Vault.
    All works fine until the “Call oView.SaveAsBitmap(…..)” – it returned an unspecified error:
    “Errore non specificato. (Eccezione da HRESULT: 0x80004005 (E_FAIL))”
    Does your code works on Inventor 2K13 on windows 7 @ 64bit?
    Thanks in advance
    Dave

  2. Hi Dave,
    I have Inventor 2013 running on Win7 64bit. I just tried my simple example in a stand alone exe and it worked ok. Does the VBA code create the .bmp file?
    Thanks,
    Wayne

  3. So this works really well but I was wondering is there a way to have the background be white. If I can do that I would also want to have this be wireframe instead of solid. Are either of these possible?

Leave a Reply to Beth M.Cancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading