}
public void OnCADCmdB(object sender, EventArgs e)
{
MessageBox.Show(@"xam:" + e.ToString());
}
public void cmdWillStart(object o, CommandEventArgs e)
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
// if (e.GlobalCommandName == "MOVE") {
ed.WriteMessage("xam begin command:[name]" + e.GlobalCommandName + "\n");
// }
switch(e.GlobalCommandName){
case "PROPERTIES":
ed.WriteMessage("xam 禁止" + e.GlobalCommandName + o.ToString() + "\n");
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
break;
}
}
catch
{
ed.WriteMessage("CommandWillStart事件的处理函数错误");
}
}
public void objOpenedForMod(object o, ObjectEventArgs e)
{
ObjectId objId = e.DBObject.ObjectId;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
ed.WriteMessage(@"xam " + e.DBObject.ObjectId.ToString() + "\n");
}
catch
{
ed.WriteMessage("ObjectOpenedForModify事件的处理函数错误");
}
}
public void cmdEnded(object o, CommandEventArgs e)
{
//判断监控命令是否是激活的
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
ed.WriteMessage("xam end command: [name] " + e.GlobalCommandName + "\n");
}
catch
{
ed.WriteMessage("CommandWillStart事件的处理函数错误");
}
}
#endregion events
[CommandMethod("HelloNet")]
public void HelloNet(){
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("使用NET开发AutoCAD 程序00");
Autodesk.AutoCAD.Windows.StatusBar stb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.StatusBar;
stb.Panes.Clear();
}
[CommandMethod("AppendEvent")]
public void AppendEvent()
{
//MessageBox.Show(@"xam:");
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try{
Database db;
Document doc;
//
doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
db = HostApplicationServices.WorkingDatabase;
db.ObjectAppended += new ObjectEventHandler(OnCADCmd);
db.ObjectOpenedForModify += new ObjectEventHandler(objOpenedForMod);
doc.CommandWillStart += new CommandEventHandler(cmdWillStart);
doc.CommandEnded += new CommandEventHandler(cmdEnded);
//bCmdActive = false;
//bReposition = false;
ed.WriteMessage("xam:绑定事件成功");
}catch{
ed.WriteMessage("添加事件错误\n");
}
}
static List<string> lstAppName;//传入的扩展数据名列表
static bool m_DbClick = false;
static Document doc;
static Editor ed;
static AcadDocument acaddoc;
static ObjectId objSelId;
static double dist = 1;//选择点与选择实体的距离偏差
static frmTrace glbFrmTrace;
[CommandMethod("AppendEventB")]
public static void AppendEventB(List<string> lstAppNameIn){
try{
glbFrmTrace = new frmTrace();
glbFrmTrace.Show();
lstAppName = lstAppNameIn;
doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
ed = doc.Editor;
acaddoc = (AcadDocument)doc.AcadDocument;
acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentLockModeChanged += new DocumentLockModeChangedEventHandler(vetoCommand);
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated);
ed.SelectionAdded += new SelectionAddedEventHandler(ed_SelectionAdded);
}catch{
}
}
[CommandMethod("AppendEventC")]//鼠标悬浮提示
public static void AppendEventC(List<string> lstAppNameIn) {
try {
doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
ed = doc.Editor;
ed.PointMonitor += new PointMonitorEventHandler(ed_PointMonitor);
} catch {
}
}
//---------------------------------------------
static void ed_PointMonitor(object sender, PointMonitorEventArgs e) {
//拾取当前实体对象提交
FullSubentityPath[] fsp = e.Context.GetPickedEntities();
if (fsp.Length > 0) {
// 显示实体ID
// doc.Editor.WriteMessage(fsp[0].GetObjectIds()[0].ToString());
doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
ed = doc.Editor;
ed.WriteMessage("ed_PointMonitor");
e.AppendToolTipText("敢创:id=" + fsp[0].GetObjectIds()[0].ToString());
DBObject obj = null;
using (Transaction transaction = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
obj = transaction.GetObject(fsp[0].GetObjectIds()[0], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, true);
transaction.Commit();
}
glbFrmTrace.txtHint.Text += "type : = "+obj.GetType().ToString() + "\n";
//if(obj.GetType().ToString()==){
// Line objLine = (Line)obj;
// glbFrmTrace.txtHint.Text += "isline" + "\n";
//}else{
// glbFrmTrace.txtHint.Text += "not line" + "\n";
//}
}
}
static void ed_SelectionAdded(object sender, SelectionAddedEventArgs e) {
glbFrmTrace.txtHint.Text += "ed_SelectionAdded \r\n";
//glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "ed_SelectionAdded \n");
try{
if (e.AddedObjects.Count == 1){
SelectedObject objSel = e.AddedObjects[0];
objSelId = objSel.ObjectId;
}
else
{
// objSelId = ObjectId.Null; //不能加上,否则就和不用此方法效果一样
}
}catch{
}
}
static void documentCreated(object sender, DocumentCollectionEventArgs e) {
glbFrmTrace.txtHint.Text += "documentCreated \r\n";
//glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "documentCreated \n");
try
{
doc = e.Document;
acaddoc = (AcadDocument)doc.AcadDocument;
acaddoc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(beginDoubleClick);
}
catch
{ }
}
static void beginDoubleClick(object PickPoint)
{
glbFrmTrace.txtHint.Text += "beginDoubleClick \r\n";
//glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "beginDoubleClick \n");
try
{
PromptSelectionResult res = ed.SelectImplied();
if (res.Status == PromptStatus.Error)//属性块双击不能选中的处理
{
if (objSelId != ObjectId.Null)
{
ShowDialog(objSelId, PickPoint, true);//true表示是上次选中的(在方法中判断是属性块时用)
objSelId = ObjectId.Null;
return;
}
}
else
{
SelectionSet SS = res.Value;
if (SS.GetObjectIds().Length == 1)
{
ObjectId oId = SS.GetObjectIds()[0];
ShowDialog(oId, PickPoint, false);
return;
}
}
m_DbClick = false;
}
catch
{
m_DbClick = false; //异常时保证面板等原来CAD自带的可用
}
}
/// <summary>
/// //实现自定义窗口的调用
/// </summary>
/// <param name="oId">选中的ObjectId</param>
/// <param name="ptPick">点取的点坐标</param>
/// <param name="isSeled">是否是上次选中的</param>
static void ShowDialog(ObjectId oId, object ptPick, bool isSeled) {
frmHint dlg = new frmHint();
dlg.ShowDialog();
m_DbClick = true;
try
{
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
string appNameUse="";
DBObject dbObj = tr.GetObject(oId, OpenMode.ForRead);
string strXData="";
bool isGet = false;
foreach (string appName in lstAppName)
{
//isGet = CADXData.TryGetXData(dbObj, appName, out strXData);
appNameUse = appName;
}
if (isGet)
{
AcadEntity ent = dbObj.AcadObject as AcadEntity;
bool isAttBlock = IsAttBlock(ent);//是否是属性块 ,需要另类处理
if (isSeled && !isAttBlock)//是非属性块时直接退出
{
m_DbClick = true;
return;
}
object objMin; object objMax; double[] ptMin; double[] ptMax;
ent.GetBoundingBox(out objMin, out objMax);
ptMin = (double[])objMin; ptMax = (double[])objMax;
double[] ptPic = ptPick as double[];
if (ptPic[0] > (ptMin[0] - dist) && ptPic[0] < (ptMax[0] + dist) &
ptPic[1] > (ptMin[1] - dist) && ptPic[1] < (ptMax[1] + dist))
{
//这个函数用委托 有可能是将全部扩展数据全显示出来
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("扩展数据名为: " + appNameUse + ",扩展数据名为: " + strXData);
m_DbClick = true;
}
}
// tr.Commit();
}
}
catch
{ }
}
//得到是否是属性块
private static bool IsAttBlock(AcadEntity ent)
{
if (ent is AcadBlockReference)
{
AcadBlockReference refEnt = ent as AcadBlockReference;
object[] atts = (object[])refEnt.GetAttributes();
if (atts.Length > 0) return true;
}
return false;
}
//屏蔽CAD命令
static void vetoCommand(object sender, DocumentLockModeChangedEventArgs e) {
glbFrmTrace.txtHint.Text += "vetoCommand \r\n";
//glbFrmTrace.txtHint.Text.Insert(glbFrmTrace.txtHint.Text.Length, "vetoCommand \n");
try
{
string com2 = e.GlobalCommandName.ToLower();
if (m_DbClick)
{
string com = e.GlobalCommandName.ToLower();
switch (com)
{
case "properties":
m_DbClick = false;
e.Veto();
break;
case "bedit":
m_DbClick = false;
e.Veto();
break;
case "eattedit":
m_DbClick = false;
e.Veto();
break;
case "ddedit":
m_DbClick = false;
e.Veto();
break;
}
}
}
catch
{ }
}
}
}