Modify expression with input from another expression
Modify expression with input from another expression
(OP)
I want to itterate the value of one expression (measurement) with input from another expression (length)
The first expression is directly relatetd to the second one.
I want to start with a value (say 2000) for the second expression (exp2), the first expression (exp1) is then 414.8119359.... (measured value)
I want to be able to set a value for instance 450.00 as a target value and a step value, for instance 10.
I want the code to function something like this:
If exp1 < target
then exp2 = exp2-step
Else end if
'loop this while exp1 < target
step = step/2
'smaller itteration steps as exp1 gets closer to target value
If exp1 > target
then exp2 = exp2+step
Else end if
and then i want to loop this over and over until exp1 reaches target value with at least 2 decimals precision.
I intend to build on this with excel imports and exports of values , i think i can handle that part but i really need some help with the itteration part.
I attached a simple partfile to further show how i want i to function.
I work in NX10 even though the part file is from NX8. I have no code to start from really and i don't expect a ready to go script in return but something to get me started :)
The first expression is directly relatetd to the second one.
I want to start with a value (say 2000) for the second expression (exp2), the first expression (exp1) is then 414.8119359.... (measured value)
I want to be able to set a value for instance 450.00 as a target value and a step value, for instance 10.
I want the code to function something like this:
If exp1 < target
then exp2 = exp2-step
Else end if
'loop this while exp1 < target
step = step/2
'smaller itteration steps as exp1 gets closer to target value
If exp1 > target
then exp2 = exp2+step
Else end if
and then i want to loop this over and over until exp1 reaches target value with at least 2 decimals precision.
I intend to build on this with excel imports and exports of values , i think i can handle that part but i really need some help with the itteration part.
I attached a simple partfile to further show how i want i to function.
I work in NX10 even though the part file is from NX8. I have no code to start from really and i don't expect a ready to go script in return but something to get me started :)





RE: Modify expression with input from another expression
However, NX does offer you a couple of approaches for doing something like this, one that can be done inside the NX model using the 'Optimization' tool, the other, doing this using the 'Goal Seek' capabilities of the Modeling Spreadsheet.
The first of these two option, the 'Optimization' tool, which is the officially recommended approach, does require the purchase of an add-on module, the 'Optimization Wizard'. So if you already have this licence, then it can be found at the bottom of the 'More' panel on the 'Tools' ribbon tab (or you could just use the Command Finder and enter 'optimization').
The second approach, using the Modeling Spreadsheet, requires no additional licenses but does require that you set-up a spreadsheet using the builtin Goal Seek feature.
To give you an idea of how the Spreadsheet approach works, I've include an example file and video showing you how to run the goal seek.
One of these two approaches should give you what you need.
John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Digital Factory
Cypress, CA
Siemens PLM:
UG/NX Museum:
To an Engineer, the glass is twice as big as it needs to be.
RE: Modify expression with input from another expression
For I=0 to 100000
"the code"
I=I+1
If |exp1-target| < 0,001
end For
100000 itterations should definitely be enough :)
I have done most of my (very limited) programming in Matlab so if this way of thinking doesent work in NX please let me know.
RE: Modify expression with input from another expression
To learn more about the Maple Worksheet Integration functionality, simply open your NX Help file and do a search on "Maple". Now there is no special NX licence needed to hook a Maple Worksheet to an NX part file via the expression system, but you will need to purchase Maple, just like you having to purchase Matlab.
John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Digital Factory
Cypress, CA
Siemens PLM:
UG/NX Museum:
To an Engineer, the glass is twice as big as it needs to be.
RE: Modify expression with input from another expression
That's not completely accurate...
The Knowledge Fusion language underlying expressions supports iterative loops quite nicely, and there are lots of examples of OOTB DesignLogic functions that use programmatic iteration to calculate answers. I just posted an answer on another thread that included this, in fact. (...which reminded me to reply to this thread.)
But there are limits on what can/should be done with iteration within expressions. We actively discourage (read: aggressively prevent) "NX model updates within NX model updates", and this is where you'll find the roadblock down this path. And the road sign announcing the dead end will look a lot like this:
Iteration that doesn't include model updates is a pretty handy thing. But we've deliberately chosen not to expose as much of the iterative KF syntax within Expressions, specifically, because it's pretty easy to find that dead end. But iterating through a list of data to choose (or calculate) a best value (for example) can be done without actually causing an NX update to occur, and so NX will allow this. And again, many OOTB DesignLogic functions already do this internally.
But getting back to your specific example...
Anything with a measurement feature in the loop is necessarily going to be performing model updates. And so as John describes, the NX Optimization function is probably the best way to do the kind of iteration you're describing. This function provides the framework for executing a large number of model updates in an organized way without causing recursion or infinite loops -- and is designed to gracefully handle occasional model update failures in corner cases, too.
Does that help a bit?
Taylor Anderson
NX Product Manager, Knowledge Reuse and NX Design
Product Engineering Software
Siemens Product Lifecycle Management Software Inc.
(Phoenix, Arizona)