How to make a dynamic component with the ScaleTool that snap
-
Hi guys!
It would be really cool if someone can help me with the following:
I drew a tube in sketchup and I managed to hide some handles to scale (via component Attributes - Behavior - Scale tool). So the user can only scale the tube in one direction. No I would like the scaling to snap on a multiple of 25.
So when my tube is 100cm and I scale it to 523, the lenght should snap to 525. When I scale the tube to 7cm, it should snap to 25.
25cm is the shortest the tube can be. 12m is the longest the tube can be.Does anyone now how to do this?
I hope the question is clear enough. If not, don't hesitate to ask me more about the problem!
Thank you in advance and kind regads!
-
Hi There,
The maths to do this is relatively simple - divide by your "snap unit", round to an integer, then multiply up again by the snap unit.
But first, you need to know about the way that new values get updated from the user interface. For this, you need the special function "current()".
Current() receives a value from your mouse (or VCB) interaction, and then makes the DC equations update once, and only once. Without this, a reference such as...
*LenX = round(LenX/25)25
...doesn't know when to stop calculating, because every change to LenX makes it change again, and again - a circular reference. In reality, this kind of error is trapped, but still gives strange results.
The correct form is...
LenX = round(current("LenX")/25) * 25
Note that you must wrap the parameter name in quotes as shown here.You could also use the functions ceiling() or floor(), if you want the snapping to always round up, or always round down - round() always goes to the closest one.
Also, watch out for small lengths that might snap to zero length - the IF function can test for this, e.g...
*LenX = if ( current("LenX")>25, round(current("LenX")/25)25, 25)
Here, if current("LenX") is over 25, you will get the result of the rounding functions, otherwise you get a fixed value of 25.And there is yet another thing to be careful of. No matter what units you use for your drawing, current(), always return the length in inches! So if you are working in metric you may have to multiply the result by 2.54 before rounding.
**LenX = if ( current("LenX")>25, round(current("LenX")2.54/25)25, 25)There's a full list of functions HERE.
-
Thank you for the quick reply. I'm going to take some time to figure this out because I'm not that well-known with formulas.
I do understand the forumulas you gave me. Now I need to combine these untill I got what I need, am I right?
Is it still possible to adjust the tube in lenght with the scale tool?
Untill now I already created an attribute LenZ. Users could enter the length in a textbow. So I figure that it is either one or the other. Of "entering the lenght" - Or scaling the length with the limitation of a snap-formula. -
@goele said:
Untill now I already created an attribute LenZ. Users could enter the length in a textbow.
@goele said:
So I figure that it is either one or the other.
Yes, you are on the right track...
To use snapping from the Options it's best to use a drop-down list - if an attribute is using a formula, it cannot also be editable in the Component Options window. As soon as the user chooses a new value in the window, the literal value will replace the formula - leaving the snapping disabled.
You can create a custom "data entry" attribute to use in the Options window, and then reference that inside the formula. But, to do that, you would still need to disable the scale tool completely, as there is no way to make the formula switch automatically between using the custom attribute when editing the window, or using current("LenZ") when you use the scale tool - it would require a function something like "Most_Recently_Changed(attr1,attr2)", which SU does not have (though it would be nice).So, as you say, it is a matter of choosing one method or the other. This will depend on the context of the component - for "snap to grid" type functions, using the scale tool is usually most natural, whereas for "real world" objects that come in standard sizes (e.g. screw threads, shoe sizes), the Options window may be more appropriate.
-
Thank you very much!
I used this formula: LenZ=ROUND(CURRENT("LenZ")*2.54/25)*25 and it works perfectly.
@unknownuser said:
it would require a function something like "Most_Recently_Changed(attr1,attr2)", which SU does not have (though it would be nice).
It would be nice indeed.
Thank you for the quick replies. The explanation was very clear!
-
You're welcome
'bout time I was contributing a little more round here - the generosity of other forum users taught me most of what I use every day in SU. -
I'll pay it forward when my I have some more knowledge about SU.
Advertisement