.NET

.NET

Reply
Valued Contributor
70 Posts
1 Kudo
Registered: ‎02-03-2012
Post 1 of 5
Accepted Solution

Change attribute color/layer per one block only

1431 Views, 4 Replies
06-19-2013 08:49 AM

Hello!

 

I've read this post:

http://through-the-interface.typepad.com/through_the_interface/2007/02/changing_the_co.html

 

Everything works fine, but I have one problem.


I just want to change colors for all objects in current model space. But this command also changes attributes/blocks definitions (I mean, any new block which I insert has the color that I specified previous).

 

Why? Where I have to change the code?

 

Maybe my english isn't so good, but I think I clearly understood whant you wnt to do.

 

If my C# is better than my English, here's a little sample to change the color of all entities in model space. Maybe you'd have some issues with MText which color is overwriten or with dimension and tables (which inherits from BlockReference).

 private void ChangeEntitesColorInModelSpace(short colorIndex)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (doc.LockDocument())
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Set the color of all entities within block definition to ByBlock
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
if (!btr.IsLayout)
{
foreach (ObjectId id in btr)
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
ent.ColorIndex = 0;
}
}
}
// Set the color of all entities within model space to the specified color
BlockTableRecord modelSpace =
(BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId id in modelSpace)
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
ent.ColorIndex = colorIndex;
// Sychronize attributes
if (ent is BlockReference)
{
BlockReference br = (BlockReference)ent;
foreach (ObjectId attId in br.AttributeCollection)
{
AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
att.ColorIndex = 0;
}
}
}
tr.Commit();
}
}

 

If I still misundertand you, here's another snippet to change the selected attribute references color:

 [CommandMethod("AttColor", CommandFlags.Modal)]
public void ChangeAttributeColor()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
RXClass rxc = RXClass.GetClass(typeof(AttributeReference));
using (Transaction tr = db.TransactionManager.StartTransaction())
{
while (true)
{
PromptNestedEntityResult res = ed.GetNestedEntity("\nSelect an attribute: ");
if (res.Status != PromptStatus.OK)
break;
if (res.ObjectId.ObjectClass != rxc)
{
ed.WriteMessage("only an attribute !");
continue;
}
AttributeReference att = (AttributeReference)tr.GetObject(res.ObjectId, OpenMode.ForWrite);
att.ColorIndex = 1;
db.TransactionManager.QueueForGraphicsFlush();
}
tr.Commit();
}
}

 

*Expert Elite*
2,142 Posts
255 Kudos
Registered: ‎04-29-2006
Post 2 of 5

Re : Change attribute color/layer per one block only

06-19-2013 09:59 AM in reply to: GrzesiekGP

Hi,

 

If you want to change the color of "all objects in current model space", you have to set the color of all block definitions components to ByBlock and change the color of all entities (block references too) in model space.

 

To set the block definitions components to ByBlock, you can use the way showned in Kean's routine.

Gilles Chanteau
Valued Contributor
70 Posts
1 Kudo
Registered: ‎02-03-2012
Post 3 of 5

Re : Change attribute color/layer per one block only

06-19-2013 11:14 AM in reply to: _gile

I think you misunderstood me.

 

Let me explain. Below code works fine - change the colors of all entities expect entities which are inside blocks:

 

 if (rbtSelection.Checked)
            {
                Autodesk.AutoCAD.ApplicationServices.DocumentLock dl = doc.LockDocument(Autodesk.AutoCAD.ApplicationServices.DocumentLockMode.ProtectedAutoWrite, null, null, true);
                using (dl)
                {
                    ObjectId msId = ObjectId.Null;

                    short colorIndex = (short)colorValue.Value;

                    LineWeight lineweight = (LineWeight)cmboxThick.SelectedValue;

                    ObjectId layerId = Layers.GetDrawingLayerObjectId(cmboxLayer.SelectedValue.ToString());

                    bool changeLineWeight = chckBoxThick.Checked;
                    bool changeLayer = chckBoxLayer.Checked;

                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTableRecord btRecord = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                        foreach (ObjectId id in btRecord)
                        {
                            Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                            ent.UpgradeOpen();
                            ent.ColorIndex = 6;
                            ent.DowngradeOpen();
                        }

                        tr.Commit();
                    }
                }
            }

 

If I go and iterate through the attributes references and change their color, any next new block which I add into my drawing, will have this color.

 

That's what I don't want.


I want something like we have "Edit attribute" per block in model space. I can set then color for this one block and any next new block will have standard color.

 

I hope, now it's more clearly :smileyhappy:

*Expert Elite*
2,142 Posts
255 Kudos
Registered: ‎04-29-2006
Post 4 of 5

Re : Change attribute color/layer per one block only

06-19-2013 12:05 PM in reply to: GrzesiekGP

Maybe my english isn't so good, but I think I clearly understood whant you wnt to do.

 

If my C# is better than my English, here's a little sample to change the color of all entities in model space. Maybe you'd have some issues with MText which color is overwriten or with dimension and tables (which inherits from BlockReference).

        private void ChangeEntitesColorInModelSpace(short colorIndex)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (doc.LockDocument())
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Set the color of all entities within block definition to ByBlock
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                    if (!btr.IsLayout)
                    {
                        foreach (ObjectId id in btr)
                        {
                            Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                            ent.ColorIndex = 0;
                        }
                    }
                }

                // Set the color of all entities within model space to the specified color
                BlockTableRecord modelSpace =
                    (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                foreach (ObjectId id in modelSpace)
                {
                    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    ent.ColorIndex = colorIndex;
                    // Sychronize attributes
                    if (ent is BlockReference)
                    {
                        BlockReference br = (BlockReference)ent;
                        foreach (ObjectId attId in br.AttributeCollection)
                        {
                            AttributeReference att = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
                            att.ColorIndex = 0;
                        }
                    }
                }
                tr.Commit();
            }
        }

 

If I still misundertand you, here's another snippet to change the selected attribute references color:

        [CommandMethod("AttColor", CommandFlags.Modal)]
        public void ChangeAttributeColor()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            RXClass rxc = RXClass.GetClass(typeof(AttributeReference));

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                while (true)
                {
                    PromptNestedEntityResult res = ed.GetNestedEntity("\nSelect an attribute: ");
                    if (res.Status != PromptStatus.OK) 
                        break;
                    if (res.ObjectId.ObjectClass != rxc)
                    {
                        ed.WriteMessage("only an attribute !");
                        continue;
                    }
                    AttributeReference att = (AttributeReference)tr.GetObject(res.ObjectId, OpenMode.ForWrite);
                    att.ColorIndex = 1;
                    db.TransactionManager.QueueForGraphicsFlush();
                }
                tr.Commit();
            }
        }

 

Gilles Chanteau
Valued Contributor
70 Posts
1 Kudo
Registered: ‎02-03-2012
Post 5 of 5

Re: Change attribute color/layer per one block only

06-19-2013 10:34 PM in reply to: GrzesiekGP

You're my master. First solution works good :smileyhappy:

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!