BuildingCoder.CmdDimensionWallsIterateFaces.GetClosestFace C# (CSharp) Method

GetClosestFace() static private method

Return the closest planar face to a given point p on the element e with a given normal vector.
static private GetClosestFace ( Element e, XYZ p, XYZ normal, Options opt ) : Face
e Element
p XYZ
normal XYZ
opt Options
return Face
        static Face GetClosestFace(
            Element e,
            XYZ p,
            XYZ normal,
            Options opt)
        {
            Face face = null;
              double min_distance = double.MaxValue;
              GeometryElement geo = e.get_Geometry( opt );

              //GeometryObjectArray objects = geo.Objects; // 2012
              //foreach( GeometryObject obj in objects ) // 2012

              foreach( GeometryObject obj in geo ) // 2013
              {
            Solid solid = obj as Solid;
            if( solid != null )
            {
              FaceArray fa = solid.Faces;
              foreach( Face f in fa )
              {
            PlanarFace pf = f as PlanarFace;

            Debug.Assert( null != pf,
              "expected planar wall faces" );

            if( null != pf
              //&& normal.IsAlmostEqualTo( pf.Normal )
              && Util.IsParallel( normal, pf.FaceNormal ) )
            {
              //XYZ q = pf.Project( p ).XYZPoint; // Project returned null once
              //double d = q.DistanceTo( p );

              XYZ v = p - pf.Origin;
              double d = v.DotProduct( -pf.FaceNormal );
              if( d < min_distance )
              {
                face = f;
                min_distance = d;
              }
            }
              }
            }
              }
              return face;
        }