.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Slow Processing Multileaders

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
BrentBurgess1980
714 Views, 6 Replies

Slow Processing Multileaders

Afternoon Everyone,

 

I am writing a drawing number transposition plugin that will iterate all MText, DText, MLeaders etc in a drawing.

 

The below code flies through the Mtext, but struggles on the MLeader.

 

private static void process_QualifiedNumbers()
            {
            DateTime start = DateTime.Now;
            processMText_Qualified();
            DateTime end = DateTime.Now;
            Ed.WriteMessage("\n{0} Mtext processed in {1}s", MText.Count, end.Subtract(start).TotalSeconds);

            start = DateTime.Now;
            processMLeader_Qualified();
            end = DateTime.Now;
            Ed.WriteMessage("\n{0} MLeaders processed in {1}s", Multileaders.Count, end.Subtract(start).TotalSeconds);
            }

        private static void processMText_Qualified()
            {
            foreach (MText mt in MText)
                {
                foreach (DrawingNumber dwg in DrawingNumbers.List)
                    {
                    if (mt.Contents.Contains(dwg.ProjectNumber))
                        {
                        mt.Contents = mt.Contents.Replace(dwg.ProjectNumber, dwg.ClientNumber);
                        }
                    }
                }
            }

        private static void processMLeader_Qualified()
            {
            foreach (MLeader ml in Multileaders)
                {
                foreach (DrawingNumber dwg in DrawingNumbers.List)
                    {
                    if (ml.MText.Contents.Contains(dwg.ProjectNumber))
                        {
                        ml.MText.Contents = ml.MText.Contents.Replace(dwg.ProjectNumber, dwg.ClientNumber);
                        }
                    }
                }
            }

 

The output is

8 Mtext processed in 0.0468003s
22 MLeaders processed in 71.9402444s

 

however the MLeader text does not change.

 

Am I doing something wrong? Is there a way to speed it up and get the MLeader text to change?

 

Cheers,

 

Brent

6 REPLIES 6
Message 2 of 7
FRFR1426
in reply to: BrentBurgess1980

Reading Contents property need some processing because the formatiing data used for word wrap calculations is removed before the string is copied. So may be you should cache the text. You can also remove the call to Contains: if there is no match, Replace will do nothing.

 

private static void processMLeader_Qualified()
{
  foreach (MLeader ml in Multileaders)
  {
    MText mText = ml.MText;
    string contents = mText.Contents;    
    foreach (DrawingNumber dwg in DrawingNumbers.List)
    {
mText.Contents = contents.Replace(dwg.ProjectNumber, dwg.ClientNumber); } } }

You should also use System.Diagnostics.StopWatch instead of DateTime.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 3 of 7
BrentBurgess1980
in reply to: FRFR1426

Thanks FRFR1426.

 

The contains does seem redundant doesnt it - Thanks for that.

 

With the Contents property, would that not apply to MText too? The MText is being processed in next to no time.

 

Cheers

B

Message 4 of 7
FRFR1426
in reply to: BrentBurgess1980

Yes, but who knows? It can only comes from the MText or the Contents properties.

 

Just enclose your code with a pair of StopWatch.StartNew/Debug.Print: 

 

var sw = StopWatch.StartNew();
MText mText = ml.MText;
Debug.Print("Elapsed ms for MText property: {0} ms", sw.ElapsedMilliseconds);
sw.Restart();
string contents = mText.Contents; 
Debug.Print("Elapsed ms for Contents property: {0} ms", sw.ElapsedMilliseconds);

 

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 5 of 7
BrentBurgess1980
in reply to: FRFR1426

Interestingly, when the below code processes in no time.

22 MLeaders processed in 0.0018731s

 

private static void processMLeaders_Qualified()
            {
            foreach (MLeader ml in Multileaders)
                {
                MText mt = ml.MText;
                foreach (DrawingNumber dwg in DrawingNumbers.List)
                    {
                    mt.Contents = mt.Contents.Replace(dwg.ProjectNumber, dwg.ClientNumber);
                    //ml.MText = mt;
                    }
                }
            }

 

 

However, once we remove the comment to make then line

ml.MText = mt;

 

The code process takes a lot longer

22 MLeaders processed in 55.1823468s

 

Still can't figure out why.

Message 6 of 7

Hi Brent,

 

Sorry, this does not answer your query on why its slower, but the right way to change the MLeader text is to assign a MText to it and not change its contents directly. As i understand, the MLeader would not reflect the change to its contents if that is not the case.

 

So I am not sure why would want to change the contents rather than assign MLeader's MText.

 

Here is a sample code that you can try :

 

Document activeDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = activeDoc.Database;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

PromptEntityOptions peo = new PromptEntityOptions("Select an entity : ");
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
ObjectId entId = per.ObjectId;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
MLeader ml = tr.GetObject(entId, OpenMode.ForWrite) as MLeader;
if (ml != null)
{
string originalText = ml.MText.Contents;

// Does not work
ml.MText.Contents = originalText.ToUpper();

// Works
//MText mt = new MText();
//mt.Contents = originalText.ToUpper();
//ml.MText = mt;
}
tr.Commit();
}

 Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Message 7 of 7

All solved - Was calling the Mleader.MText multiple times slowing it down.

 

This way only calls it once.

 

private static void processMLeaders_Qualified()
            {
            foreach (MLeader ml in Multileaders)
                {
                MText mt = ml.MText;

                foreach (KeyValuePair<string, string> pair in DrawingNumbers.List)
                    {
                    mt.Contents = mt.Contents.Replace(pair.Key, pair.Value);
                    ml.MText = mt;
                    }
                }
            }

 

Cheers Balaji for the help in pointing that out.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost