String to DS Code

Hi All,

Is there a way to use a string as design code? I have a string imported from excel which was then formatted as design script, but cant seem to get it to work. The picture attached should make it obvious what I’m trying to do.

I think your problem may actually have a simpler solution. If you isolate each of the coordinates, you can provide these directly to the standard Point.ByCoordinates node. See below:


str to pt.dyn (25.3 KB)

There are more elegant ways to parse strings if you use Python, but this OOTB solution works as well.

1 Like

@Aparajit_Pratap - is there a function to eval DS code?
you could probably do this in a more manual way by writing a .ds file and importing it with file import.

I guess the point of this exercise was just to see if it was possible to run DS code from some formatted strings out of curiosity.

I had to place some elements based on points I extracted from another project and I was playing around with ways to convert the exported points for my family placement. I ended up creating a custom node that split the string apart and created points using OOTB nodes.

@cgartland Thanks for the suggestion. I did something similar to your example, but yours is much cleaner.

@Julian_Safar

hmm - actually looking at this again - it looks like this is for evaluating a function - not compiling and executing some arbitrary DS - :frowning:

probably the easiest way to do this that I can think of is to have an extension that creates codeblocks and sets their code property to your string.

interesting exercise
here is 2 alternative with python script

code_1

import re
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

def gener_pts(str):
	lst_ = str.split(",")
	for x in lst_:
		ptx = re.findall("[\d.]", x)
		yield ("".join(ptx))
			
data_str = IN[0]

if not isinstance(data_str, list):
	data_str = [data_str]

out = []

for x in data_str:
	ptx_lst = gener_pts(x)
	ptx_X = float(ptx_lst.next())
	ptx_Y = float(ptx_lst.next())
	ptx_Z = float(ptx_lst.next())
	pts = Point.ByCoordinates(ptx_X,ptx_Y,ptx_Z)
	out.append(pts)

OUT = out

code_2

import re
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


data_str = IN[0]
out = []

if not isinstance(data_str, list):
	data_str = [data_str]

for x in data_str:
	_temp = re.sub ("[XYZ= Point]", "",x )
	out.append( eval("Point.ByCoordinates" + _temp))

OUT = out

Yes, like you said we can write DS scripts in a file with .DS extension and import it into Dynamo using File->Import menu. This is just like importing a dll, where importing it adds new nodes in the library except that in this case the nodes will be implemented in DesignScript.

1 Like