Table Of Contents

Previous topic

Usage

This Page

API

This part of the documentation covers all the interfaces of pyautocad

Main Autocad interface

class pyautocad.api.Autocad(create_if_not_exists=False, visible=True)[source]

Main AutoCAD Automation object

Parameters:
  • create_if_not_exists – if AutoCAD doesn’t run, then new instanse will be crated
  • visible – new AutoCAD instance will be visible if True (default)
app[source]

Returns active AutoCAD.Application

if Autocad was created with create_if_not_exists=True, it will create AutoCAD.Application if there is no active one

doc[source]

Returns ActiveDocument of current Application

ActiveDocument

Same as doc

Application

Same as app

model[source]

ModelSpace from active document

iter_layouts(doc=None, skip_model=True)[source]

Iterate layouts from doc

Parameters:
  • doc – document to iterate layouts from if doc=None (default), ActiveDocument is used
  • skip_model – don’t include ModelSpace if True
iter_objects(object_name_or_list=None, block=None, limit=None, dont_cast=False)[source]

Iterate objects from block

Parameters:
  • object_name_or_list – part of object type name, or list of it
  • block – Autocad block, default - ActiveDocument.ActiveLayout.Block
  • limit – max number of objects to return, default infinite
  • dont_cast – don’t retrieve best interface for object, may speedup iteration. Returned objects should be casted by caller
iter_objects_fast(object_name_or_list=None, container=None, limit=None)[source]

Shortcut for iter_objects(dont_cast=True)

Shouldn’t be used in normal situations

find_one(object_name_or_list, container=None, predicate=None)[source]

Returns first occurance of object which match predicate

Parameters:
  • object_name_or_list – like in iter_objects()
  • container – like in iter_objects()
  • predicate – callable, which accepts object as argument and returns True or False
Returns:

Object if found, else None

best_interface(obj)[source]

Retrieve best interface for object

prompt(text)[source]

Prints text in console and in AutoCAD prompt

get_selection(text='Select objects')[source]

Asks user to select objects

Parameters:text – prompt for selection
static aDouble(*seq)

shortcut for pyautocad.types.aDouble()

static aInt(*seq)

shortcut for pyautocad.types.aInt()

static aShort(*seq)

shortcut for pyautocad.types.aShort()

pyautocad.api.ACAD

Constants from AutoCAD type library, for example:

text.Alignment = ACAD.acAlignmentRight

Types

class pyautocad.types.APoint[source]
3D point with basic geometric operations and support for passing as a
parameter for AutoCAD Automation functions

Usage:

>>> p1 = APoint(10, 10)
>>> p2 = APoint(20, 20)
>>> p1 + p2
APoint(30.00, 30.00, 0.00)

Also it supports iterable as parameter:

>>> APoint([10, 20, 30])
APoint(10.00, 20.00, 30.00)
>>> APoint(range(3))
APoint(0.00, 1.00, 2.00)

Supported math operations: +, -, *, /, +=, -=, *=, /=:

>>> p = APoint(10, 10)
>>> p + p
APoint(20.00, 20.00, 0.00)
>>> p + 10
APoint(20.00, 20.00, 10.00)
>>> p * 2
APoint(20.00, 20.00, 0.00)
>>> p -= 1
>>> p
APoint(9.00, 9.00, -1.00)

It can be converted to tuple or list:

>>> tuple(APoint(1, 1, 1))
(1.0, 1.0, 1.0)
x[source]

x coordinate of 3D point

y[source]

y coordinate of 3D point

z[source]

z coordinate of 3D point

distance_to(other)[source]

Returns distance to other point

Parameters:otherAPoint instance or any sequence of 3 coordinates
pyautocad.types.distance(p1, p2)[source]

Returns distance between two points p1 and p2

pyautocad.types.aDouble(*seq)[source]

Returns array.array of doubles (‘d’ code) for passing to AutoCAD

For 3D points use APoint instead.

pyautocad.types.aInt(*seq)[source]

Returns array.array of ints (‘l’ code) for passing to AutoCAD

pyautocad.types.aShort(*seq)[source]

Returns array.array of shorts (‘h’ code) for passing to AutoCAD


Utility functions

pyautocad.utils.unformat_mtext(s, exclude_list=('P', 'S'))[source]

Returns string with removed format information

Parameters:
  • s – string with multitext
  • exclude_list – don’t touch tags from this list. Default (‘P’, ‘S’) for newline and fractions
>>> text = ur'{\fGOST type A|b0|i0|c204|p34;TEST\fGOST type A|b0|i0|c0|p34;123}'
>>> unformat_mtext(text)
u'TEST123'
pyautocad.utils.mtext_to_string(s)[source]

Returns string with removed format innformation as unformat_mtext() and P (paragraphs) replaced with newlines

>>> text = ur'{\fGOST type A|b0|i0|c204|p34;TEST\fGOST type A|b0|i0|c0|p34;123}\Ptest321'
>>> mtext_to_string(text)
u'TEST123\ntest321'
pyautocad.utils.string_to_mtext(s)[source]

Returns string in Autocad multitext format

Replaces newllines \n with \P, etc.

pyautocad.utils.text_width(text_item)[source]

Returns width of Autocad Text or MultiText object

pyautocad.utils.timing(*args, **kwds)[source]

Context manager for timing execution

Usage:

with timing('some operation'):
    do_some_actions()

Will print:

some operation: 1.000 s  # where 1.000 is actual execution time
pyautocad.utils.dynamic_print(text)[source]

Prints text dynamically in one line

Used for printing something like animations, or progress


Working with Tables

class pyautocad.contrib.tables.Table[source]

Represents table with ability to import and export data to following formats:

  • csv
  • xls
  • xlsx (write only)
  • json

When you need to store some data, it can be done as follows:

table = Table()
for i in range(5):
    table.writerow([i, i, i])

table.save('data.xls', 'xls')

To import data from file, use data_from_file():

data = Table.data_from_file('data.xls')
writerow(row)[source]

Add row to table

Parameters:row (list or tuple) – row to add
append(row)[source]

Synonym for writerow()

clear()[source]

Clear current table

save(filename, fmt, encoding='cp1251')[source]

Save data to file

Parameters:
  • filename – path to file
  • fmt – data format (one of supported, e.g. ‘xls’, ‘csv’
  • encoding – encoding for ‘csv’ format
convert(fmt)[source]

Return data, converted to format

Parameters:fmt – desirable format of data

Note: to convert to csv format, use to_csv()

See also available_write_formats()

to_csv(stream, encoding='cp1251', delimiter=';', **kwargs)[source]

Writes data in csv format to stream

Parameters:
  • stream – stream to write data to
  • encoding – output encoding
  • delimitercsv delimiter
  • kwargs – additional parameters for csv.writer
static data_from_file(filename, fmt=None, csv_encoding='cp1251', csv_delimiter=';')[source]

Returns data in desired format from file

Parameters:
  • filename – path to file with data
  • fmt – format of file, if it’s None, then it tries to guess format from filename extension
  • csv_encoding – encoding for csv data
  • csv_delimiter – delimiter for csv data

Format should be in available_read_formats()