Select to view content in your preferred language

Having trouble with python script for "Assign Default To Field" for multiple features

957
6
06-16-2011 10:57 AM
philiptivel
Emerging Contributor
I'm trying to create a python script that will be run in arctoolbox that will allow users to choose multiple feature classes and default the same field in all of the feature classes to the same user entered default value. Is there an easy way to do this?

Also if there are different subtypes for each feature class will that be a problem? Currently the default values need to be assigned to the feature class and its subtypes for this geoprocessing tool to work. It would be a pain to have to specify each subtype in a script I think unless there was a way to just automatically include all of them for a given feature class.
Tags (2)
0 Kudos
6 Replies
MikeMacRae
Frequent Contributor
Hey Phil,

I am a little uncertain as to what it is your user is trying to do. Is he/she trying to add a default field or select a default field? What is the end result?

Cheers,
Mike
0 Kudos
philiptivel
Emerging Contributor
Mike, the field is already there. I have about 30 feature classes that all have a "text" field. I want the user to be able to set the default value of this field in all of the feature classes to whatever value they enter. it will be the same value in all 30 feature classes.

Thanks,

Phil
0 Kudos
MikeMacRae
Frequent Contributor
Hey Phil, it counds like you want them to populate each record in the field.

Try to set your script like this

                           


import arcpy
from arcpy import env

userinputworkspace = sys.argv[1]
userinputvalue = sys.argv[2]

arcpy.env.workspace = userinputworkspace
fcs = arcpy.ListFeatureClasses()

for fc in fcs:

    arcpy.CalculateField_management(fc, "yourfieldnamehere", userinputvalue)



The help for calculate field is here:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000.htm

The help for listing feature classes is here:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001n000000.htm
0 Kudos
philiptivel
Emerging Contributor
Ideally I want the field already populated when they create a new feature. They will be starting with a blank database with no data (except for the empty feature classes and subtypes built into the database), so I would like to use: arcpy.AssignDefaultToField_management(fc, "Field_Name", User_Input_Value", *Not sure what to put here for the subtypes*)

It seems like your code work if I put that in there instead of arcpy.CalculateField_management(fc, "yourfieldnamehere", userinputvalue), but how would I input the subtype values to include all of them for every feature class?

Thanks,

Phil
0 Kudos
MikeMacRae
Frequent Contributor
Ah, you will have to go to the FC properties and select the "SubType" tab. You will see the "Subtypes:" listed.

In the portion of the syntax to enter sub types, enter the code and the description seperated by commas and enclosed in square brackets. Something like:

import arcpy
from arcpy import env

userinputworkspace = sys.argv[1]
userinputvalue = sys.argv[2]

arcpy.env.workspace = userinputworkspace
fcs = arcpy.ListFeatureClasses()

for fc in fcs:

         AssignDefaultToField_management (fc, field_name, userinputvalue, ["0: first description", "1: second description", etc])


That should work if the subtypes are all the same from one fc to another, but if you need to add or set subtypes, try these syntaxes

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Set_Subtype_Field/0017000000mw000000/
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Add_Subtype/0017000000ms000000/
0 Kudos
philiptivel
Emerging Contributor
Yeah, the subtypes are different from one feature class to the next.

With what you've shown me in the last few posts though I feel like I have a good shot of figuring this out now. Thanks so much for your help!

Phil
0 Kudos