.NET

.NET

Reply
Active Contributor
29 Posts
0 Kudos
Registered: ‎03-23-2011
Post 1 of 3
Accepted Solution

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

183 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
341 Posts
53 Kudos
Registered: ‎05-12-2009
Post 2 of 3

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
29 Posts
0 Kudos
Registered: ‎03-23-2011
Post 3 of 3

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
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!