Move Object using BoundingBox measurements

Hi,

Fairly new to scripting. This seems like something I should be able to accomplish on my own, but I’m struggling to solve.

I’m trying to move a group of object in the X axis. I’d like the move distance to be full distance of it’s bounding box + 3mm. Then I paste the original copy.

The result will be two identical objects next to each other with a gap of 3mm in the middle of the object.

I don’t fully understand how arrays work. but this is the code I have so far.

Thanks so much for any help!

Rhino.Command "-SelAll"
Rhino.Command "-Copy I Enter"
arrSelected = Rhino.SelectedObjects

Dim arrBoundingBox
arrBoundingBox = Rhino.BoundingBox(arrSelected)
'	arrSelected = Rhino.MoveObjects(arrSelected, Array(0, 0, 0), Array(0, 0, arrBoundingBox(0)(2)))
		
arrSelected = Rhino.MoveObjects(arrSelected, Array(arrBoundingBox(0)(2) + 3, 0, 0), Array(0, 0, 0))

Rhino.Command "-Paste"
Rhino.UnselectAllObjects

Have a look at the following and see if it makes sense… I used all native Rhinoscript functions.
Let me know if there is something you don’t understand. --Mitch

Option Explicit

Call Main()
Sub Main()
	
	Dim arrObjs,arrBoundingBox, arrNewInsertPt,dblMoveDist,arrCopies
	
	'Rhino.NormalObjects should be all selectable objects'
	arrObjs = Rhino.NormalObjects
	If Not ISNull(arrObjs) Then
		arrBoundingBox = Rhino.BoundingBox(arrObjs)
		'arrBoundingBox(0) is lower left corner'
		'arrBoundingBox(1) is lower right corner'
		
		'Get length of bounding box X, add 3'
		dblMoveDist = arrBoundingBox(1)(0) - arrBoundingBox(0)(0) + 3
		
		'copy objects from original point to new point'
		arrCopies = Rhino.CopyObjects(arrObjs, Array(dblMoveDist, 0, 0))
	End If
End Sub

Wow Mitch, that worked perfectly. I really appreciate the comments you added to explain the script. It really helped to dissect whats happening. I’ve modified it to do different things to gain a better understanding and I think I get it!!

Can you tell me if the other corners of a bounding box follow the same convention? Can I reference all 8 corners?

‘arrBoundingBox(0) is lower left corner’
‘arrBoundingBox(1) is lower right corner’

Yes, and yes, you can.
0, 1, 2, and 3 are the corners of the lower BB face starting in the lower left corner and traveling CCW.
4, 5, 6 and 7 are the corners of the upper BB face starting in the lower left corner (of the upper face) and traveling CCW.

–Mitch

Mitch, Thanks very much for the help!

~J

I’m having trouble applying this concept to other situations. If I want to measure the height of the object could i use any of the following?

Thanks,
~J

zoffset = arrBoundingBox(4)(0) - arrBoundingBox(0)(0)
OR
zoffset = arrBoundingBox(5)(1) - arrBoundingBox(1)(1)
OR
zoffset = arrBoundingBox(7)(3) - arrBoundingBox(3)(3)
OR
zoffset = arrBoundingBox(6)(2) - arrBoundingBox(2)(2)

What you’re looking at here is an array of arrays, so maybe the notation is confusing you…

  • The outer array is the array of bounding box corners - 8 in total, with indices from 0 to 7
    arrBB(0), arrBB(1)…arrBB(7)

  • Each corner point is in itself an array of 3 coordinates (x,y,z). In a point array, the “0” index is always the x coordinate, the “1” index is the y coordinate and the “2” index is the z coordinate.

So, when you have arrBB(0), you are first addressing the lower left hand corner point of the box
When you have arrBB(0)(0) you are addressing the x coordinate value of the lower left hand corner point of the box.

So, to get the Z height of your box, you can actually subtract any of the upper 4 box corner point Z coordinates (they are all the same) from any of the lower box point z coordinates… the (n)(2) is always the Z coordinate…

So the following work:

zoffset = arrBoundingBox(4)(2) - arrBoundingBox(0)(2)
OR
zoffset = arrBoundingBox(5)(2) - arrBoundingBox(1)(2)
OR
zoffset = arrBoundingBox(7)(2) - arrBoundingBox(3)(2)
OR
zoffset = arrBoundingBox(6)(2) - arrBoundingBox(2)(2)

But something like

zoffset = arrBoundingBox(6)(2) - arrBoundingBox(0)(2)

will also work…

HTH, --Mitch

to confirm my understanding…

If I wanted to find the X distance it could read.
zoffset = arrBoundingBox(0)(0) - arrBoundingBox(1)(0)
OR
zoffset = arrBoundingBox(3)(0) - arrBoundingBox(2)(0)
OR
zoffset = arrBoundingBox(4)(0) - arrBoundingBox(5)(0)
OR
zoffset = arrBoundingBox(7)(0) - arrBoundingBox(6)(0)

If I wanted to find the Y distance it could read.
zoffset = arrBoundingBox(0)(1) - arrBoundingBox(3)(1)
OR
zoffset = arrBoundingBox(1)(1) - arrBoundingBox(2)(1)
OR
zoffset = arrBoundingBox(4)(1) - arrBoundingBox(7)(1)
OR
zoffset = arrBoundingBox(5)(1) - arrBoundingBox(6)(1)

If I wanted to find the Z distance it could read.
zoffset = arrBoundingBox(4)(2) - arrBoundingBox(0)(2)
OR
zoffset = arrBoundingBox(5)(2) - arrBoundingBox(1)(2)
OR
zoffset = arrBoundingBox(7)(2) - arrBoundingBox(3)(2)
OR
zoffset = arrBoundingBox(6)(2) - arrBoundingBox(2)(2)

Yep. Except, as you are subtracting coordinates, it’s not commutative, you need to have the right order to have a positive value.

For example,

xoffset = arrBoundingBox(0)(0) - arrBoundingBox(1)(0)

will be negative, as the X coodinate of point (1) is greater than the x coordinate of point (0)…

You can also use Rhino.Distance(ptA,ptB) to get a positive distance.

–Mitch

Mitch, again, many thanks. Im digesting this concept. It’ll be really useful!
~J