修改块中属性值,属性文多文本类型。
C#程序修改块中的属性,当属性是多文本类型是,为何总不成功?关键代码如下:
string ls = "wewefwe//Pwsefwefw";
AttributeReference ar = (AttributeReference)ta.GetObject(id, OpenMode.ForWrite);
ar.MTextAttribute.Contents = ls;
请问为何原因?
Attribute prompt from AttributeReference ?
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using AcRx = Autodesk.AutoCAD.Runtime;
namespace Autodesk.AutoCAD.DatabaseServices
{
public static class ExtensionMethods
{
static RXClass attDefClass = RXClass.GetClass(typeof(AttributeDefinition));
public static void SynchronizeAttributes(this BlockTableRecord target)
{
if (target == null)
throw new ArgumentNullException("target");
Transaction tr = target.Database.TransactionManager.TopTransaction;
if (tr == null)
throw new AcRx.Exception(ErrorStatus.NoActiveTransactions);
List attDefs = target.GetAttributeDefinitions();
foreach (ObjectId id in target.GetBlockReferenceIds(true, false))
{
BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForWrite);
br.ResetAttributes(attDefs, tr);
}
if (target.IsDynamicBlock)
{
target.UpdateAnonymousBlocks();
foreach (ObjectId id in target.GetAnonymousBlockIds())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
attDefs = btr.GetAttributeDefinitions();
foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
{
BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForWrite);
br.ResetAttributes(attDefs, tr);
}
}
}
}
public static List GetAttributeDefinitions(this BlockTableRecord target)
{
if (target == null)
throw new ArgumentNullException("target");
Transaction tr = target.Database.TransactionManager.TopTransaction;
if (tr == null)
throw new AcRx.Exception(ErrorStatus.NoActiveTransactions);
List attDefs = new List();
foreach (ObjectId id in target)
{
if (id.ObjectClass == attDefClass)
{
AttributeDefinition attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
attDefs.Add(attDef);
}
}
return attDefs;
}
private static void ResetAttributes(this BlockReference br, List attDefs, Transaction tr)
{
Dictionary attValues = new Dictionary();
foreach (ObjectId id in br.AttributeCollection)
{
if (!id.IsErased)
{
AttributeReference attRef = (AttributeReference)tr.GetObject(id, OpenMode.ForWrite);
if (!attValues.ContainsKey(attRef.Tag))
{
attValues.Add(attRef.Tag,
attRef.IsMTextAttribute ? attRef.MTextAttribute.Contents : attRef.TextString);
}
attRef.Erase();
}
}
foreach (AttributeDefinition attDef in attDefs)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
if (attDef.Constant)
{
attRef.TextString = attDef.IsMTextAttributeDefinition ?
attDef.MTextAttributeDefinition.Contents :
attDef.TextString;
}
else if (attValues.ContainsKey(attRef.Tag))
{
attRef.TextString = attValues;
}
br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
} 厉害,学习学习! 学习了。好像单行文本可以直接赋值,多行文本却不行? 有办法修改多行文本属性的宽度吗?
页:
[1]