Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Camera.SaveAsBitmap Resets Background Color

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
rjay75
1246 Views, 7 Replies

Camera.SaveAsBitmap Resets Background Color

When calling the Camera.SaveAsBitmap method and specifying a background color, should it reset the background color of the current view? Calling it in 2014 does this.

 

Also is there a way to apply the camera settings without changing the current view? I want to change the camera settings but just save the images to a file?

7 REPLIES 7
Message 2 of 8
philippe.leefsma
in reply to: rjay75

Hi Thomas,

 

I think that method should not change the view...

 

A couple of suggestions for you:

 

1/ You could try to add a new view: Doc.Views.Add, then modify that view camera, take the picture and close the view.

 

2/ Use a transient camera. Below is a piece of code that was used in an Apprentice sample:

 

        ' Open the specified file.
        Dim doc As Inventor.ApprenticeServerDocument = m_apprentice.Open(filename)

        ' Create a camera.
        m_camera = m_apprentice.TransientObjects.CreateCamera

        ' Associate the camera with the part's component definition.
        m_camera.SceneObject = doc.ComponentDefinition

        m_camera.Fit()
        m_camera.ApplyWithoutTransition()

        Dim tg As Inventor.TransientGeometry = m_apprentice.TransientGeometry
        m_camera.Eye = tg.CreatePoint(-10, 10, 10)
        m_camera.UpVector = tg.CreateUnitVector(0, 1, 0)
        m_camera.Target = tg.CreatePoint(0, 0, 0)
        m_camera.Fit()
        m_camera.ApplyWithoutTransition()

        Dim picDisp As stdole.IPictureDisp
        picDisp = m_camera.CreateImage(PictureBox1.Width, PictureBox1.Height)

        ' Return the image.
        PictureBox1.Image = Microsoft.VisualBasic.Compatibility.VB6.IPictureDispToImage(picDisp)

I hope it helps,

Philippe.

 



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Message 3 of 8
rjay75
in reply to: philippe.leefsma

Using a Transient Camera Object in Inventor 2014 has unpredictable results. Sometimes the addin continues and sometimes it doesn't. But it doesn't throw any errors or exceptions. It just stops and eventually hangs Inventor. I'm trying to show a dialog after creating the camera. Setting a new camera object wants the SceneObject to be a View object. Setting a new camera to an existing view behaves unpredictably too.

 

Using a new view works. But it throws a ComException when attempting to close the view as well. ComException errors occur that don't seem to be caught by a try statement in c#. The view closes as expected. It also seems to raise the OnNewView event as well. But my code that responds to it doesn't run til after the command completes at which point the View object they operate on may be invalid.  It appears that accessing the view com object hangs the debugger in Visual Studio. Running Inventor without debugging and using a new view works as expected. But I suspect that com errors are being thrown in the background. My guess it has to do with the order of events firing.

 

Excuting it using a new view works and accomplishes the task but seems very hackish to avoid having the background color being changed on a view. The simplest solution would be to save the image and reset the background to the original colors. But I haven't found a way to get what the colors are. I could save my view images and then reset the camera to the original settings and save a temporary image using the original background colors to reset the view to avoid the apperance of a view change.

 

At the moment adding a new view works the best and is the most transparent to the user. Unfortunately while debugging and running the command causes a ComException error that I can't catch and have to ignore.

Message 4 of 8
philippe.leefsma
in reply to: rjay75

Can you give a try at those 3 VBA samples below. All of them seem to work fine on my side.

 

When you mentioned initially that Camera.SaveAsBitmap is reseting the background color, what does it do exactly?

 

Thanks,

Philippe.

 

Sub CamSaveAsBitmap1()

    Dim view As view
    Set view = ThisApplication.ActiveView
    
    Dim camera As camera
    Set camera = view.camera
    
    Dim topClr As color
    Set topClr = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
    
    Dim bottomClr As color
    Set bottomClr = ThisApplication.TransientObjects.CreateColor(0, 255, 0)
    
    Call camera.SaveAsBitmap("C:\Temp\test.png", view.width, view.height, topClr, bottomClr)

End Sub

Sub CamSaveAsBitmap2()

    Dim doc As Document
    Set doc = ThisApplication.ActiveDocument
    
    Dim view As view
    Set view = doc.views.Add
    
    Dim camera As camera
    Set camera = view.camera
    
    Dim topClr As color
    Set topClr = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
    
    Dim bottomClr As color
    Set bottomClr = ThisApplication.TransientObjects.CreateColor(0, 255, 0)
    
    Call camera.SaveAsBitmap("C:\Temp\test.png", view.width, view.height, topClr, bottomClr)

    view.Close

End Sub

Sub CamSaveAsBitmap3()

    Dim doc As Document
    Set doc = ThisApplication.ActiveDocument
    
    Dim view As view
    Set view = ThisApplication.ActiveView

    Dim camera As camera
    Set camera = ThisApplication.TransientObjects.CreateCamera
    
    camera.SceneObject = doc.ComponentDefinition
    
    camera.ViewOrientationType = kIsoTopLeftViewOrientation
    camera.Fit
    camera.ApplyWithoutTransition
    
    Dim topClr As color
    Set topClr = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
    
    Dim bottomClr As color
    Set bottomClr = ThisApplication.TransientObjects.CreateColor(0, 255, 0)
    
    Call camera.SaveAsBitmap("C:\Temp\test.png", view.width, view.height, topClr, bottomClr)

End Sub

 



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Message 5 of 8
rjay75
in reply to: philippe.leefsma

Thanks for your help. I was calling SaveAsBitmap only supplying the top color. Doing this would change the background color to white when the view was updated after saving. Supplying both colors doesn't seem to have this result.

I used method three and it gives me the result I want.

The strange com errors seemed to be a result of the order events were firing. The OnNewView event didn't seem to fire with my add-in until after the method the view was created and closed in completed. By that time the view object was no longer valid.
Message 6 of 8
MaciejWojda
in reply to: rjay75

plate.png

Hello@philippe.leefsma@rjay75 

I also used CamSaveAsBitmap3 for my needs and works great excluding one detail. I am getting coordinate system arrows in the resulting picture. Do you know guys how to disable that?

Message 7 of 8
dalton98
in reply to: rjay75

ThisApplication.DisplayOptions.Show3DIndicator = False  

https://forums.autodesk.com/t5/inventor-forum/ilogic-save-image-of-model/td-p/6875045 

Message 8 of 8
MaciejWojda
in reply to: dalton98

Thank you @dalton98 \, that's it!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report