#!/usr/bin/env python # -*- coding: UTF-8 -*- """ Name: ImportCADandMerge.py Description: Imports and merges polylines from one workspace into a single feature class Author: ESRI http://desktop.arcgis.com/fr/desktop/latest/manage-data/cad/using-python-to-load-cad-data.htm """ # Import system modules import arcpy #------------------------------------------------------------------------------- # MAIN #------------------------------------------------------------------------------- def main(): """ Main du module : importe un calque de plusiuers DWG dans une classe d'entites d'une FGDB (elle doit exister) """ # Ecrase la classe d'entites cible si elle existe arcpy.env.overwriteOutput = True # Espace de travail ou sont les fichiers DWG arcpy.env.workspace = r'G:\Expert\03_DGST\Poubelle\DAOP1' # Create a value table that will hold the input feature classes for Merge vtab = arcpy.ValueTable() # WhereClause : calque DWG que l'on veut importer whereclause = "\"Layer\" = 'R-EPIC-CANA'" # Choix des champs que l'on veux garder : Layer, Color, DocName et DocType champsaconsever = ["Layer", "Color", "DocName"] #compteur des fichiers DWG nbfiledwg = 0 # Step through each dataset in the list for fdataset in arcpy.ListDatasets('*.dwg'): layername = u'{0}'.format(fdataset) layername = layername.replace(u'É', 'E') layername = layername.replace(' +', 'plus') layername = layername.replace(' -', 'moins') layername = layername.replace(' ', '_') print u'_{0}_'.format(layername) # Get the fields from the input fields = arcpy.ListFields(fdataset) # Create a fieldinfo object fieldinfo = arcpy.FieldInfo() # Iterate through the fields and set them to fieldinfo for field in fields: if field.name in champsaconsever: fieldinfo.addField(field.name, field.name, "VISIBLE", "") else: fieldinfo.addField(field.name, field.name, "HIDDEN", "") # Select only the Polyine features on the drawing layer (whereclause) arcpy.MakeFeatureLayer_management(u'{0}\Polyline'.format(fdataset) , layername , whereclause , '' , fieldinfo) # Creation ajout de la couches dans la liste des couches a fusionner try: vtab.addRow(layername) nbfiledwg = nbfiledwg +1 except Exception, errordesc: print arcpy.GetMessages() errmesg = 'PYTHON ERRORS: {0}'.format(errordesc) print errmesg print "Echec de l'ajout de la couche {0} ({1})".format(layername, fdataset) # Merge the CAD features into one feature class mergefc = r'G:\Expert\03_DGST\Poubelle\Poubelle.mdb\EPIC' try: arcpy.Merge_management(vtab, mergefc) except Exception, errordesc: print arcpy.GetMessages() errmesg = 'PYTHON ERRORS: {0}'.format(errordesc) print errmesg # Fin print u'{0} fichiers DWG importes et fusionnes'.format(nbfiledwg) return True if __name__ == '__main__': STATUS = main() #===============================================================================