Programmatically change CAD Layer's data source

648
2
02-07-2020 07:14 AM
TedChapin
Occasional Contributor III

I am trying to change the data source of a CAD layer in a Pro map using arcpy. Looking at the documentation on the arcpy.mp.Layer class I can see that the updateConnectionProperties method is the one to use. Get the connection properties dictionary from the existing layer, change what needs to be changed, and give the new connection properties to this method. But, the connectionProperties does not include the name of the drawing file, just the folder containing the file.

Here's an example: I have 2 layers in a Pro map from the Polyline layer of 2 different drawings, MyDrawing1.dwg and MyDrawing2.dwg in the same folder C:\CAD DataSources.

Here are the connectionProperties for MyDrawing1.dwg/Polyline:

arcpy.mp.ArcGISProject("CURRENT").activeMap.listLayers()[0].connectionProperties

{'dataset': 'Polyline', 'workspace_factory': 'Cad', 'connection_info': {'database': 'C:\\CAD DataSources'}}

Here are the connectionProperties for MyDrawing2.dwg/Polyline:

arcpy.mp.ArcGISProject("CURRENT").activeMap.listLayers()[1].connectionProperties

{'dataset': 'Polyline', 'workspace_factory': 'Cad', 'connection_info': {'database': 'C:\\CAD DataSources'}}

They are exactly the same, so my_layer.updateConnectionProperties(current_connection_info, new_connection_info) does nothing.

The dataSource property reflects the dwg file name, but I don't see how that property plays in the updateConnectionProperties method, and that property is read-only.

arcpy.mp.ArcGISProject("CURRENT").activeMap.listLayers()[0].dataSource

'C:\\CAD DataSources\\MyDrawing1.dwg\\Polyline'

arcpy.mp.ArcGISProject("CURRENT").activeMap.listLayers()[1].dataSource

'C:\\CAD DataSources\\MyDrawing2.dwg\\Polyline'

Using the Set Data Sources GUI correctly reassigns the layer's data source.

Tags (4)
0 Kudos
2 Replies
AdrianWelsh
MVP Honored Contributor

Hey Ted,

This is a great question and I wonder the answer too. You might have better luck moving this post to the Python‌ space so the Python experts can chime in.

0 Kudos
TedChapin
Occasional Contributor III

The answer is to use the CIM definition of the layer instead of the updateConnectionProperties method. If you notice in the layer properties dialog box, the drawing filename is stored in the "Feature Dataset" property. You can use the CIM to change this property. Here's the code:

cad_layer = my_map.listLayers("My CAD Layer")[0]
cad_layer_cim = cad_layer.getDefinition("V2")
cad_layer_cim.featureTable.dataConnection.featureDataset = new_dwg_file_name
cad_layer.setDefinition(cad_layer_cim)