如何绘制一条PolyLine,添加扩展属性,并返回这个Polyline的ObjectId
本帖最后由 介之推 于 2012-7-7 09:39 编辑大家好。我想绘制一个矩形框,然后给这个矩形框添加一个名称和标记的扩展数据。然后返回这个矩形框的ObjectId。扩展数据使用DataTable。在绘制以后在通过这个矩形的ObjectId重新获得这个矩形的名称和标记。如何做到?下面是我写的一部分代码,没有完成,请大家帮忙了。
'通过用户指定点后再绘制这个矩形,矩形的长和宽也作为参数
Public Shared Function AddRectangle(ByVal cenPt As Point3d, ByVal Height As Double, _
ByVal Length As Double) As ObjectId
Dim Pt(4) As Point3d
Pt(0) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
Pt(1) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y - Height * 0.5, 0)
Pt(2) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y + Height * 0.5, 0)
Pt(3) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y + Height * 0.5, 0)
Pt(4) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
Dim Pts As New Point3dCollection(Pt)
' 在内存中创建一个未经拟合的标准三维多段线对象.
Dim ent As New Polyline3d(Poly3dType.SimplePoly, Pts, False)
'Add the rectangle polyline entity to model space
Dim acEntId As ObjectId = AppendEntity(ent)
Dim dt As DataTable = New DataTable()
dt.TableName = "属性表"
dt.AppendColumn(CellType.CharPtr, "Name")
dt.AppendColumn(CellType.CharPtr, "Tag")
Dim row As DataCellCollection = New DataCellCollection()
Dim Name As DataCell = New DataCell()
Dim Tag As DataCell = New DataCell()
Name.SetString("矩形框")
Tag.SetString("矩形标记")
row.Add(Name)
row.Add(Tag)
dt.AppendRow(row, True)
ent.CreateExtensionDictionary()
Dim exDic As DBDictionary = New DBDictionary()'如何将扩展数据记录下来啊?
exDic.SetAt("属性表", dt)
'返回ObjectId
Return acEntId
End Function
' 将图形对象加入到模型空间的函数.
Public Shared Function AppendEntity(ByVal ent As Entity) As ObjectId
' 得到当前文档图形数据库.
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim entId As ObjectId
Using trans As Transaction = db.TransactionManager.StartTransaction
' 以读方式打开块表.
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
' 以写方式打开模型空间块表记录.
Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
' 将图形对象的信息添加到块表记录中,并返回ObjectId对象.
entId = btr.AppendEntity(ent)
' 把图形对象添加到事务处理中.
trans.AddNewlyCreatedDBObject(ent, True)
' 提交事务处理.
trans.Commit()
End Using
Return entId
End Function
本帖最后由 sxpd 于 2012-7-7 09:56 编辑
用XDATA不是更简单
你的扩展数据用法有问题
贴个相关的程序你看看
public static class DBDictionaryEx
{
public static IEnumerable<T> GetAllObjects<T>(this DBDictionary dict, Transaction tr) where T : DBObject
{
foreach (DBDictionaryEntry e in dict)
{
yield return
tr.GetObject(e.Value, OpenMode.ForRead) as T;
}
}
public static T GetAt<T>(this DBDictionary dict, Transaction tr, string key) where T : DBObject
{
if (dict.Contains(key))
{
ObjectId id = dict.GetAt(key);
if (!id.IsNull)
{
return tr.GetObject(id, OpenMode.ForRead) as T;
}
}
return null;
}
public static void SetAt<T>(this DBDictionary dict, Transaction tr, string key, T obj) where T : DBObject
{
using (dict.UpgradeOpenAndRun())
{
dict.SetAt(key, obj);
tr.AddNewlyCreatedDBObject(obj, true);
}
}
#region GetSubDictionary
internal static List<string> GetDictNames(string[] keys, out string key)
{
List<string> dictNames = new List<string>(keys);
if (dictNames.Count > 0)
{
int index = dictNames.Count - 1;
key = dictNames;
dictNames.RemoveAt(index);
}
else
{
key = "*";
}
return dictNames;
}
internal static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, IEnumerable<string> dictNames)
{
Database db = dict.Database;
DBDictionary subdict;
if (createSubDictionary)
{
foreach (string name in dictNames)
{
if (dict.Contains(name))
{
subdict = dict.GetAt(name).Open<DBDictionary>();
dict.Close();
dict.Dispose();
dict = subdict;
}
else
{
using (dict.UpgradeOpenAndRun())
{
subdict = new DBDictionary();
dict.SetAt(name, subdict);
db.AddDBObject(subdict);
}
dict.Close();
dict.Dispose();
dict = subdict;
}
}
}
else
{
foreach (string name in dictNames)
{
if (dict.Contains(name))
{
subdict = dict.GetAt(name).Open<DBDictionary>();
dict.Close();
dict.Dispose();
dict = subdict;
}
else
return null;
}
}
return dict;
}
public static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, params string[] dictNames)
{
return
GetSubDictionary(
dict,
createSubDictionary,
(IEnumerable<string>)dictNames);
}
internal static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, IEnumerable<string> dictNames)
{
if (createSubDictionary)
{
using (dict.UpgradeOpenAndRun())
dict.TreatElementsAsHard = true;
foreach (string name in dictNames)
{
if (dict.Contains(name))
{
dict = dict.GetAt(name).GetObject<DBDictionary>(tr);
}
else
{
DBDictionary subDict = new DBDictionary();
dict.SetAt(tr, name, subDict);
dict = subDict;
dict.TreatElementsAsHard = true;
}
}
}
else
{
foreach (string name in dictNames)
{
if (dict.Contains(name))
dict = dict.GetAt(name).GetObject<DBDictionary>(tr);
else
return null;
}
}
return dict;
}
internal static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, IEnumerable<string> dictNames)
{
ObjectId id = obj.ExtensionDictionary;
if (id.IsNull)
{
using (obj.UpgradeOpenAndRun())
{
obj.CreateExtensionDictionary();
}
id = obj.ExtensionDictionary;
}
DBDictionary dict = id.GetObject<DBDictionary>(tr);
return
GetSubDictionary(
dict,
tr,
createSubDictionary,
dictNames);
}
/// <summary>
/// 获取子字典
/// </summary>
/// <param name="dict">根字典</param>
/// <param name="createSubDictionary">是否创建子字典</param>
/// <param name="dictNames">键值列表</param>
/// <returns></returns>
public static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, params string[] dictNames)
{
return
GetSubDictionary(
dict,
tr,
createSubDictionary,
(IEnumerable<string>)dictNames);
}
/// <summary>
/// 获取实体字典的子字典
/// </summary>
/// <param name="obj">图元对象</param>
/// <param name="createSubDictionary">是否创建子字典</param>
/// <param name="dictNames">键值列表</param>
/// <returns></returns>
public static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, params string[] dictNames)
{
return
GetSubDictionary(
obj,
tr,
createSubDictionary,
(IEnumerable<string>)dictNames);
}
#endregion
}
扩展数据应该用XRecord保存在字典 sxpd 发表于 2012-7-7 09:58 static/image/common/back.gif
扩展数据应该用XRecord保存在字典
多谢大师指导了啊。哈哈。 学习!!!!!!!!!!!!!!!!!!
页:
[1]