如何设置常量属性attributerreference

本文关键字:常量 属性 attributerreference 设置 何设置 | 更新日期: 2023-09-27 18:09:53

是否可以更改attributerreference的Constant属性?(属性IsConstant为只读)

如何设置常量属性attributerreference

我不知道objectax,但是在。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();
            }
        }

您需要更改块表记录(属性Constant)中的AttributeDefinition,而不是AttributeReference。此属性与所有AttributeReference共享。

From the docs:

AutoCAD本身从不创建一个常量attributerreference对象。AutoCAD为每个BlockReference创建attributerreference对象基于引用中的AttributeDefinition对象BlockTableRecord。如果遇到常量AttributeDefinition,然后AutoCAD使用AttributeDefinition本身,而不是创建一个匹配AttributeReference。