import arcpy import os 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() self.mapTemplateParam = self.params[0] self.type1Param = self.params[1] return def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" self.mapTemplateParam.Value = self.mapTemplateParam.filter.list[0] self.getMapTemlateList(); return def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" self.getMapTemplateList() return def getMapTemplateList(self): folderPath=r"folderPathHere"#Folder of Map Templates mapTemplates = [] for filename in os.listdir(folderPath): fullpath = os.path.join(folderPath, filename) if os.path.isfile(fullpath): basename, extension = os.path.splitext(fullpath) if extension.lower() == ".mxd": mapTemplates.append(fullpath) self.mapTemplateParam.filter.list = mapTemplates return
Value is an object with a property also named Value, so the correct code is [INDENT]os.path.split(self.params[0].Value.Value)[/INDENT]
Is it just me, or wasn't it easier when everything was a string and you could always assume that was the case? I guess it's cooler and more flexible that you can pass objects, but...
import os class ToolValidator: """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog.""" basePath = r"\\snarf\am\div_lm\ds\for_inv\data" def __init__(self): """Setup the Geoprocessor and the list of tool parameters.""" import arcgisscripting as ARC self.GP = ARC.create(9.3) self.params = self.GP.getparameterinfo() self.fripnameParam = self.params[0] self.fristypeParam = self.params[1] return def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" self.fristypeParam.Value = self.fristypeParam.Filter.List[0] self.updateFripnamePickList() 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.""" self.updateFripnamePickList() return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" fripname = self.fripnameParam.Value fristype = self.fristypeParam.Value if fripname and fristype: if not os.path.isdir(os.path.join(self.basePath, fristype, fripname)): self.fripnameParam.SetErrorMessage(fripname + " does not exist") return def updateFripnamePickList(self): pickList = [] for d in self.listSubdirectories(os.path.join(self.basePath, self.fristypeParam.Value)): pickList.append(d) self.fripnameParam.Filter.List = pickList return def listSubdirectories(self, directory): subdirectories = [] for f in os.listdir(directory): if os.path.isdir(os.path.join(directory, f)): subdirectories.append(f) return subdirectories # The thing in square brackets (next line) is a "list comprehension". #return [f for f in os.listdir(directory) if os.path.isdir(os.path.join(directory, f))]
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.