﻿using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

public class Form1
{

    private void Button1_Click(object sender, EventArgs e)
    {
        DirectoryInfo objFolder = null;
        FileInfo objFileInfo = null;
        SolidEdgeFramework.Application objApplication = null;
        SolidEdgeFramework.Documents objDocuments = null;
        SolidEdgePart.SheetMetalDocument objSheetMetalDocument = null;
        SolidEdgePart.Sketch objSketch = null;

        SolidEdgePart.Profile objProfile = null;
        SolidEdgePart.Profiles objProfCollection = null;
        SolidEdgePart.Profile[] objProfArray = new SolidEdgePart.Profile[3];
        int i = 0;
        SolidEdgePart.NormalCutout objNormCutOut = null;
        SolidEdgePart.Model objModel = null;
        try
        {

            objFolder = GetTrainingFolder();
            objFileInfo = new FileInfo(Path.Combine(objFolder.FullName, "Foot1.psm"));
            // Start Solid Edge
            objApplication = Activator.CreateInstance(Type.GetTypeFromProgID("SolidEdge.Application"));
            //Get running instance of Solid Edge
            //objApplication = Marshal.GetActiveObject("SolidEdge.Application")
            objApplication.Visible = true;
            objDocuments = objApplication.Documents;

            objSheetMetalDocument = objDocuments.Open(objFileInfo.FullName);
            objModel = objSheetMetalDocument.Models.Item(1);

            // Add a sketch & profile 
            objSketch = objSheetMetalDocument.Sketches.Add;
            objProfile = objSketch.Profiles.Add(objSheetMetalDocument.RefPlanes.Item(1));
            objProfile.Circles2d.AddByCenterRadius(x:0.01, y:0.05, Radius:0.05);
            objProfile.Visible = false;
            objProfCollection = objProfile.Parent.Profiles;
            for (i = 0; i <= objProfCollection.Count; i++)
            {
                objProfArray[i] = objProfCollection(i);
            }

            objNormCutOut = objModel.NormalCutouts.AddThroughAllMulti(1, objProfArray, SolidEdgePart.FeaturePropertyConstants.igLeft, SolidEdgePart.FeaturePropertyConstants.igSMFaceCutout);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {

        }

    }
    public DirectoryInfo GetTrainingFolder()
    {
        SEInstallDataLib.SEInstallData objInstallData = null;
        DirectoryInfo objInstallFolder = null;
        DirectoryInfo objTrainingFolder = null;

        try
        {
            objInstallData = new SEInstallDataLib.SEInstallData();
            objInstallFolder = new DirectoryInfo(objInstallData.GetInstalledPath());
            objTrainingFolder = new DirectoryInfo(Path.Combine(objInstallFolder.Parent.FullName, "Training"));
        }
        catch
        {
        }
        finally
        {
            if (objInstallData != null)
            {
                Marshal.FinalReleaseComObject(objInstallData);
                objInstallData = null;
            }
        }

        return objTrainingFolder;
    }


    public Form1()
    {
    	SubscribeToEvents();
    }

	private bool EventsSubscribed = false;
	private void SubscribeToEvents()
	{
		if (EventsSubscribed)
			return;
		else
			EventsSubscribed = true;

		Button1.Click += Button1_Click;
	}

}