Conditions and Functions in Code Block

I am using extensively functions in 0.7. Take into account:

1.Imperative code seems to have issues . Loops are not working at all for example (at least the last time i checked). I would avoid it by the moment.

2.There have been some changues in the lenguaje. And some of then are related to quite usual tasks. If you have experience before and you take a look to the library in 0.7, you will figure out quite fast what is going on. But if not, if you are starting to learn now using the manual for autocad DS, it can be very confusing. I suppose that we will have a new updated manual soon.

1. About conditions, as I said in 1, better to avoid imperative code. No issues until now with conditions in associative code.

I would recommend you try first with a very simple function. Maybe you can post here a specific sample that is giving you trouble.

1 Like

Conditional statements (If-Else, while) do not seem to work

However, what works is something in the format …

s = (a == b ? “Are equal” : “Are not equal”);

I’m a beginner, but, the Design Script manual still seems like a decent place to start. Some of the changes in the language, I figure out by referring to the names of the nodes while some others with a little imagination (BSplines now seem to be Nurbs) and then there are some I just can’t figure out. But since I’m presently just exploring the language, it’s alright.

Functions work (when not Imperative)

I was probably doing something wrong earlier

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

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)

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:

1 Like

Hi Dimitar,

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.