The document discusses the author's efforts to format a sequence of numbers with leading zeros. Initially, the author tried to add a "0" prefix in Dynamo but that approach did not work as intended. The author then learned about Python's string formatting method, which provides a robust solution to format numbers with leading zeros for any range of values. The author created a custom Dynamo node that implements this Python solution, allowing numbers to be efficiently formatted within Dynamo projects.
1. By WOJCIECH KLEPACKI wklepa@gmail.com
At some point in a past, I bumped on the Dynamo problem how to add a prefix “0” to the
sequence of numbers. Basically, what I wanted to achieve seemed quite easy to solve. My idea was to
add “0” to the sequence of numbers from 1 to 1000. I’ve developed the script but wasn’t very happy
with the outcome. The script added “0” to literally all numbers. I realized that instead of adding a
prefix I want to format the numbers in some witty and smart way. Well, the logical background was
based on the assumption that the length of the maximum item in a range converted to the string is 4.
Then every number with length 1 should look like 0001, 0002 … 0009 after formatting, subsequently
number with length 2 should take the form of 0010, 0020 … 0099. The same manner should be applied
to numbers with a length of 3 but numbers with 4 in length should stay as they are.
I want to make it clear that I use this procedure very often in a real-world scenario. It’s a vital
part of many of my Dynamo scripts. Renumbering in sequence doors, windows, rooms or other
elements plays an important part in my coding activities.
The matrix of numbers after formatting should look exactly like the one below:
1 - 0001
2 - 0002
.. - ..
9 - 0009
10 - 0010
20 - 0020
.. - ..
100 - 0100
200 - 0200
.. - ..
999 - 0999
1000 - 1000
Looks easy? That’s exactly what I thought so I rolled my sleeves up and wrote a short procedure in
the Design Script. This my Code Block:
2. By WOJCIECH KLEPACKI wklepa@gmail.com
The first line (after comments marked in green) converts the sequence of numbers to the list
of strings. It’s not compulsory but I want to keep it just to make the concept clear. The reason for
conversion is simple. You are not allowed to add “0” to the number, you have to convert it to the
string first. This is exactly what this line of code does:
conStr=inNum+"";
It takes the range of numbers and adds blank space “” at the end.
Subsequently, script adds “0” prefix based on the assumption:
addPrf=inNum < 10? "000"+conStr:
inNum < 100? "00"+conStr:
inNum < 1000? "0"+conStr: conStr;
If the number is less than 10, add “000” to number converted to string, if number is less than 100,
add “00”, and if number is less than 1000 leave the value as it is.
The script does the job, however, if the range is different and spans from 1 … 100 or 10 … 10000 it
will not work without tweaking. This is an annoying problem to solve in Design Script.
I’ve started to learn Python recently and I came up with a brilliant solution to use a Python
code to solve the problem once and for all. My friend Miguel gave me a hint to use a string.format
method to effectively sort the thing out. This is a snapshot of the Jupyter notebook:
3. By WOJCIECH KLEPACKI wklepa@gmail.com
And a ready solution “encapsulated” in a custom node:
It works in a very elegant way, formatting any sequence of numbers. The script after putting
everything into Revit – Dynamo world looks exactly like the one in the snapshot below:
4. By WOJCIECH KLEPACKI wklepa@gmail.com
It’s not only robust but elegant as well, isn’t it?