Arcpy add schematic diagram to MXD

02-19-2018 06:06 AM
New Contributor

I need a python script that I can use to create a schematic diagram, and add it to an mxd. This must be done in an independent script and I cannot do it in arcmap.

The problem is I can't get arcpy to add the diagram layer to the map. I run arcpy.schematics.CreateDiagram to create the diagram and that works, but when I try to use arcpy.mapping.AddLayer to add the return from the diagram it throws an AssertionError. I have also tried to use arcpy.mapping.Layer but that throws the same error.

Can someone PLEASE tell me how to add a schematic diagram to an MXD with arcpy?

3 Replies
New Contributor II


I had to deal with the same requirement and I ended up to convert the diagram to features which results in feature classes in a feature dataset. You can easily add these feature classes to the MXD then. You need a template-MXD with the correct symbology for all layers, because the symbology is lost when converting the diagrams to features. You can then programatically switch the data sources from the template-MXD to point to the converted feature classes.

Convert Diagram To Features—Help | ArcGIS for Desktop 

If you want to export the map/layout to PDF/PNG and you have rotated symbols or label expressions, please note that you might have to use python 32 bit and not 64 bit because of the following Bugs: BUG-000093736; BUG-000092016

As far as I know this is the only solution but if somebody knows another solution I'm happy to hear about it.

Best regards,


Occasional Contributor II

I've been working my way through options to try not to do what you've suggested but I've not been able to find an alternative that works.

MakeFeatureLayer doesn't appear to support Schematic Datasets and gives an error using arcpy.

You can't update the data source on a Schematic Dataset using arcpy tools.  Top note in the 'known limitations' section.

Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop 

I've been considering trying to make one schematic that I keep overwriting so that I only need one mxd, or having 1000 mxd files but those sound terrible and painful.

I'm going to implement the idea you mentioned above.  I'm going to export my schematic to a temporary geodatabase and use that to create a grid on top of, generate my pdf series for that schematic #with Data Driven pages, then move on to the next schematic.

schematics ddp data driven pages

Occasional Contributor II

Some slight changes to my process now that I've actually gotten something working, this keeps almost all the data created during the process.

  • Generate schematic start point and barrier feature classes (barriers can also be starts sometimes)
  • Generate a schematic for each start point stopping at each barrier (CreateDiagram_schematics)
    • trace the start point without any barriers first, then use that to select by location which barriers you want to use.  Less barriers makes for a faster trace (TraceGeometricNetwork_management)
    • I had to copy out each of my linear features to a temp feature class (CopyFeatures_management) from my trace result (TraceGeometricNetwork_management) in order to use them in SelectLayerByLocation_management
  • Loop through all schematic datasets and export those to a feature dataset (ConvertDiagram_schematics)
  • Loop through all the feature datasets
    • using a template mxd, change the data source of each layer in the mxd.  Everything followed a naming pattern that allowed me to just replace the user defined schematic name in each feature class name / source. (layer.dataSource.replace)
    • refresh and export the Data Driven Pages in the mxd which had been configured when creating the template (mxd.dataDrivenPages.refresh() and mxd.dataDrivenPages.exportToPDF())

Nice things I get from this are all of the feature classes and schematic diagrams continue to exist after the bulk process.  You could open or update them individually if you wanted to make a one off.  You could let this data structure live forever and next time through just update instead of create everything from scratch.  I haven't saved out individual mxd files yet after repointing the data, but that could be done.  This process is being used to automate generating 900-1000 diagrams.