Hello everyone,
how can this code block function be further simplified:
e.g. fewer definitions str1, str2 …?
Best regards
Rolf
Hello everyone,
how can this code block function be further simplified:
e.g. fewer definitions str1, str2 …?
Best regards
Rolf
Rather than defining the search strings and then calling them in the function you could just define them in the function itself. You do still need to step through each replacement individually, since Dynamo won’t replace strings recursively like this.
Another option is to pull the string apart and then replace everything at once.
EDIT: You may also be able to simplify things even more with an Imperative block. I don’t have much experience here, so I’m not sure if you can make it any simpler or not.
Hi Nick,
thank you very much, this script look corresponds to my “aesthetic” ideas.
Thank you very much, this script look corresponds to my “aesthetic” ideas.
See you for my next problem then.
Best regards
Rolf
Here is another option for you, courtesy of @jacob.small and me playing
def sets()
{
return =
[
/*
A list of all your characters to replace
of any amount. Last character in list needs
to be the one you want the full list to
replace to in the form of:
[fromThis1, fromThis2, fromThisN, tothis]
*/
["à","á","â","ä","æ","ã","å","ā","a"],
["ô","ö","ò","œ","ø","ō","õ","ó","o"]
];
};
def altClean(str : string)
{
characterSet = sets();
str = [Imperative]
{
for (group in characterSet)
{
for (c in group)
{
s = str;
s = String.Replace(s,c,group[-1]);
str = s;
}
}
return = str;
}
return = str;
};
definition_AltClean.dyn (5.3 KB)
Could you tell me some resources to learn design script language ?
Thanks alot.
Hi @Mo-Samir , here are a few resources
This is the best source.
Indeed!
Shamelessly piling on to the Vikram hype.
Thank you @solamour
you are amazing
Thanks for being so kind and generous @solamour @Nick_Boyts @Robert_Younger
I was tring to do it with OOTB nodes splitting a text by the text to be replaced and adding in between the split list the replacements, but I am impressed of what I see posted here
A variant with Python (.Net)
import sys
import clr
import System
def altClean(txt):
tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(txt)
return System.Text.Encoding.UTF8.GetString(tempBytes)
OUT = altClean(IN[0])
If you’re in Manual mode, you may not have hit Run
You could also try this associative alternative to @solamour’s code.
// Function
def alt(str:var[]..[],chr:var[]..[])
{
r = List.LastItem(chr<1>);
a = String.Replace(str<1>,chr,r);
b = List.SetDifference(a,str);
c = List.Flatten([str,b]);
return List.LastItem(c);
};
// Characters
chr =
[["à","á","â","ä","æ","ã","å","ā","a"],
["ô","ö","ò","œ","ø","ō","õ","ó","o"]];
// Replacing
spl = String.Split(str,"");
con = String.Concat(alt(spl<1>,chr));
Hi,
thanks for the other versions.
The problem with code block can be localized:
Warning: Multiple definitions for ‘String’ found as DSCore.String, List.String, Orchid.Common.String, WombatDynamo.String
Multiple definitions for ‘String’ found as DSCore.String, List.String, Orchid.Common.String, WombatDynamo.String
Multiple definitions for ‘String’ found as DSCore.String, List.String, Orchid.Common.String, WombatDynamo.String
But why do I need DSCore.String?
The Python variant is also very nice.
Best regards
Rolf
This is a Namespace problem.
In DesignScript you can call the standard (DSCore) (short for DesignScript) function for String.
In some Packages however, programmers used the same name for that function in their package.
What you have to do here is making sure you call the function embedded in that specific package.
So DSCore.String should be replaced by Wombat.String etc. or Orchid.String.
This is related to the old naming convention in older versions of that Package.
Updating the Package might help too.
Hi Marcel,
Thank you for the information.
All of my packages have been updated with Revit 22.
To be on the safe side, I always use DSCore \ Orchid …
Best regards
Rolf
A small update for use with NetCore
code updated
import clr
import sys
import System
clr.AddReference("System.Text.Encoding.CodePages")
from System.Text import Encoding, CodePagesEncodingProvider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
def altClean(txt):
tempBytes = Encoding.GetEncoding("ISO-8859-8").GetBytes(txt)
return Encoding.UTF8.GetString(tempBytes)
OUT = altClean(IN[0])