//--- #New: 18.12.2019/J.Fes
//--- #Doc: code Example to use CallBacks during NX part processes
//--- #Doc: This is an example and must be adapted regarding your own need
//--- #Doc: usage on your own risk

--------------------------------------------------------------------------------
using System;
using NXOpen;
using NXOpen.UF;

namespace AnalyseRefSet
{
    public class AnalyseRefSet
    {
        // class members
        private static Session _theSession;
        private static UFSession _ufSession;
        public static AnalyseRefSet _theAnalyseRefSet;
        public static bool _isDisposeCalled;
        private static BasePart _workPart = null;
        private static int _returnCode = 0;
        static int[] callBackID = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        //------------------------------------------------------------------------------
        // Constructor
        //------------------------------------------------------------------------------
        public AnalyseRefSet()
        {
            try
            {
                _theSession = Session.GetSession();
                _ufSession = UFSession.GetUFSession();
                _isDisposeCalled = false;
            }
            catch (NXOpen.NXException ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
            }
        }

        public static int Main(string[] _args)
        {
            _theAnalyseRefSet = new AnalyseRefSet();
            return _returnCode;
        }

        public static int Startup()
        {
            try
            {
                _theAnalyseRefSet = new AnalyseRefSet();

                // Callback definieren
                //callBackID[0] = _theSession.Parts.AddPartOpenedHandler(new PartCollection.PartOpenedHandler(CallBackPartOpened));
                //callBackID[1] = _theSession.Parts.AddPartClosedHandler(new PartCollection.PartClosedHandler(CallBackPartClosed));
                //callBackID[2] = _theSession.Parts.AddPartModifiedHandler(new PartCollection.PartModifiedHandler(CallBackPartModified));
                //callBackID[3] = _theSession.Parts.AddPartCreatedHandler(new PartCollection.PartCreatedHandler(CallBackPartCreated));
                callBackID[4] = _theSession.Parts.AddWorkPartChangedHandler(new PartCollection.WorkPartChangedHandler(cbPartChangeWorkPart));
                //callBackID[5] = _theSession.Parts.AddPartRenamedHandler(new PartCollection.PartRenamedHandler(CallBackPartRename));
                //callBackID[6] = _theSession.Parts.AddPartSavedHandler(new PartCollection.PartSavedHandler(CallBackPartSave));
                //callBackID[7] = _theSession.Parts.AddPartSavedAsHandler(new PartCollection.PartSavedAsHandler(CallBackPartSaveAs));

                _theAnalyseRefSet.Dispose();
            }
            catch (NXOpen.NXException ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                _returnCode = 999;
            }
            return _returnCode;
        }

        public static void cbPartChangeWorkPart(BasePart cPart)
        {
            try
            {
                // Do some thing
            }
            catch (NXOpen.NXException ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                _returnCode = 999;
            }
        }

        //------------------------------------------------------------------------------
        // Following method disposes all the class members
        //------------------------------------------------------------------------------
        public void Dispose()
        {
            try
            {
                if (_isDisposeCalled == false)
                {
                    //TODO: Add your application code here
                }
                _isDisposeCalled = true;
            }
            catch (NXOpen.NXException ex)
            {
                // ---- Enter your exception handling code here -----

            }
        }

    }
}
