By Adam Nagy
Wrote a few articles on drawing views already, but they do not seem to be enough :)
http://adndevblog.typepad.com/manufacturing/2014/11/rotate-drawing-view-around-x-axis-of-sheet.html
http://adndevblog.typepad.com/manufacturing/2014/11/rotate-camera-around-any-axis-any-angle.html
http://adndevblog.typepad.com/manufacturing/2014/11/drawing-coordinate-systems-and-camera.html
So, now here is one on aligning the view to show a given face. To make this work directly in a drawing, where only edges can be selected, we'll let the user select two drawing curve segments. From those we'll find the corresponding model edges, and then we'll find the common face of those edges. Using the Face's normal we'll set the camera's view direction, and using the second selected Edge's direction we'll set the UpVector of the view.
Note that the below code only works with planar faces and linear edges, and the direction of the edge used to set the UpVector may be opposite to what you'd like in which case the view will be upside-down.
Function GetFace(edges() As Edge) As Face
Dim i As Integer
Dim j As Integer
For i = 1 To 2
For j = 1 To 2
If edges(0).Faces(i) Is edges(1).Faces(j) Then
Set GetFace = edges(0).Faces(i)
Exit Function
End If
Next
Next
End Function
Sub SetViewDirection()
Dim doc As DrawingDocument
Set doc = ThisApplication.ActiveDocument
Dim edges(1) As Edge
Set edges(0) = doc.SelectSet(1).Parent.ModelGeometry
Set edges(1) = doc.SelectSet(2).Parent.ModelGeometry
Dim f As Face
Set f = GetFace(edges)
Dim p As Plane
Set p = f.Geometry
Dim dv As DrawingView
Set dv = doc.SelectSet(1).Parent.Parent
Dim c As Camera
Set c = dv.Camera
' Set Eye or Target
Dim pt As Point
If f.IsParamReversed Then
Set pt = c.Eye.Copy
Call pt.TranslateBy(p.Normal.AsVector())
c.Target = pt
Else
Set pt = c.Target.Copy
Call pt.TranslateBy(p.Normal.AsVector())
c.Eye = pt
End If' Set UpVector
Dim ls As LineSegment
Set ls = edges(1).Geometry
c.UpVector = ls.Direction
Call c.ApplyWithoutTransition
End SubHere is the result: