.NET

Reply
Active Contributor
brianaubert
Posts: 38
Registered: ‎03-22-2005
Message 1 of 6 (237 Views)

Working in PaperSpace

237 Views, 5 Replies
11-30-2012 09:22 AM
      Dim acDoc As Document
      Dim acCurDb As Database
      Dim acBlkTbl As BlockTable
      Dim acBlkTblRec As BlockTableRecord
      Dim acText As DBText = New DBText()

      acDoc = Application.DocumentManager.MdiActiveDocument
      acCurDb = acDoc.Database

      Using acLckDoc As DocumentLock = acDoc.LockDocument

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

          acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

          acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), OpenMode.ForWrite)

          acText = New DBText()

          acText.Position = New Point3d(31.5359, 2.5536, 0)

          acBlkTblRec.AppendEntity(acText)

          acTrans.AddNewlyCreatedDBObject(acText, True)

          acTrans.Commit()

        End Using

        acDoc.Editor.UpdateScreen()

      End Using

 

I'm trying to fill out a sheet stocklist in paperspace. What is the trick to get the new text object located in the proper place? When I run the attached code, the text always is located at 0,0,0. Is there an offset I need to apply?

Mentor
khoa.ho
Posts: 219
Registered: ‎09-15-2011
Message 2 of 6 (223 Views)

Re: Working in PaperSpace

11-30-2012 11:44 AM in reply to: brianaubert

Hi,

 

Your code looks right to insert the text at a specified position in the first layout of paper space. I don't know why your text is located at (0, 0, 0). You may post your detailed code to help to see the problem.

 

-Khoa

 

Active Contributor
brianaubert
Posts: 38
Registered: ‎03-22-2005
Message 3 of 6 (219 Views)

Re: Working in PaperSpace

11-30-2012 11:55 AM in reply to: brianaubert
    Public Shared Sub New_Text(ByVal acObjIdColl As ObjectIdCollection, ByVal sSpace As String, ByVal Text_Location As Point3d, _
                               ByVal Text_Height As Double, ByVal Text_String As String, ByVal Layer As String, ByVal Text_Style As String, _
                               ByVal Color As String, ByVal Justification As String, ByVal Vertical_Alignment As String)
      Dim acDoc As Document
      Dim acCurDb As Database
      Dim acBlkTbl As BlockTable
      Dim acBlkTblRec As BlockTableRecord
      Dim acText As DBText

      acDoc = Application.DocumentManager.MdiActiveDocument
      acCurDb = acDoc.Database
      Using acLckDoc As DocumentLock = acDoc.LockDocument
        Application.SetSystemVariable("CLAYER", Layer)
        Application.SetSystemVariable("TEXTSTYLE", Text_Style)
        Application.SetSystemVariable("CECOLOR", Color)
        '' Start a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
          '' Open the Block table for read
          acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
          '' Open the Block table record Model space for write
          If sSpace = "Model" Then
            acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
          Else
            acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), OpenMode.ForWrite)
          End If
          acText = New DBText()
          acText.SetDatabaseDefaults()
          acText.Position = Text_Location
          acText.Height = Text_Height
          acText.TextString = Text_String
          acBlkTblRec.AppendEntity(acText)
          Select Case LCase(Justification)
            Case "right"
              If LCase(Vertical_Alignment) = "middle" Then
                acText.HorizontalMode = TextHorizontalMode.TextRight
                acText.VerticalMode = TextVerticalMode.TextVerticalMid
              Else
                acText.HorizontalMode = TextHorizontalMode.TextRight
              End If
            Case "center"
              If LCase(Vertical_Alignment) = "middle" Then
                acText.HorizontalMode = TextHorizontalMode.TextCenter
                acText.VerticalMode = TextVerticalMode.TextVerticalMid
              Else
                acText.HorizontalMode = TextHorizontalMode.TextRight
              End If
            Case "left"
              If LCase(Vertical_Alignment) = "middle" Then
                acText.HorizontalMode = TextHorizontalMode.TextLeft
                acText.VerticalMode = TextVerticalMode.TextVerticalMid
              Else
                acText.HorizontalMode = TextHorizontalMode.TextRight
              End If
          End Select
          acTrans.AddNewlyCreatedDBObject(acText, True)
          '' Save the changes and dispose of the transaction
          acTrans.Commit()
        End Using
        acDoc.Editor.UpdateScreen()
      End Using
    End Sub

 

Thank you for the response Khoa.

 

Here is the complete Sub.

 

I'm able to insert the frame work of the Stock list in the correct location. I don't understand the problem with the Text.

 

Brian

Mentor
khoa.ho
Posts: 219
Registered: ‎09-15-2011
Message 4 of 6 (211 Views)

Re: Working in PaperSpace

11-30-2012 02:03 PM in reply to: brianaubert

Hi Brian,

 

Sorry for a bit lag. I don't have a chance to test your code yet. I will check it again after work.

 

-Khoa

 

Mentor
khoa.ho
Posts: 219
Registered: ‎09-15-2011
Message 5 of 6 (203 Views)

Re: Working in PaperSpace

11-30-2012 05:59 PM in reply to: brianaubert

Hi Brian,

 

AutoCAD uses DBText.AlignmentPoint property to adjust alignment of a text if it is not horizontal left (TextHorizontalMode.TextLeft) and vertical base (TextVerticalMode.TextBase) by default.

 

DBText.Position defines a text insertion point while DBText.AlignmentPoint defines its alignment point. They are different. AlignmentPoint does not need to set (it will get default value 0,0,0), if the text is default horizontal left and vertical base (it will fail if we try to change AlignmentPoint at this time).

 

If we change horizontal and vertical alignments to be different with their default values, the Position property (lower left point of the text) becomes useless because it will be different depending on text height and width. Instead, we need to set AlignmentPoint property. The AlignmentPoint will be the text location at this time.

 

The problem is that the text is always at (0, 0, 0) point regardless of any DBText.Position because this text has alignment properties different with its default values.

 

So you just need to add acText.AlignmentPoint = Text_Location after modifying horizontal and vertical modes. I rewrite your code (in C#) with a test method to add texts in both Model and Paper spaces. The issue is the text alignment, not belongs to Paper space.

 

[CommandMethod("New_Text")]
public static void New_Text()
{
    New_Text(null, "Model", new Point3d(5, 10, 0), 2, "My model text", "0", "Standard", "2", "center", "middle");
    New_Text(null, "Layout", new Point3d(5, 10, 0), 2, "My paper text", "0", "Standard", "3", "left", "middle");
}

public static void New_Text(ObjectIdCollection acObjIdColl, string sSpace, Point3d Text_Location, double Text_Height,
                            string Text_String, string Layer, string Text_Style, string Color, string Justification,
                            string Vertical_Alignment)
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;
    using (DocumentLock acLckDoc = acDoc.LockDocument())
    {
        Application.SetSystemVariable("CLAYER", Layer);
        Application.SetSystemVariable("TEXTSTYLE", Text_Style);
        Application.SetSystemVariable("CECOLOR", Color);
        // Start a transaction
        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            // Open the Block table for read
            BlockTable acBlkTbl = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
            // Open the Block table record Model space for write
            BlockTableRecord acBlkTblRec;
            if ((sSpace == "Model"))
            {
                acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
            }
            else
            {
                acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
            }
            DBText acText = new DBText();
            acText.SetDatabaseDefaults();
            acText.Position = Text_Location;
            acText.Height = Text_Height;
            acText.TextString = Text_String;
            acBlkTblRec.AppendEntity(acText);
            switch (Justification.ToLower())
            {
                case "right":
                    if ((Vertical_Alignment.ToLower() == "middle"))
                    {
                        acText.HorizontalMode = TextHorizontalMode.TextRight;
                        acText.VerticalMode = TextVerticalMode.TextVerticalMid;
                    }
                    else
                    {
                        acText.HorizontalMode = TextHorizontalMode.TextRight;
                    }
                    break;
                case "center":
                    if ((Vertical_Alignment.ToLower() == "middle"))
                    {
                        acText.HorizontalMode = TextHorizontalMode.TextCenter;
                        acText.VerticalMode = TextVerticalMode.TextVerticalMid;
                    }
                    else
                    {
                        acText.HorizontalMode = TextHorizontalMode.TextRight;
                    }
                    break;
                case "left":
                    if ((Vertical_Alignment.ToLower() == "middle"))
                    {
                        acText.HorizontalMode = TextHorizontalMode.TextLeft;
                        acText.VerticalMode = TextVerticalMode.TextVerticalMid;
                    }
                    else
                    {
                        acText.HorizontalMode = TextHorizontalMode.TextRight;
                    }
                    break;
            }
            acText.AlignmentPoint = Text_Location;

            acTrans.AddNewlyCreatedDBObject(acText, true);
            // Save the changes and dispose of the transaction
            acTrans.Commit();
        }
    }
}

 

-Khoa

*Expert Elite*
_gile
Posts: 2,106
Registered: ‎04-29-2006
Message 6 of 6 (182 Views)

Re: Working in PaperSpace

12-01-2012 06:22 AM in reply to: khoa.ho

Hi,

 

By my side, I use a SetPosition() extension method for the DBText type. This method takes care of setting AlignmentPoint according to the text Justification.

 

C#

    public static class DBTextExtension
    {
        public static void SetPosition(this DBText text, Point3d pt, AttachmentPoint justif)
        {
            text.Position = pt;
            text.Justify = justif;
            if (text.HorizontalMode != TextHorizontalMode.TextLeft || 
                text.VerticalMode != TextVerticalMode.TextBase)
            {
                text.AlignmentPoint = pt;
                text.AdjustAlignment(HostApplicationServices.WorkingDatabase);
            }
        }
    }

 

F#

type DBText with
    member txt.SetPosition(pt, justif) =
        txt.Position <- pt
        txt.Justify <- justif
        if txt.HorizontalMode <> TextHorizontalMode.TextLeft ||
            txt.VerticalMode <> TextVerticalMode.TextBase then
            txt.AlignmentPoint <- pt
            txt.AdjustAlignment(HostApplicationServices.WorkingDatabase)

 

VB

    Module DBTextExtensions

        <System.Runtime.CompilerServices.Extension()> _
        Public Sub SetPosition(ByVal text As DBText, ByVal pt As Point3d, ByVal justif As AttachmentPoint)
            text.Position = pt
            text.Justify = justif
            If text.HorizontalMode <> TextHorizontalMode.TextLeft OrElse _
                text.VerticalMode <> TextVerticalMode.TextBase Then
                text.AlignmentPoint = pt
                text.AdjustAlignment(HostApplicationServices.WorkingDatabase)
            End If
        End Sub

    End Module

 

Gilles Chanteau
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.