简体   繁体   English

如何设置常量属性AttributeReference?

[英]How to set the Constant property AttributeReference?

Is it possible to change the Constant property of an AttributeReference ? 是否可以更改AttributeReference的Constant属性? (the property IsConstant is readonly) (属性IsConstant为只读)

I don't know about objectarx, but in .Net (since you stated c#), try this: 我不了解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();
            }
        }

Edit: I just realized you also said AttributeReference and not AttributeDefinition. 编辑:我刚刚意识到你也说AttributeReference而不是AttributeDefinition。 Without verifying for sure, I think the reference cannot be modified directly since the value is constant. 如果不确定,我认为该引用不能直接修改,因为该值是恒定的。 Because of this, you'll have to update the block's definition in the BlockTableRecord, same basic process once you've got it. 因此,您必须在BlockTableRecord中更新块的定义,这是获得基本过程的基本过程。

Here's the code for updating: 这是更新代码:

       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();
            }
        }

You need to change the AttributeDefinition in the block table record (property Constant ), not on the AttributeReference . 您需要更改AttributeDefinition在块表记录(财产Constant ),而不是在AttributeReference This property is shared with all the AttributeReference . 此属性与所有AttributeReference共享。

From the docs: 从文档:

AutoCAD itself never creates a constant AttributeReference object. AutoCAD本身从不创建常量AttributeReference对象。 AutoCAD creates the AttributeReference objects for each BlockReference based on the AttributeDefinition objects within the referenced BlockTableRecord. AutoCAD根据所引用的BlockTableRecord中的AttributeDefinition对象为每个BlockReference创建AttributeReference对象。 If a constant AttributeDefinition is encountered, then AutoCAD uses the AttributeDefinition itself instead of creating a matching AttributeReference. 如果遇到常量AttributeDefinition,则AutoCAD将使用AttributeDefinition本身而不是创建匹配的AttributeReference。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM