I wrote a addins to transfer selected objects from one distributed database to another. Each layer participates in a geographic service (MapService). The program works successfully sometimes, and sometimes it hangs for a long time and nothing happens. Any suggestions on how to make the script work stable?
# -*- coding: cp1251 -*-
import arcpy
import pythonaddins
import sys
class CopyToArchiv(object):
"""Implementation for script_addin.CopyToArchiv (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
global staticVal # the first layer with attachments
global staticVal_arch # the second layer to archive the objects from the first layer
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument('current')
layer1 = arcpy.mapping.Layer(staticVal)
layer2 = arcpy.mapping.Layer(staticVal_arch)
staticVal_arch_f=layer2.datasetName
#workspace
worksps1=layer1.dataSource[:(layer1.dataSource.lower()).find(".sde")]+".sde\\"
worksps2=layer2.dataSource[:(layer2.dataSource.lower()).find(".sde")]+".sde\\"
field_names2 = [f.name for f in arcpy.ListFields(layer2)]
if "ID_SAVE" in field_names2:
pass
else:
answer = pythonaddins.MessageBox(u'There is no ID_SAVE field! Add?', u'Message', 4)
if answer == 'Yes':
arcpy.AddField_management(layer2,"ID_SAVE","LONG")
else:
pythonaddins.MessageBox(u'Exit', u'Message', 0)
sys.exit()
#calculate field ID_SAVE=lID
arcpy.CalculateField_management(layer2,"ID_SAVE","!OBJECTID!","PYTHON")
#check the ID numbers of selected objects
list_GID=''
with arcpy.da.SearchCursor(layer1, "OBJECTID") as cursor:
for row in cursor:
list_GID=list_GID+str(row[0])+','
list_GID=list_GID[:len(list_GID)-1]
list_GID='('+list_GID+')'
#copy objects from one layer to another
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(layer2)
fieldmap = fieldmappings.getFieldMap(fieldmappings.findFieldMapIndex("ID_SAVE"))
fieldmap.addInputField(layer1, "OBJECTID")
fieldmappings.replaceFieldMap(fieldmappings.findFieldMapIndex("ID_SAVE"), fieldmap)
arcpy.Append_management(layer1,layer2,"NO_TEST",fieldmappings)
layer2_attach=worksps2+layer2.datasetName+"__ATTACH"
layer1_attach=worksps1+layer1.datasetName+"__ATTACH"
try:
arcpy.Delete_management("layer1_attach")
except arcpy.ExecuteError:
msgs = arcpy.GetMessages(2)
pythonaddins.MessageBox(msgs, u"Error", 0)
arcpy.MakeTableView_management(layer1_attach,"layer1_attach","REL_OBJECTID in " + list_GID)
arcpy.Append_management("layer1_attach", layer2_attach, "NO_TEST")
try:
arcpy.Delete_management("layer2_attach")
except arcpy.ExecuteError:
msgs = arcpy.GetMessages(2)
pythonaddins.MessageBox(msgs, u"Error", 0)
arcpy.MakeTableView_management(layer2_attach,"layer2_attach")
arcpy.AddJoin_management("layer2_attach","REL_OBJECTID",layer2,"ID_SAVE","KEEP_COMMON")
#calculate in attachments2 rel_ID=ID(from layer2)
arcpy.CalculateField_management("layer2_attach", "REL_OBJECTID", "!" + staticVal_arch_f + ".OBJECTID!", "PYTHON")
#удаляем связь
arcpy.RemoveJoin_management("layer2_attach")
#
try:
arcpy.Delete_management("layer1_attach")
arcpy.Delete_management("layer2_attach")
except arcpy.ExecuteError:
msgs = arcpy.GetMessages(2)
pythonaddins.MessageBox(msgs, u"Error", 0)