1

I have a problem converting multiple DXF files into shapefiles. In particular I use the script that is shown below for conveying multiple files. But the output shapefile is very large due to the number of attributes. I would like to be able to export only the Elevation field (I am interested only to points entities) in the shp file in output. But I cannot use the FieldMapping class effectively. I have an elementary knowledge of ArcPy.

What should I do?

import arcpy
import os    
from arcpy import env
env.overwriteOutput = True
in_workspace    = arcpy.GetParameterAsText(0)
outFolder       = arcpy.GetParameterAsText(1)
env.workspace   = in_workspace
datasets = arcpy.ListDatasets()
for dataset in datasets:
    filesets = arcpy.ListFiles(dataset)    
    for f in filesets:
        print f
        InPoint = os.path.join(env.workspace,f,"Point")    # the CAD points
        SaniF   = f.replace(" ","_").replace("-","_").replace(".","_")  # remove invalid chars..
        if int(arcpy.GetCount_management(InPoint).getOutput(0)) == 0:
            arcpy.AddWarning("- No points")
        else:
            outPointShape = '{}_Point.shp'.format(SaniF)
            arcpy.AddMessage('- Exporting Points')
            myfields = ['Elevation']
            mapS = arcpy.FieldMappings()
            for field in myfields:
                map = arcpy.FieldMap()
                map.addInputField(inPoint, field)
                mapS.addFieldMap(map)
            arcpy.FeatureClassToFeatureClass_conversion(InPoint,outFolder,outPointShape,"#", mapS)
  • 2
    I'd simply delete other fields after export. – FelixIP Feb 7 at 19:24
  • Or populate a temporary file geodatabase feature class, which won't be subject to size limitations, the extract only what is needed. – Vince Feb 8 at 0:14
  • for a dxf of 40mb I get a shp of 600mb because of the .dbf file. then deleting the fields takes a long time. for this I would like to be able to select the fields to be exported before the coversion. – Cinzia Forcina Feb 8 at 5:10
0

After you find point part of drawing, apply this:

destFC=r'C:\SCRATCH\abcd.dwg\Point'
arcpy.MakeFeatureLayer_management(destFC, "AB")
destFields=arcpy.ListFields("AB")
desc = arcpy.Describe("AB")
field_info = desc.fieldInfo
for i,f in enumerate(destFields):
    if f.name in ("FID","Shape","Elevation"):continue
    field_info.setVisible (i, "HIDDEN")
addLayer="TO_CALC"
arcpy.MakeFeatureLayer_management(destFC, addLayer,"","",field_info)
arcpy.CopyFeatures_management(addLayer,"c:/scratch/points.shp")
1

Thank you!!! Below is the final script I used:

import arcpy
import os    
from arcpy import env
env.overwriteOutput = True
in_workspace    = arcpy.GetParameterAsText(0)
outFolder       = arcpy.GetParameterAsText(1)
env.workspace   = in_workspace
datasets = arcpy.ListDatasets()
for dataset in datasets:
    filesets = arcpy.ListFiles(dataset)    
    for f in filesets:
        print f
        InPoint = os.path.join(env.workspace,f,"Point")    # the CAD points
        SaniF   = f.replace(" ","_").replace("-","_").replace(".","_")  # remove invalid chars..

        if int(arcpy.GetCount_management(InPoint).getOutput(0)) == 0:
            arcpy.AddWarning("- No points")
        else:
            outPointShape = '{}_Point.shp'.format(SaniF)
            arcpy.AddMessage('- Exporting Points')
            arcpy.MakeFeatureLayer_management(InPoint, "AB")
            destFields=arcpy.ListFields("AB")
            desc = arcpy.Describe("AB")
            field_info = desc.fieldInfo
            for i,f in enumerate(destFields):
                if f.name in ("FID","Shape","Elevation"):continue
                field_info.setVisible (i, "HIDDEN")
            addLayer="TO_CALC"
            arcpy.MakeFeatureLayer_management(InPoint, addLayer,"","",field_info)
            arcpy.FeatureClassToFeatureClass_conversion(addLayer,outFolder,outPointShape)
  • The best way to say thank you here is accepting solution posted. – FelixIP Feb 11 at 2:08

Your Answer

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

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