This is a continuation of a previous post introducing cameras.
A Camera Gotcha
When working with cameras associated with a graphics view you use the Camera property of the View object. Typically you’ll get the View object using the Application.ActiveView property. The behavior of the View.Camera property is that every time you call it, it returns a new camera. This behavior makes it easy to run into a very common problem when working with cameras. The code below illustrates the problem. It calls the Camera property of the View object and then calls the ViewOrientationType property of the Camera object, setting the camera position to be a standard isometric view. Once it does this it calls the camera’s Apply method. The result is that nothing happens.
' Set the camera orientation to an iso view.
invApp.ActiveView.Camera.ViewOrientationType = _
' Apply the camera changes to the view.
The reason that nothing happens is that in the first line the Camera property is called and a camera is created and returned. On this camera the isometric view is specified. On the next line the Camera property is called again and another camera is created and returned. On this camera the Apply method is called, but this camera hasn’t had the view orientation set but is the same as the current view so you don’t see any visible change. The code below illustrates the correct procedure and does create the desired result. In this case the Camera property is called and the camera is returned. The orientation for that camera is defined as an isometric view and the Apply method on the same camera is called. The view then updates to reflect the new camera position.
' Get the camera from the view.
Dim cam As Camera
Set cam = invApp.ActiveView.Camera
cam.ViewOrientationType = kIsoTopLeftViewOrientation
' Apply the camera changes to the view.
Applying Camera Changes
Setting a property on a camera doesn’t have an immediate effect. This is because typically you’ll want to make several changes to the camera before you force the view to update. For example you might want to change both the eye and target points and then update the camera. There are two methods that cause changes to the camera take effect. They are Apply and ApplyWithoutTransition. The Apply method uses the same transition technique that inventor uses when you change to a new view orientation; it smoothly transitions from one position to another. The ApplyWithoutTransition immediately shows the new camera position without any transition. The ApplyWithoutTransition is used when you’re doing your own camera animation because you’ll be computing all of the camera positions yourself and don’t want the delay as Inventor transitions from one camera position to another.
Modifying the Camera
There are three properties on the camera that control the position and orientation of the camera; the Eye, Target, and UpVector properties. There are other properties that let you control the camera’s field of view. the Perspective property lets you toggle the camera from perspective to parallel. When the camera is a perspective camera you can use the PerspectiveAngle property to control the field of view. When it’s a parallel camera you can use the GetExtents and SetExtents methods to get and set the field of view. In addition to these functions that let you directly get and set the underlying properties of a camera there are also some convenience functions that make it easier to manipulate the camera. For example, the ViewOrientationType lets you specify standard view positions, (i.e. top, front, top-left iso, etc.) and Inventor will modify the eye, target, and up vector as-needed. The Fit method takes into account the size of the model and modifies the camera so the entire model is visible. There is also a function that takes mouse input and modifies the camera to reflect the mouse movement or scrolling.
Below is some VBA code that will fly the camera around the model. To simplify the code, the target is hard coded at the origin and the up vector is the positive Z.
Public Sub RotateCamera()
' Get the active camera.
Dim cam As Camera
Set cam = ThisApplication.ActiveView.Camera
Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry
' Define the number of steps in the animation.
Dim steps As Integer
steps = 360
' Define the distance between the eye and target.
Dim eyeDistance As Double
eyeDistance = 15
' Calculate pi.
Dim pi As Double
pi = Atn(1) * 4
' Iterate the specified number of steps.
Dim i As Integer
For i = 1 To steps
' Calculate the x and y coordinates of the eye.
Dim x As Double
Dim y As Double
x = eyeDistance * Cos(i / steps * (2 * pi))
y = eyeDistance * Sin(i / steps * (2 * pi))
' Set the eye using calculated x, y and a hard coded z value.
cam.Eye = tg.CreatePoint(x, y, 3)
' Define the up vector as positive z.
cam.UpVector = tg.CreateUnitVector(0, 0, 1)
' Apply the current camera definition to the view.
Why Use the Camera?
There are several reasons you might want to use the Camera object. The first is that you have a need to control the view within Inventor. For example, you can create a custom fly-trough of an assembly or write some utilities that allow you to quickly go to certain views. The sample code above demonstrates the procedure to set the camera properties and apply it to a view. Watch for another post about how to make the camera follow a curve like what was demonstrated in the video in this earlier post.
A camera is also used to create non-standard drawing views. Using a camera you can create any view orientation you want. The following code illustrates using a camera to create a custom drawing view that is a perspective isometric view.
' Create a camera to be used to define the view orientation.
Dim cam As Camera
Set cam = ThisApplication.TransientObjects.CreateCamera
' Set up the camera.
cam.Perspective = True
cam.ViewOrientationType = kIsoTopRightViewOrientation
' Create a drawing view using the camera.
Dim viewPnt As Point2d
Set viewPnt = ThisApplication.TransientGeometry.CreatePoint2d(12, 12)
Dim drawView As DrawingView
Set drawView = drawSheet.DrawingViews.AddBaseView(doc, viewPnt, 1, _
kHiddenLineRemovedDrawingViewStyle, , _
The Camera object can also be used to save an image as a file. You essentially capture whatever the camera is currently seeing and save that as an image file. The format of the file can be any of several popular image types, (i.e. jpg, bmp, png, gif, and tiff). This can be useful within Inventor but is particularly useful with Apprentice because Apprentice doesn’t have a visible graphics window. You can use a camera to create images for parts, assemblies, and drawings. The VB.Net code below demonstrates creating an image of a part or assembly using Apprentice. It assumes you’ve already created an instance of Apprentice and have opened the part or assembly, which are both passed into this sub, along with the filename to use for the image. The type of image created is dependent on the extension of the filename.
Private Sub CreateImage( _
Apprentice As Inventor.ApprenticeServerComponent, _
PartOrAssembly As Inventor.ApprenticeServerDocument, _
Filename As String)
' Create a new camera.
Dim cam As Inventor.Camera
cam = Apprentice.TransientObjects.CreateCamera
' Associate the document with the camera.
cam.SceneObject = PartOrAssembly.ComponentDefinition
' Define the camera and apply the changes.
cam.ViewOrientationType = _
' Save the result, defining the background color of the image.
cam.SaveAsBitmap(Filename, 800, 600, _
Apprentice.TransientObjects.CreateColor(255, 255, 0), _
Apprentice.TransientObjects.CreateColor(15, 15, 15))
The resulting image is shown below.
That’s the basics of using cameras. Watch for an upcoming post where we’ll look at a tool to better visualize the camera and how to use geometry in the model to control the camera.