3

I'm using model builder to iterate through feature classes in datasets. With each sucessive process step (each model) I'm using the %Name% variable to name new feature classes in a new dataset.

I found that I can't just use %Name% without adding a little to the name of the new feature class; the model didn't seem to like working with two feature classes with the same name. To write the new feature classes to disk, I'm using something like %Name%_AppendedBit. The problem is that with sucessive steps I keep adding more and more bits to the name of the feature class.

I tried using Calculate Value with something like Replace(%Name%,_AppendedBit,_JustIdentifyThisStep) to make a variable with which to name the new feature class, but %output_value% doesn't appear to have any value (just "1"). I tried this as a variant and as a string. I was using vb Replace in the "expression" box, not messing with the "code block" box.

I could use %n% to name the feature classes, but then I lose useful detail from the original feature class name. This could be worked around by doing a spatial join later.

So, the long and the short of it is I'd like to use some kind of string truncation with inline variable substitution.

3
  • 1
    Are you trying to use the original name of a feature class to name the new feature class?
    – kkaszas
    Sep 18, 2013 at 16:20
  • Yes. I attempted to use just the name of the feature class as the name of a new feature class in a different data set. Maybe this should have worked, but I got an error saying that the feature class was locked. That's when I appended a tag (some characters) to the end of the name, and it worked. Now I'd like to be able to remove those characters in order to use a different tag.
    – Scott
    Sep 18, 2013 at 18:22
  • I have a similar question: how to truncate a full feature class path? I am using Calculate Value in Model Builder in ArcGIS Pro 2.3 Expression: fn(%fullPath%) Code Block: def fn(featureClass): index = featureClass.rfind("\\") featureClass = featureClass[index:] return featureClass Data Type: String where "fullPath" is the in-line variable I am passing in and the backslash \ is to truncate the feature class proper name to the rest of the path So far, this piece of code do not work for no reason!
    – Vincenty
    Feb 26, 2019 at 16:02

2 Answers 2

8

Slicing should do the trick. To slice a string do this "%var%"[x:y], where x and y are indices representing the start and end positions of the bits of the string you want to keep. The first character of a string has index 0.

say you got:

var = "myfc_clip"

and you want

myfc_buffer

not

myfc_clip_buffer

you would do something like this

"%var%"[0:5] + "buffer"

this will give you

myfc_buffer
5
  • Ok. Would I use the slicing and concatenating in Calculate Value?
    – Scott
    Sep 18, 2013 at 19:26
  • Yes, building up the full name of your new output feature class in Calculate Value using Python is usually the best choice. This way you can ensure the name you get is actually what you want, and you can bypass some of ModelBuilder's "quirks" when it comes to inline variable substitution. Sep 19, 2013 at 5:17
  • 1
    Ok, I got it worked out. ModelBuilder can be quirky. I used "%Name%".replace("AppendedChain","NewSuffix") to get rid of the chain of appended suffixes that accreted through several processes. I found that I couldn't just wipe out the appended chain, apparently because feature classes with the original names were already sitting in another dataset in the same file geodatabase. This prevented Copy Features from writing to the new dataset. I also found that the output_value in Calculate Value didn't follow the iterator very well, so it was necessary to build the model anew for it to work.
    – Scott
    Sep 25, 2013 at 22:50
  • Related
    – Nxaunxau
    Nov 21, 2013 at 11:59
  • Slicing did the trick, though, I did not get it to work when I originally asked.
    – Scott
    Mar 13, 2014 at 20:14
1

To truncate feature class name I am using Calculate Value in Model Builder

Expression: changeName("%Name%")

Code Block: def changeName(name): return str(name[:3])

Data Type: String

where "Name" is the in-line variable I am passing in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.