﻿using System;
using System.Runtime.InteropServices;

namespace Examples
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            SolidEdgeFramework.Application application = null;
            SolidEdgePart.PartDocument partDocument = null;
            SolidEdgePart.Models models = null;
            SolidEdgePart.Model model = null;
            SolidEdgeGeometry.Body body = null;
            SolidEdgeGeometry.Faces faces = null;
            SolidEdgeGeometry.Face face = null;
            SolidEdgeGeometry.BSplineSurface bSplineSurface = null;

            try
            {
                // See "Handling 'Application is Busy' and 'Call was Rejected By Callee' errors" topic.
                OleMessageFilter.Register();

                // Attempt to connect to a running instance of Solid Edge.
                application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");
                partDocument = application.ActiveDocument as SolidEdgePart.PartDocument;

                if (partDocument != null)
                {
                    models = partDocument.Models;
                    model = models.Item(1);
                    body = (SolidEdgeGeometry.Body)model.Body;

                    var FaceType = SolidEdgeGeometry.FeatureTopologyQueryTypeConstants.igQuerySpline;
                    faces = (SolidEdgeGeometry.Faces)body.Faces[FaceType];

                    for (int i = 1; i <= faces.Count; i++)
                    {
                        face = (SolidEdgeGeometry.Face)faces.Item(i);
                        bSplineSurface = face.Geometry as SolidEdgeGeometry.BSplineSurface;

                        if (bSplineSurface != null)
                        {
                            var Order = Array.CreateInstance(typeof(int), 0);

                            bSplineSurface.GetOrder(ref Order);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                OleMessageFilter.Unregister();
            }
        }
    }
}