If statement doesn't work well

Hi all,

I’m a newbie to Design Script, would be glad for some help:

I have a bunch of elements I want to create an opening for.
First I created an opening and then I use an If-statment to change it’s type (in the pic):
If a type with the given name exists it change the openings type to this type.
If it doesn’t exist, it should create the type, define the diameter and then change the openings type.

The “if-false” part works perfectly (which surprised me, I thought I would need a loop to get it to work for each element).
The “if-true” part works fine when there is only one element selected, but when working with a few elements- it doesn’t change the type!

Any suggestions?!

Thanks in advance,
Ifat.

This is just a guess, because I can’t see the lists you are feeding it, but I suspect there is some kind of a mismatch between the number of elements in each list you are feeding it.

“Open Instance” is a list of elements (I’m guessing)? “doesNameExist” is a matching list with a true/false for each element in the list “OpenInstance”?

However, “listContainsTrue” is a list with a single element: Either true or False. So when this code attempts to iterate through the lists, you only get one shot at the first part of the IF statement.

Instead, your “IF” test should check whether the value for each entry in the “doesNameExist” is True or False, and then operate on the corresponding element from the “OpenInstance” list. This may be easier to do as a separate function, something like the attached image (see the end of this post).

Actually, it seems like you could do a lot of this right in your code block, instead of generating a bunch of lists outside the code block and attempting to pass them in.
Many of the parameters that you are passing in as lists: Element Type, Element Type Name, Opening diameter, could all be gotten on the fly within the loop, with less list management.
Also, your checks and generation of new data could be done right within the loops. For example, your test of “does the type already exist”: that is, if “currentType” is a list that matches each element in the “OpenInstance” list, and “allRndTypes” is the list you are searching through.

Though, the more I think about it- if you are adding a new Type for each size that doesn’t exist, you probably want to update the list of existing types after each iteration of the loop, since you may have more than one opening of a size that doesn’t have a type yet? Something like this: “OpenInstance” is a list of opening elements.

OpenInstance;
[Imperative]
{
	for (opening in OpenInstance)
		//(Do whatever you did outside of here to generate the allRndTypes list)
		allRndTypes = {};
		currentType = Element.Type(Opening);
		currentTypeName = Element.GetParameterValueByName(opening,"Type");
		if(List.ContainsItem(currentTypeName, allRndTypes))
		{
			// It's not clear to me why you need to do anything if the current type already
			// exists, but here's where you'd do it anyway
     	}
     	elseif (!List.ContainsItem(currentTypeName, allRndTypes))
     	{
			//(Do whatever you did outside of here to generate the newTypeName)
			newOpenType = ElementTypes.Duplicate(currentType,newTypeName);
			// Whatever else you're doing
			Elements.View.SetParameterByName(opening,"Type",newOpenTypeFinal);
		}
		else
		{
			return = "error";
		}
	}
}

good luck!
Joe

Hi Joe,
Looks good, I will try your solution and let you know how it went.

Thanks!
Ifat.