Author Topic: How to get and apply the correct MLeader vertiex??  (Read 5438 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: How to get and apply the correct MLeader vertiex??
« Reply #15 on: January 09, 2018, 10:34:22 AM »
I don't know how I stumbled upon this, but, I was trying to assign the textheight to the mtext entity instead of the mleader.  When I assign the height to mleader.textheight instead of mleader.mtext.textheight, it seems to work and the properties show the correct text height for paper space.

Now for more testing just to make sure...
Mexican Custard mentions that in link I posted below.

sonny3g

  • Newt
  • Posts: 27
Re: How to get and apply the correct MLeader vertiex??
« Reply #16 on: January 09, 2018, 10:47:55 AM »
That is likely where I got it from then.  Thanks to all who provided pointers and suggestions!!  I couldn't have got it to work without your help.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: How to get and apply the correct MLeader vertiex??
« Reply #17 on: January 10, 2018, 03:37:53 PM »
Okay, since back in the day I struggled with MLeaders this caught my attention, Today I had some time to go back through old code and see what I did and why. This is what I came up with.

Code - C#: [Select]
  1. using Autodesk.AutoCAD.DatabaseServices;
  2. using Autodesk.AutoCAD.EditorInput;
  3. using Autodesk.AutoCAD.Runtime;
  4. using MLeaderTest;
  5. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  6.  
  7. [assembly: CommandClass(typeof(Commands))]
  8.  
  9. namespace MLeaderTest
  10. {
  11.     public class Commands
  12.     {
  13.         [CommandMethod("COPYMODIFYMLEADER")]
  14.         public static void CopyModifyMLeader()
  15.         {
  16.             var db = Application.DocumentManager.MdiActiveDocument.Database;
  17.             var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  18.  
  19.             var peo = new PromptEntityOptions("Pick MLeader to copy");
  20.             peo.SetRejectMessage("Pick a MLeader");
  21.             peo.AddAllowedClass(typeof(MLeader), false);
  22.             var per = ed.GetEntity(peo);
  23.  
  24.             using (var tr = db.TransactionManager.StartTransaction())
  25.             {
  26.                 var existingLeader = (MLeader)tr.GetObject(per.ObjectId, OpenMode.ForRead);
  27.                 var mleader = (MLeader)existingLeader.Clone();
  28.  
  29.                 //Add MLeader to database before changing. AutoCAD changes text properties when you add it.
  30.                 var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  31.                 var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  32.                 ms.AppendEntity(mleader);
  33.                 tr.AddNewlyCreatedDBObject(mleader, true);
  34.  
  35.                 //Now make changes and Commit().
  36.                 //These seem to be the only properties that you have to set post clone to get a copy that behaves the same.
  37.                 var mtext = new MText
  38.                 {
  39.                     Contents = "COPIED\\PLEADER",
  40.                     TextStyleId = existingLeader.MText.TextStyleId
  41.                 };
  42.                 mleader.MText = mtext;
  43.                 mleader.TextHeight = existingLeader.TextHeight;
  44.                 mleader.TextAlignmentType = existingLeader.TextAlignmentType;
  45.                 mleader.TextLocation = existingLeader.TextLocation;
  46.  
  47.                 tr.Commit();
  48.             }
  49.         }
  50.     }
  51. }

This works for me even after saving and reopening the drawing. Hopefully it will be of some use to you.
Revit 2019, AMEP 2019 64bit Win 10