Setting position of an MText for each annotation scale

<?xml encoding=”UTF-8″>By Balaji Ramamoorthy

For an MText that is annotative, its positions can be changed using its grip. The position is specific to the current annotative scale of the drawing. The API to set the position of an annotative entity for each scale programmatically is not available at present as part of the public API. A way to workaround this is to set the drawing annotation scale before changing the position. Here is a sample code to iterate the object context collection of the database and set the position of an MText for each scale.

 Document doc = 
 Application.DocumentManager.MdiActiveDocument;
 Database db = doc.Database;
 Editor ed = doc.Editor;
 
 PromptEntityOptions peo 
 = <span>new</span><span>  PromptEntityOptions(<span>"\nSelect an MText : "</span><span> );</span></span>
 peo.SetRejectMessage(<span>"\nMust be an MText ..."</span><span> );</span>
 peo.AddAllowedClass(<span>typeof</span><span> (MText), <span>true</span><span> );</span></span>
 
 PromptEntityResult per = ed.GetEntity(peo);
 
 <span>if</span><span>  (per.Status != PromptStatus.OK)</span>
     <span>return</span><span> ;</span>
 
 ObjectId mtId = per.ObjectId;
 
 ObjectContextManager ocm = db.ObjectContextManager;
 ObjectContextCollection occ 
 = ocm.GetContextCollection(<span>"ACDB_ANNOTATIONSCALES"</span><span> );</span>
 
 <span>if</span><span>  (ocm == <span>null</span><span> )</span></span>
     <span>return</span><span> ;</span>
 
 <span>foreach</span><span>  (ObjectContext oc <span>in</span><span>  occ)</span></span>
 <span>{</span>
     <span>using</span><span>  (Transaction tr </span>
         = db.TransactionManager.StartTransaction())
     <span>{</span>
         MText mt 
         = tr.GetObject(mtId, OpenMode.ForRead) <span>as</span><span>  MText;</span>
         Point3d pos = mt.Location;
         <span>if</span><span>  (mt.HasContext(oc))</span>
         <span>{</span>
             AnnotationScale annoScale 
             = oc <span>as</span><span>  AnnotationScale;</span>
             
             <span>if</span><span>  (annoScale != <span>null</span><span> )</span></span>
                 db.Cannoscale = annoScale;
 
             mt.UpgradeOpen();
             
             mt.Location = pos 
             + Vector3d.XAxis * 3 
             + Vector3d.YAxis * 3;
         <span>}</span>
         tr.Commit();
     <span>}</span>
 <span>}</span>
 

Here are two screenshots showing the scale specific positions before and after the change

BeforeAfter


Comments

One response to “Setting position of an MText for each annotation scale”

  1. Bruno GEOFFROY Avatar
    Bruno GEOFFROY

    Why they do not give access to the annotation scale info?
    I have some (plenty) drawings where the annotative position of block attributes have been set to a wrong z values (how? I really do not know how draughtsmen have made this…).
    I have tried with .net, VBA or ObjectARX to get access to the position points for each scale and set their z to zero for all but it does not seem to be available.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading