2

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?

Code:

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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 1, 2019 at 12:40
0

1 Answer 1

2

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

answered Mar 1, 2019 at 13:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.