Depending on the version of Inventor that you're using, I actually would shy away from using the Camera function to change views, for now, unless you know that your code won't be used by someone who uses the Perspective View type instead of Orthographic.
Why?
Because I think that there is currently a bug with how Inventor (2015 and probably up I'd imagine) handles the math to change the Perspective Angle when the view is set to Perspective = True, and there are no visible occurrences on the screen. It essentially creates an overflow bug that will force a crash or create an instability, which is never fun.
Recently I have reworked my view code to:
Sub FitStageToScreen ()
'Dim cam As Inventor.Camera
'cam = ThisApplication.ActiveView.Camera
'cam.ViewOrientationType = Inventor.ViewOrientationTypeEnum.kIsoTopRightViewOrientation
'cam.Apply()
'cam.Fit()
'cam.ApplyWithoutTransition()
Call ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomAllCmd").Execute
Call ThisApplication.ActiveView.GoHome
End Sub
I believe that this should accomplish what you are wanting to do.
However, for those of you who would like to have a little fun crashing / messing with your session of inventor, feel free to use to use the following code to do so! (*** This actually should not cause any problems in 2012, but SHOULD cause a problem in 2015 and up. I'm not sure when it was introduced into Inventor, so if you have a version that is different from the ones listed above, it would actually be nice to get some feedback to find out if it's causing bad things to happen or not!)
Imports Inventor.ViewOrientationTypeEnum
If ThisApplication.ActiveDocument.DocumentType _
<> kAssemblyDocumentObject Then
MessageBox.Show ("Run from an Assembly!!!")
Return
End If
' Set the current assembly to the active document
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
' Set the document's Assembly Component Definition
Dim oCompDef As AssemblyComponentDefinition
oCompDef = oDoc.ComponentDefinition
' Setting the Reps Manager
Dim oRepManager As RepresentationsManager
oRepManager = oCompDef.RepresentationsManager
' This used to test to see if you were on a 'Locked'
' ViewRep, but that doesn't work because for whatever reason,
' If you're on the Master View Rep, it is ***NOT*** flagged
' as being locked through the ActiveDesignViewRepresentation
' , even though it is locked if you were to look at the Design View
' directly through the Items collection.
'
' This might be another bug.
If oRepManager.ActiveDesignViewRepresentation.Name <> "View1" Then
Try
oRepManager.DesignViewRepresentations.Item("View1").Activate
Catch
oRepManager.DesignViewRepresentations.Add("View1")
Finally
oRepManager.DesignViewRepresentations.Item("View1").Activate
End Try
End If
' Let's set our oViewRep to the Active Design View
Dim oViewRep As DesignViewRepresentation
oViewRep = oRepManager.ActiveDesignViewRepresentation
' Setting the DesignViewReps
Dim oViewReps As DesignViewRepresentations
oViewReps = oRepManager.DesignViewRepresentations
' Getting all of the Components from the assembly
Dim oCompOccs As ComponentOccurrences
oCompOccs = oCompDef.Occurrences
' Setting an empty ComponentOccurrence container
Dim oCompOcc As ComponentOccurrence
' Change the view of ALL Components to Invisible
' The Perspective / Camera error will only occure
' if there are no visible Components.
For Each oCompOcc In oCompOccs
oCompOcc.Visible = False
Next
' Let's get the Camera from the Active View Rep
Dim oCam As Camera
oCam = ThisApplication.ActiveView.Camera
' If your set for Ortho, we'll change it to Perspective.
' If you're already set to Perspective, this doesn't
' really do anything.
oCam.Perspective = True
oCam.Apply
' NO matter, while on Perspective, and
' with no parts visible, once you change the -
' ViewOrientationType to any valid enum, the
' PerspectiveAngle Is changed to a value outside
' of the Type Bounds For a Double.
oCam.ViewOrientationType = kIsoTopRightViewOrientation
oCam.Fit()
Call oCam.Apply()
' Typically doing the above will crash you, however
' occasionally it will not. Adding a new View Rep
' 'which will change the active camera' will push it over
' the edge, but even sometimes this is just as spotty.
Dim oNewViewRep As DesignViewRepresentation
oNewViewRep = oViewReps.Add("Crash")
oNewViewRep.Activate
oNewViewRep.Delete
Dim oOriginalCamera As Camera
oOriginalCamera = oViewRep.Camera
' Just in case you haven't crashed yet
' I'm throwing this in there because it where I will
' absolutely run into problems. Trying to turn the Occurrences
' back to visible will make bad things happen!
For Each oCompOcc In oCompOccs
oCompOcc.Visible = True
Next
If my solution worked or helped you out, please don't forget to hit the kudos button 🙂iLogicCode Injector:
goo.gl/uTT1IB