云顶国际8588yd(有限公司)官方网站-Weixin百科

反复插入块时闪退

我有一个开发任务。程序的逻辑是先调用函数createBlock,将几个图形创建成块,再反复调用creareBlockRef插入块,当插入几个块时无异常,但是插入较多块时就会闪退

public override void createBlock(string sfn, string blkname, double[] bp)
{

try
{
// 加载源文件到临时数据库

using (var ttrans = db.TransactionManager.StartTransaction())
{
BlockTable tbt = ttrans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;// 获取目标块表
if (tbt.Has(blkname)) throw new sys.Exception("块名称已存在:" + blkname);// 检查块名是否存在,若存在则可以选择处理或报错

var newBlock = new BlockTableRecord();// 创建新块定义
newBlock.Name = blkname;
newBlock.Origin = toP3d(bp);

using (Database sdb = new Database(false, true))
{
if (!File.Exists(sfn)) throw new sys.Exception("文件不存在:\n" + sfn);
sdb.ReadDwgFile(sfn, FileOpenMode.OpenForReadAndAllShare, true, null);
sdb.CloseInput(true);

using (var strans = sdb.TransactionManager.StartTransaction())
{

// 复制源模型空间中的实体到新块
var sbt = (BlockTable)strans.GetObject(sdb.BlockTableId, OpenMode.ForRead);
var sbtr = (BlockTableRecord)strans.GetObject(sbt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

var linearcs = new List();
foreach (ObjectId entityId in sbtr)
{
Entity entity = strans.GetObject(entityId, OpenMode.ForRead) as Entity;
if (entity != null)
{
// 深拷贝实体到当前数据库
Entity clonedEntity = entity.Clone() as Entity;
clonedEntity.SetDatabaseDefaults(db);

if (!(clonedEntity is Line || clonedEntity is Arc) ||
clonedEntity.Color.ColorIndex != 6/*洋红*/)
{
newBlock.AppendEntity(clonedEntity);
}
else
{
linearcs.Add(clonedEntity);
}
}
}

linkLineArcCls.linkLineArc(linearcs, newBlock);

foreach (var tent in linearcs)
{
newBlock.AppendEntity(tent);
}

strans.Commit();
}
}//std
// 将新块添加到目标块表并获取其ID
tbt.Add(newBlock);
ttrans.AddNewlyCreatedDBObject(newBlock, true);//在块中插入对象时,不需要此函数,创建块或在模型空间插入对象时需要此函数,删除实体前,实体需要open模式

ttrans.Commit();
}//trans
}
catch (sys.Exception ex)
{
throw new sys.Exception("创建块失败:" + ex.ToString());
}
}//s

public override void creareBlockRef(blockrefInfo refinfo)
{
using (var trans = db.TransactionManager.StartTransaction())
{
try
{
var bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
var btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

var blkname = refinfo.blkname;
if (!bt.Has(blkname)) throw new sys.Exception("块名称不存在:" + blkname);
var blkid = bt[blkname];

var bref = new BlockReference(toP3d(refinfo.insertP), blkid);
bref.SetDatabaseDefaults();

if (refinfo.needRotate)
{
bref.TransformBy(Matrix3d.Rotation(refinfo.RotateAng, new Vector3d(0, 0, 1), toP3d(refinfo.RotateP)));
}

btr.AppendEntity(bref);
trans.AddNewlyCreatedDBObject(bref, true);

trans.Commit();

}
catch (System.Exception ex)
{
trans.Abort();
throw ex;
}
}
}
已邀请:

你好,请私信我您的联系方式,我联系您沟通处理。

要回复问题请先登录注册

Baidu
sogou