Help with Rhinoscript (for a user with basic macro knowledge)

Hi all, this is my first post.

I am a Rhinoceros user, firstly, I started using this programme for work, I had no CAD background prior to using, but a decent amount of Geometry / Vector / Math knowledge, and some limited coding know-how. I have really grown to like this programme a lot.

Anyway, so far, I have sped up a lot of my workflow with the use of macros.

I am wanting to start automating further, with the help of Rhinoscript. The vast majority of my work is in 2D. So to get to grips with syntax, I want to start with inputting data assigned to variables, to create rectangles, then manipulating, moving and offsetting these from the variables I have defined.

I am falling at the first hurdle, as I know what I need to do, but do not quite understand how to work the syntax / “pass” the data

For instance, to create a rectangle, I know I first need to define a centre point, width, and height. How do I then pass the information into the "Rhino.Command “_Rectangle” command?

This is what I have started with so far:

Dim arrCentre
arrCenter = Rhino.GetPoint(“Center point of Rectangle”)
Dim intRectangleW, intRectangleH
intRectangleW = “w” & Rhino.GetReal(“Rectangle Width”)
intRectangleH = “w” & Rhino.GetReal(“Rectangle Height”)

Rhino.Command “_Rectangle” & intRectangleW & intRectangleH

I understand I might have missed a step in the last line, for when I enter the “Rectangle” command into the command line, before I enter any co-ordinates, I first need to select “3Point.” How do I do this with Rhinoscript?

Any pointers going forward would be greatly appreciated, I understand Python is perhaps a more comprehensive language, but VBscript / Rhinoscript is something I would very much like to get to grips with first as I use a lot of Excel in conjunction with Rhino.

Thanks very much in advance everyone.

Hi Tom,
You have quite a few examples in the developer Guides side of the Rhino dark web :wink: Here a link to the Rhinoscript section :

BTW, I’m an ol’ VBA freak & Pascal guy and the oldtimers here on the forum tried to convince me to go for C# instead (which I had not used before). I was reluctant at first but they were right, I finally gave up all the Rhinoscript and VB.Net stuff and went for C#. Best choice ever. At least for the Rhino stuff.

I’m still using VBA with Excel and I still like it no matter what other people say (and even VBScript, in EA Architect), but C# really was a pleasant language to learn and it performs very well too.

But with Rhino/Grasshopper, don’t waste your time with Rhinoscript. Just sayin’

You also have this page, the most important one, namely RhinoCommon, where you also have some code examples for C#, VB and Python:
https://developer.rhino3d.com/api/RhinoCommon/html/R_Project_RhinoCommon.htm

// Rolf

1 Like

I support this message.

I personally chose Python because I use other software which can also be scripted with Python and because I do scientific and technical work, where Cpython is extremely useful. I am very happy with this decision. The Rhino developer docs describe Python as a modern substitute for RhinoScript. If you are only scripting Rhino and/or other dotNet apps then C# is probably the best choice.

Happy coding !

Graham

1 Like

Well, to answer the original question - there are a number of things wrong.

If you want to script with Rhino.Command, you need to have a string that is exactly what the command line expects - in the right order, with the right info, with spaces where necessary etc. - all in the form of one long string.

GetPoint returns an array of 3 numbers, these need to be converted to a string (Rhino.Pt2Str method)
GetReal returns a number, so that needs to be converted into a string (Cstr function in VB)
The rectangle needs a start point - note this by default is the lower left corner, not the center. If you want from center, you need to add the ‘Center’ option on the command line.
Then it’s just a mater of concatenating the string and including the necessary spaces (VB makes this unnecessarily complicated).

Option Explicit

Call Main()
Sub Main()
	
	Dim arrCentre, intRectangleW, intRectangleH
	arrCentre = Rhino.GetPoint("Center point of Rectangle")
	intRectangleW = Rhino.GetReal("Rectangle Width")
	intRectangleH = Rhino.GetReal("Rectangle Height")

	Rhino.Command "_Rectangle Center " & Rhino.Pt2Str(arrCentre) & " " & Cstr(intRectangleW) & " " & Cstr(intRectangleH)
End Sub

Al that being said, I would not do it this way. I would learn to do it with native Rhinoscript methods, not calling Rhino commands via Rhinoscript. And like the others above, I would not prefer Rhinoscript, but rather Python with rhinoscriptsyntax.

There is no AddRectangle method in Rhinoscript, so the way to make a rectangle is to do it with a polyline Rhino.AddPolyline(points) and 4 corner points. That requires a bit more arithmetic and a couple more lines of code, but maybe you want to try it for yourself… Check out the Rhinoscript help pages.

2 Likes

Thanks for this Helvetosaur.

Rhino.Command seems longer winded than I was expecting.

I chose doing this in RhinoScript as I am most familiar with Rhino commands from making my own macros.
Your explanation of my code helps me see the drawbacks of using this… as I don’t know all of the command line “parameters”.

I think I will have to reconsider, and look into Python, as I struggle to find documentation on all the parameters for Rhino.Commands.

Basically, it’s exactly what you see on the command line when you run the native command, so the ‘documentation’ is basically run the command in Rhino with all the options and copy and paste (with some formatting adjustments).

But yeah, try Python/rhinoscriptsyntax and see if you can start doing what you want with the provided methods. It’s a bit more of a hump to get over at first, but once you are sliding down the other side, you will not look back.