Monday, December 19, 2011

Using a While Loop to “Backsolve” a calculation.


For this experiment I have some geometry that must maintain a particular weight as my part changes. As features are added, subtracted or modified the diameter of the large outer cylinder is the variable that must change to meet the target weight.
To make it easy to modify the critical values of the part I've created a few user parameters and assembled them together in a nice tidy little form.



The code will evaluate the "mass target" value and begin to increment the outer diameter up or down until the value matches the actual mass (or as close as it can get within the given increment).
The user can make multiple changes to the variables and with one click of a button find the new value of the outer diameter!
 

To facilitate the experiment I created one custom iProperty called "Weight" that I will reference in the form. (I could use the actual "lbsmass" iProperty value from the model but this method will allow me to control the rounding factor of the value with a line of code).
I created some custom parameters and linked the model parameters to them accordingly:

Finally I added an iLogic rule that will do all the work:


iProperties.Material = Material
If iProperties.Mass < TargetMass Then          
    While iProperties.Mass < TargetMass

    Diameter = Diameter + .031
     RuleParametersOutput()
    InventorVb.DocumentUpdate(True)
End While

ElseIf iProperties.Mass > TargetMass Then
    While iProperties.Mass > TargetMass
    Diameter = Diameter - .031
    RuleParametersOutput()
    InventorVb.DocumentUpdate(True)
    End While
End If


Now any change to the model geometry will prompt the code to search for a diameter that satisfies my targeted mass requirement.

MAGIC!
Download the sample file and let me know if you can think of other ways code like this can help you automate your designs.

No comments:

Post a Comment