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: 

Reset and Define Angle of Drawing View

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
C_Haines_ENG
134 Views, 2 Replies

Reset and Define Angle of Drawing View

Afternoon everyone!

 

I'm trying to find the code to rotate a drawing view and while I have found this snippet:

 

Dim oApp As Application
oApp  = ThisApplication

Dim oActiveSheet As Sheet
oActiveSheet = oApp.ActiveDocument.Activesheet

Dim oBaseView As DrawingView
oBaseView = oActiveSheet.DrawingViews.Item(1)

'rotate by 45 degrees (converted from Radians)
'True = Clockwise
oBaseView.RotateByAngle(45*(PI / 180), True)

And while this does rotate the view, it cant rotate the view "Up" or "Down".

 

Does anyone know how to rotate a view on all different axis?

2 REPLIES 2
Message 2 of 3
J-Camper
in reply to: C_Haines_ENG

@C_Haines_ENG,

When you say rotate Up/Down, it sounds like you actually want to rotate the Camera of the view, not the 2D View.  If this is correct, then something like this would work:

Sub Main
	
	Dim PickView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a base view")
	If IsNothing(PickView) Then Exit Sub ' If nothing gets selected then we're done
		
	If PickView.ParentView IsNot Nothing Then Logger.Debug("Can only change the view cube of a base view") : Exit Sub
	
	Dim ModelDoc As Document = PickView.ReferencedDocumentDescriptor.ReferencedDocument 
	If ModelDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And ModelDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Logger.Debug("Drawing view is not for a Part or assembly") : Exit Sub
	
	Dim RotationAngle As Double = 45 'in degress [negative for opposite rotation
	RotationAngle *= (Math.PI / 180) 'convert to radians
	
	Dim RotationFromWorkAxis As WorkAxis = ModelDoc.ComponentDefinition.WorkAxes.Item(1) '1 = X axis, 2 = Y axis, 3 = Z axis
	Dim RotationAxis As Vector = RotationFromWorkAxis.Line.Direction.AsVector 'Direction is a unitVector, so the Vector will have a length of 1, but that doesn't matter
	Dim ViewCam As Camera = PickView.Camera
	
	RotateCam(ViewCam, RotationAngle, RotationAxis, ViewCam.Target, ViewCam.UpVector)

End Sub

Sub RotateCam(Camera As Camera, ByVal offsetRad As Double, rotAxis As Vector, center As Point, upVector As UnitVector)
    Dim Matrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
    Call Matrix.SetToRotation(offsetRad, rotAxis, center)
    Dim newEye As Point = Camera.Eye
    Call newEye.TransformBy(Matrix)
    Camera.Eye = newEye
    Camera.UpVector = upVector
    Call Camera.ApplyWithoutTransition
End Sub

 

Let me know if you have any questions.

Message 3 of 3
J-Camper
in reply to: J-Camper

I decided to go a little further than a simple example:

Sub Main
	'View Selection For Testing
	Dim PickView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a base view")
	If IsNothing(PickView) Then Exit Sub ' If nothing gets selected then we're done
	If PickView.ParentView IsNot Nothing Then Logger.Debug("Can only change the view cube of a base view") : Exit Sub
	
	'Setup
	Dim ViewCam As Camera = PickView.Camera
	Dim TargetVector As Vector = ViewCam.Eye.VectorTo(ViewCam.Target)
	TargetVector.Normalize()
	Dim CamUpVector As Vector = ViewCam.UpVector.AsVector
	CamUpVector.Normalize()
	Dim UpPoint As Point = ViewCam.Eye.Copy
	UpPoint.TranslateBy(CamUpVector)
	Dim EyeTargetUpPlane As Plane = ThisApplication.TransientGeometry.CreatePlaneByThreePoints(ViewCam.Eye,  ViewCam.Target, UpPoint)
	Dim OtherVector As Vector = EyeTargetUpPlane.Normal.AsVector
	OtherVector.Normalize
	Dim RotationMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
	Dim newEye As Point = ViewCam.Eye.Copy
	
	'User input for rotation
	'Angle
	Dim UserInputRotationAngle As String = InputBox("How much to rotate? [Degrees expected]", "Rotation Angle", "45") 'in degress [negative for opposite rotation
	Dim RotationAngle As Double = 0
	Try
		RotationAngle = Val(UserInputRotationAngle)
	Catch
		Logger.Debug("Could not convert the string: """ & UserInputRotationAngle & """ to a double")
		Exit Sub
	End Try
	If RotationAngle = 0 Then Logger.Debug("0 was input, so no rotation needed.") : Exit Sub
	RotationAngle *= (Math.PI / 180) 'convert to radians
	'Direction
	Dim OptionsList As New List(Of String)
	OptionsList.AddRange({"Up", "Down", "Left", "Right", "Clockwise", "Counter-Clockwise" })
	Dim UserInput As String = InputListBox("Which direction should the view rotate?", OptionsList, OptionsList.Item(0), "Rotation Direction")
	Select Case UserInput
		Case OptionsList.Item(0)
			OtherVector.ScaleBy(-1) 'Invert axis for rotation
			Call RotationMatrix.SetToRotation(RotationAngle, OtherVector, ViewCam.Target)
			Call newEye.TransformBy(RotationMatrix)
		Case OptionsList.Item(1)
			Call RotationMatrix.SetToRotation(RotationAngle, OtherVector, ViewCam.Target)
			Call newEye.TransformBy(RotationMatrix)
		Case OptionsList.Item(2)
			Call RotationMatrix.SetToRotation(RotationAngle, CamUpVector, ViewCam.Target)
			Call newEye.TransformBy(RotationMatrix)
		Case OptionsList.Item(3)
			CamUpVector.ScaleBy(-1)'Invert axis for rotation
			Call RotationMatrix.SetToRotation(RotationAngle, CamUpVector, ViewCam.Target)
			CamUpVector.ScaleBy(-1)'revert axis after rotation
			Call newEye.TransformBy(RotationMatrix)
		Case OptionsList.Item(4)
			TargetVector.ScaleBy(-1)'Invert axis for rotation
			Call RotationMatrix.SetToRotation(RotationAngle, TargetVector, ViewCam.Target)
			Call UpPoint.TransformBy(RotationMatrix)
			CamUpVector = newEye.VectorTo(UpPoint)
		Case OptionsList.Item(5)
			Call RotationMatrix.SetToRotation(RotationAngle, TargetVector, ViewCam.Target)
			Call UpPoint.TransformBy(RotationMatrix)
			CamUpVector = newEye.VectorTo(UpPoint)
		Case Else
			Logger.Debug("Un-handled option: " & UserInput)
			Exit Sub
	End Select
	
	ViewCam.Eye = newEye
	ViewCam.UpVector = CamUpVector.AsUnitVector
	Call ViewCam.ApplyWithoutTransition
	
End Sub

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report