I'm using gdal, ezdxf, sqlite python packages and sqlite ODBC driver. They work fine, and in AutoCAD I can easily manually connect to my dbase.sqlite (read only) using _DBCONFIGURE and _DBCONNECT commands. Then, in AutoCAD, I manually make a connection template like table1Connection1 and select the primary key (in my case GID) from dbase. Then I save an empty DXF with dbconection and use it as header and trailer parameters in gdal to create a new dataset. Now using gdal, sqlite and\or ezdxf I want to convert to GIS files (mapinfo, shapefile, Filegdb etc.) with TABLE OF ATTRIBUTE. I've made a couple of hand-made connections to my dabse.sqlite in AutoCAD and saved it as sqlite_connected.dxf and then ran the following code:

#! c:\Python27\ArcGIS10.2\python.exe
# -*- coding: utf-8 -*- import ezdxf import os


def test_dxf(source_drawing):
    source_drawing = ezdxf.readfile_as_utf8(old_dxf, errors='strict')
    source_drawing.encoding = 'utf-8'
    modelspace = source_drawing.modelspace()

    for element in modelspace:
        print element.dxftype()
        print 'Valid_tag_list'
        print  element.valid_dxf_attrib_names()
        handle = element.dxf.handle
        tags = source_drawing.entitydb[handle]
        for tag in tags:
            print tag

if __name__ == '__main__':
    old_dxf = os.path.abspath('../../data/sqlite_connected.dxf')       

prints shows the following:

'[u'shadow_mode', u'layer', u'handle', u'linetype', u'color', u'start_param', u'major_axis', u'end_param', u'true_color', u'ltscale', u'color_name', u'transparency', u'owner', u'ratio', u'extrusion', u'invisible', u'paperspace', u'center']
DXFTag(code=0, value=u'ELLIPSE')
DXFTag(code=5, value=u'246')
DXFTag(code=330, value=u'1F')
DXFTag(code=100, value=u'AcDbEntity')
DXFTag(code=8, value=u'0')
DXFTag(code=62, value=3)
DXFTag(code=100, value=u'AcDbEllipse')
DXFTag(code=10, value=(-64.72147324890284, 1396.049664909873, 0.0))
DXFTag(code=11, value=(-432.5005430651363, 17.30093799269207, 0.0))
DXFTag(code=210, value=(0.0, 0.0, 1.0))
DXFTag(code=40, value=0.4321803290912964)
DXFTag(code=41, value=2.392818306675625)
DXFTag(code=42, value=6.340229330820504)
DXFTag(code=1001, value=u'DCO15')
DXFTag(code=1071, value=2)
DXFTag(code=1071, value=2)
DXFTag(code=1000, value=u'pipelineconnection1')
DXFTag(code=1004, value=u'02000000')
DXFTag(code=1071, value=1)
DXFTag(code=1000, value=u'pipelineconnection1')
DXFTag(code=1004, value=u'01000000')

As I understand I need to SET 3 tags in each entity I want to connect to dbase. These are:

  1. 1071 - the ordinal number of connection element.
  2. 1000 - ASCII string (up to 255 bytes long) in extended data. ('pipelineconnection1' in my case)
  3. 1004 - Chunk of bytes (up to 127 bytes long) in extended data. (primary key or connectoin filed number in hex)

But I can't use set methods to these kind of tags:

set_dxf_attrib(1071, 3)
set_dxf_attrib(1000, 'pipelineconnection1')
set_dxf_attrib(1004, hex(123))

This gives me an error: AttributeError: 1000 I can set only tags in the Valid_tag_list e.g. set_dxf_attrib('layer', 'my_layer').

How can I set tag by it's code not the name? Are there any other methods to connect attribute table to DXF entity using python?

Documentation: ezdxf , dxf specification

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.