请问大家如何在图纸空间中添加对象
一般情况下我们用程序生成的对象都在模型空间里显示,我需要在图纸空间的输出一段文字,请教一下,需要怎样才能实现呢?我用的是vb.net,C#的实现方法也可以,谢谢大家!本帖最后由 gulong2001 于 2011-10-28 14:17 编辑
/// <summary>
/// 开始当前事务处理
/// </summary>
/// <returns>开始当前事务处理</returns>
public static Autodesk.AutoCAD.DatabaseServices.TransactionStartTransaction
{
get
{
return Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
}
}
/// <summary>
/// 图元添加到模型空间
/// </summary>
public static ObjectId AddToModelSpace(this Autodesk.AutoCAD.DatabaseServices.Entity ent)
{
return ent.AddToSomeSpace("Model");
}
/// <summary>
/// 图元添加到任意空间
/// </summary>
public static ObjectId AddToSomeSpace(this Autodesk.AutoCAD.DatabaseServices.Entity ent, string LayoutName)
{
ObjectId entId = ObjectId.Null;
using (Transaction trans = StartTransaction)
{
DBDictionary dict = (DBDictionary)trans.GetObject(CurrentDatabase.LayoutDictionaryId, OpenMode.ForRead);//得到所有空间,包括模型及布局
ObjectId lyid = dict.GetAt(LayoutName);
Layout ly = (Layout)trans.GetObject(lyid, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(ly.BlockTableRecordId, OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
代码一看就应该明白。可以添加到任意空间,用了扩展方法(你不用就修改下)ent的AddToSomeSpace,参数里面加上空间的名字即可加到对应空间具体可见:http://blog.sina.com.cn/s/blog_69e8fdf00100zv4r.html
本帖最后由 guohq 于 2011-10-28 23:14 编辑
'''
''' 向任意空间添加多个对象
'''
'''
'''
'''
'''
Public Function AppendEntity(ByVal Ents() As Entity, ByVal LayoutName As String) As ObjectId()
Dim rtnIDs(Ents.Length - 1) As ObjectId
Using Trans As Transaction = DB.TransactionManager.StartTransaction
Dim DictLy As DBDictionary = Trans.GetObject(DB.LayoutDictionaryId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
If DictLy.Contains(LayoutName) = False Then
For I As Integer = 0 To rtnIDs.Length - 1
rtnIDs(I) = ObjectId.Null
Next
Trans.Commit()
Return rtnIDs
End If
Dim Lyid As ObjectId = DictLy.GetAt(LayoutName)
Dim Ly As Layout = Trans.GetObject(Lyid, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
Dim BTR As BlockTableRecord = Trans.GetObject(Ly.BlockTableRecordId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
Dim Ent As Entity = Nothing
For I As Integer = 0 To Ents.Length - 1
Ent = Ents(I)
rtnIDs(I) = BTR.AppendEntity(Ent)
Trans.AddNewlyCreatedDBObject(Ent, True)
Next
Trans.Commit()
End Using
Return rtnIDs
End Function
页:
[1]