here is my code that gets it closer without selecting a face, now i wish that someone can help me get it all the way:
Sub Main()
' iLogic rule to set view orientation to the closest face normal in a part or assembly document
Dim oDoc As Document = ThisDoc.Document
Dim oCompDef As ComponentDefinition
Dim oCamera As Camera = ThisApplication.ActiveView.Camera
' Determine the document type and get the ComponentDefinition
If TypeOf oDoc Is PartDocument Then
oCompDef = oDoc.ComponentDefinition
ElseIf TypeOf oDoc Is AssemblyDocument Then
oCompDef = oDoc.ComponentDefinition
Else
MsgBox("This script only works in Part or Assembly environment.")
Exit Sub
End If
Dim maxDotProduct As Double = -1
Dim closestFace As Face = Nothing
' Get the current view direction
Dim currentViewVector As UnitVector = oCamera.Eye.VectorTo(oCamera.Target).AsUnitVector
' Collect faces
Dim allFaces As New List(Of Face)()
If TypeOf oCompDef Is PartComponentDefinition Then
For Each oFace As Face In oCompDef.SurfaceBodies(1).Faces
allFaces.Add(oFace)
Next
ElseIf TypeOf oCompDef Is AssemblyComponentDefinition Then
allFaces.AddRange(GetAllFaces(oCompDef))
Else
MsgBox("Unsupported ComponentDefinition.")
Exit Sub
End If
For Each oFace As Face In allFaces
' Get a point on the face
Dim pointOnFace As Point = oFace.PointOnFace
' Get the normal vector of the face at the point
Dim pointCoords(2) As Double
pointCoords(0) = pointOnFace.X
pointCoords(1) = pointOnFace.Y
pointCoords(2) = pointOnFace.Z
Dim normals(2) As Double
Call oFace.Evaluator.GetNormalAtPoint(pointCoords, normals)
' Create a UnitVector from the normals array
Dim faceNormal As UnitVector = ThisApplication.TransientGeometry.CreateUnitVector(normals(0), normals(1), normals(2))
' Calculate the dot product between the face normal and the current view direction
Dim dotProduct As Double = Abs(currentViewVector.DotProduct(faceNormal))
' Find the face with the largest dot product (closest to being perpendicular to the view direction)
If dotProduct > maxDotProduct Then
maxDotProduct = dotProduct
closestFace = oFace
End If
Next
If Not closestFace Is Nothing Then
' Get a point on the closest face
Dim closestPointOnFace As Point = closestFace.PointOnFace
' Get the normal vector of the closest face at the point
Dim closestPointCoords(2) As Double
closestPointCoords(0) = closestPointOnFace.X
closestPointCoords(1) = closestPointOnFace.Y
closestPointCoords(2) = closestPointOnFace.Z
Dim closestNormals(2) As Double
Call closestFace.Evaluator.GetNormalAtPoint(closestPointCoords, closestNormals)
' Create a UnitVector from the closestNormals array
Dim closestFaceNormal As UnitVector = ThisApplication.TransientGeometry.CreateUnitVector(closestNormals(0), closestNormals(1), closestNormals(2))
' Set the camera to look along the closest face normal
oCamera.Eye = ThisApplication.TransientGeometry.CreatePoint(closestPointOnFace.X - 100 * closestFaceNormal.X, closestPointOnFace.Y - 100 * closestFaceNormal.Y, closestPointOnFace.Z - 100 * closestFaceNormal.Z)
oCamera.Target = closestPointOnFace
oCamera.UpVector = ThisApplication.TransientGeometry.CreateUnitVector(0, 1, 0) ' Y-axis as the up direction
oCamera.Fit
' Refresh the view
oCamera.Apply()
ThisApplication.ActiveView.Update()
Else
MsgBox("No faces found.")
End If
End Sub
' Function to get all faces in an assembly
Function GetAllFaces(oCompDef As AssemblyComponentDefinition) As List(Of Face)
Dim allFaces As New List(Of Face)()
' Iterate through all occurrences in the assembly
For Each oOccurrence As ComponentOccurrence In oCompDef.Occurrences
Dim occurrenceCompDef As ComponentDefinition = oOccurrence.Definition
For Each oSurfaceBody As SurfaceBody In occurrenceCompDef.SurfaceBodies
For Each oFace As Face In oSurfaceBody.Faces
allFaces.Add(oFace)
Next
Next
Next
Return allFaces
End Function