2009年1月13日星期二

plugin | unroll srf or brep into ordered plan

Snap2 Snap3 Snap4 Snap5

This little code is just for unroll a bunch of surfaces. The parameter include ordering by "bounding box size" or by spatial location, with or without labels.

if no srf or brep is selected, will show:Snap6 , if only one srf  is selected will show: Snap7 , lol….

the core of the code is the “order by size” and the “smash” rhino command.

these are the samples:

snap0426 snap0427

                without label                                          with/without label                     

snap0428 snap0429

                   by spatial relation                                             by size

 

Click to download plugin: drag to your rhino and you are all set.

Run it by typing command "xUnroll" or go to the menu "xTools".

__________________________________________________________

Option Explicit
'Script written by <xarch.blogspot.com>
'Script copyrighted by <xarch.blogspot.com>
'Script version Monday, January 12, 2009 4:33:17 PM

Call Main()
Sub main()

    Dim breps: breps=rhino.getobjects("select Srfs or breps",,True,True)
    If isnull(breps) Then Exit Sub
    Dim spacingfactor:
    spacingfactor=rhino.getreal("spacing factor?",2,0.001) 'x time biggest panel size
    If isnull(spacingfactor) Then Exit Sub
    Dim BestGuessSwitch
    BestGuessSwitch=rhino.getstring("Order method, by spatical location?","Yes",array("Size"))
    If isnull(BestGuessSwitch) Then Exit Sub
    If BestGuessSwitch="Yes" Then BestGuessSwitch=1
    If BestGuessSwitch="Size" Then BestGuessSwitch=2
    '1=order by spatical location(exploded)
    '2=order by size   
    Dim ShowTagSwitch
    ShowTagSwitch=rhino.getstring("add Label?","No",array("Yes"))
    If ShowTagSwitch="Yes" Then ShowTagSwitch=1
    If ShowTagSwitch="No" Then ShowTagSwitch=0
    Dim brep,n1,brepstemp()
    n1=0
    For Each brep In breps
        If rhino.ObjectType(brep)=8 Or rhino.ObjectType(brep)=16 Then
            ReDim Preserve brepstemp(n1)
        brepstemp(n1)=brep
            n1=n1+1       
        End If
    Next
    If n1=0 Then
        rhino.MessageBox "No srf selected"
        Exit Sub
    End If
    Call unrollsrfs(brepstemp,spacingfactor,BestGuessSwitch,ShowTagSwitch)
End Sub

Function unrollsrfs(ByVal breps,ByVal spacingfactor,ByVal BestGuessSwitch,ByVal ShowTagSwitch)
    Dim n:n=ubound(breps)
    If n<=0 Then
        rhino.MessageBox "Need 2 or more srfs to run, you lazybone!"
        Exit Function
    End If
    Dim allcenter:allcenter=rhino.Pointscale(rhino.PointAdd(rhino.BoundingBox(breps)(0),rhino.BoundingBox(breps)(6)),0.5)
    Dim sortsize,spacing,center
    sortsize=SortSrfBySize(breps)
    spacing=sortsize(1)(0)
    center=sortsize(2)
    ' testing
    'rhino.AddTextDot "the biggest panel is "&spacing , sortsize(2)(0)
    'rhino.AddTextDot "the smallest panel is "&sortsize(1)(n) , sortsize(2)(n)
    Dim k,obj,arrobj,arrUnroll
    k=-1
    arrUnroll = Array()
    If BestGuessSwitch=0 Then arrobj=breps
    If BestGuessSwitch=1 Then arrobj=sortsize(0)
    If BestGuessSwitch=2 Then arrobj=sortsize(0)
    rhino.EnableRedraw False
    For Each obj In arrobj
        k = k + 1
        If ShowTagSwitch=1 Then rhino.addtextdot k, center(k)
        Call Rhino.UnselectAllObjects
        Call Rhino.SelectObject(obj)
        'Call Rhino.Command (CStr("_smash LinearDirection=Natural _Enter Explode=No Labels=No _Enter"), vbFalse)       
        Call Rhino.Command (CStr("_smash _Enter _Enter"), vbFalse)       
        ReDim Preserve arrUnroll(k)
        arrUnroll(k) = Rhino.FirstObject
        If BestGuessSwitch=2 Then
            rhino.MoveObject arrUnroll(k),array(0,0,0),array((k Mod 10)*spacingfactor*spacing,(k\10)*spacingfactor*spacing,0)
            If ShowTagSwitch=1 Then rhino.addtextdot k&"'", array((k Mod 10)*spacingfactor*spacing,(k\10)*spacingfactor*spacing,0)
        End If
        If BestGuessSwitch=1 Then
            Dim vecMove:vecMove=rhino.vectorScale(rhino.VectorUnitize(_
                rhino.vectorcreate(center(k),allcenter)),((rhino.distance(center(k),allcenter)+spacingfactor*spacing)*2))
            rhino.MoveObject arrUnroll(k),array(0,0,0),array(vecMove(0),vecMove(1),0)
            If ShowTagSwitch=1 Then rhino.addtextdot k&"'", array(vecMove(0),vecMove(1),0)
        End If
    Next
    rhino.EnableRedraw True
End Function

Function SortSrfBySize (ByVal objs)
    Dim n:n=ubound(objs)
    Dim i,j,k,boxLong(),objtemp,boxcenter(),temp(2),boxLongTemp
    For k=0 To n       
        ReDim Preserve boxLong(k)
        boxLong(k)=rhino.distance(rhino.BoundingBox(objs(k))(0),rhino.BoundingBox(objs(k))(6))
    Next
    For i=0 To n-1
        For j=0 To n-1
            If j<n-1-i Then
                If boxLong(j)<boxLong(j+1) Then
                    objtemp=objs(j)
                    objs(j)=objs(j+1)
                    objs(j+1)=objtemp
                    boxLongTemp=boxLong(j)
                    boxLong(j)=boxLong(j+1)
                    boxLong(j+1)=boxLongTemp
                End If
            End If       
        Next       
    Next
    For k=0 To n       
        ReDim Preserve boxcenter(k)   
        boxcenter(k)=rhino.Pointscale(rhino.PointAdd(rhino.BoundingBox(objs(k))(0),rhino.BoundingBox(objs(K))(6)),0.5)
    Next

    temp(0)=objs
    temp(1)=boxLong
    temp(2)=boxcenter
    SortSrfBySize=temp
    'SortSrfBySize(0) are the sorted srfs ID
    'SortSrfBySize(1) are the sorted size dimension - SortSrfBySize(1)(0) is the biggest srf diaganal size
    'SortSrfBySize(2) are the array of the center points
End Function

没有评论:

发表评论