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

public class Form1
{

    private void Button1_Click(object sender, EventArgs e)
    {
        SolidEdgeFramework.Application objApp = null;
        Type objType = null;
        SolidEdgeFramework.MatTable objMatTable = null;
        string strLibrary = "Materials";
        SolidEdgePart.PartDocument objDocument = null;
        string strMaterial = "Copper";
        SolidEdgeConstants.MatTablePropIndexConstants iMatPropIndex = null;
        object varGetPropVal = null;
        string strMessage = null;

        try
        {
            // Get SE handle
            objApp = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");

            if (objApp == null)
            {
                // Get the type from the Solid Edge ProgID
                objType = Type.GetTypeFromProgID("SolidEdge.Application");
                // Start Solid Edge
                objApp = Activator.CreateInstance(objType);
                // Make Solid Edge visible
                objApp.Visible = true;
            }
            objDocument = objApp.Documents.Add("SolidEdge.PartDocument");

            // Get material table object handle
            objMatTable = objApp.GetMaterialTable();

            // Set active document handle
            objMatTable.SetActiveDocument(objDocument);
            strMaterial = "ABS Plastic, high impact";
            objMatTable.ApplyMaterialToDoc(objDocument, strMaterial, strLibrary);

            iMatPropIndex = SolidEdgeConstants.MatTablePropIndexConstants.seMaterialName;
            objMatTable.GetMaterialPropValueFromDoc(objDocument, iMatPropIndex, varGetPropVal);
            strMessage = string.Format("Material Name = {0}", varGetPropVal.ToString());
            MessageBox.Show(strMessage);

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

    }


    public Form1()
    {
    	SubscribeToEvents();
    }

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

		Button1.Click += Button1_Click;
	}

}