.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Working in PaperSpace

5 REPLIES 5
Reply
Message 1 of 6
brianaubert
456 Views, 5 Replies

Working in PaperSpace

      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?

5 REPLIES 5
Message 2 of 6
khoa.ho
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

 

Message 3 of 6
brianaubert
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

Message 4 of 6
khoa.ho
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

 

Message 5 of 6
khoa.ho
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

Message 6 of 6
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost