c#如何删除块中块,保留外层块?
各位大佬,赐教用c#如何删除块中块,保留最外层块。如块A中包含有块B、块C等等,删除块A中的块B。急求,先感谢各位大佬:handshakepsr.Value.GetEntities<Entity>().ForEach(x => x.ForWrite(e =>
{
if (e is BlockReference brf)
{
using var tr = new DBTrans();
var btr = tr.BlockTable.GetRecord(brf.BlockTableRecord, OpenMode.ForRead);
foreach (ObjectId id in btr)
{
var e1 = tr.GetObject<Entity>(id, OpenMode.ForWrite);
if (e1 is BlockReference brf1)
{
var btr1 = tr.BlockTable.GetRecord(brf1.BlockTableRecord, OpenMode.ForRead);
foreach (ObjectId id1 in btr1)
{
var ent1 = tr.GetObject<Entity>(id1, OpenMode.ForWrite);
ent1.ColorIndex = colorI;
}
}
else
{
e1.ColorIndex = colorI;
}
}
}
if (e is Entity ent)
{
ent.ColorIndex = colorI;
}
})); 本帖最后由 wang2006zhi 于 2021-12-20 14:32 编辑
if (ent is BlockReference)
{
using (Transaction trans1 = db.TransactionManager.StartTransaction())
{
BlockTable bt1 = (BlockTable)trans1.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockReference br1 = (BlockReference)ent;
string blkname1 = br1.Name;
ObjectId brId1 = bt1;
BlockTableRecord btr1 = (BlockTableRecord)brId1.GetObject(OpenMode.ForRead);
foreach (ObjectId entId1 in btr1)
{
Entity ent1 = trans1.GetObject(entId1, OpenMode.ForWrite) as Entity;
ent1.ChangeEntityColor(1);
}
} trans1.Commit();
}
666SHUN 发表于 2021-12-24 17:51
ChangeEntityColor()那这个就是自定义方法了,官方文件没有。这个方法代码可以贴来不?
/// <summary>
/// 改变图形颜色
/// </summary>
/// <param name="c1Id">图形的ObjectId</param>
/// <param name="colorIndex">颜色索引</param>
public static void ChangeEntityColor(this ObjectId c1Id, short colorIndex)
{
//图形数据库
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForRead);
//获取图形对象
Entity ent1 = (Entity)c1Id.GetObject(OpenMode.ForWrite);
ent1.ColorIndex = colorIndex;
trans.Commit();
}
} 遍历图块,就是一个blocktable 跟模型空间一样,你怎么遍历模型空间实体,你就怎样操作块 图块本身和模型空间一样,都是blocktable brainstorm 发表于 2021-12-18 20:58
图块本身和模型空间一样,都是blocktable
OK,感谢:handshake 本帖最后由 666SHUN 于 2021-12-19 11:57 编辑
666SHUN 发表于 2021-12-19 11:51
OK,感谢有时又不会,这是怎么回事呢 brainstorm 发表于 2021-12-18 20:58
图块本身和模型空间一样,都是blocktable
另想问问大佬,C#批量绑定图纸时,图纸的图层怎么控制和原来一样呢?遇到的情况是:有时绑定后,会把绑定前处理的图层全部打开了,有时又不会,这是怎么回事呢 wang2006zhi 发表于 2021-12-20 12:49
if (ent is BlockReference)
{
using (Tr ...
Entity没有ChangeEntityColor()方法?这是你的自定义方法吗,大佬 我这月12接触这语言,这个改颜色的方法是抄袭书上的案例,主要是表达两重块的两次遍历,三重或更多估计得递归写法 wang2006zhi 发表于 2021-12-24 00:29
我这月12接触这语言,这个改颜色的方法是抄袭书上的案例,主要是表达两重块的两次遍历,三重或更多估计得递 ...
ChangeEntityColor()那这个就是自定义方法了,官方文件没有。这个方法代码可以贴来不?:handshake
页:
[1]
2