# More scripting help... for translating into a spreadsheet (clipboard)

OK So basically I am attempting to create a bounding box around a selection then find the centroid of that, then copy the coordinates of the centroid to the clipboard separated by tabs so they go into the correct spreadsheet cells.

I’ve got the bounding box figured out.

So Now I need to get the values from the bounding box array (0) and (1) and use Mean to find the middle of that? How do I get the two values I need out of the bounding box array into another array that Mean will swallow?

Or Maybe I could use SurfaceVolumeCentroid… But I have a similar issue. It wants a string so how would I GetObject and pick my bounding box from step 1?

Hi Ryan - use Bounding box corners (0) and (6) to find the center of the BB.

here’s a function to average points - it is a little overkill for just the two but it might come in handy elsewhere:

Function AveragePoints(aPts)
'Finds the average coordinates of an array
’of points.

``````Dim X, Y, Z
Dim i

X = 0
Y = 0
Z = 0

For i=0 To UBound(aPts)

X = X + aPts(i)(0)

Y = Y + aPts(i)(1)

Z = Z + apts(i)(2)
Next

'Divide by the number of points to
'get the average  for each
'create the output array from the 3 averages
AveragePoints = array(X / (UBound(aPts) + 1), Y / (UBound(aPts) + 1), Z / (UBound(aPts) + 1))
``````

End Function

-Pascal

If you have the 8 corner points, center of a bounding box is the average of point[0] and point[6]. If you are using Python, getting the average point between two points is as simple as adding them together and dividing by two:

bb is your list/array of 8 corner points

``````bb_ctr=(bb[0]+bb[6])/2
``````

If you are using vbscript, it’s easiest to use some Rhinoscript methods:

``````bb_ctr=Rhino.PointDivide(Rhino.PointAdd(bb(0),bb(6)),2)
``````

–Mitch

Thanks Guys.

Pascal in the interest of not going overkill and keeping this really simple so I can hopefully work towards figuring this out.

I’ve got my bounding box BB.

So for example If I wanted to see it I would Rhino.Addbox(BB)

I am trying just to do something like this

Dim ObjectX, Obje…
ObjectX = (BB(0) + BB(1)) / 2
’ y and z lines
Rhino.ClipboardText (ObjectX, tab, tab, tab, ObjectY…) or however the syntax works there.

OR maybe

Dim ObjectCG
Dim BBforCentroid
BBforCentroid = Rhino.GetObject (box we just made?)
ObjectCG = Rhino.SurfaceVolumeCentroid (BBforCentroid)
Rhino.ClipboardText(ObjectCG(1), tab, tab …)

Copy/Paste To Excel

In RhinoScript use Rhino.Pt2Str(Pt) to make a string from the point x,y,z array - is that what you need? Or you can do it yourself with custom separators:

dim strPt: strPt = Pt(0) & Chr(9) & Pt(1) & chr(9) & pt(2)

?

-Pascal

Call Main()
Sub Main()
Dim ObjectforCG
ObjectforCG = Rhino.GetObject(“Pick”, 0, True, True)

``````Dim BB
BB = Rhino.BoundingBox(ObjectforCG)
Rhino.AddBox(BB) 'just to make sure I'm picking the right thing

Dim X

X = (BB(0) + BB(1)) / 2

Rhino.MessageBox(X)

'Rhino.ClipboardText(X)
``````

End Sub

Im getting a type mismatch, assuming because I am trying to pass a number when it’s expecting a string… ?

Standby I think I’ve got a winner.

I actually missed mitch’s post hence the dumb one. This works:

``````Call Main()
Sub Main()
Dim ObjectforCG
ObjectforCG = Rhino.GetObject("Pick", 0, True, True)

Dim BB
BB = Rhino.BoundingBox(ObjectforCG)
'Rhino.AddBox(BB) 'just to make sure I'm picking the right thing

Dim bb_ctr

Dim strPt: strPt = bb_ctr(0) & Chr(9) & Chr(9) & Chr(9) & bb_ctr(1) & Chr(9) & Chr(9) & Chr(9) & bb_ctr(2)

'Rhino.MessageBox(strPt)

Rhino.ClipboardText(strPt)

End Sub
``````

If I wanted to check for a selection before the script runs (as it is now basically) and prompt the user to draw a box if there is no selection… would that be something for a macro? Or would you include that in the script?

So basically the flow would be select something and then run the script if the item is already in the model.

or run the script, prompted for a box, mess around with your box using gumball, press enter when done changing it, then the script finds the centroid… something deletes the box.

Also is there like a constant I could include append to the strPt string with today’s date? So my spreadsheet would have a column for ‘date updated from model’ or whatever…

–update

between Option Explicit and Call Main()

and it seems to go okay when I drag it on Rhino

But if I try and run it with the alias I get a type mismatch on line 1 char 0. I must be doing something obviously wrong.

Hi Ryan,

Can you post your complete script file so we can help clean it up? You can attached the file by poking the Upload button (in the Discourse editor).

– D

Thanks Dale

PtToCb.rvb (497 Bytes)

CGtoCB.rvb (549 Bytes)

I’d actually like these to be combined. Haven’t really started to try and figure that out yet.

If you run the script with nothing pre-selected, it will prompt you to pick points. (PtToCb.rvb)

If you run the script with something already selected it will just use that with the bounding box method (CGtoCB.rvb)

Here is a drag & drop ready version that contains both scripts, slightly altered.

ToClipboard.rvb (1.9 KB)

Sweet thanks Dale.

Could you point me in the direction for checking if the user has something already selected? Can a macro do that? I.e. in the button? Then the macro uses the appropriate alias?

Is it possible to issue shell commands in Rhinoscript? Like could I minimize the rhino window after we grab the info for the clipboard?

I wouldn’t bother investing the time into the scrip… but I’m going to have to do this thousands and thousands of times over the next year. It’s worth some time screwing around to make it efficient.

You can try this:

``````    Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys "% n"
``````

Thanks Jarek.

I googled for it and came up with the same snippet. Gave an error code which I didn’t write down.

works over here. knowing the error may help–