1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #!/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\DAO.gdb\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()
#=============================================================================== |
Partager