- 积分
- 8063
- 明经币
- 个
- 注册时间
- 2013-8-9
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
该程序为根据扩展信息来判断关联。- using System;
- using System.Collections.Generic;
- using System.Text;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using DotNetARX;
- using System.Text.RegularExpressions;
- using Autodesk.AutoCAD.Windows;
- using System.Windows.Forms;
- namespace MoveCircleEvent
- {
- public class MovePoint
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- bool bMove;
- Point3d startPoint;
- void commandWillStart(object sender, CommandEventArgs e)
- {
- //设置全局变量bMove为True,表示移动命令开始
- bMove = true;
- }
- void objectOpenedForModify(object sender, ObjectEventArgs e)
- {
- //判断AutoCAD命令是否为移动
- if (bMove == false)
- //如果AutoCAD命令为非移动,则返回
- return;
- DBPoint pt = e.DBObject as DBPoint;
- //判断将要移动的对象是否为
- if (pt != null)
- {
- //获取文字的中心,就是同心文字的文字心
- startPoint = pt.Position;
- }
- }
- /// <summary>
- /// 去掉字符串中的数字及小数点
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string RemoveNumber(string key)
- {
- return System.Text.RegularExpressions.Regex.Replace(key, @"[\d \.]", "");
- }
- /// <summary>
- /// 去掉字符串中的字母及等号
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string RemoveNotNumber(string key)
- {
- return System.Text.RegularExpressions.Regex.Replace(key, @"[A-Z a-z \=]*", "");
- }
- void objectModified(object sender, ObjectEventArgs e)
- {
- //判断AutoCAD命令是否为移动
- if (bMove == false)
- //如果AutoCAD命令为非移动,则返回
- return;
- //断开所有的事件处理函数
- removeEvents();
- //判断移动过的对象是否为点
- DBPoint startPoint = e.DBObject as DBPoint;
- if (startPoint == null)
- return;
- //设置选择集过滤器,只选择图形中的文字
- TypedValueList values = new TypedValueList();
- values.Add(typeof(DBText));
- SelectionFilter filter = new SelectionFilter(values);
- PromptSelectionResult resSel = ed.SelectAll(filter);
- //如果选择的是文字
- if (resSel.Status == PromptStatus.OK)
- {
- //获取选择集中的文字对象
- SelectionSet sSet = resSel.Value;
- ObjectId[] ids = sSet.GetObjectIds();
- //开始事务处理
- try
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- TypedValueList list1 = startPoint.ObjectId.GetXData("测点");
- if (list1 == null)
- {
- MessageBox.Show("点无扩展信息");
- }
- //循环遍历选择集中的文字
- foreach (ObjectId id in ids)
- {
- //以读的方式打开文字对象
- DBText followText = trans.GetObject(id, OpenMode.ForRead) as DBText;
- double fText = Convert.ToDouble(RemoveNotNumber(followText.TextString.ToString()));
- string qText = RemoveNumber(followText.TextString.ToString());
- TypedValueList list2 = id.GetXData("测点");
- string mc1 = "a", mc2 = "b", dh1 = "c", dh2 = "d";
- if (list2 != null)
- {
- mc1 = Convert.ToString(list1[0].Value.ToString());
- mc2 = Convert.ToString(list2[0].Value.ToString());
- dh1 = Convert.ToString(list1[2].Value.ToString());
- dh2 = Convert.ToString(list2[2].Value.ToString());
- }
- else
- {
- continue;
- }
- //通过判断点与文字的扩展信息是否相同
- if (qText == "Y=" && mc1 == mc2 && dh1 == dh2)
- {
- //因为上面以读的方式打开了文字,所以必须改变为写
- followText.UpgradeOpen();
- //改变文字的内容保持和点位置Y坐标相同
- followText.TextString = "Y=" + startPoint.Position.X.ToString("0.000");
- }
- else if (qText == "Z=" && mc1 == mc2 && dh1 == dh2)
- {
- //因为上面以读的方式打开了文字,所以为了改变文字的文字心必须改变为写
- followText.UpgradeOpen();
- //改变文字的内容保持和点位置Z坐标相同
- followText.TextString = "Z=" + startPoint.Position.Z.ToString("0.000");
- }
- else if (qText == "X=" && mc1 == mc2 && dh1 == dh2)
- {
- //因为上面以读的方式打开了文字,所以为了改变文字的文字心必须改变为写
- followText.UpgradeOpen();
- //改变文字的内容保持和点位置X坐标相同
- followText.TextString = "X=" + startPoint.Position.Y.ToString("0.000");
- }
- }
- //提交事务处理
- trans.Commit();
- }
- }
- catch
- {
- ed.WriteMessage("\n错误");
- }
- }
- //连接所有的事件处理函数
- AddEvents();
- }
- void commandEnded(object sender, CommandEventArgs e)
- {
- if (bMove == true)//判断AutoCAD命令是否为移动
- bMove = false;//设置全局变量bMove为False,表示移动命令结束
- }
- [CommandMethod("Add")]
- public void AddEvents()
- {
- //把事件处理函数与相应的事件进行连接
- db.ObjectOpenedForModify += objectOpenedForModify;
- db.ObjectModified += objectModified;
- doc.CommandWillStart += commandWillStart;
- doc.CommandEnded += commandEnded;
- }
- [CommandMethod("Remove")]
- public void removeEvents()
- {
- //断开所有的事件处理函数
- db.ObjectOpenedForModify -= objectOpenedForModify;
- db.ObjectModified -= objectModified;
- doc.CommandWillStart -= commandWillStart;
- doc.CommandEnded -= commandEnded;
- }
- }
- }
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|