3

One might expect that the "Color" CAD field would control this, but the driver doesn't allow it to be set.

schema = {'Layer': 'str', 'Color': 'int'}

with fiona.open(path, 'w', 'DXF', schema=schema) as f:
    f.writerecords(records)

Gives:

ERROR: failed to export vectors: DXF layer does not support arbitrary field creation, field 'Color' not created.

The driver documentation suggests that it attempts to interpret OGR feature styles, but I see no mention of these in the Fiona docs. Is it possible to set arbitrary colors with Fiona?

1 Answer 1

1

I had the same question. I did not find a solution to this direct question, but implemented a workaround using ezdxf.

I export from geopandas (using a fiona driver), import into ezdxf, set the layer colors, and resave.

doc = ezdxf.readfile(inputFileName)
layerDict = getLayerDic(doc)
#... manipulations of that same file in geopandas ...
exportGPDwPolygonstoDXF(df).to_file(outputFileName)
resaveWLayerColor(outputFileName, layerDict)

This is really a clunky workaround, but it may provide some short term solution.

def exportGPDwPolygonstoDXF(df):
    df1 = df.copy()
    df1['geometry'] = df['geometry'].apply(lambda x: LineString(x.exterior.coords))
    return df1
def resaveWLayerColor(fileName, layerDict):
    doc = ezdxf.readfile(fileName)
    modelspace = doc.modelspace()
    for layer in doc.layers:
        layer.color = layerDict[layer.dxf.name.split('_')[0]]
    doc.saveas('{}_c.dxf'.format(os.path.splitext(fileName)[0]))
def getLayerDic(doc):
    layers = doc.layers
    return {lay.dxf.name: lay.color for lay in layers}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.