[求助]ObjectAppended事件,画Polylin报错?【有代码】
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;
namespace TestClassLib
{
public class Class1 : IExtensionApplication
{
#region IExtensionApplication 成员
public void Initialize()
{
AddDatabaseEvent();
}
public void Terminate()
{
}
#endregion
public static bool b_DbEvent;
public void AddDatabaseEvent()
{
// Get the current document
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database db = acDoc.Database;
db.ObjectAppended +=
new ObjectEventHandler(DatabaseAppended);
acDoc.Editor.WriteMessage("\nEvent added!\n");
b_DbEvent = true;
}
public void RemoveDatabaseEvent()
{
// Get the current document
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database db = acDoc.Database;
db.ObjectAppended -=
new ObjectEventHandler(DatabaseAppended);
acDoc.Editor.WriteMessage("\nEvent removed!\n");
b_DbEvent = false;
}
public void DatabaseAppended(object senderObj,
ObjectEventArgs dbAdded)
{
//App.ShowAlertDialog("Database added!");
RemoveDatabaseEvent();
InterZ();
AddDatabaseEvent();
}
public void InterZ()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using(DocumentLock docLock = doc.LockDocument())
{
PromptSelectionResult curSR;
curSR = ed.SelectLast();
if (curSR.Status != PromptStatus.OK || curSR.Value.Count == 0)
return;
ObjectId[] ids = curSR.Value.GetObjectIds();
DBPoint dbpt = null;
Polyline pln = null;
foreach (ObjectId id0 in ids)
{
#region foreach
Transaction tr = db.TransactionManager.StartTransaction();
Entity ent = tr.GetObject(id0, OpenMode.ForRead) as Entity;
dbpt = ent as DBPoint;
pln = ent as Polyline;
tr.Dispose();
#endregion
}
}
}
}
}
画直线、点的时候,都没有问题,画多义线(Polyline、Polyline3d)的时候,就会有致命错误另外,选中一个点,复制,粘贴,也会报错,但不知道错在哪里!?
V C# 2005 + AutoCAD2007,附件要改成.cs文件
请各位指教
<p>ObjectEventArgs的属性里应该有当前添加的实体</p>
<p>PL添加点的时候会向数据库添加子实体的</p>
<p>所以,你获取的是应该是子实体,而不是pl本身</p>
回复:(lzh741206)ObjectEventArgs的属性里应该有当...
本帖最后由 作者 于 2010-8-23 15:42:50 编辑你好!
根据你的提示,我把DatabaseAppended() 改成这个样子了,画Polyline还是不行,InterZ()中如果有“Transaction tr = db.TransactionManager.StartTransaction();”的代码就会报错!去掉就没事,不知道为什么?!
public void DatabaseAppended(object senderObj,
ObjectEventArgs dbAdded)
{
//App.ShowAlertDialog("Database added!");
RemoveDatabaseEvent();
DBObject obj = dbAdded.DBObject;
string str = obj.GetType().Name.ToString();
if (str == "Polyline")
{
InterZ();
}
AddDatabaseEvent();
}
另外:
如果DatabaseAppended() 改成这个样子
public void DatabaseAppended(object senderObj,
ObjectEventArgs dbAdded)
{
//App.ShowAlertDialog("Database added!");
RemoveDatabaseEvent();
DBObject obj = dbAdded.DBObject;
string str = obj.GetType().Name.ToString();
if (str == "DBPoint")
{
DBPoint dbpt = obj as DBPoint;
}
AddDatabaseEvent();
}
选中点复制、粘贴的时候,调试的过程中可以看到,“粘贴”的时候,database中加入了blockreference,和一个DBPoint,但是这个
DBPoint的坐标是(0,0,0)!!!而不是我鼠标指定的位置,我想取到真正的坐标,怎么办啊?
<p>你的代码想实现什么?</p>
<p>一直没看懂.......</p>
回复:(lzh741206)你的代码想实现什么?一直没看懂...
<br/>InterZ()是用来内插高程Z的,里面的具体代码我省略了。。。<br/><br/>在已经加载有已知三维坐标(X,Y,Z)的密集点的情况下,要在画了一个点之后,立刻内插其高程Z,复制、粘贴一个点的时候也需要内插Z<br/><br/>移动点的时候,也需要。。。(这里好像可以用ObjectModified来监听事件)<br/> <p>C 的看不太懂!</p><p>但是我碰到个事,也许能够参考一下!就是transaction这个玩意!</p>
<p>如果用ObjectAppend监视,尽管只是只读打开一下图元,只要一画实体,就报错。</p>
<p>因为我一直想着只是打开看一下,所以我用完transaction之后只是把它dispose了</p>
<p>后来,我在dispose前面加了个comit就不报错了!</p>
<p>不知道是不是这么回事!</p>
页:
[1]