如何更新块定义?
书上、视频里面都是讲了新建块和添加块参照,却没有讲重新定义块。
起初我考虑修改块表,先删除块中所有图元,然后重新加入,运行没有报错,但不知为何运行完代码后变成了匿名块;
后来我考虑删除块记录,然后重新加入块记录,仍然不成功,代码如下。
这个问题困扰我两天了,希望有前辈指点,谢谢。
我用的是C#:
public static ObjectId AddBlockTableRecord(this Database db, string btrName, List<Entity> ents)
{
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable) trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr;
if (!bt.Has(btrName))
{
btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < ents.Count; i++)
{
btr.AppendEntity(ents);
}
bt.UpgradeOpen();
bt.Add(btr);
bt.DowngradeOpen();
trans.AddNewlyCreatedDBObject(btr, true);
}
else
{
btr = bt.GetObject(OpenMode.ForWrite) as BlockTableRecord;
btr.Erase();
btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < ents.Count; i++)
{
btr.AppendEntity(ents);
}
bt.UpgradeOpen();
bt.Add(btr);
bt.DowngradeOpen();
trans.AddNewlyCreatedDBObject(btr, true);
}
trans.Commit();
btrId = bt;
}
return btrId;
}
修改或删除块表记录里的图元,没问题 但是块表记录加入块表后,我还想往块表记录里加图元就会变匿名块…… 试了好多次,还就这种写法没问题,块可以正常定义和更新,大神们有什么更简便的写法么?
代码如下:
public void kdy()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "块定义";
PromptSelectionResult psr = ed.GetSelection(pso);
if (psr.Status != PromptStatus.OK) { return; }
PromptPointOptions ppo = new PromptPointOptions("选择块原点");
ppo.AllowNone = true;
PromptPointResult ppr = ed.GetPoint(ppo);
Point3d insertPoint = new Point3d();
if (ppr.Status != PromptStatus.OK)
{
return;
}
else
{
insertPoint = ppr.Value;
}
db.DefineABlock("MINE", psr, insertPoint);
}
public void crk()
{
Database db = HostApplicationServices.WorkingDatabase;
// 是否存在 MINE 块记录
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has("MINE")) return;
}
// 确定块插入点
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions ppo = new PromptPointOptions("选择块插入点");
ppo.AllowNone = true;
PromptPointResult ppr = ed.GetPoint(ppo);
Point3d insertPoint = new Point3d();
if (ppr.Status != PromptStatus.OK)
{
return;
}
else
{
insertPoint = ppr.Value;
}
// 模型空间中插入块参照
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = bt["MINE"].GetObject(OpenMode.ForRead) as BlockTableRecord;
BlockReference blkRef = new BlockReference(insertPoint, btr.ObjectId);
BlockTableRecord modelSpace = trans.GetObject(bt, OpenMode.ForWrite) as BlockTableRecord;
modelSpace.AppendEntity(blkRef);
trans.AddNewlyCreatedDBObject(blkRef, true);
trans.Commit();
}
}
/// <summary>
/// 定义块
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="btrName">块名</param>
/// <param name="psr">GetSelection的结果</param>
/// <param name="insertPoint">块原点</param>
/// <returns>块表记录Id</returns>
public static ObjectId DefineABlock(this Database db, string btrName, PromptSelectionResult psr, Point3d insertPoint)
{
SelectionSet SS = psr.Value;
ObjectId btrId = ObjectId.Null;
if (SS.Count == 0) return btrId;
//提取选择集中的各个图元
ObjectId[] entIds = SS.GetObjectIds();
List<Entity> entList = new List<Entity>();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < entIds.Count(); i++)
{
Entity ent = entIds.GetObject(OpenMode.ForRead) as Entity;
Vector3d vec = insertPoint.GetVectorTo(Point3d.Origin);
Matrix3d mt = Matrix3d.Displacement(vec);
ent = ent.GetTransformedCopy(mt);
entList.Add(ent);
}
}
//判断是否已有同名块表记录
bool existed;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (bt.Has(btrName))
{
existed = true;
}
else
{
existed = false;
}
}
// 定义块表
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (existed)
{//重定义块表
BlockTableRecord btr = bt.GetObject(OpenMode.ForWrite) as BlockTableRecord;
//记录下已添加到模型空间的块参照
List<BlockReference> oldRefs = new List<BlockReference>();
foreach (ObjectId objID in btr.GetBlockReferenceIds(false, true))
{
BlockReference blkRef = objID.GetObject(OpenMode.ForWrite) as BlockReference;
oldRefs.Add(blkRef);
}
//删除旧的块表记录
btr.Erase();
//增加新的同名块表记录
btr = new BlockTableRecord();
btr.Name = btrName;
foreach (Entity ent in entList)
{
btr.AppendEntity(ent);
}
bt.UpgradeOpen();
bt.Add(btr);
bt.DowngradeOpen();
trans.AddNewlyCreatedDBObject(btr, true);
//更新模型空间中的块参照
foreach (BlockReference blkRef in oldRefs)
{
blkRef.BlockTableRecord = btr.ObjectId;
//blkRef.RecordGraphicsModified(true);
}
}
else
{//新定义块表
BlockTableRecord btr = new BlockTableRecord();
btr.Name = btrName;
foreach (Entity ent in entList)
{
btr.AppendEntity(ent);
}
bt.UpgradeOpen();
bt.Add(btr);
bt.DowngradeOpen();
trans.AddNewlyCreatedDBObject(btr, true);
}
trans.Commit();
}
return btrId;
} 顶起赞一个,
页:
[1]