Use c# from visual studio in GH

Hi,

I am new at using GH. Could anyone help with how could I use the code below I wrote in visual studio in GH using c#and a panel component? Is there any component that allows a value to be entered manually and change the output?

The objective of this simple piece of coding is simple. It is to generate what the Console.WriteLine statement says at the end before Console.ReadLine. It is a bit interactive as it asks the user to enter the numbers, hence why I asked the question about a GH component that allows values to be entered manually.

////////////

Console.WriteLine(“Please enter radius value:”);
double r = Double.Parse(Console.ReadLine());

Console.WriteLine(“Please enter number of circles:”);
int number = Int32.Parse(Console.ReadLine());

double area = Math.PI * r * r;
double totalarea = number * area;

Console.WriteLine(“For {0} circles of radius {1} the total area is {2}:”, number, r, totalarea);

Console.ReadLine();

/////////

Many thanks in advance for any help!

Will

How about something like this?:

example.gh (4.8 KB)

private void RunScript(double Radius, int NumberOfCircles, ref object Area, ref object AreaTotal)
{

double area = Math.PI * Math.Pow(Radius, 2);
double areaTotal = area * NumberOfCircles;

Area = area;
AreaTotal = areaTotal;

RhinoApp.WriteLine("The area is " + area.ToString());
RhinoApp.WriteLine("The total area is " + areaTotal.ToString());

}

1 Like

Many thanks Manuel!

Hi manuel but the circles are not visible in Rhino main screen why? sorry for asking if it is stupid question I am just very new to programming and trying to learn!

The example above only performs calculations but does not create any geometric objects.

If you want to create a circle you can use the “new Circle()” constructor.
This function and many other are documented in the RhinoCommon API documentation:
https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_Circle.htm

private void RunScript(Point3d Origin, double Radius, ref object A)
{
Circle circle = new Circle(Origin, Radius);
A = circle;
}

exampleCircle.gh (4.5 KB)

1 Like

Hi Manuel… thanks for the tip but I don’t think you understood what I am trying to create here. I want to create circles that are controlled by the number of circles so when I move the slider the number of circles doesn’t change why? thank you so much for your help!! Please, have a look at the file below and tell me when I am making the mistake?
Thanks in advanceexample.gh|attachment (14.5 KB)example.gh (14.5 KB)

Er … you use 3 of my C#'s (Lila works for me, I added a simple question in order to deal with the red thing). So … what exactly are you trying to do? Circles like in the 1st C#?

Circles_V1.gh (16.8 KB)

A good start is to avoid using C# keywords as variable names (“var” is a C# keyword, hence the red underline markers). And even a valid variable name must be used in the rest of the code to have any effect.

image

// Rolf

no I am not trying to create anything like this ! I just want a surface or a 2D grid that I can be able to control the number of circles because so far I can control the radius but not the number of circles! That’s all look at my file…

example.gh (14.1 KB)

thank you :slight_smile:

Pardon me but what means this? (not to mention your example). Provide a sketch that clearly explains the goal (like talking to a very stupid robot):

For instance:

  1. I have a List of Surfaces (or better: BrepFaces),
  2. I want to create a grid in U/V (that yields a Tree),
  3. Using the nodes (with this or that way) I want to create objects (of this or that type) according … blah, blah.
  4. Then using the objects (and the Surfaces/BrepFaces) I want to do this (or that)

well I will tell you what I want to create; the goal is :

Create a small function that outputs the total area of a number of circles with the same radius. The radius and number of circles should be variable inputs.
The output should be a string that writes: For “n” circles of “R” radius the total area is “A”.

This is what I was trying to achieve !

So I tried to create a 2D grid to put the circles in. I did manage to do this and all the circles are created have the same radius … so far so good but when I am changing the number of circles, the circles in rhino space do not change because it is been controlled by the grid I think? So I want to change the number or circles that my grid has at times so sometimes I have 5 circles sometimes I have 10 circles in the system … I ll put a quick sketch to show you … thanks in advance!

OK, that’s very easy. I’ll do it as soon as I can recover from what happened in Suzuka F1 race (Lewis was 10 seconds ahead everybody (10 laps to go) … and then Totto - or was James ? - did the ultra genius thing: he pitted him !!!). Later Totto said that this was a 50-50 call (between stupidity and honesty I guess).

1 Like

is it really easy ? I am so new to that!!! please do it and explain to me as well … please!!! so I can understand! I am not as smart as you and I am new to programming! so you are with Lwis Hamilton? same as me and my dad!!! hopefully he will be alright! THANK YOU SO MUCH!!!

PS: The days of no-action are over: IF Lewis encounters ANY other “similar” issue in the remaining races this year (or in any future race in the years to come), I the Lord of Darkness (keeper of the Truth Out There etc etc) … well Totto you get the gist of my message - I do hope.

Other than that, on more trivial matters:

CirclesOnGrid_EntryLevel_V1.gh (125.7 KB)

Notify if you are brave enouph to dive into the abyss for doing this properly for the general case (clustering, any query, classes, cats, dogs etc etc).

1 Like

EVERYTHING LOOKS GREAT!!! oh my God!!! BUT as I told you the radius of the circles shouldn’t change you know the radius of the circles should be the same! I think what they ask in the exercise it is something more simple. A simpler version of this. cause we just started programming you know. Man please how did you do that … I don’t know! Can you give me your email ? Do you offer online tutorials etc??

My dear Watson: If they are steady why bother talking?

Anyway … since you support Lewis (and Valentino I do hope) you’ll get extra stuff.

CirclesOnGrid_EntryLevel_V1A.gh (126.7 KB)

And no: I don’t offer online tutorials (not to mention that I hate Internet and computers and Windows and this and that).

Hello Peter,

Are you really sure you can’t give any tutorials ? I will need some support during my studies. I am very knew to programming - I don’t know a thing you know :$. Anyway I really appreciate your help … thank you so much!

Kind Regards,

Margarita


PeterFotiadis
Pfotiad0

    October 13

new:
shouldn’t change

My dear Watson: If they are steady why bother talking?

Anyway … since you support Lewis (and Valentino I do hope) you’ll get extra stuff.

CirclesOnGrid_EntryLevel_V1A.gh (126.7 KB)

And no: I don’t offer online tutorials (not to mention that I hate Internet and computers and Windows and this and that).

I’m not an Academic nor I have any interest to become one. But some times I help people who walk the right walk (that’s critical if you are in the AEC market sector).

I would suggest these:

  1. C# 7.0 in a Nutshell (Joseph/Ben Albahari) - my favorite.
  2. C# in Depth (Jon Skeet) - the bible.
  3. Effective, More Effective C# (Bill Wagner)
  4. Concurrency in C# Cookbook (Stephen Cleary)
  5. C# 6 for Programmers (Paul/Harvey Dietel)
1 Like

Hi Peter,

I am sorry to bother you but can you please explain to me how to do this algorithm in grasshopper? I have attached the word file.

I would really appreciate your help :slight_smile:

Kind Regards,

Margarita

PeterFotiadis
Pfotiad0

    October 13

new:
shouldn’t change

My dear Watson: If they are steady why bother talking?

Anyway … since you support Lewis (and Valentino I do hope) you’ll get extra stuff.

CirclesOnGrid_EntryLevel_V1A.gh (126.7 KB)

And no: I don’t offer online tutorials (not to mention that I hate Internet and computers and Windows and this and that).

(Attachment workshop.docx is missing)