Forgot password?

Create an account!

Forum

« back

RhinoScript – Packing

Messages

Please log in to write a message.

  • 5. mattman (Oct 31, 2012 20.15):

    Great script. Curious if it would be possible to mod the script to organize a set of predefined cubes (like building program)

  • 4. Hanno (Feb 20, 2009 18.05):

    Hi,

    this:

    'Add next box
    rhino.addbox arrBox    
                     
    arrMyCube = rhino.lastobject

    is wrong, because lastobject gives you the FIRST object you created (its not like "sellast" command). So you later delete the wrong cube. You can directly use the return value of addBox to get your object id:

    arrMyCube = rhino.addBox(arrBox)

    And a second thing: in your overlap test line the "< 4" is missing for the z value.

    Hope that helps!

    Hanno

  • 3. mmathey1 (Feb 19, 2009 03.01):

    Hi Hanno,

    Thanks for the tip on packing with spheres. I am trying to write a similar script that deals with a cube geometry. I think I am getting pretty close to solving it but I still experience issues with cubes overlapping every once in a while when I execute the script. Could you or anybody else take a look at this code and tell me where my problem might be?

    Option Explicit
    'Script written by <Mark M>
    'Script copyrighted by <University of Cincinnati DAAP>
    'Script version Tuesday, February 17, 2009 4:44:50 PM

    'Generate placement boxes in a volume without overlapping.

    Call Main()
    Sub Main()

            Dim arrBox(7), ArrMyCubeY(), ArrMyCubeX(), ArrMyCubeZ(), arrMyCube
            Dim arrPt1(2), arrPt2(2), arrPt3(2), arrPt4(2), arrPt5(2), arrPt6(2), arrPt7(2), arrPt8(2)
            Dim x, y, z
            Dim i, j, h
            Dim Overlap, pass
           
            'set layer to determin when box was created
            Rhino.CurrentLayer "Layer 00"
           
            'generate points of first box
            x = rnd * 10
            y = rnd * 10
            z = rnd * 10
                   
            arrPt1(0) = x
            arrPt1(1) = y
            arrPt1(2) = z
           
            arrPt2(0) = x
            arrPt2(1) = y + 4       
            arrPt2(2) = z
           
            arrPt3(0) = x + 4
            arrPt3(1) = y + 4
            arrPt3(2) = z
           
            arrPt4(0) = x + 4
            arrPt4(1) = y
            arrPt4(2) = z
           
            arrPt5(0) = x
            arrPt5(1) = y
            arrPt5(2) = z + 4
           
            arrPt6(0) = x
            arrPt6(1) = y + 4
            arrPt6(2) = z + 4
           
            arrPt7(0) = x + 4
            arrPt7(1) = y + 4
            arrPt7(2) = z + 4
           
            arrPt8(0) = x + 4
            arrPt8(1) = y
            arrPt8(2) = z + 4       
                   
            arrBox(0) = arrPt1
            arrBox(1) = arrPt2
            arrBox(2) = arrPt3
            arrBox(3) = arrPt4
            arrBox(4) = arrPt5
            arrBox(5) = arrPt6
            arrBox(6) = arrPt7
            arrBox(7) = arrPt8
           
           
            'Re-dimension array for storing x,y and z coordinate of previous boxes
            ReDim Preserve arrMyCubeX(0)
            ReDim Preserve arrMyCubeY(0)
            ReDim Preserve arrMyCubeZ(0)
                           
            arrMyCubeX(0) = x
            arrMyCubeY(0) = y
            arrMyCubeZ(0) = z
           
            'Add first box
            rhino.addbox arrBox     
           
            For i = 1 To 4
                   
                   
                    'Set a different layer for each box to see which ones are intersecting
                    If i = 1 Then
                           
                            Rhino.CurrentLayer "Layer 01"
                                                                   
                    End If
                   
                    If i = 2 Then
                           
                            Rhino.CurrentLayer "Layer 02"
                                                   
                    End If
                   
                    If i = 3 Then
                           
                            Rhino.CurrentLayer "Layer 03"
                           
                    End If
                   
                    If i = 4 Then
                           
                            Rhino.CurrentLayer "Layer 04"
                           
                    End If
                   
                    'Variable to test of boxes overlay
                    Overlap = "true"
                   
                    'Test to see if script may pass to the next loop
                    pass = "yes"
                           
                    Do Until Overlap = "false" And pass = "yes"
                           
                            pass = "yes"
                           
                            ReDim Preserve arrMyCubeX(i)
                            ReDim Preserve arrMyCubeY(i)
                            ReDim Preserve arrMyCubeZ(i)
                           
                            'Generate points for next box
                            x = rnd * 10
                            y = rnd * 10
                            z = rnd * 10
                   
                            arrPt1(0) = x
                            arrPt1(1) = y
                            arrPt1(2) = z
           
                            arrPt2(0) = x
                            arrPt2(1) = y + 4       
                            arrPt2(2) = z
           
                            arrPt3(0) = x + 4
                            arrPt3(1) = y + 4
                            arrPt3(2) = z
           
                            arrPt4(0) = x + 4
                            arrPt4(1) = y
                            arrPt4(2) = z
           
                            arrPt5(0) = x
                            arrPt5(1) = y
                            arrPt5(2) = z + 4
           
                            arrPt6(0) = x
                            arrPt6(1) = y + 4
                            arrPt6(2) = z + 4
           
                            arrPt7(0) = x + 4
                            arrPt7(1) = y + 4
                            arrPt7(2) = z + 4
           
                            arrPt8(0) = x + 4
                            arrPt8(1) = y
                            arrPt8(2) = z + 4       
                   
                            arrBox(0) = arrPt1
                            arrBox(1) = arrPt2
                            arrBox(2) = arrPt3
                            arrBox(3) = arrPt4
                            arrBox(4) = arrPt5
                            arrBox(5) = arrPt6
                            arrBox(6) = arrPt7
                            arrBox(7) = arrPt8
                   
                            'Add next box
                            rhino.addbox arrBox     
                           
                            arrMyCube = rhino.lastobject
                           
                            'Store
                            arrMyCubeX(i) = x
                            arrMyCubeY(i) = y
                            arrMyCubeZ(i) = z
                   
                            'Begin loop to test for overlap
                            For j = 0 To (i - 1)
                                   
                                    'Ensure current box does not overlap any previous boxes (I think this may be where I'm going wrong)
                                    If abs(arrMyCubeX(i) - arrMyCubeX(j)) < 4 And abs(arrMyCubeY(i) - arrMyCubeY(j)) < 4 And abs(arrMyCubeZ(i) - arrMyCubeZ(j)) Then
                                           
                                           
                                            'Set variables to repeat loop and delete the box that was just made
                                            Overlap = "True"
                                            rhino.deleteobject arrMyCube
                                            pass = "no"
                                                                                                                   
                                    Else
                                           
                                            'Box does not overlap, loop passes
                                            Overlap = "false"
                                                                                                                           
                                    End If
                                                                                                                                                                                                                                                                   
                            Next
                           
                            'Test if loop may pass to next iteration and delete box if answer is no
                            If pass = "no" Then
                                   
                                    rhino.deleteobject arrMyCube
                                   
                            End If
                                                           
                    Loop   
                   
            Next
                   
    End Sub

    I feel like the concept is pretty simple but I'm still having trouble constructing the logic for it. I am writing an MArch thesis on the topic of scripting in architecture and this is one of the experiments I'm including. No one at my school really understand the methods behind scripting in design so I was hoping perhaps you could help me out on this. I appreciate it.

    Thanks.

    p.s. The script works some of the time, if you run it in Rhino it may take several executions before you start to see some cubes overlapping. (is it ironic to end this message with a post script ?)

  • 2. Hanno (Jan 21, 2009 03.08):

    I would solve this geometrically, like: check for each new sphere, if the distance between its center point and any other center point is smaller than both radii: if yes, then the spheres overlap.

    Hope that helps!

    Hanno

  • 1. mmathey1 (Jan 20, 2009 21.06):

    I was wondering what command should be used to insure that a newly created object does not occupy any of the same points in space as a previously created object. For example say I wanted to create 100 spheres of random sizes and locations within a volume. How would I be able to ensure that a sphere added at the end of the cycle did not occupy or intersect the spheres created earlier in the cycle?

    Thanks.

Recommend

Why are these buttons gray?