﻿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.Loops loops = null;
            SolidEdgeGeometry.Loop loop = null;
            SolidEdgeGeometry.EdgeUses edgeUses = null;
            SolidEdgeGeometry.EdgeUse edgeUse = null;
            SolidEdgeGeometry.EdgeUse previousEdgeUse = 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.igQueryAll;
                    faces = (SolidEdgeGeometry.Faces)body.Faces[FaceType];

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

                        loops = (SolidEdgeGeometry.Loops)face.Loops;

                        for (int j = 1; j <= loops.Count; j++)
                        {
                            loop = (SolidEdgeGeometry.Loop)loops.Item(j);
                            edgeUses = (SolidEdgeGeometry.EdgeUses)loop.EdgeUses;

                            for (int k = 1; k <= edgeUses.Count; k++)
                            {
                                edgeUse = (SolidEdgeGeometry.EdgeUse)edgeUses.Item(k);

                                var MethodsParams = Array.CreateInstance(typeof(double), 1);
                                var FirstDerv = Array.CreateInstance(typeof(double), 0);
                                var SecondDerv = Array.CreateInstance(typeof(double), 0);
                                var ThirdDerv = Array.CreateInstance(typeof(double), 0);

                                MethodsParams.SetValue(0, 0);

                                edgeUse.GetDerivatives(MethodsParams.Length, ref MethodsParams, ref FirstDerv, ref SecondDerv, ref ThirdDerv);
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                OleMessageFilter.Unregister();
            }
        }
    }
}