.NET

.NET

Reply
Active Contributor
anikakramer2006
Posts: 29
Registered: ‎03-23-2011
Message 1 of 3 (177 Views)
Accepted Solution

How to get all MLeaders, even in Blocks nested ones?

177 Views, 2 Replies
08-13-2013 10:12 PM

Hello alltogether,

 

I need to EnableFrameText to all MLeaders in a drawing,

I also want to get the MLeaders who are nested in Blocks

automatically...

I started this way but It just works fine with the Mleaders in Modelspace

or if I open the Blockeditor to edit Mleaders nested in a Block...

I know I'm not going to "deep", maybe I need an other Transaction,

or thirst open each Block to edit... not right sure...

maybe I'm on a wrong way, absolutely...

can somebody help me?

 

Thank you :smileysurprised:)

 

my example:

 

        //Ändert leider nur die MLeader die nicht in einem Block sind....

        [CommandMethod("MLeaderIterator")]
        public static void MLeaderIterator_Method()
        {
            Database database = HostApplicationServices.WorkingDatabase;
            using (Transaction transaction = database.TransactionManager.StartTransaction())
            {
                BlockTable blkTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btRecord = (BlockTableRecord)transaction.GetObject(blkTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    
                foreach(ObjectId id in btRecord)
                {

                    if(id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(MLeader))))
                    {
                        MLeader _mLeader = (MLeader)transaction.GetObject(id, OpenMode.ForRead);
                        if (_mLeader.EnableFrameText == true)
                            _mLeader.UpgradeOpen();
                            _mLeader.EnableFrameText = false;
                            _mLeader.TextColor = Color.FromColorIndex(ColorMethod.ByColor, 240);
                            _mLeader.DowngradeOpen();
                    }
                
                }
                transaction.Commit();
            }
        }

Just iterate all BlockTableRecords in the BlockTable.

 

A 45 second refactor from previous snippet

 

 [CommandMethod("MLeaderIterator")]
public static void MLeaderIterator_Method()
{
IntPtr mleaderPtr = RXClass.GetClass(typeof(MLeader)).UnmanagedObject;
Database database = HostApplicationServices.WorkingDatabase;
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
BlockTable blkTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
foreach (ObjectId btRecordId in blkTable)
{
BlockTableRecord btRecord = (BlockTableRecord)transaction.GetObject(btRecordId, OpenMode.ForRead);
if (!(btRecord.IsDependent || btRecord.IsFromExternalReference || btRecord.IsAProxy))
{
foreach (ObjectId id in btRecord)
{
if (id.ObjectClass.UnmanagedObject == mleaderPtr)
{
MLeader _mLeader = (MLeader)transaction.GetObject(id, OpenMode.ForRead);
if (_mLeader.EnableFrameText == true)
{
_mLeader.UpgradeOpen();
_mLeader.EnableFrameText = false;
_mLeader.TextColor = Color.FromColorIndex(ColorMethod.ByColor, 240);
}
}
}
}
}
transaction.Commit();
}
}

 

Valued Mentor
jeff
Posts: 338
Registered: ‎05-12-2009
Message 2 of 3 (172 Views)

Re: How to get all MLeaders, even in Blocks nested ones?

08-13-2013 10:32 PM in reply to: anikakramer2006

Just iterate all BlockTableRecords in the BlockTable.

 

A 45 second refactor from previous snippet

 

       [CommandMethod("MLeaderIterator")]
       public static void MLeaderIterator_Method()
       {
           IntPtr mleaderPtr = RXClass.GetClass(typeof(MLeader)).UnmanagedObject;
           Database database = HostApplicationServices.WorkingDatabase;
           using (Transaction transaction = database.TransactionManager.StartTransaction())
           {
               BlockTable blkTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);

               foreach (ObjectId btRecordId in blkTable)
               {
                   BlockTableRecord btRecord = (BlockTableRecord)transaction.GetObject(btRecordId, OpenMode.ForRead);
                   if (!(btRecord.IsDependent || btRecord.IsFromExternalReference || btRecord.IsAProxy))
                   {
                       foreach (ObjectId id in btRecord)
                       {

                           if (id.ObjectClass.UnmanagedObject == mleaderPtr)
                           {
                               MLeader _mLeader = (MLeader)transaction.GetObject(id, OpenMode.ForRead);
                               if (_mLeader.EnableFrameText == true)
                               {
                                   _mLeader.UpgradeOpen();
                                   _mLeader.EnableFrameText = false;
                                   _mLeader.TextColor = Color.FromColorIndex(ColorMethod.ByColor, 240);
                               }
                           }
                       }
                   }
               }
               transaction.Commit();
           }
       }

 

You can also find your answers @ TheSwamp
Active Contributor
anikakramer2006
Posts: 29
Registered: ‎03-23-2011
Message 3 of 3 (168 Views)

Re: How to get all MLeaders, even in Blocks nested ones?

08-13-2013 10:49 PM in reply to: jeff

Good Morning,

wow, with an blink of an eye...

it worked perfect!

The only thing I had to add on the end of the transaction was:

 

Application.DocumentManager.MdiActiveDocument.Editor.Regen();

 

so the frames of the MLeaders are all gone.

 

Thank you so much!

This example will also help me for other selections.

Big help! :smileysurprised:)

Have a nice day,

 

Anika

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.