0

I am trying to write some code that will get the current workspace name and if it matches a particular datasource path, it will re-path the SDE layers to the new database. Initially I had the code working by just running the re-source code on ArcMap startup but this caused problems with the MXD's "Store relative pathnames" option in MXD and would turn it off. The problem I am having is that I am not sure how to tell it "if the workspace is "blah.sde" then only run the resource code. The MXD's connect to 3 SDE's. Here is what I have:

import arcpy
import pythonaddins
from arcpy import env

class ExtensionClass1(object):
"""Implementation for python_addin.extension2 (Extension)"""
def __init__(self):
    # For performance considerations, please remove all unused methods in 
this class.
    self.enabled = True
def startup(self):
    mxd = arcpy.mapping.MapDocument("CURRENT")
    if arcpy.env.workspace = r"C:\ArcGIS\blah.sde":
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah.sde", 
                                     r"C:\ArcGIS_10.3.1\blah.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah2.sde", 
                                     r"C:\ArcGIS_10.3.1\blah2.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah3.sde", 
                                     r"C:\ArcGIS_10.3.1\blah3.sde")
        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()


def openDocument(self):
    mxd = arcpy.mapping.MapDocument("CURRENT")
    if arcpy.env.workspace = r"C:\ArcGIS\blah.sde":
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah.sde", 
                                     r"C:\ArcGIS_10.3.1\blah.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah2.sde", 
                                     r"C:\ArcGIS_10.3.1\blah2.sde")
        mxd.findAndReplaceWorkspacePaths(r"C:\ArcGIS\blah3.sde", 
                                     r"C:\ArcGIS_10.3.1\blah3.sde")
        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()
  • While arcpy.Describe is the canonical form, string.endswith() would suffice. – Vince May 2 '18 at 11:50
  • Hi Vince, How does that apply to the above? – Daz May 2 '18 at 12:06
  • Has anyone had problems with "store relative Paths" and Python startup scripts? – Daz May 2 '18 at 13:54
2

One option would be to check the layers source in the mxd and use the .find() method to check the returned string for any instance of ".sde":

for lyr in arcpy.mapping.ListLayers(mxd):
    found = lyr.dataSource.find('.sde')
    if found > -1:
        print 'sde layer found'
        # run resource code
  • Thanks artwork, that works but it still stops "Store relative paths" option in the MXD from working. I am not sure why my code interferes with the Map document properties even though they are on by default. The MXD's also connect to a local gdb, but it disconnects some layers when i run the script. – Daz May 2 '18 at 13:20

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.