Bricscad Python



In this tutorial I show you how to get data from an existing DXF drawing.

The previous example shows how to group entities by a single DXF attribute, but it is also possible to group entities by a custom key, to do so create a custom key function, which accepts a DXF entity as argument and returns a hashable value as dict-key or None to exclude the entity. The Python script is executed. BricsCAD reports the output in the command line panel. If the BimActivatePython user preference is already set: Displays a File dialog box. A new scripting engine will be built into the next release of ADACX that will enable both BricsCAD, AutoCAD and ADACX to be scripted in the user friendly Python programming language! This will enable end users to create simple text based automation scripts to automate not only ADACX but also BricsCAD/AutoCAD.

Loading the DXF file:

This works well with DXF files from trusted sources like AutoCAD or BricsCAD,for loading DXF files with minor or major flaws look at theezdxf.recover module.

See also

Layouts¶

I use the term layout as synonym for an arbitrary entity space which can containDXF entities like LINE, CIRCLE, TEXT and so on. Every DXF entity can only residein exact one layout.

There are three different layout types:

Bricscad Python Download

  • Modelspace: this is the common construction space
  • Paperspace: used to to create print layouts
  • BlockLayout: reusable elements, every block has itsown entity space

A DXF drawing consist of exact one modelspace and at least of one paperspace.DXF R12 has only one unnamed paperspace the later DXF versions support more thanone paperspace and each paperspace has a name.

Bricscad Python Tutorial

Iterate over DXF entities of a layout¶

Iterate over all DXF entities in modelspace. Although this is a possible way toretrieve DXF entities, I would like to point out that entity queries are thebetter way.

All layout objects supports the standard Python iterator protocol and thein operator.

Python

Access DXF attributes of an entity¶

Check the type of an DXF entity by e.dxftype(). The DXF type is alwaysuppercase. All DXF attributes of an entity are grouped in the namespaceattribute dxf:

See Common graphical DXF attributes

If a DXF attribute is not set (a valid DXF attribute has no value), aDXFValueError will be raised. To avoid this use theget_dxf_attrib() method with adefault value:

An unsupported DXF attribute raises an DXFAttributeError.

Getting a paperspace layout¶

Retrieves the paperspace named layout0, the usage of theLayout object is the same as of the modelspace object.DXF R12 provides only one paperspace, therefore the paperspace name in themethod call doc.layout('layout0') is ignored or can be left off.For the later DXF versions you get a list of the names of the availablelayouts by layout_names().

Retrieve entities by query language¶

ezdxf provides a flexible query language for DXF entities.All layout types have a query() method to startan entity query or use the ezdxf.query.new() function.

Bricscad

The query string is the combination of two queries, first the required entityquery and second the optional attribute query, enclosed in square brackets:'EntityQuery[AttributeQuery]'

The entity query is a whitespace separated list of DXF entity names or thespecial name *. Where * means all DXF entities, all other DXF nameshave to be uppercase. The * search can exclude entity types by adding theentity name with a presceding ! (e.g. *!LINE, search all entities except lines).

The attribute query is used to select DXF entities by its DXF attributes. Theattribute query is an addition to the entity query and matches only if theentity already match the entity query. The attribute query is aboolean expression, supported operators: and, or, !.

Get all LINE entities from the modelspace:

The result container EntityQuery also provides thequery() method, get all LINE entities at layer construction:

The * is a wildcard for all DXF types, in this case you could also useLINE instead of *, * works here because lines just containsentities of DXF type LINE.

All together as one query:

The ENTITIES section also supports the query() method:

Get all modelspace entities at layer construction, but excluding entitieswith linetype DASHED:

Retrieve entities by groupby() function¶

Search and group entities by a user defined criteria. As example let’s groupall entities from modelspace by layer, the result will be a dict with layernames as dict-key and a list of all entities from modelspace matching this layeras dict-value. Usage as dedicated function call:

Bricscad Python Free

The entities argument can be any container or generator which yieldsDXFEntity or inherited objects. Shorter and simplerto use as method of BaseLayout (modelspace,paperspace layouts, blocks) and query results asEntityQuery objects:

The previous example shows how to group entities by a single DXF attribute,but it is also possible to group entities by a custom key, to do so create acustom key function, which accepts a DXF entity as argument and returns ahashable value as dict-key or None to exclude the entity.The following example shows how to group entities by layer and color, soeach result entry has a tuple (layer,color) as key and a list of entitieswith matching DXF attributes:

Bricscad Python

To exclude entities from the result container the key function should returnNone. The groupby() function catchesDXFAttributeError exceptions while processing entities andexcludes this entities from the result container. So there is no need to worryabout DXF entities which do not support certain attributes, they will beexcluded automatically.

See also

groupby() documentation