我还是没有测试成功,一直出错,请帮助看看是什么原因...
public void CreateCircle()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (doc.LockDocument())
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
Circle c1 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 10);
Circle c2 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 15);
DBObjectCollection objs = new DBObjectCollection();
objs.Add(c1);
objs.Add(c2);
CreateBlockDef("Test", objs);
btr.AppendEntity(c1);
btr.AppendEntity(c2);
tr.AddNewlyCreatedDBObject(c1, true);
tr.AddNewlyCreatedDBObject(c2, true);
tr.Commit();
}
} 看三楼
CreateBlockDef也改了下,忘掉事务提交了 狐哥:
我用上面我贴出来画画的代码来测试,还是没有测试成功,一直出错,请帮助测试看看是什么原因... 我这里测试没问题的 是否与CAD版本有关啊,我用的是CAD2008+VS2008 我也是
建议,你还是把手册再仔细看一遍吧,你自己说看了n遍,但看了n遍应该是不会代码写成这样的,汗
C#的语法还是要多熟悉
你犯的都是常识性错误 我在去试试。。。 重写了一遍,CreateXXX其实感觉没必要的,
简单的东西没必要搞一大堆函数。
其次,建议的方式是一个事务到底,否则会出问题的
public void test24()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
string blkname = "Test";
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Circle c1 = new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
Circle c2 = new Circle(Point3d.Origin, Vector3d.ZAxis, 15);
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkdefid = GetRecorId(tr, bt, blkname);
if (blkdefid == ObjectId.Null)
{
BlockTableRecord blkdef = new BlockTableRecord { Name = blkname };
bt.UpgradeOpen();
blkdefid = bt.Add(blkdef);
tr.AddNewlyCreatedDBObject(blkdef, true);
bt.DowngradeOpen();
AddEntity(tr, blkdef, c1);
AddEntity(tr, blkdef, c2);
}
BlockReference bref = new BlockReference(Point3d.Origin, blkdefid);
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
AddEntity(tr, btr, bref);
tr.Commit();
}
}
/// <summary>
/// 在符号表中获取对应键值的记录Id
/// </summary>
/// <param name="table">符号表</param>
/// <param name="key">记录键值</param>
/// <returns>对应键值的记录Id</returns>
public static ObjectId GetRecorId<T>(Transaction tr, T table, string key) where T : SymbolTable
{
if (table.Has(key))
{
if (Application.Version.Major < 18)
{
ObjectId idres = table;
if (!idres.IsErased)
return idres;
foreach (ObjectId id in table)
{
if (!id.IsErased)
{
SymbolTableRecord str = id.GetObject<SymbolTableRecord>(tr);
if (str.Name == key)
return str.ObjectId;
}
}
}
else
{
return table;
}
}
return ObjectId.Null;
}
public ObjectId AddEntity(Transaction tr, BlockTableRecord btr, Entity ent)
{
ObjectId id = btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
return id;
} 本帖最后由 chpmould 于 2010-12-6 21:20 编辑
谢谢狐,你昨天帮我写的那块函数,我昨天已调试通过了,今天你重新写的这个我现是越看越糊涂啦...其实我只是想学习一下如自定义一些通用函数... 本帖最后由 介之推 于 2013-4-10 19:51 编辑
雪山飞狐_lzh 发表于 2010-12-6 20:53 http://bbs.mjtd.com/static/image/common/back.gif
重写了一遍,CreateXXX其实感觉没必要的,
简单的东西没必要搞一大堆函数。
其次,建议的方式是一个事务到 ...
你好啊,版主,我调试你第18楼的代码时出错了啊,在GetObjectId函数中的这一行代码: SymbolTableRecord str = id.GetObject<SymbolTableRecord>(tr);VS提示说:
非泛型 方法“Autodesk.AutoCAD.DatabaseServices.ObjectId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode, bool, bool)”不能与类型实参一起使用
这是怎么回事啊?
我现在也想插入块,和楼主要实现的目的一致啊。
页:
1
[2]