Intermediate Scripting 01: Inch Precision

Software: 
Tag: 
Video Duration: 
7 minutes
Author: 
Zach Downey

This tutorial isn't really advanced, but then again it isn't really intro either, hence it is intermediate.  This isn't a mind blowing script by any means, however, it is very useful.  In this particular case it was used to comunicate construction/fabrication measurements to a team of students at the University of Tennessee assembling a suspended cable ceiling.  Since all the measurements were done with a tape measure, the most realistic accuracy we were going to achieve was about +/- 1/8".  Grasshopper's default dimensional output is a decimal floating point number, which is unsuitable for that kind of "hand measured" dimension.  So this script allows the adjustment of the fractional inch by setting up a tolerance slider.  I did this script on the fly while we were fabricating the pieces, so I'm sure there is room to improve it, but for my purposes it did the job.  Maybe it is useful to someone out there, I hope so, if not it at least illustrates the String.Format function of C# and also how to explicitly type cast from a double to an integer and of course the system Math.Round() method

int wholeNumber = (int) x;
 
double inchPart = Math.Round((x-wholeNumber) * inchPrecision);
if (inchPart == inchPrecision) {
    inchPart = 0;
    wholeNumber = wholeNumber + 1;
}
//The following line is not necessary I just used it as a check
B = wholeNumber + (inchPart / inchPrecision);
 
string answer = String.Format("{0} {1}/{2}", wholeNumber, inchPart, inchPrecision);
 
A = answer;

Rating

Please rate this tutorial below. Thanks.

5
Average: 5 (5 votes)

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.

:)