Problem when changing the settings of the active views camera

Problem when changing the settings of the active views camera

c_heineke
Contributor Contributor
364 Views
7 Replies
Message 1 of 8

Problem when changing the settings of the active views camera

c_heineke
Contributor
Contributor

I want to change the setting of the camera of the active view. However my settings will be applied only when setting them a second time on the camera. After the first time, the view is different from what i have set on the camera. See the attached script in order to reproduce the problem. The first call to SetCamera will not align the block correctly. Only after calling it a second time (comment in the second SetCamera call) the block is displayed correctly. Can somebody please explain to me what i am doing wrong?

 

Public Sub Main()
CreateBlock ("d:/block.ipt")

ThisApplication.Documents.Open ("d:/block.ipt")

' This does not set the camera as expected
SetCamera

' The second call finally sets the camera as expected
'SetCamera
End Sub

Public Sub SetCamera()


' Get the active camera.
Dim cam As Camera
Set cam = ThisApplication.ActiveView.Camera

Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry

Dim v As UnitVector
Set v = cam.UpVector

cam.UpVector = tg.CreateUnitVector(1, 0, 0)

cam.Eye = tg.CreatePoint(0, 0, 1)
cam.Target = tg.CreatePoint(0, 0, 0)


cam.ApplyWithoutTransition


End Sub

Public Sub CreateBlock(argStrFilename As String)

Dim oApp As Inventor.Application
Set oApp = ThisApplication

Dim oPartDoc As PartDocument
Set oPartDoc = oApp.Documents.Add(kPartDocumentObject, _
oApp.GetTemplateFile(kPartDocumentObject))

Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition

Dim oTrans As TransientGeometry
Set oTrans = oApp.TransientGeometry

Dim oWorkPlanes As WorkPlanes
Set oWorkPlanes = oCompDef.WorkPlanes

Dim oMyWorkplane As WorkPlane
Set oMyWorkplane = oWorkPlanes.AddByPlaneAndOffset(oWorkPlanes(3), 10, True)

' Create sketch
Dim oMySketch As PlanarSketch
Set oMySketch = oCompDef.Sketches.Add(oMyWorkplane)
Dim firstRectPoint As Point2d
Set firstRectPoint = oTrans.CreatePoint2d(0, 0)
Dim secondRectPoint As Point2d
Set secondRectPoint = oTrans.CreatePoint2d(1, 1)
oMySketch.SketchLines.AddAsTwoPointRectangle firstRectPoint, secondRectPoint

Dim oMyProfile As Profile
Set oMyProfile = oMySketch.Profiles.AddForSolid

' Create the block
Dim oExtrudeDef As ExtrudeDefinition
Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oMyProfile, kJoinOperation)
Dim oExtrude As ExtrudeFeature
Set oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)

oPartDoc.SaveAs argStrFilename, True

End Sub

0 Likes
365 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @c_heineke.  It looks like you are telling the camera to look at the origin point of the model (Target), from 1 unit of measurement in the positive Z axis position (Eye), and want the positive X axis of the model to be pointing towards the top of your model screen (not out at the user's eye).  When I created an iLogic rule, then converted your SetCamera code from VBA to vb.net in that rule, and ran it, that is the result I get, and that is what I expected it to do.  Is that not what it is doing for you?  If that is not what you want it to do, then what do you want it to do.

Below is the version of that code that I used in an iLogic rule for testing:

Dim oView As Inventor.View = ThisApplication.ActiveView
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oCam As Inventor.Camera = oView.Camera
oCam.Target = oTG.CreatePoint(0, 0, 0)
oCam.Eye = oTG.CreatePoint(0, 0, 1)
oCam.UpVector = oTG.CreateUnitVector(1, 0, 0)
'oCam.Apply()
oCam.ApplyWithoutTransition()

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

c_heineke
Contributor
Contributor

Your code is absolutely correct. However there seem to be cases where it does not work. I have created such a case in my script in my initial post. The problem is actually that the camera related code is not doing what i wanted it to do. It moves the cube to a slightly wrong position. Only after calling the same camera code a SECOND time, the cube is moved to the correct position. I have seen this using Inventor 2025 Build 293 Release 2025.2.1. Have you run my script in your Inventor?

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

OK.  No, I did not test your overall code in my Inventor, because it is in VBA, and stopped using VBA a couple years ago, plus it was saving and opening a file.  Maybe try adding in a couple more lines of code like the Document.Update2(True) followed by something like ThisApplication.ActiveView.Update() method.  Not sure they would help any, but worth a try.  I am still using Inventor Pro 2024.3.3 at the moment.

 

I just copied your code over into an internal iLogic rule of a 'new' part, the converted it to vb.net, then eliminated the file saving, opening, and such, then ran it.  I can now see that the camera is at an angle in the view in the new part when it is done.  I am not sure why.  Below is that code I tested with, so others can look into this.  I don't have much time right now.

Sub Main
	CreateBlock()'"d:/block.ipt")
	'ThisApplication.Documents.Open("d:/block.ipt")
	' This does not set the camera as expected
	SetCamera()
	' The second call finally sets the camera as expected
	'SetCamera
End Sub

Sub SetCamera()
	' Get the active camera.
	Dim cam As Camera = ThisApplication.ActiveView.Camera
	Dim tg As TransientGeometry = ThisApplication.TransientGeometry
	Dim v As UnitVector = cam.UpVector
	cam.UpVector = tg.CreateUnitVector(1, 0, 0)
	cam.Eye = tg.CreatePoint(0, 0, 1)
	cam.Target = tg.CreatePoint(0, 0, 0)
	cam.ApplyWithoutTransition
End Sub

Sub CreateBlock()'argStrFilename As String)
	Dim oApp As Inventor.Application = ThisApplication
	Dim oPartDoc As PartDocument = oApp.Documents.Add(kPartDocumentObject, _
	oApp.FileManager.GetTemplateFile(kPartDocumentObject))
	Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
	Dim oTrans As TransientGeometry = oApp.TransientGeometry
	Dim oWorkPlanes As WorkPlanes = oCompDef.WorkPlanes
	Dim oMyWorkplane As WorkPlane = oWorkPlanes.AddByPlaneAndOffset(oWorkPlanes(3), 10, True)
	' Create sketch
	Dim oMySketch As PlanarSketch = oCompDef.Sketches.Add(oMyWorkplane)
	Dim firstRectPoint As Point2d = oTrans.CreatePoint2d(0, 0)
	Dim secondRectPoint As Point2d = oTrans.CreatePoint2d(1, 1)
	oMySketch.SketchLines.AddAsTwoPointRectangle(firstRectPoint, secondRectPoint)
	Dim oMyProfile As Profile = oMySketch.Profiles.AddForSolid
	' Create the block
	Dim oExtrudeDef As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oMyProfile, kJoinOperation)
	Dim oExtrude As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
	'oPartDoc.SaveAs(argStrFilename, True)
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

J-Camper
Advisor
Advisor

I'm not getting any weird results in 2023, haven't upgraded yet.  Is it at an angle to the face, or is it at an angle in relation to the right side being up?

If at an angle to the front face, try setting ViewOrientationType to Front then only change the up vector:

Sub SetCamera()
	' Get the active camera.
	Dim cam As Camera = ThisApplication.ActiveView.Camera
	Dim tg As TransientGeometry = ThisApplication.TransientGeometry
	cam.ViewOrientationType = ViewOrientationTypeEnum.kFrontViewOrientation
	cam.UpVector = tg.CreateUnitVector(1, 0, 0)
	cam.ApplyWithoutTransition
End Sub

 

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor

Hi @J-Camper.  Thanks for replying and looking into this.

After I run that last rule I posted above, a new part is created, a new rectangular prism solid body is created, then it sets the camera.

In that new part, the view is:

  • looking at the model's origin, as planned
  • looking at it from the positive Z position, as planned
  • but the X-axis is not pointing straight up towards the top of the screen (up vector spec), but is pointing about 30 degrees towards the upper left of the screen from top center.

It is looking straight (flatly) at the 'Front' face of the view cube (for me anyways), but the view cube, but the top of the word 'Front' within the view cube is primarily towards the left of the screen, but angled about 30 degrees.  If I had used the ViewOrientationTypeEnum.kFrontViewOrientation setting, that would have rotated the view around 120 degrees to the right, which would not have worked out the way we wanted.  But I know that we can all have our view cube's orientation set-up differently, so what I'm saying here may not be universally true for everyone.  Mine has the Positive Z axis pointing the same direction as the Front face of the view cube is pointing ; positive Y axis is pointing same direction as the Top face of the view cube ; and X axis is pointing same direction as the Right face of the view cube...just as a reference.

I'm not sure why the resulting view seemed to be abound 30 degrees off from what was specified in the code.  I even tried adding those two 'update' lines at the end of the CreateBlock method, but it did not seem to have any effect.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Ah...I just fixed it.  It seems like it is all due to the 'order of operations', because all I had to do to fix it was:

change this:

Sub SetCamera()
	' Get the active camera.
	Dim cam As Camera = ThisApplication.ActiveView.Camera
	Dim tg As TransientGeometry = ThisApplication.TransientGeometry
	Dim v As UnitVector = cam.UpVector
	cam.UpVector = tg.CreateUnitVector(1, 0, 0)
	cam.Eye = tg.CreatePoint(0, 0, 1)
	cam.Target = tg.CreatePoint(0, 0, 0)
	cam.ApplyWithoutTransition
End Sub

to this:

Sub SetCamera()
	' Get the active camera.
	Dim cam As Camera = ThisApplication.ActiveView.Camera
	Dim tg As TransientGeometry = ThisApplication.TransientGeometry
	cam.Target = tg.CreatePoint(0, 0, 0)
	cam.Eye = tg.CreatePoint(0, 0, 1)
	cam.UpVector = tg.CreateUnitVector(1, 0, 0)
	cam.ApplyWithoutTransition()
End Sub

...so, I got rid of the:

Dim v As UnitVector = cam.UpVector

line of code (which was not doing anything anyways), then rearranged the other 3 lines of code to specify the Target, then Eye, then Up Vector, in that order.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

c_heineke
Contributor
Contributor

Your code fixes the problem from the script. However i have several other examples here with the same problem where your fix has no effect. These examples are more complex and i cannot provide a script for their creation.

0 Likes