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
_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)
if (val == 0)
//if (flag == false)
NewFile(_employeerID, _documentName);
//flag = true;
val = 0;
// flag = false;
void NewFile(int _Empid, string _filename)
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();
finally
con.Close();
cmd.Dispose();
con.Dispose();
void CloseFile(int _Empid, string _filename)
cmd.CommandText = "sp_fileClose";
void DocumentManager_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
val += 1;
int IempId = GetEmpId();
CloseFile(IempId, _documentName);
void IExtensionApplication.Terminate()