Author Topic: Insert a text line in every layout  (Read 4942 times)

0 Members and 1 Guest are viewing this topic.

teslaxx

  • Guest
Insert a text line in every layout
« on: November 29, 2010, 07:36:02 AM »
1. How can I loop through all the layouts and make changes to all the layouts?  I need to insert a line of text in each layer, beginning with layer number 5.

2. How can I set the font and the size for a mtext? I found the Height attribute, but it doesn't seam it has any effect, because I created 2 MText objects with 2 different height values and I don't see any difference between those 2 lines of text ( in fact the height is a different value - not those that I enter). On the same issue, how can I set the font to be Arial?

Could you give some code please?  :-)


P.S.  I have experience with programming, but I don't know where to look to see some examples. In fact, where could I find the API for Autocad .Net, something similar of Java Library? ( http://download.oracle.com/javase/1.4.2/docs/api/ )

teslaxx

  • Guest
Re: Insert a text line in every layout
« Reply #1 on: November 29, 2010, 08:25:59 AM »
For question number 2:

For setting the height of the text:
Code: [Select]
MText objText = new MText();
objText.TextHeight = 2.2;

For setting the font:
 
Code: [Select]
private void setFont(Document acDoc, Database acCurDb, String newFont) {
            // Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {

                // Open the current text style for write
                TextStyleTableRecord acTextStyleTblRec;
                acTextStyleTblRec = acTrans.GetObject(acCurDb.Textstyle, OpenMode.ForWrite) as TextStyleTableRecord;

                // Get the current font settings
                Autodesk.AutoCAD.GraphicsInterface.FontDescriptor acFont;
                acFont = acTextStyleTblRec.Font;

                // Update the text style's typeface with the given font
                Autodesk.AutoCAD.GraphicsInterface.FontDescriptor acNewFont;
                acNewFont = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(newFont, acFont.Bold, acFont.Italic, acFont.CharacterSet, acFont.PitchAndFamily);
                acTextStyleTblRec.Font = acNewFont;
                acDoc.Editor.Regen();

                // Save the changes and dispose of the transaction
                acTrans.Commit();
            }
}


But no progress towards the first issue! :) Help! :)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Insert a text line in every layout
« Reply #2 on: November 29, 2010, 09:10:31 AM »
If you change TextHeight to  the TextstyleTableRecord TextSize or change the the Database TextSize it will equal that

Code: [Select]
acTextStyleTblRec.TextSize = 2.2;
objText.TextHeight = acTextStyleTblRec.TextSize;



Code: [Select]

Database.TextSize = acTextStyleTblRec.TextSize;



I would you the first because the second is used if your text height is set to zero just to keep consistent

teslaxx

  • Guest
Re: Insert a text line in every layout
« Reply #3 on: November 29, 2010, 09:56:14 AM »
Thanx a lot!!
But no ideas rigarding
Quote
How can I loop through all the layouts and make changes to all the layouts?  I need to insert a line of text in each layer, beginning with layer number 5.
?


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Insert a text line in every layout
« Reply #4 on: November 29, 2010, 11:42:38 AM »
Layouts are stored within a dictionary associated with the database.  Just loop through that, and grab the BlockTableRecord for each, and add the text there.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Insert a text line in every layout
« Reply #5 on: November 29, 2010, 07:54:32 PM »
I forgot to change the layers it is easy to figure out

Creats 10 layers & add 5 mtext to each layout

Code: [Select]
[CommandMethod("AddToEachLayout")]
        public static void AddToEachLayout()
        {
            Database db = HostApplicationServices.WorkingDatabase;
           
            using (Transaction trx = db.TransactionManager.StartTransaction())
         
            {
                LayerTable layerTble = db.LayerTableId.GetObject(OpenMode.ForWrite) as LayerTable;
                for (int i = 1; i < 11; i++)
                {
                    LayerTableRecord layerTblRec = new LayerTableRecord();
                    layerTblRec.Name = "Layer" + i;
                    layerTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, (short)i);
                    layerTble.Add(layerTblRec);
                    trx.AddNewlyCreatedDBObject(layerTblRec, true);
                }
                DBDictionary  layoutDictionary = db.LayoutDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;
                Point3d pnt = Point3d.Origin;
                foreach (DBDictionaryEntry DictEntry in layoutDictionary)
                {
                    Layout layout = DictEntry.Value.GetObject(OpenMode.ForRead) as Layout;
                    BlockTableRecord layoutBtr = layout.BlockTableRecordId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
                    TextStyleTableRecord txtStyle = db.Textstyle.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    txtStyle.TextSize = 2.2;
                    for (int j = 0; j < 50; j = j + 10)
                    {
                    MText mtxt = new MText();
                    mtxt.Contents = "Here Is Text";
                    mtxt.Height = txtStyle.TextSize;
                    mtxt.Location = new Point3d(pnt.X + j, pnt.Y + j, 0);
                    layoutBtr.AppendEntity(mtxt);
                    trx.AddNewlyCreatedDBObject(mtxt, true);
                   
                    }
                }           
           
                trx.Commit();
            }


        }


teslaxx

  • Guest
Re: Insert a text line in every layout
« Reply #6 on: November 30, 2010, 03:04:21 AM »
Thank you!!! Jeff H and T.Willey  :-)

In fact, where could I find the API for Autocad .Net, something similar of Java Library? ( http://download.oracle.com/javase/6/docs/api/ )
« Last Edit: November 30, 2010, 04:12:15 AM by teslaxx »

kaefer

  • Guest
Re: Insert a text line in every layout
« Reply #7 on: November 30, 2010, 05:18:31 AM »
In fact, where could I find the API for Autocad .Net,

Visual Studio Object Browser for casual use, Red Gate's Reflector for heavy duty isn't good enough?

Seriously, introductory texts are here: http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer%27s%20Guide/index.html,
a more complete documentation in Windows CHM-format based on Rel. 2010 is included in the ObjectARX download, available here: http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=785550.

HTH

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Insert a text line in every layout
« Reply #8 on: November 30, 2010, 05:36:02 AM »
As  kaefer indicated :-
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

fixo

  • Guest
Re: Insert a text line in every layout
« Reply #9 on: November 30, 2010, 02:34:15 PM »
Thanx a lot!!
But no ideas rigarding
Quote
How can I loop through all the layouts and make changes to all the layouts?  I need to insert a line of text in each layer, beginning with layer number 5.
?


Try this quick example
Code: [Select]

        public void LoopLayouts()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Open dictionary for reading
                DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                // Loop through dictionary entries
                foreach (DictionaryEntry entry in layoutDict)
                {
                    Layout ltr = (Layout)tr.GetObject((ObjectId)entry.Value, OpenMode.ForWrite);

                    // Check for the tab number (zero-based , 0 is for modelspace)
           
                    if ((ltr.IsWriteEnabled) && (ltr.TabOrder > 5))
                    {
                        ed.WriteMessage("\n{0}\t{1}", ltr.TabOrder,ltr.LayoutName);
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(ltr.BlockTableRecordId, OpenMode.ForWrite);
                        DBText txt = new DBText();
                        txt.SetDatabaseDefaults();
                        txt.Position = new Point3d(0, 0, 0);
                        txt.Height = 12.0;
                        txt.ColorIndex = 30;
                        txt.TextString = DateTime.Now.ToLongDateString();
                        txt.HorizontalMode = TextHorizontalMode.TextCenter;
                        txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                        txt.AlignmentPoint = txt.Position;
                        btr.AppendEntity(txt);
                        tr.AddNewlyCreatedDBObject(txt, true);
                    }


                }
                tr.Commit();
            }
        }