新建和插入块函数
本帖最后由 chpmould 于 2010-12-4 20:41 编辑请教老师:
例如:首先判断当前图档中是否有一图块“A”,如果没有则新建,如果有则直接插入。
下面是我写的一部分,但是有问题,请帮助修正一下... public static ObjectId CreateBlock(string blockname, ObjectIdCollection[] objIds)
{
DocumentLock doclock = Application.DocumentManager.MdiActiveDocument.LockDocument();
ObjectId blockid = new ObjectId();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blockname;
if (!bt.Has(blockname)) //判断是否存在名为blockname的块,如果没有则新建
{
for (int i = 0; i <= objIds.Length; i++)
{
btr.AppendEntity(btr);
}
blockid = bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
tr.Commit();
}
else //如果当前图面内存中有此块,则插入此块
{
BlockTableRecord block = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForRead);
BlockReference blockRef = new BlockReference(point, bt);
blockRef.ScaleFactors = scale;
blockRef.Rotation = rotateAngle;
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(blockRef);
trans.AddNewlyCreatedDBObject(blockRef, true);
trans.Commit();
}
}
doclock.Dispose();
return blockid;
} 本帖最后由 lzh741206 于 2010-12-5 17:58 编辑
你的代码,哎,惨不忍睹
public static ObjectId CreateBlockDef(string blockname, DBObjectCollection objs)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
if (bt.Has(blockname))
{
return bt;
}
else
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blockname;
ObjectId id = bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
foreach (Entity ent in objs)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
tr.Commit();
return id;
}
}
}
public static ObjectId CreateBlockRef(string blockname, Point3d position, double rotateAngle, double scale)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockReference blockRef = new BlockReference(position, bt);
blockRef.ScaleFactors = new Scale3d(scale, scale, scale);
blockRef.Rotation = rotateAngle;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
ObjectId id = btr.AppendEntity(blockRef);
tr.AddNewlyCreatedDBObject(blockRef, true);
tr.Commit();
return id;
}
}
public static void Test8()
{
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);
CreateBlockRef("Test", Point3d.Origin, 0, 1);
}
本想只是打击你一下,不想斑竹也在这里浪费时间,但看到后面我后悔了,文字已经写了一些,还是贴出来吧。
你向别人请教,至少你的代码通过编译吧。
我的文字只是挑毛病,并不有助于完成函数,得罪了!
public static ObjectId CreateBlock(string blockname, ObjectIdCollection[] objIds)
{
DocumentLock doclock = Application.DocumentManager.MdiActiveDocument.LockDocument();
//因你这个函数里对AutoCAD数据库操作并不是处于一种外部环境(简单说不是在对话框环境),所以你这里没有必要锁定。你非得要锁定也行,但既然你锁定了,后面应该进行异常捕捉,以确保在后面解锁
ObjectId blockid = new ObjectId();
//首先ObjectId没有无参数的构造函数,这里使用new也没有任何意义, 建议ObjectId blockid =ObjectId.Null
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blockname;
if (!bt.Has(blockname))
{//你为什么要加一个逻辑非!呢?你把后面的代码块互换一下不就少麻烦一下CPU吗?
for (int i = 0; i <= objIds.Length; i++)
{
btr.AppendEntity(btr);
//这里什么意思?自己添加自己
}
blockid = bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
tr.Commit();
}
else
{
//这里的代码不是你写的吧,point scale rotateAngle trans 这些变量孙悟空帮你变出来?
BlockTableRecord block = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForRead);
BlockReference blockRef = new BlockReference(point, bt);
blockRef.ScaleFactors = scale;
blockRef.Rotation = rotateAngle;
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
//知不知道btr你在前面声明过了
btr.AppendEntity(blockRef);
trans.AddNewlyCreatedDBObject(blockRef, true);
trans.Commit();
}
}
doclock.Dispose();
return blockid;
} 非常感谢谢谢各位老师的耐心指导!!! 以上代码是我参照一些例子写的,有很多地方还没有理解,所以问题一大堆... 我调用新建块函数没有任何反应,我是按以下方式调用,请问我是那里出错了?(但是调用那插入块测试成功)ObjectIdCollection Blk01 = new ObjectIdCollection();
Blk01.Add(Cir01);
Blk01.Add(Cir02);
// 定义一个集合数组
ObjectIdCollection[] blks = new ObjectIdCollection;
blks.SetValue(Blk01, 0);
ObjectId BlkId1 = CreateBlockDef("Test", blks); 没修改你的代码的声明就直接改了,楼上的代码已更正 我还是没有测试成功,例如: 我是按以下方式画出两个圆,下面如何调用CreateBlockDef函数public void test()
{
ObjectId cir01 = AddCircle(new Point3d(0, 0, 0),10); //第一个圆
ObjectId cir02 = AddCircle(new Point3d(0, 0, 0),15); //第二个圆
//
这里如何调用CreateBlockDef新建块
//
} Circle c1 = new Circle(new Point3d(0, 0, 0),10);
Circle c2 = new Circle(new Point3d(0, 0, 0),10);
DBObjectCollection objs = new DBObjectCollection();
objs.Add(c1);
objs.Add(c2);
CreateBlockDef("Test", objs);
本帖最后由 chpmould 于 2010-12-5 17:50 编辑
如果是用ObjectIdCircle c1 = new Circle(new Point3d(0, 0, 0),10);可以吗?
下面我将我的测试代码贴上了你看看。。。
页:
[1]
2