algorithmic modeling for Rhino
Hello.
I would like to make a C# component in Rhino, with following C# code:
#include<stdio.h>
#include<math.h>
void main(){
double f0;
double n21,n22,f;
double A=0.373;
printf("# f0, f, n21,n22\n");
for(f0=1.0e-10;f0<=0.1;f0=f0*pow(10.0,0.02)){
n21=-2.0+floor(1.29*pow(f0,-0.25));
n22=-2.0+floor(A*pow(f0,-0.25)*(12.0)*sqrt((n21+2.0)/(11.0*n21+43.0)));
f=(27.0/2.0)*f0*pow(n21+2.0,-2.0)*pow(n22+2.0,-2.0);
printf("%g %g %i %i\n",f0,f,(int)n21,(int)n22);
}
}
Any help with this please?
I am beginner with both C# and creation of C# components in Grasshopper.
I attached the .c file with the same above code.
Thank you.
Tags:
Hello,
The code below should work when pasted into a C# script component. For now, there is an error because floor is not defined anywhere.
double f0;
double n21, n22, f;
double A = 0.373;
output = "# f0, f, n21, n22 \n";
for (f0 = 1.0e-10; f0 <= 0.1; f0 = f0 * Math.Pow(10.0, 0.02))
{
n21 = -2.0 + floor(1.29 * Math.Pow(f0, -0.25));
n22 = -2.0 + floor(A * Math.Pow(f0, -0.25) * (12.0) * Math.Sqrt((n21 + 2.0) / (11.0 * n21 + 43.0)));
f = (27.0 / 2.0) * f0 * Math.Pow(n21 + 2.0, -2.0) * Math.Pow(n22 + 2.0, -2.0);
output = output + f0.ToString() + " " + f.ToString() + " " + ((int) n21).ToString() + " " + ((int) n22).ToString() + "\n";
}
Hello again!
It seems that the C# script component requires at least one input parameter [1], even if it is not used. So you will have to fix that in the component in my solution above.
[1] Then the RunScript method will look like this:
private void RunScript(object x, ref object output)
{blah...blah}
Yeah this is a bug in the current version. The Script components don't like having no inputs or no outputs. I've fixed it for 0.9
--
David Rutten
david@mcneel.com
Poprad, Slovakia
You need to assign a value to the 'ref object A' argument in the RunScript method signature. You're declaring another variable called A inside your method 'double A = ...'. I'm not sure how this is legal, I'd expected a compiler error.
Do not declare A again, just assign your outputs directly to the A that already exists.
--
David Rutten
david@mcneel.com
Poprad, Slovakia
C is an old language. It was developed in the early 1970's and it is quite primitive. Bjarne Stroustrup (among others) extended C with the notion of OOP and it became 'C with classes' or 'C plus plus'. C++ arose in the early 80's and it is the language in which Rhino itself is written. C# is the next major version in this language family and it is the one used in .NET development. C# first appeared somewhere around 2001 and has gone through 4 major upgrades since then.
Grasshopper is written in C# and VB.NET (which is the .NET flavour of the Visual Basic language family) and the script components also allow only C# and VB.NET, none of the other c-style or vb-style languages out there.
There is no way to flawlessly convert code from C++ to C#. The two languages have non-overlapping features. Simple mathematical algorithms though tend to be highly portable.
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Hello, a couple of things in addition to what David pointed out:
1. Replace floor with Math.Floor
2. You cannot declare local variables inside the RunScript method with the same names as that of the input parameters (since they becomes the names of the arguments for the RunScript method). This gives an error. So either
1. You forgot to replace floor with Math.Floor inside the code.
2. In my example, I had set my output parameter name as output (see above, it says ref object output). But since you have renamed the output parameter to r, wherever it says output in the code, replace it with r.
Thank you Sridevi! It works!
Sorry for being lazy.
One more question, if I may ask: is there a way for this script to show the results (output = "# f0, f, n21, n22 \n";) by indexes (indices)?
Ah, so that is why there was a # symbol in the heading :)
Assuming that you want the numbering to start from 1, add/edit the following lines:
int index;
for (f0 = 1.0e-10, index = 1; f0 <= 0.1; f0 = f0 * Math.Pow(10.0, 0.02), index++)
output = output + index.ToString() + " " + f0.ToString() + " " + f.ToString() + " " + ((int) n21).ToString() + " " + ((int) n22).ToString() + "\n";
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by