by Chris Smith
Introduction
Sometimes when creating automation in iLogic, we may find the need to loop through several objects, like parameters, parts, property values etc. One of the tools available is the FOR loop.
Breakdown
A FOR loop allows the user to simplify their code by cycling through the same code block until specific criteria has been met. This can help in several ways, but mainly by keeping the code as simple as possible while being easier to understand when shared with other users.
Example.
Imagine we have several parts in an assembly, and we would like to change the appearance colour based on user selection. In the model below we would like to change the appearance for different wood types.
We could write the code like the below example, referencing each part for each appearance option.
The above code will update the appearances as expected, however editing the code, adding more appearances or additional components would require over a dozen edits per appearance.
With a ‘For’ loop, we can streamline the code. The below code will cycle through each component in the assembly and change the material/appearance.
The code is much simpler than the previous code example and allows for much faster updates.
Another example below will cycle through numbers 1 to 10 and display a message box to display the current value.
As below.
In summary, the For loop can be a very powerful tool not only to streamline your code, but to also make future editing simpler as well.
By Chris Smith
Komentarze (Komentarze: 0)