Conditions and Functions in Code Block

Functions work (when not Imperative)

I was probably doing something wrong earlier

20140522-2

If, else and while are from imperative code.

(condition) ? (what happens if true) : (what happen in false) ; …is asociative code . Associative code is just the normal code you can use directly in CBN (imperative you have to start with var=[Imperative] and put the imperative block between {} ).

For example, You have a curve (Cu) and you want to create a Point in the middle only when its length is bigger than D. If not, a point in one third of the distance

NewPoint=Cu.Length>D ? Cu.PointAtParameter(0.5): Cu.PointAtParameter(1/3) ;

This sample points one of the difference between AutocadDS and Dynamo

Autocad DS—Points (and Planes and Coordinate systems) related to curves or surfaces ---- “create” methods , in this case

Point.AtParameter (Cu,0.5)

Dynamo —Points (and Planes and Coordinate systems) related to curves or surfaces —“action” methods, in this case

Cu.PointAtParameter(0.5)

 

 

 

 

 

 

 

2 Likes

There was a mistake in the code I pasted earlier.

Below is the corrected version

20140522-2

Thanks Eduarado.

I’ll stay away from exploring Imperative code for now.

Yep, by the moment it is better to avoid to use imperative code at all

Anyway, for simple conditional statements, normal associative code is far more direct and convenient to use.

Regards,

Eduardo

1 Like

It seems that imperative code is working now (use last 0.7.1 builds)
01

The main use of imperative code is loops and conditionals with complex definition of what to do in every option

1 Like

Thanks for the update Eduardo.

Some cases in Imperative we have fixed in recent builds - try the following with latest daily builds.

if you trying conditional - you can try inline conditional -

a=5;
b=a==5?1:2;

OR

b=[Imperative]
{
a=1;
if(true)
{
return =2;
}
else
{
return=3;
}
};

e.g. for while

c=[Imperative]
{
i=0;
b;
while(i<2)
{
b=i+10;
i=i+1;
}
return=b;
};

E.g. for loop

c=[Imperative]
{
a={1,2,3,4,5};
sum=0;
for (i in a)
{
sum=sum+i;
}
return=sum;
};

This is still in testing stages. So give it a try !

Monika

1 Like

Is there a way to reference within, variables and collections declared outside the Imperative block?

Doesn’t feel right, but I am currently pasting copies of the same collection within multiple Imperative code blocks.

Thanks.

Regards,

Vikram Subbaiah

I am not 100% sure. But I think it is bug. I just posted this problem in github

1 Like

Hello Vikram Subbaiah,

<span style=“line-height: 1.5em;”>We are working on the fix and will get back to you once it is available in Daily Build.</span>

Keep using Dynamo!

Thanks,

Ritesh

1 Like

Thanks Ritesh!

I have been actively working on designing with Design Script over the last few weeks and at the moment it seems like it’s potential is only limited by my ignorance of various utilities and features that probably already exist.

However, it is inconsistent. Geometry doesn’t generate sometimes (The code block turns yellow with warning), but on restarting Dynamo, the same code works.

Regards,

Vikram Subbaiah

 

Hi all,

Sorry for reviving an old burried thread but I have been trying to do a If-ElseIf-Else Statement in a Design Script format without using a Imperative Code. Is that now feasible in the current version on Dynamo ? If yes can someone post an example, I haven’t been able to find the right syntax.

Apologies for my ignorance… Been using this for a few days.

Cheers,

Thomas

The associative DS syntax for the IF function is: test ? true_value : false_value

If you want to simulate an IF - ElseIf -Else functionality, you’ll need to nest one if function inside the other. Another nice approach is to use the “Formula” node to do the same:

2015-08-07_09-57-03

1 Like

Hi Dimitar,

Thanks for your answer ! You’re crazy fast at answering these posts, pretty impressive…

I had seen the “Formula” node but I don’t find it super convenient as you cannot add several lines to it, like a Code Block. I had notice that it worked a bit like a Excel If Statement were you could nest Ifs inside an If statement. Didn’t realize you could do the same with Design Script.

Thanks !

Hey guys, did we ever reach resolution on Vikram’s inquiry?:

“Is there a way to reference within, variables and collections declared outside the Imperative block?” And please pardon my limited knowledge of design script at this point (I don’t believe I’ve written an “Imperitive” block yet, but maybe this is the same question:

Is it possible to reference variables and/or collections declared outside a typical code block defined function?:

 

 

Better picture:

 

Nick, a solution to the Imperative code block issue can be found in this discussion on the forum.

It is possible to reference variables, however, those references need to be brought in as input parameters in the function being defined.

In your example, for the function to return linepreview, it first needs to have linepreview brought in as an input parameter

def active(linepreview)

{

return=linepreview;

};


Important: This example serves no real purpose and does not in any way demonstrate the purpose or utility of functions. The utility of the example is limited to the issue raised here.

 

 

Hi Vikram,

Unfortunately, the link is broken.

I am importing Excel-data and would like to use functions to call lists instead of carrying the data throughout the sciprt. Is this possible?

link

here