
Re: How to orient the view normal to a plane defined by 3 points and then zoom to fit.
Michael Spens Mar 7, 2016 1:55 PM (in response to Brad Phelan)Hi Brad,
You have two options, easy or hard. If you create a plane through the three points in the first place, the following code will do the job. Easy! Change your plane name in the SelectByID2 line if it isn't named "Plane1".
'
Set swApp = _
Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.ShowNamedView2 "*Normal To", 1
Part.ViewZoomToSelection
'
If you don't create the plane, and you're comfortable with direction vectors and math transforms, you could use the Orientation3 method of the ModelView interface. You can find some good starter code in the API help here.
2016 SOLIDWORKS API Help  Orientation3 Property (IModelView)

Re: How to orient the view normal to a plane defined by 3 points and then zoom to fit.
Brad Phelan Mar 8, 2016 7:18 AM (in response to Michael Spens)I was already aware that Orientation3 exists. However I'm not sure how to construct the view matrix to perform what I am asking. The documentation is also contradictive. It says you must use Orientation3 and Translation3 but Orientation3 is a matrix is a combined rotation and translation matrix so why would you need to do both.
The presented code is not "good starter code". It gives very little justification for what it does. For example what is "dPoint". Is it a point the camera is looking at or is the location of the camera or is it something else?
"swVectorT" is never assigned a value and it is unclear what it's purpose is. Again. Is it the direction the camera is looking or something else?

Re: How to orient the view normal to a plane defined by 3 points and then zoom to fit.
Brad Phelan Mar 8, 2016 7:17 AM (in response to Brad Phelan)I think this might be of assistance.
Camera Transformation and View Matrix
This was the code we used in the end. ( All helper extension methods can be found at GitHub  Weingartner/SolidworksAddinFramework))
using SolidworksAddinFramework;
using SolidWorks.Interop.sldworks;
namespace WeinCadSW.Macros.ToolAligner
{
/// <summary>
/// Provides the orientation matrix for IModelView::Orientation3. If you
/// have 3 points that define a plane and you wish to look at the center
/// of those three points in a direction normal to the plane then
/// use this.
/// </summary>
public class ViewOrientationFor3Points
{
public MathPoint P0 { get; set; }
public MathPoint P1 { get; set; }
public MathPoint P2 { get; set; }
/// <summary>
/// The center of the three points
/// </summary>
public MathPoint PCenter { get; }
/// <summary>
/// The normal to the plane defined by the three points
/// </summary>
public MathVector Normal { get; }
/// <summary>
/// Orientation matrix for IModelView::Orientation3
/// </summary>
public MathTransform Orientation { get; set; }
public ViewOrientationFor3Points(IMathUtility math, double[] p0, double[] p1, double[] p2)
{
P0 = math.Point(p0);
P1 = math.Point(p1);
P2 = math.Point(p2);
var pTool = p0;
var pCurve = p1;
var pAxis = p2;
var va = math.Vector(pTool, pCurve).Normalise();
var vb = math.Vector(pTool, pAxis).Normalise();
var w = ((MathVector)va.Cross(vb)).Normalise();
var u = math.Vector(new[] {1.0, 0, 0}).CrossTs(w);
var v = w.CrossTs(u);
var c = math.Vector(new[] { pTool, pCurve, pAxis }.Average());
var trans = math.ComposeTransform(u, v, w, c, 1);
Orientation = (MathTransform)trans.Inverse();
Normal = w;
PCenter = math.Point(new[] {p0, p1, p2}.Average());
}
}
}

