Example #1
0
    def onClick(self):
        try:
            workspace = autoPath()
            mxd = arcpy.mapping.MapDocument("CURRENT")
            ddp = mxd.dataDrivenPages
            dfLst = arcpy.mapping.ListDataFrames(mxd)
            pageName = ddp.pageRow.getValue(ddp.pageNameField.name)

            # Set data frame name to credits field (can change to description field) for use in lyrString.
            for df in dfLst:
                for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
                    if isinstance(
                            lyr, arcpy.mapping.Layer) and not lyr.isGroupLayer:
                        lyr.credits = df.name

            lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()
            if not isinstance(lyr, arcpy.mapping.Layer) or lyr.isGroupLayer:
                pythonaddins.MessageBox(
                    'Please select one (1) layer (not a group or data frame or multiple layers) in the Table Of Contents',
                    'Layer Selection Error', 0)
            else:
                lyrString = "%s_%s_%s.lyr" % (pageName, lyr.name, lyr.credits)
                arcpy.SaveToLayerFile_management(lyr,
                                                 workspace + "\\" + lyrString,
                                                 "ABSOLUTE")
                pythonaddins.MessageBox(
                    "%s layer saved to:\n\n%s\n\nas:\n\n%s" %
                    (lyr.name, workspace, lyrString), "Layer Saved", 0)
        except Exception as e:
            pythonaddins.MessageBox(e, "Error")
Example #2
0
    def onClick(self):
        parentDir = os.path.abspath(os.path.join(autoPath(), os.pardir))
        defaultLayersWorkspace = os.path.join(parentDir, "z_Default_Layers")
        arcpy.env.workspace = defaultLayersWorkspace
        mxd = arcpy.mapping.MapDocument("CURRENT")

        # Create Default Layers directory if none exists.
        if not os.path.exists(defaultLayersWorkspace):
            os.makedirs(defaultLayersWorkspace)
            pythonaddins.MessageBox("Default layers yet not created.\nRun createDefaultLyrs.py inside the interactive python window of this MXD.", "Layers Not Created", 0)

        # Load Default Layers.
        dfLst = arcpy.mapping.ListDataFrames(mxd)

        # Initialize empty lists for MessageBox.
        lyrsReset, lyrsNotReset = [], []

        # Iterate features and dataframes. Replace with default layer file. NOTE: Make sure feature class names are unique.
        for df in dfLst:
            for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
                if isinstance(lyr, arcpy.mapping.Layer) and not lyr.isGroupLayer:
                    lyrString = "default_%s.lyr" % (lyr.name)
                    if lyrString in arcpy.ListFiles("*.lyr"):
                        ref_layer = arcpy.mapping.ListLayers(mxd, lyr.name, df)[0]
                        insert_layer = arcpy.mapping.Layer("%s\\%s" % (defaultLayersWorkspace, lyrString))
                        arcpy.mapping.InsertLayer(df, ref_layer,insert_layer, "AFTER")
                        arcpy.mapping.RemoveLayer(df, ref_layer)
                        # Add swapped layers to list and show user with pythonaddins.MessageBox().
                        lyrsReset.append("{} - {}".format(lyrString, df.name))
                    else:
                        lyrsNotReset.append("{} - {}".format(lyrString, df.name))
        pythonaddins.MessageBox("Layers Reset:\n{} \n\nLayers NOT Reset:\n{}".format(lyrsReset, lyrsNotReset),"Layer Reset Summary", 0)
Example #3
0
    def onClick(self):
        try:
            # Create an instance of the ResetLayers class.
            reset = ResetLayers()
            # Call ResetLayers onClick method.
            reset.onClick()

            workspace = autoPath()
            os.chdir(workspace)

            mxd = arcpy.mapping.MapDocument("CURRENT")
            ddp = mxd.dataDrivenPages
            pageName = ddp.pageRow.getValue(ddp.pageNameField.name)
            dfLst = arcpy.mapping.ListDataFrames(mxd)
            layers = arcpy.mapping.ListLayers(mxd)

            # Add description to layers if blank.
            for layer in arcpy.mapping.ListLayers(mxd):
                if layer.description == "":
                    layer.description = "This is a default description for the '%s' layer. Feel free to change or update this description." % (
                        layer.name)

            # Initialize empty list for MessageBox.
            lyrsRestored = []
            # Iterate features and dataframes. Replace if layerfile exists for feature.
            for df in dfLst:
                for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
                    if isinstance(
                            lyr, arcpy.mapping.Layer) and not lyr.isGroupLayer:
                        lyrString = "%s_%s_%s.lyr" % (pageName, lyr.name,
                                                      df.name)
                        if lyrString in arcpy.ListFiles("*.lyr"):
                            ref_layer = arcpy.mapping.ListLayers(
                                mxd, lyr.name, df)[0]
                            insert_layer = arcpy.mapping.Layer(
                                "%s\\%s" % (workspace, lyrString))
                            arcpy.mapping.InsertLayer(df, ref_layer,
                                                      insert_layer, "AFTER")
                            arcpy.mapping.RemoveLayer(df, ref_layer)
                            lyrsRestored.append(lyrString + df.name)

            pythonaddins.MessageBox("Layers Restored:\n%s" % lyrsRestored,
                                    "Restored Layers Summary", 0)
        except Exception as e:
            pythonaddins.MessageBox(e, "Error")
Example #4
0
    def onClick(self):
        parentDir = os.path.abspath(os.path.join(autoPath(), os.pardir))
        defaultLayersWorkspace = os.path.join(parentDir, "z_Default_Layers")
        arcpy.env.workspace = defaultLayersWorkspace
        mxd = arcpy.mapping.MapDocument("CURRENT")

        # Create Default Layers directory if none exists.
        if not os.path.exists(defaultLayersWorkspace):
            os.makedirs(defaultLayersWorkspace)
            pythonaddins.MessageBox(
                "Default layers yet not created.\nRun createDefaultLyrs.py inside the interactive python window of this MXD.",
                "Layers Not Created", 0)

        # Load Default Layers.
        dfLst = arcpy.mapping.ListDataFrames(mxd)

        # Initialize empty lists for MessageBox.
        lyrsReset, lyrsNotReset = [], []

        # Iterate features and dataframes. Replace with default layer file. NOTE: Make sure feature class names are unique.
        for df in dfLst:
            for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
                if isinstance(lyr,
                              arcpy.mapping.Layer) and not lyr.isGroupLayer:
                    lyrString = "default_%s.lyr" % (lyr.name)
                    if lyrString in arcpy.ListFiles("*.lyr"):
                        ref_layer = arcpy.mapping.ListLayers(
                            mxd, lyr.name, df)[0]
                        insert_layer = arcpy.mapping.Layer(
                            "%s\\%s" % (defaultLayersWorkspace, lyrString))
                        arcpy.mapping.InsertLayer(df, ref_layer, insert_layer,
                                                  "AFTER")
                        arcpy.mapping.RemoveLayer(df, ref_layer)
                        # Add swapped layers to list and show user with pythonaddins.MessageBox().
                        lyrsReset.append("{} - {}".format(lyrString, df.name))
                    else:
                        lyrsNotReset.append("{} - {}".format(
                            lyrString, df.name))
        pythonaddins.MessageBox(
            "Layers Reset:\n{} \n\nLayers NOT Reset:\n{}".format(
                lyrsReset, lyrsNotReset), "Layer Reset Summary", 0)
Example #5
0
    def onClick(self):
        try:
            # Create an instance of the ResetLayers class.
            reset = ResetLayers()
            # Call ResetLayers onClick method.
            reset.onClick()

            workspace = autoPath()
            os.chdir(workspace)

            mxd = arcpy.mapping.MapDocument("CURRENT")
            ddp = mxd.dataDrivenPages
            pageName = ddp.pageRow.getValue(ddp.pageNameField.name)
            dfLst = arcpy.mapping.ListDataFrames(mxd)
            layers = arcpy.mapping.ListLayers(mxd)

            # Add description to layers if blank.
            for layer in arcpy.mapping.ListLayers(mxd):
                if layer.description == "":
                    layer.description = "This is a default description for the '%s' layer. Feel free to change or update this description." % (layer.name)

            # Initialize empty list for MessageBox.
            lyrsRestored = []
            # Iterate features and dataframes. Replace if layerfile exists for feature.
            for df in dfLst:
                for lyr in arcpy.mapping.ListLayers(mxd,"*", df):
                    if isinstance(lyr, arcpy.mapping.Layer) and not lyr.isGroupLayer:
                        lyrString = "%s_%s_%s.lyr" % (pageName, lyr.name, df.name)
                        if lyrString in arcpy.ListFiles("*.lyr"):
                            ref_layer = arcpy.mapping.ListLayers(mxd, lyr.name, df)[0]
                            insert_layer = arcpy.mapping.Layer("%s\\%s" % (workspace, lyrString))
                            arcpy.mapping.InsertLayer(df, ref_layer,insert_layer, "AFTER")
                            arcpy.mapping.RemoveLayer(df, ref_layer)
                            lyrsRestored.append(lyrString + df.name)

            pythonaddins.MessageBox("Layers Restored:\n%s" % lyrsRestored,"Restored Layers Summary", 0)
        except Exception as e:
            pythonaddins.MessageBox(e, "Error")
Example #6
0
    def onClick(self):
        try:
            workspace = autoPath()
            mxd = arcpy.mapping.MapDocument("CURRENT")
            ddp = mxd.dataDrivenPages
            dfLst = arcpy.mapping.ListDataFrames(mxd)
            pageName = ddp.pageRow.getValue(ddp.pageNameField.name)

            # Set data frame name to credits field (can change to description field) for use in lyrString. 
            for df in dfLst:
                for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
                    if isinstance(lyr, arcpy.mapping.Layer) and not lyr.isGroupLayer:
                        lyr.credits = df.name

            lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()
            if not isinstance(lyr, arcpy.mapping.Layer) or lyr.isGroupLayer:
                pythonaddins.MessageBox('Please select one (1) layer (not a group or data frame or multiple layers) in the Table Of Contents', 'Layer Selection Error', 0)
            else:
                lyrString = "%s_%s_%s.lyr" % (pageName, lyr.name, lyr.credits)
                arcpy.SaveToLayerFile_management(lyr, workspace + "\\" + lyrString, "ABSOLUTE")
                pythonaddins.MessageBox("%s layer saved to:\n\n%s\n\nas:\n\n%s" % (lyr.name, workspace, lyrString), "Layer Saved", 0)
        except Exception as e:
            pythonaddins.MessageBox(e, "Error")
Example #7
0
import arcpy
import os
import pythonaddins

sys.path.append(os.path.dirname(__file__)) # Add this script to system path. Used to separate main script from other code packages.
from autoPath import autoPath

arcpy.env.overwriteOutput = True
parentDir = os.path.abspath(os.path.join(autoPath(), os.pardir))
workspace = os.path.join(parentDir, "z_Default_Layers")
if not os.path.exists(workspace):
    os.makedirs(workspace)

arcpy.env.workspace = workspace


mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

annoLst = []
for lyr in arcpy.mapping.ListLayers(mxd,data_frame=arcpy.mapping.ListDataFrames(mxd)[0]):
    if lyr.supports("DATASOURCE"):
        arcpy.env.workspace = os.path.dirname(lyr.dataSource)
        annoLst.append(arcpy.ListFeatureClasses(feature_type="Annotation"))

# Create layerfile for layers in TOC that support layerfile creation.
for lyr in [item for item in arcpy.mapping.ListLayers(mxd, data_frame=df) if item not in annoLst]:
    if isinstance(lyr, arcpy.mapping.Layer) and not lyr.isGroupLayer and "index" not in lyr.name and lyr.supports("DATASETNAME") and not lyr.isBroken:
    	lyrString = "default_%s.lyr" % (lyr.name)
        try:
            arcpy.SaveToLayerFile_management(lyr,workspace + "\\" + lyrString, "ABSOLUTE")
Example #8
0
import arcpy
import os
import pythonaddins

sys.path.append(
    os.path.dirname(__file__)
)  # Add this script to system path. Used to separate main script from other code packages.
from autoPath import autoPath

arcpy.env.overwriteOutput = True
parentDir = os.path.abspath(os.path.join(autoPath(), os.pardir))
workspace = os.path.join(parentDir, "z_Default_Layers")
if not os.path.exists(workspace):
    os.makedirs(workspace)

arcpy.env.workspace = workspace

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

annoLst = []
for lyr in arcpy.mapping.ListLayers(
        mxd, data_frame=arcpy.mapping.ListDataFrames(mxd)[0]):
    if lyr.supports("DATASOURCE"):
        arcpy.env.workspace = os.path.dirname(lyr.dataSource)
        annoLst.append(arcpy.ListFeatureClasses(feature_type="Annotation"))

# Create layerfile for layers in TOC that support layerfile creation.
for lyr in [
        item for item in arcpy.mapping.ListLayers(mxd, data_frame=df)
        if item not in annoLst