shaju sakir

shaju sakir

  • NA
  • 3
  • 7.7k

Bringing autocad events in C#

Mar 1 2011 6:59 AM
Hi All,
            In my C# windows application ,I am using autocad plugin..and trying to track autocad events in my .Net Application and succeeded to track the document activated and document destroyed events...and now i need to track the autocad save as dialoge box save event in my .Net application...for example [ if one is saving a file in autocad ...then it will display "saved"  message box , that is from my .Net application]
       anybody could help me.....my current code look like this



using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using System.Data.SqlClient;

using System.IO;

using System.Data;

using System;

using System.Collections.Generic;

using System.Reflection;

using System.Resources;

using Microsoft.Win32;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Windows;

 

// This line is not mandatory, but improves loading performances

[assembly: ExtensionApplication(typeof(AutoCad_Monitor.MyPlugin))]

 

namespace AutoCad_Monitor

{

 

 

    public class MyPlugin : IExtensionApplication

    {

        //Db Class

        SqlConnection con;

        SqlCommand cmd;

        string _documentName;

        int _employeerID;

        string[] _arrpjct;//=new string[];

        static string _documentOpenTime;

        // bool flag=false;

        static int val = 0;

        void IExtensionApplication.Initialize()

        {

 

            RegisterForDemandLoading();

 

            #region test1

 

            _documentName = Application.DocumentManager.MdiActiveDocument.Name;

            _documentOpenTime = System.DateTime.Now.ToShortTimeString();

 

 

            //Events

 

            Application.DocumentManager.DocumentToBeDestroyed += new DocumentCollectionEventHandler(DocumentManager_DocumentToBeDestroyed);

            Application.DocumentManager.DocumentActivated += new DocumentCollectionEventHandler(DocumentManager_DocumentActivated);

            //Application.DocumentManager.

            //DataBase

            try

            {

                //int _employeerID = 100;

                System.Windows.MessageBoxResult msresult = System.Windows.MessageBox.Show("Is it References", "References", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Question);

                if (msresult == System.Windows.MessageBoxResult.Yes)

                {

                    Application.DocumentManager.DocumentToBeDestroyed -= new DocumentCollectionEventHandler(DocumentManager_DocumentToBeDestroyed);

                }

                else

                {

                    _documentName = Application.DocumentManager.MdiActiveDocument.Name;                   

                    _arrpjct = _documentName.Split('\\');

                    _documentName = _arrpjct[_arrpjct.Length - 2].ToString();

                    _employeerID = GetEmpId();                   

                    //NewFile(_employeerID, _documentName);

 

                }

            }

            catch (System.Exception)

            {

 

            }

            #endregion

        }

 

 

 

 

 

        #region test

 

        public int GetEmpId()

        {

            RegistryKey key = Registry.CurrentUser.OpenSubKey("EmpId");

            string str = key.GetValue("EmpId").ToString();

            return Convert.ToInt32(str);

        }

 

        void DocumentManager_DocumentActivated(object sender, DocumentCollectionEventArgs e)

        {

            try

            {

                if (val == 0)

                {

 

 

                    //if (flag == false)

                    {

                        //int _employeerID = 100;

                        System.Windows.MessageBoxResult msresult = System.Windows.MessageBox.Show("Is it References", "References", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Question);

                        if (msresult == System.Windows.MessageBoxResult.Yes)

                        {

                            Application.DocumentManager.DocumentToBeDestroyed -= new DocumentCollectionEventHandler(DocumentManager_DocumentToBeDestroyed);

                        }

                        else

                        {

                            _documentName = Application.DocumentManager.MdiActiveDocument.Name;

                            _arrpjct = _documentName.Split('\\');

                            _documentName = _arrpjct[_arrpjct.Length - 2].ToString();

                            _employeerID = GetEmpId();

                            NewFile(_employeerID, _documentName);

 

                        }

                        //flag = true;

 

                    }


                }

                else

                {

                    val = 0;

                    // flag = false;

                }

 

 

            }

            catch (System.Exception)

            {

 

            }

 

 

 

        }

 

 

        void NewFile(int _Empid, string _filename)

        {

            try

            {

                con = new SqlConnection(@"Data Source=AQLANZA-501;Initial Catalog=AutoCad_Monitor;User ID=sa;Password=sasa");

                cmd = new SqlCommand();

                cmd.Connection = con;

 

                cmd.CommandText = "sp_fileExsists";

                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();

                cmd.Parameters.AddWithValue("@EmpID", _Empid);

                cmd.Parameters.AddWithValue("@DocName", _filename);

                //cmd.Parameters.AddWithValue("@DocStartTime", opentime);

                cmd.ExecuteNonQuery();

            }

            catch (System.Exception)

            {

 

            }

 

            finally

            {

                con.Close();

                cmd.Dispose();

                con.Dispose();

            }

 

        }

        void CloseFile(int _Empid, string _filename)

        {

            try

            {

                con = new SqlConnection(@"Data Source=AQLANZA-501;Initial Catalog=AutoCad_Monitor;User ID=sa;Password=sasa");

                cmd = new SqlCommand();

                cmd.Connection = con;

 

                cmd.CommandText = "sp_fileClose";

                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();

                cmd.Parameters.AddWithValue("@EmpID", _Empid);

                cmd.Parameters.AddWithValue("@DocName", _filename);

                //cmd.Parameters.AddWithValue("@DocStartTime", opentime);

                cmd.ExecuteNonQuery();

            }

            catch (System.Exception)

            {

 

            }

 

            finally

            {

                con.Close();

                cmd.Dispose();

                con.Dispose();

            }

 

        }

 

 

 

        void DocumentManager_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)

        {

 

            val += 1;

            int IempId = GetEmpId();

            CloseFile(IempId, _documentName);

 

        }

 

 

 

 

 

 

        void IExtensionApplication.Terminate()

        {

 

        }

        #endregion

 

    }

 

}

 



Answers (1)