I have created a script. I'm run the script using ArcMap and a layer should be added to the open ArcMap document.

I have earlier used MXD.save() witch work fine when I'm running the script outside ArcMap (through an IDE).

How can I change the code below such that the layer is added to the open ArcMap document?


MXD = arcpy.mapping.MapDocument(r"C:\Users\mgv\Desktop\Koncept\Koncept.mxd")
DF = arcpy.mapping.ListDataFrames(MXD, "Layers")[0]
arcpy.MakeFeatureLayer_management(in_features=line + "_autodimensioned", out_layer=line + "_autodimensioned_layer")
layer = arcpy.mapping.Layer(line + "_autodimensioned_layer")
arcpy.mapping.AddLayer(DF, layer, add_position="AUTO_ARRANGE")

# Add symbology for the layer showing the pipes, symbology for the pipe_type_sup field
updateLayer = arcpy.mapping.ListLayers(MXD, "C:\Users\mgv\Desktop\Koncept\Network.mdb\Network\line_autodim_autodimensioned_layer", DF)[0]
sourceLayer = arcpy.mapping.Layer(r"C:/Users/mgv/Desktop/Koncept/Symbology_configuration_layer.lyr") #task: aendre
arcpy.mapping.UpdateLayer(DF, updateLayer, sourceLayer, True)

# Add label with the pipe type for the supply pipe (DN)
lcs = updateLayer.labelClasses
lc = lcs[0]
lc.expression = "[pipe_type_sup]"
updateLayer.showLabels = True

MXD.save() #Works if running in PyCharm / running the code outside ArcMap / ArcGIS for Desktop

Try changing first line to:

MXD = arcpy.mapping.MapDocument("CURRENT")

And you might also need to RefreshActiveView:

Refreshes the active view and table of contents of the current map document

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.