import arcpy, sys, os from arcpy import env env.overwriteOutput = True temp_lyr_location = r"\Cases_Geodatabase.gdb\temp_parcel_lyr" parcel = r"Parcel" try: lyr = os.getcwd() + temp_lyr_location mxd = arcpy.mapping.MapDocument("CURRENT") except Exception as e: arcpy.AddError('ERROR initializing: /n' + e.message) #------------------------------------------------------------------------------- def MakeCaseFeature(): ''' Dissolve selected parcel features into one temporary lyr file. Append it to the Cases feature class. Delete the temporary lyr file. ''' clear = "CLEAR_SELECTION" target = "Cases" schemaType = "NO_TEST" fld = "CENTRACT" # no data is stored in this field selectType = "HAVE_THEIR_CENTER_IN" try: # make temporary parcel lyr file from selected parcels, dissolving them # into one feature arcpy.Dissolve_management(parcel, lyr, fld) # add case feature in temporary layer to Cases arcpy.Append_management(lyr, target, schemaType, "", "") # select new case feature arcpy.SelectLayerByLocation_management(target, selectType, lyr) # delete temporary layer arcpy.Delete_management(lyr) # clear selection on parcels arcpy.SelectLayerByAttribute_management(parcel, clear) except Exception as e: arcpy.AddError("ERROR in MakeCaseFeature: \n" + e.message) #------------------------------------------------------------------------------- def main(): ''' Check how many parcels are selected. Count returns all parcels if none are selected, or only selected ones if there are any. ''' try: count = int(arcpy.GetCount_management(parcel).getOutput(0)) arcpy.AddMessage(str(count) + " parcels selected") # make sure parcels are selected before running rest of script, otherwise exit if count >= 0 and count <= 10: MakeCaseFeature() # create the case feature arcpy.RefreshActiveView() arcpy.RefreshTOC() else: arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n") arcpy.AddError("Quitting the Create Case tool \n") except Exception as e: arcpy.AddError('ERROR in main: /n' + e.message) #------------------------------------------------------------------------------- if __name__ == '__main__': main()
Solved! Go to Solution.
def ClearINMEM(): ## clear out the IN_MEMORY workspace of any featureclasses try: gp.Workspace = "IN_MEMORY" fcs = gp.ListFeatureClasses() ### for each FeatClass in the list of fcs's, delete it. for f in fcs: gp.Delete_management(f) gp.AddMessage("deleted: " + f) except: gp.AddMessage("The following error(s) occured attempting to clear in_memory space " + gp.GetMessages()) return