- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 csharp 于 2014-6-9 02:37 编辑
[CommandMethod("EditBlockAtt")]
public void EditBlockAtt()
{
var acDb = HostApplicationServices.WorkingDatabase;
var acEd = AcadApplication.DocumentManager.MdiActiveDocument.Editor;
var blockNamePrompt = acEd.GetString(Environment.NewLine + "Enter block name: ");
if (blockNamePrompt.Status != PromptStatus.OK) return;
var blockName = blockNamePrompt.StringResult;
var attNamePrompt = acEd.GetString(Environment.NewLine + "Enter attribute name: ");
if (attNamePrompt.Status != PromptStatus.OK) return;
var attName = attNamePrompt.StringResult;
var acPo = new PromptStringOptions(Environment.NewLine + "Enter new attribute value: "){ AllowSpaces = true };
var newValuePrompt = acEd.GetString(acPo);
if (newValuePrompt.Status != PromptStatus.OK) return;
var newValue = newValuePrompt.StringResult;
using (var acTrans = acDb.TransactionManager.StartTransaction())
{
var acBlockTable = acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (acBlockTable == null) return;
var acBlockTableRecord = acTrans.GetObject(acBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
if (acBlockTableRecord == null) return;
foreach (var blkId in acBlockTableRecord)
{
var acBlock = acTrans.GetObject(blkId, OpenMode.ForRead) as BlockReference;
if (acBlock == null) continue;
if (!acBlock.Name.Equals(blockName, StringComparison.CurrentCultureIgnoreCase)) continue;
foreach (ObjectId attId in acBlock.AttributeCollection)
{
var acAtt = acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;
if (acAtt == null) continue;
if (!acAtt.Tag.Equals(attName, StringComparison.CurrentCultureIgnoreCase)) continue;
acAtt.UpgradeOpen();
acAtt.TextString = newValue;
}
}
acTrans.Commit();
}
}
Then from an interop AcadApplication, netload the dll and call the method from the commandline in this format:
(Command "EditBlockAtt" "BlockName" "AttributeName" "NewValue")However if you want to go pure Interop, this may get you what you need given you have an AcadDocument object at runtime:
foreach (AcadEntity ent in acadDoc.ModelSpace)
{
var block = ent as AcadBlockReference;
if (block == null) continue;
{
if (!block.Name.Equals("BlockName", StringComparison.CurrentCultureIgnoreCase)) continue;
var atts = block.GetAttributes() as object[];
if (atts == null) continue;
foreach (var attribute in atts.OfType<AcadAttributeReference>()
.Where(attribute => attribute.TagString.Equals("AttributeName",
StringComparison.CurrentCultureIgnoreCase)))
{
attribute.TextString = "New Value";
}
}
}
|
|