Sunday 22 April 2018

arcpy - How to Load a Record Set Parameter in a tool dialog



I am trying to create a tool that allows a user to update a table row without starting an edit session. I have a script tool set up with a record set parameter and I use this set up to add new rows to the table. It works well. What I would like to do now is load the record set parameter with the rows that will be edited. I am trying to do this in the updateParameters() method. However, when I load the parameter nothing happens in the dialog. I can see the record set in table of contents and its getting loaded but its as though the parameter is not updating.


Anyone had any success with something like this?


I have tried several ways to load the record set. My latest attempt is with a cursor. Here is the tool validation code for this latest attempt. I have three parameters, an input table view, an input record set, and an input string that I use for printing debug statements. The input record set has the schema imported from the table view I am trying to load.


import arcpy


def rows_as_dicts(cursor):
colnames = cursor.fields
for row in cursor:
yield dict(zip(colnames, row))

def newRowFromDict(cursorFieldsTuple, inDict):
newRow = []
for field in cursorFieldsTuple:
newRow.append(None)


for item in inDict:
newRowIndex = cursorFieldsTuple.index(item)
newRow[newRowIndex] = inDict[item]
return newRow


class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""


def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()

def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
return


def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parmater
has been changed."""
pymsg,msgs = " No "," Problems"
if self.params[0].altered and not self.params[0].hasBeenValidated:
if not self.params[0].value == None:

thetable = self.params[0].value
recordSet = self.params[1].value

try:
inCur = arcpy.da.InsertCursor(recordSet, "*")
inDict = dict.fromkeys(inCur.fields)
inFields = inCur.fields

searhcur = arcpy.da.SearchCursor(thetable, "*")
searchDict = rows_as_dicts(searhcur)

for row in searchDict:
for item in row:

if item in inFields:
inDict[item] = row[item]
newRow = newRowFromDict(inCur.fields, inDict)
inCur.insertRow(newRow)

del inCur
self.params[1].value.load(arcpy.RecordSet(recordSet))

except:
import sys

import os
import traceback
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"

self.params[2].value = "Validation" + pymsg + msgs + " end"
return


def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...