反復(fù)插入塊時閃退
我有一個開發(fā)任務(wù)。程序的邏輯是先調(diào)用函數(shù)createBlock,將幾個圖形創(chuàng)建成塊,再反復(fù)調(diào)用creareBlockRef插入塊,當(dāng)插入幾個塊時無異常,但是插入較多塊時就會閃退
public override void createBlock(string sfn, string blkname, double[] bp)
{
try
{
// 加載源文件到臨時數(shù)據(jù)庫
using (var ttrans = db.TransactionManager.StartTransaction())
{
BlockTable tbt = ttrans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;// 獲取目標(biāo)塊表
if (tbt.Has(blkname)) throw new sys.Exception("塊名稱已存在:" + blkname);// 檢查塊名是否存在,若存在則可以選擇處理或報錯
var newBlock = new BlockTableRecord();// 創(chuàng)建新塊定義
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())
{
// 復(fù)制源模型空間中的實體到新塊
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)
{
// 深拷貝實體到當(dāng)前數(shù)據(jù)庫
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
// 將新塊添加到目標(biāo)塊表并獲取其ID
tbt.Add(newBlock);
ttrans.AddNewlyCreatedDBObject(newBlock, true);//在塊中插入對象時,不需要此函數(shù),創(chuàng)建塊或在模型空間插入對象時需要此函數(shù),刪除實體前,實體需要open模式
ttrans.Commit();
}//trans
}
catch (sys.Exception ex)
{
throw new sys.Exception("創(chuàng)建塊失敗:" + 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;
}
}
}
public override void createBlock(string sfn, string blkname, double[] bp)
{
try
{
// 加載源文件到臨時數(shù)據(jù)庫
using (var ttrans = db.TransactionManager.StartTransaction())
{
BlockTable tbt = ttrans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;// 獲取目標(biāo)塊表
if (tbt.Has(blkname)) throw new sys.Exception("塊名稱已存在:" + blkname);// 檢查塊名是否存在,若存在則可以選擇處理或報錯
var newBlock = new BlockTableRecord();// 創(chuàng)建新塊定義
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())
{
// 復(fù)制源模型空間中的實體到新塊
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)
{
// 深拷貝實體到當(dāng)前數(shù)據(jù)庫
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
// 將新塊添加到目標(biāo)塊表并獲取其ID
tbt.Add(newBlock);
ttrans.AddNewlyCreatedDBObject(newBlock, true);//在塊中插入對象時,不需要此函數(shù),創(chuàng)建塊或在模型空間插入對象時需要此函數(shù),刪除實體前,實體需要open模式
ttrans.Commit();
}//trans
}
catch (sys.Exception ex)
{
throw new sys.Exception("創(chuàng)建塊失敗:" + 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;
}
}
}
沒有找到相關(guān)結(jié)果
已邀請:
1 個回復(fù)
浩辰軟件的糕糕
贊同來自:
你好,請私信我您的聯(lián)系方式,我聯(lián)系您溝通處理。