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

12 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. And also, we DO accept tips 😀

Leave a Reply

Your email address will not be published.

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

Close

Meta