Author Topic: DBText HorizontalMode in a drawing database  (Read 3434 times)

0 Members and 1 Guest are viewing this topic.

wolfensteed

  • Guest
DBText HorizontalMode in a drawing database
« on: September 28, 2007, 12:01:59 PM »
When I insert a DBText object into an open drawing, I can set the HorizontalMode to TextMid and it seems to work fine. If I try the same thing to a database that I open, using the ReadDwgFile method, it doesn't seem to work correctly. When I open the drawing, the newly created Text object appears left justified. Viewing the Properties, it says that the justification is Middle center even though it is still left justified.
Is this an AutoCAD bug or PEBKAC?
I put together a small code sample to illustrate the problem.

      [Autodesk.AutoCAD.Runtime.CommandMethod("InsertCenteredText")]
      public void InsertCenteredText()
      {
         string sDwgPath = "C:\\Temp\\TestDrawing.dwg";
         if( System.IO.File.Exists(sDwgPath))
         {
         Database db = new Database(false, true);
         db.ReadDwgFile(sDwgPath, System.IO.FileShare.ReadWrite, true, null);
         Point3d testPoint = new Point3d(20, 20, 0);
         string sText = "TextHorizontalMode.TextCenter";
         DBText dbArcText = AddCenterJustifiedText(db, testPoint, sText, 45, 20);
         db.SaveAs(sDwgPath, DwgVersion.Current);
         db.CloseInput(true);
         }
      }

      public DBText AddCenterJustifiedText(Database db, Point3d aPnt, string sText, double dRotation, double dScale)
      {
         Transaction aT = null;
         BlockTable aBT;
         BlockTableRecord aBTR = null;
         DBText aText = null;
         try
         {
            aText = new DBText();
            aText.TextString = sText;
            aText.Height = .09;
            aText.HorizontalMode = TextHorizontalMode.TextMid;
            aText.VerticalMode = TextVerticalMode.TextVerticalMid;
            aText.SetDatabaseDefaults(db);
            aText.Rotation = dRotation * Math.PI / 180;
            aPnt = new Point3d(aPnt.X, aPnt.Y - (aText.Height * dScale * 1.5), aPnt.Z);
            aText.Position = aPnt;
            aText.AlignmentPoint = aPnt;
            Matrix3d matScale = Matrix3d.Scaling(dScale, aText.Position);
            aText.TransformBy(matScale);

            aT = db.TransactionManager.StartTransaction();
            aBT = (BlockTable)aT.GetObject(db.BlockTableId, OpenMode.ForRead);
            aBTR = (BlockTableRecord)aT.GetObject(aBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
            aBTR.AppendEntity(aText);
            aT.AddNewlyCreatedDBObject(aText, true);
            aT.Commit();
         }
         catch (Exception ex)
         {
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(System.Environment.NewLine + ex.Message + System.Environment.NewLine);
         }
         finally
         {
            if (aT != null && aT.IsDisposed == false) { aT.Dispose(); }
            if (aBTR != null && aBTR.IsDisposed == false) { aBTR.Dispose(); }
         }
         return aText;
      }

Thank you,
Walter Steed

T.Willey

  • Needs a day job
  • Posts: 5251
Re: DBText HorizontalMode in a drawing database
« Reply #1 on: September 28, 2007, 02:16:15 PM »
Moderator please move this to the .Net forum.  Thanks.


No to answer the op's question:
When working with text and attributes that are not left justified you have to make the database you are wanting to add the items to you current working database, but you MUST RETURN THE OLD DATABASE (the one open in the editor) TO THE CURRENT WORKING DATABASE!

ps.  Welcome to theSwamp Walter!
Tim

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

Please think about donating if this post helped you.

wolfensteed

  • Guest
Re: DBText HorizontalMode in a drawing database
« Reply #2 on: September 28, 2007, 03:02:03 PM »
Oh, well that was easy.
Thanks Tim.
I added ,,
Database dCurrent = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = db;
before the code and ..
HostApplicationServices.WorkingDatabase = dCurrent;
after and it works now.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: DBText HorizontalMode in a drawing database
« Reply #3 on: September 28, 2007, 03:20:36 PM »
You're welcome Walter.  I had a long thread on here talking about ObjectDBX (for .Net), but it wasn't needed, and the very reason is what I told you.  Once I learned that item it was committed to memory forever.  :wink:
Tim

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

Please think about donating if this post helped you.

wolfensteed

  • Guest
Re: DBText HorizontalMode in a drawing database
« Reply #4 on: September 28, 2007, 03:28:46 PM »
When I set the "HostApplicationServices.WorkingDatabase = db" it opens the drawing.
There are quite a few things I've been able to do without doing this.
Is there some general guidlines on when it is required and when it isn't?
Working in the DB without opening it can be much quicker.
I can annotate several drawing databases in the time it takes to open, annotate, save and close one.
Until I came across this justification problem, I was having a fair amount of sucess with adding and moving text, polylines, blockreferences, etc.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: DBText HorizontalMode in a drawing database
« Reply #5 on: September 28, 2007, 03:34:30 PM »
The only reasons I have found were text (attribute) based routines that change the value, or add new items with justification different than left.  Other people have said that they don't have the set the new DB as the Working DB, and they have a lot more experience with .Net than I do, so I figure they are correct.

Edit: Here is a link to one of Glenn R.'s post about him not having to change the DB to the Working DB,  So I would think it only needs to be done to update/show correctly attributes and text that isn't left justified.
« Last Edit: September 28, 2007, 03:53:39 PM by T.Willey »
Tim

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

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: DBText HorizontalMode in a drawing database
« Reply #6 on: October 01, 2007, 02:33:23 AM »
You will need to change the working dbase if you're doing textual mojo on an in-memory dbase...acad needs a point of reference so to speak.
If it's open in the editor (graphically), then you don't need to as the working dbase is the one associated with the current document.