Help with Export to CAD

2659
9
01-11-2017 01:39 PM
DawidB_1
New Contributor II

In this script works clip to layer, but not work Export to Cad. I dont know how to do that, layer which have been clip goes to export to CAD

import arcpy
import os

arcpy.env.workspace = arcpy.GetParameterAsText(0)
obszar = arcpy.GetParameterAsText(1)
out = arcpy.GetParameterAsText(2)

try:

datasetList = arcpy.ListFeatureClasses()

for dataset in datasetList:

featureClassName = arcpy.ValidateTableName(dataset, out)
outFeatureClass = os.path.join(out, featureClassName)

if dataset != os.path.basename(obszar):
arcpy.Clip_analysis(dataset, obszar, outFeatureClass, "")

except:
print arcpy.GetMessages()

Nowy = arcpy.GetParameterAsText(3)


try:

datasetList2 = arcpy.ListFeatureClasses()

for dataset2 in datasetList2:

arcpy.ExportCAD_conversion(dataset2, "DWG_R2010", Nowy, "Ignore_Filenames_in_Tables", "Overwrite_Existing_Files", "")

except:
print arcpy.GetMessages()

0 Kudos
9 Replies
ClintonDow1
Occasional Contributor II

In this case it is probably easier to use the result from the Clip rather than ListFeatureClasses() to find the input dataset for ExportCAD. For example:

result = arcpy.Clip_analysis('input_fc', 'roads', 'output_fc')
result_cad = arcpy.ExportCAD_conversion(result, 'DWG_R2010', 'result_cad.dwg')
result_cad
<Result 'C:\\Users\\user\\Documents\\ArcGIS\\Projects\\MyProject4\\result_cad.dwg'>‍‍‍‍

In your case if the workspace and location of outFeatureClass are different then it will not find the result from Clip_analysis() when using ListFeatureClasses()

DawidB_1
New Contributor II

Thank you for the remark! I appreciate it. 

I removed the second "try" and "except", but it still does not return my result in dwg format. I do not know how to use a "result" so that my whole clip's passage could be. Could you show me how to edit the code? In the Annex toolbox with this tool.

 

import arcpy 
import os

arcpy.env.workspace = arcpy.GetParameterAsText(0)
obszar = arcpy.GetParameterAsText(1)
out = arcpy.GetParameterAsText(2)

try:

    datasetList = arcpy.ListFeatureClasses()

    for dataset in datasetList:   
        
        featureClassName = arcpy.ValidateTableName(dataset, out)
        outFeatureClass = os.path.join(out, featureClassName)
        
        if dataset != os.path.basename(obszar):
            result = arcpy.Clip_analysis(dataset, obszar, outFeatureClass, "")
            result_cad = arcpy.ExportCAD_conversion(result, "DWG_R2010", 'result_cad.dwg')
            
            
except:
        print arcpy.GetMessages()
0 Kudos
LukeWebb
Occasional Contributor III

What happends if you try to do it using the path you passed in instead of a result object:

We are obviously only expecting 1 CAD drawing to be output, as it will crash / overwrite it as we are saving it to the same path each time.  e.g.

arcpy.GetParameterAsText(0) + "\\"+ "result_cad.dwg"

import arcpy 
import os

arcpy.env.workspace = arcpy.GetParameterAsText(0)
obszar = arcpy.GetParameterAsText(1)
out = arcpy.GetParameterAsText(2)

try:

    datasetList = arcpy.ListFeatureClasses()

    for dataset in datasetList:   
        
        featureClassName = arcpy.ValidateTableName(dataset, out)
        outFeatureClass = os.path.join(out, featureClassName)
        
        if dataset != os.path.basename(obszar):
            arcpy.Clip_analysis(dataset, obszar, outFeatureClass, "")
            arcpy.ExportCAD_conversion(outFeatureClass, "DWG_R2010", 'result_cad.dwg')
            
            
except:
        print arcpy.GetMessages()
DawidB_1
New Contributor II

The point is that I would like to have all the layers that will be clip, have been exported to the CAD file. After opening it in AutoCAD layers are all those clip .shp.

It was noticed that the code above cut me only the first 2 layers and creates a CAD file in the folder input, but only 1 clip shp.

0 Kudos
LukeWebb
Occasional Contributor III

As I said, if you look at the errors the code will output, you should see it says:

"Output dataset already exists" as part of your 

except:
        print arcpy.GetMessages()

As you have the try, except outside the loop, what happens is:

Clip 1st layer

Export first layer to CAD

Clip 2nd layer

Crash as output New.DWG already exists (You have this message being printed..)

Then, it exits the try except loop so stops processing.

Here I have given each CAD a unique name, and moved the try except so that it does not stop ALL processing when it crashes. (Just skip the current dataset)

arcpy.env.workspace = arcpy.GetParameterAsText(0)
obszar = arcpy.GetParameterAsText(1)
out = arcpy.GetParameterAsText(2)



datasetList = arcpy.ListFeatureClasses()
index = 0
for dataset in datasetList:  
    index +=1
    featureClassName = arcpy.ValidateTableName(dataset, out)
    outFeatureClass = os.path.join(out, featureClassName)
    
    if dataset != os.path.basename(obszar):
        try:
            arcpy.Clip_analysis(dataset, obszar, outFeatureClass, "")
            output_CAD_name = 'CAD_' + str(index) + '_New.Dwg'
            arcpy.ExportCAD_conversion(outFeatureClass, "DWG_R2010", output_CAD_name)
        
        except:
                print arcpy.GetMessages()
DawidB_1
New Contributor II

Unluckily after using your solution the tool clips layers, but doesnt export anything to CAD. Maybe there is some misprint in your code?

0 Kudos
LukeWebb
Occasional Contributor III

Just to be sure, did you refresh the "Input" folder in catalog? 

As we are not providing a workspace for the CAD outputs, the CAD drawings should be going into the arcpy.env.workspace you set, which is your input folder (WSADOWY), and not the output folder you highlight in the image..

I did notice an indentation error under the "Except" statement which might cause issues. 

DawidB_1
New Contributor II

UPDATE!!! WORK!!!  

I have tried many times and in the end it works. I do not know if I can do that all created CAD files are in one DWG?

0 Kudos
LukeWebb
Occasional Contributor III

Hey, try this code and see if it works!:

__author__ = 'WebbL'

import arcpy, os


arcpy.env.workspace = arcpy.GetParameterAsText(0)
obszar = arcpy.GetParameterAsText(1)
out = arcpy.GetParameterAsText(2)


clip_outputs = []   # An Empty list where we will keep track of all our clip outputs

datasetList = arcpy.ListFeatureClasses()
for dataset in datasetList:

    featureClassName = arcpy.ValidateTableName(dataset, out)
    outFeatureClass = os.path.join(out, featureClassName)

    if dataset != os.path.basename(obszar):
        try:
            arcpy.Clip_analysis(dataset, obszar, outFeatureClass, "")

            #Remember this output for the export to CAD step step
            clip_outputs.append(outFeatureClass)

        except:
                print arcpy.GetMessages()


#Now we export to CAD
Cad_path = os.path.join(out, "CAD_Output.DWG")
arcpy.ExportCAD_conversion(clip_outputs, "DWG_R2010", Cad_path)