如何设置常量属性 AttributeReference?
How to set the Constant property AttributeReference?
是否可以更改 AttributeReference 的常量 属性?
(属性 IsConstant 是只读的)
我不知道 objectarx,但在 .Net 中(因为你说的是 c#),试试这个:
Database currentDB = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction transaction = currentDB.TransactionManager.StartTransaction())
{
BlockTable blockTable = (BlockTable)transaction.GetObject(currentDB.BlockTableId, OpenMode.ForRead);
//Create the block if it doesn't exist
if (!blockTable.Has("MyBlock"))
{
using (BlockTableRecord myBlock = new BlockTableRecord())
{
myBlock.Name = "MyBlock";
myBlock.Origin = Point3d.Origin;
//You can add geometry here, but I'm just going to create the attribute
using (AttributeDefinition attribute = new AttributeDefinition())
{
attribute.Position = Point3d.Origin;
attribute.Tag = "Constant";
attribute.Prompt = "Enter value: ";
attribute.TextString = "My value";
attribute.Height = 0.5;
attribute.Justify = AttachmentPoint.BottomLeft;
attribute.Constant = true;
myBlock.AppendEntity(attribute);
//add to the block table
blockTable.UpgradeOpen();
blockTable.Add(myBlock);
transaction.AddNewlyCreatedDBObject(myBlock, true);
}
}
transaction.Commit();
}
}
编辑: 我刚刚意识到您还说了 AttributeReference 而不是 AttributeDefinition。在不确定验证的情况下,我认为不能直接修改引用,因为该值是常量。因此,您必须在 BlockTableRecord 中更新块的定义,一旦获得它,基本过程相同。
更新代码如下:
Database currentDB = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction transaction = currentDB.TransactionManager.StartTransaction())
{
BlockTable blockTable = (BlockTable)transaction.GetObject(currentDB.BlockTableId, OpenMode.ForRead);
//Create the block if it doesn't exist
if (blockTable.Has("MyBlock"))
{
//Get the block
ObjectId myBlockID = blockTable["MyBlock"];
BlockTableRecord myBlock = (BlockTableRecord)transaction.GetObject(myBlockID, OpenMode.ForRead);
//iterate through objects and update the attribute definition
if (myBlock.HasAttributeDefinitions)
{
foreach (ObjectId oid in myBlock)
{
DBObject dbObject = transaction.GetObject(oid, OpenMode.ForRead);
if (dbObject is AttributeDefinition)
{
AttributeDefinition attribute = (AttributeDefinition)dbObject;
attribute.UpgradeOpen();
attribute.TextString = "NewValue";
attribute.Constant = true;
}
}
}
//Remember... BlockRerefences are glorified pointers to the BlockTableRecord that defines them
//so let's get all block references associated to it and update them
foreach (ObjectId oid in myBlock.GetBlockReferenceIds(false, true))
{
BlockReference blockReference = (BlockReference)transaction.GetObject(oid, OpenMode.ForWrite);
blockReference.RecordGraphicsModified(true);
}
transaction.Commit();
}
}
您需要更改块 table 记录(属性 Constant
)中的 AttributeDefinition
,而不是 AttributeReference
。此 属性 与所有 AttributeReference
共享。
来自文档:
AutoCAD itself never creates a constant AttributeReference object.
AutoCAD creates the AttributeReference objects for each BlockReference
based on the AttributeDefinition objects within the referenced
BlockTableRecord. If a constant AttributeDefinition is encountered,
then AutoCAD uses the AttributeDefinition itself instead of creating a
matching AttributeReference.
是否可以更改 AttributeReference 的常量 属性? (属性 IsConstant 是只读的)
我不知道 objectarx,但在 .Net 中(因为你说的是 c#),试试这个:
Database currentDB = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction transaction = currentDB.TransactionManager.StartTransaction())
{
BlockTable blockTable = (BlockTable)transaction.GetObject(currentDB.BlockTableId, OpenMode.ForRead);
//Create the block if it doesn't exist
if (!blockTable.Has("MyBlock"))
{
using (BlockTableRecord myBlock = new BlockTableRecord())
{
myBlock.Name = "MyBlock";
myBlock.Origin = Point3d.Origin;
//You can add geometry here, but I'm just going to create the attribute
using (AttributeDefinition attribute = new AttributeDefinition())
{
attribute.Position = Point3d.Origin;
attribute.Tag = "Constant";
attribute.Prompt = "Enter value: ";
attribute.TextString = "My value";
attribute.Height = 0.5;
attribute.Justify = AttachmentPoint.BottomLeft;
attribute.Constant = true;
myBlock.AppendEntity(attribute);
//add to the block table
blockTable.UpgradeOpen();
blockTable.Add(myBlock);
transaction.AddNewlyCreatedDBObject(myBlock, true);
}
}
transaction.Commit();
}
}
编辑: 我刚刚意识到您还说了 AttributeReference 而不是 AttributeDefinition。在不确定验证的情况下,我认为不能直接修改引用,因为该值是常量。因此,您必须在 BlockTableRecord 中更新块的定义,一旦获得它,基本过程相同。
更新代码如下:
Database currentDB = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction transaction = currentDB.TransactionManager.StartTransaction())
{
BlockTable blockTable = (BlockTable)transaction.GetObject(currentDB.BlockTableId, OpenMode.ForRead);
//Create the block if it doesn't exist
if (blockTable.Has("MyBlock"))
{
//Get the block
ObjectId myBlockID = blockTable["MyBlock"];
BlockTableRecord myBlock = (BlockTableRecord)transaction.GetObject(myBlockID, OpenMode.ForRead);
//iterate through objects and update the attribute definition
if (myBlock.HasAttributeDefinitions)
{
foreach (ObjectId oid in myBlock)
{
DBObject dbObject = transaction.GetObject(oid, OpenMode.ForRead);
if (dbObject is AttributeDefinition)
{
AttributeDefinition attribute = (AttributeDefinition)dbObject;
attribute.UpgradeOpen();
attribute.TextString = "NewValue";
attribute.Constant = true;
}
}
}
//Remember... BlockRerefences are glorified pointers to the BlockTableRecord that defines them
//so let's get all block references associated to it and update them
foreach (ObjectId oid in myBlock.GetBlockReferenceIds(false, true))
{
BlockReference blockReference = (BlockReference)transaction.GetObject(oid, OpenMode.ForWrite);
blockReference.RecordGraphicsModified(true);
}
transaction.Commit();
}
}
您需要更改块 table 记录(属性 Constant
)中的 AttributeDefinition
,而不是 AttributeReference
。此 属性 与所有 AttributeReference
共享。
来自文档:
AutoCAD itself never creates a constant AttributeReference object. AutoCAD creates the AttributeReference objects for each BlockReference based on the AttributeDefinition objects within the referenced BlockTableRecord. If a constant AttributeDefinition is encountered, then AutoCAD uses the AttributeDefinition itself instead of creating a matching AttributeReference.