Working with AutoCAD texts in pyautocad

While working with AutoCAD, correct description is very important. In such cases, we have to add texts to our AutoCAD drawings. In this blog post we are going to discuss how to work with normal texts & multiline texts using Python.

Setting up the environment for pyautocad

First of all, we will set up our work environment to integrate Python with AutoCAD by importing our pyautocad library.

from pyautocad import Autocad, APoint

acad = Autocad()

Working with normal texts with pyautocad

To insert a text string in an AutoCAD template, we need a very simple command. Let’s check out the syntax and use it.

Syntax:

acad.model.AddText(Text String, Insertion Point, Text Height)

t1 = acad.model.AddText("Hello", APoint(75, 50), 25)
Figure 1.1: Text using pyautocad

We can observe from the property box in Figure 1, that the text has been inserted as per the parameters passed by us.

Moving forward, we can also fetch some properties related to the text, for instance, text alignment, height, rotation, scale factor, etc.

Let’s take a look at them.

print("Text content: " + t1.TextString)
print("Text style: " + t1.StyleName)
print("Text insertion point: ", end=" " )
print(t1.InsertionPoint)
print("Text alignment: " + str(t1.Alignment))
print("Text alignment point: " + str(t1.TextAlignmentPoint))
print("Text height: " + str(t1.Height))
print("Text rotation: " + str(t1.Rotation))
print("Text scale factor: " + str(t1.ScaleFactor))
print("Is the text upside down: " + str(t1.UpsideDown))

O/p:

Text content: Hello
Text style: Standard
Text insertion point:  (75.0, 50.0, 0.0)
Text alignment: 0
Text alignment point: (0.0, 0.0, 0.0)
Text height: 25.0
Text rotation: 0.0
Text scale factor: 1.0
Is the text upside down: False

We can change the text and its properties using these functions. Let’s try the same.

t1.TextString = "Hi"
t1.Height = 50
t1.Rotation = 2
t1.ScaleFactor = 1
t1.UpsideDown = True
Figure 1.2: Changed properties of text using pyautocad

Working with multi-line AutoCAD text objects in pyautocad

Sometimes, we need to be more descriptive in the drawing to explain certain attributes, in that case, we need long sentences and hence the need for multiline texts arises. With this, we can set horizontal limits for the text we want to enter.

The command for multiline text is as simple as that of normal text.

Syntax:

acad.model.AddMText(Insertion Point, Width, Text String)

mt1 = acad.model.AddMText(APoint(275, 150), 100, "This is auotocad text")
mt1.Height = 25
Figure 2: Multi-line text with pyautocad

Here, after using the text command, we have set the text height specifically, otherwise, the text would have got created using the default text size in that AutoCAD template.

Leave a Reply

20 thoughts on “Working with AutoCAD texts in pyautocad

  1. Can you please tell me how to align(centre) text using pyautocad? I tried to do it but didn’t know its syntax.

    1. Hello Mr. Deshpande,

      Thanks for your question, we have a method called, Alignment, but that works only with the Text object and not multiline texts. This can be set to the desired value by using “text.Alignment = 0/1/2 and so on”.

      But the results we get are a bit odd as compared to what we expect. Hopefully, soon we will bring some solution for the same.

      Have a nice day!

      1. Hi, thank you for the reply. Could you please give an example by implementing the above method of text.Alignment = 0/1/2.
        Thank you.

  2. Where can I find the syntax and use of all the functions in pyautocad?
    Are there any books or documentation(other than readthedocs.io) for pyautocad? Please let me know.
    Thanks.

    1. Hello Mr. Deshpande,

      There is no particular page for using python syntax for automating AutoCAD. Hence we are documenting how to use pyautocad on this blog.
      But you can find the pyautocad module’s official page and ActiveX documentation by Autodesk for more information to understand how to use the same.

      Have a great day!

  3. How do you draw in paperspace or one of the layout
    I see all the method in acad.model.

    1. Hi,

      Thanks for the question.
      When we use acad it is actually generating a template with sheets named model, layout1 and layout2 after we assign acad to .AutoCAD(create_if_not_exists=True).

      Whatever we draw on that model space gets replicated to paperspace.

    1. Hi,

      Thanks for your question.
      Yes you can use AddDimAligned method as given below:

      object.AddDimAligned(ExtLine1Point, ExtLine2Point, TextPosition)

      where,
      ExtLine1Point : First end point (3 elemental array of doubles)
      ExtLine2Point : Second end point (3 elemental array of doubles)
      TestPosition : (3 elemental array of doubles)

      You can pass the values by fetching the same from the object on which you want to add dimension.

      Have a nice day!

      1. Fantastic! Thank Alot!

        How do I change the scale of it?

        I’m trying to configure it in ways like My_Dim.DimScale = 5 but no luck!

        1. Thank you for the question Tekoha.

          As Tanmay stated he will soon publish a blog post on this.

          If you have a specific problem we offer consulting at a fair fee. For this you can just contact us via the contact form or on LinkedIn.

  4. I am trying to take make the text that I generate go to a specific Layer. Do you know what command does that?
    I have currently:
    SO_Text = acad.model.AddText = (“SO NUMBER”, APoint(0, 0), 100)
    I have a layer called INPUT_TEXT, so i tried
    SO_Text.Layer = INPUT_TEXT
    but this didnt work

    1. Hi Michael,

      Thanks for the question.

      Have you tried using layer as string?
      e. g.
      SO_Text.Layer = “INPUT_TEXT”

      Because when we use Layer method against any object it returns a string.

      Have a nice day!

  5. Hello Linnart Felkl M.Sc. I have created 10 different sizes of rectangles that I want to automatically number.
    Of the 10 created rectangles, 3ea are the same size and the remaining 7ea are the same size. Can 2 different numbers be generated automatically in pyautocad?

    1. Eddy, thank you for your question. Yes, this is for sure possible and easy to implement. I suggest you send us your drawing and specify exactly what you want the program to do. Also specify if you are OK with executing the program via the terminal (e.g. powershell). You have the rectangles available in some directory or are you drawing them and adding to the drawing, and then your want them to replicated and numbered? Or are they all already available in the drawing?

      You can send us this data and info via the contact form on our website. We can implement this for you or a very small fee. Contact form is here: https://www.supplychaindataanalytics.com/contact/

  6. hi when I try to add multi-line text and it contains “{”
    this letter deleted
    example:
    copyw =”””H{lhgD ‘,G HgqgU 50.00 ljv
    Hglshp_m HB{lhgdm = 2500.00 L2
    “””

    it appears:
    HlhgD ‘,G HgqgU 50.00 ljv
    Hglshp_m HBlhgdm = 2500.00 L2

    and this is the code
    t2=acad.model.AddMText(APoint(0, 0), 100, copyw)

  7. when I try to add a multi-line text, and it contains “{” this letter disappears

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close

Meta