2009年6月3日星期三

xZigzag

for the Mughal carpet design

052-

 052 

06-

snap1119

 

Option Explicit
'Script written by <insert name>
'Script copyrighted by <insert company name>
'Script version Tuesday, June 02, 2009 5:08:23 PM

Call Main()
Sub Main()
    Dim segpts,dataSeg,datadist,linetype,maxDist,offsetRate
    Dim LINES:LINES=RHINO.GetObjects("select base lines", 4, True,True)
    If isnull(lines) Then Exit Sub
    maxDist=minLength(lines)
    If isnull(rhino.GetDocumentData("zigzag")) Then
        rhino.SetDocumentData"zigzag","d0","1" '"By Segment(1), or by distance(0)"
        rhino.SetDocumentData"zigzag","d1","0" '"fixed 90deg(0),or offset percent(vs.width)"
        rhino.SetDocumentData"zigzag","d2","1" '"Curve on 2sides(1),or right only(0)"
        rhino.SetDocumentData"zigzag","d3"," "
        rhino.SetDocumentData"zigzag","d4","10" '"if bySegment,How Many?"
        rhino.SetDocumentData"zigzag","d5","1.0" '"if byLength,How Long?(0=pickup on Screen)"
        rhino.SetDocumentData"zigzag","d6",maxDist&"(read only)"    '"The shortest curve length is:"

    End If
    Dim d1,d2,d0,d3,d5,d6,d4
    d0=rhino.GetDocumentData("zigzag","d0")
    d1=rhino.GetDocumentData("zigzag","d1")
    d2=rhino.GetDocumentData("zigzag","d2")
    d3=rhino.GetDocumentData("zigzag","d3")
    d4=rhino.GetDocumentData("zigzag","d4")
    d5=rhino.GetDocumentData("zigzag","d5")
    d6=rhino.GetDocumentData("zigzag","d6")
    Dim box:box=Rhino.PropertyListBox(array("By Segment(1), or by distance(0)",_
        "fixed 90deg(0),or offset percent(vs.width)",_
        "Curve on 2sides(1),or right only(0)",_
        " ",_
        "if bySegment,How Many?>1",_
        "if byLength,How Long?(0=pickup on Screen)",_
        "The shortest curve length is:"),_
        array(d0,d1,d2,d3,d4,d5,d6),"options:","Zigzagize lines:")
    If isnull(box) Then Exit Sub
    rhino.SetDocumentData "zigzag","d0",box(0)
    rhino.SetDocumentData "zigzag","d1",box(1)
    rhino.SetDocumentData "zigzag","d2",box(2)
    rhino.SetDocumentData "zigzag","d3"," "
    rhino.SetDocumentData "zigzag","d4",box(4)
    rhino.SetDocumentData "zigzag","d5",box(5)
    rhino.SetDocumentData "zigzag","d6",maxDist&"(read only)"
    'rhino.SetDocumentData "zigzag","d3",box(3)

    If box(0)="1" Then 'by segment
        dataSeg=rhino.Str2Pt(rhino.GetDocumentData("zigzag","d4")&",0,0")(0)       
        Dim numseg:numseg=dataSeg
        If box(1)="0" Then
            linetype="^^^^"
            offsetRate=0.5
        Else
            offsetRate=rhino.Str2Pt(box(1)&",0,0")(0)/100
            linetype=rhino.GetString("Select linetype","VVVV",array("SSSS","unun","UUUU","OOOO"))
            If isnull(linetype) Then Exit Sub
        End If
        segpts=pts_segment(lines,numseg,linetype,box(2),offsetRate)
    End If
    If box(0)="0" Then 'by length
        If rhino.GetDocumentData("zigzag","d5")="0" Then
            Dim user2pts
            user2pts=rhino.GetPoints(True,,"pickup segment length,pick first point:","pickup second point:",2)
            If isnull(user2pts) Then Exit Sub
            datadist=rhino.Distance(user2pts(0),user2pts(1))
            rhino.setDocumentData "zigzag","d5",datadist
        Else           
            datadist=rhino.Str2Pt(rhino.GetDocumentData("zigzag","d5")&",0,0")(0)           
        End If   
        If datadist<0.0001 Or datadist>maxDist Then Exit Sub   
        If box(1)="0" Then
            linetype="^^^^"
            offsetRate=0.5
        Else
            offsetRate=rhino.Str2Pt(box(1)&",0,0")(0)/100
            linetype=rhino.GetString("Select linetype","VVVV",array("SSSS","unun","UUUU","OOOO"))
            If isnull(linetype) Then Exit Sub
        End If
        segpts=pts_distance(lines,datadist,linetype,box(2),offsetRate)
    End If

End Sub

Function pts_segment(ByVal lines,ByVal numseg,ByVal linetype,ByVal mirror,ByVal offsetRate)' provides points array for each line (by segment)
    rhino.EnableRedraw False
    Dim ptsonLine()
    Dim num:num=ubound(lines)
    Dim i,j
    For i=0 To num
        ReDim Preserve ptsonLine(i)
        ptsonLine(i)=rhino.dividecurve(lines(i),numseg)
        If rhino.Distance(rhino.CurveStartPoint(lines(i)),rhino.curveendpoint(lines(i)))=0 Then
            ptsonLine(i)=pts_add_1pt(ptsonLine(i),rhino.CurveEndPoint(lines(i)))
        End If
        'rhino.AddPoints ptsonLine(i)
        decoLine ptsonLine(i),linetype,mirror,offsetRate
    Next
    rhino.EnableRedraw True
End Function
Function pts_distance(ByVal lines,ByVal distseg,ByVal linetype,ByVal mirror,ByVal offsetRate)' provides points array for each line (by distance)
    rhino.EnableRedraw False
    Dim ptsonLine()
    Dim num:num=ubound(lines)
    Dim i,j,ptsNum
    For i=0 To num
        ReDim Preserve ptsonLine(i)
        ptsonLine(i)=rhino.DivideCurveLength (lines(i),distseg)
        ptsNum=ubound(ptsonLine(i))
        If rhino.distance(ptsonLine(i)(ptsNum),rhino.CurveEndPoint(lines(i)))>0 Then
            ptsonLine(i)=pts_add_1pt(ptsonLine(i),rhino.CurveEndPoint(lines(i)))
            '    rhino.AddPoints ptsonLine(i)
            decoLine ptsonLine(i),linetype,mirror,offsetRate
        End If
    Next
    rhino.EnableRedraw True
End Function

Function pts_add_1pt(ByVal pts,ByVal pt)' add the last point into points array
    Dim n:n=ubound(pts)
    Dim i,strpts,ptsfromstr
    strpts=rhino.Pt2Str(pts(0))
    For i=1 To n
        strpts=strpts&" "&rhino.Pt2Str(pts(i))       
    Next
    strpts=strpts&" "&rhino.Pt2Str(pt)
    pts_add_1pt=rhino.Str2PtArray(strpts)

End Function
Function minLength(ByVal lines)
    Dim n:n=ubound(lines)
    Dim leng(),i
    For i=0 To n
        ReDim Preserve leng(i)
        leng(i)=rhino.CurveLength(lines(i))
    Next
    Dim j,temp,k
    For k=0 To n-1
        For j=0 To n-1
        If leng(j)>leng(j+1) Then
            temp=leng(j+1)
            leng(j+1)=leng(j)
            leng(j)=temp
        End If       
        Next
    Next
    minLength=leng(0)
end function
Function decoLine(ByVal pts,ByVal linetype,ByVal mirror,ByVal offsetRate)
    Dim newpts1(),newpts2(),lines(1),i,n,c1,c2,q1,q2,p1,p2,c3,cc1,cc2,cc3,cc4,t1,t2,t3,t4,t5,t6,t7,t8
    n=ubound(pts)
    If linetype="^^^^" Or linetype="VVVV"  Then
        For i=0 To n-1
            ReDim Preserve newpts1(i*2)
            newpts1(i*2)=pts(i)           
            ReDim Preserve newpts1(i*2+1)
            newpts1(i*2+1)=offptfinder1(pts(i),pts(i+1),offsetrate)   
            ReDim Preserve newpts2(i*2)
            newpts2(i*2)=pts(i)           
            ReDim Preserve newpts2(i*2+1)
            newpts2(i*2+1)=offptfinder1(pts(i+1),pts(i),offsetrate)   
        Next
        ReDim Preserve newpts1(2*n)
        newpts1(2*n)=pts(n)
        ReDim Preserve newpts2(2*n)
        newpts2(2*n)=pts(n)

        If     linetype="^^^^" Or linetype="VVVV" Then
        lines(0)=rhino.Addpolyline(newpts1)
        If mirror="1" Then
            lines(1)=rhino.Addpolyline(newpts2)   
            End If
        Else
            lines(0)=rhino.Addinterpcurve(newpts1)
            If mirror="1" Then
                lines(1)=rhino.Addinterpcurve(newpts2)   
            End If
        End If   
    End If   
    If linetype="unun" Then
        For i=0 To n-1
            c1=rhino.PointScale(rhino.PointAdd(pts(i),pts(i+1)),0.5)
            ReDim Preserve newpts1(i*4)
            newpts1(i*4)=pts(i)           
            ReDim Preserve newpts1(i*4+1)
            newpts1(i*4+1)=offptfinder1(pts(i),c1,offsetrate)
            ReDim Preserve newpts1(i*4+2)
            newpts1(i*4+2)=offptfinder1(pts(i),pts(i+1),offsetrate)               
            ReDim Preserve newpts1(i*4+3)
            newpts1(i*4+3)=offptfinder1(c1,pts(i+1),offsetrate)
            ReDim Preserve newpts2(i*4)
            newpts2(i*4)=pts(i)           
            ReDim Preserve newpts2(i*4+1)
            newpts2(i*4+1)=offptfinder1(c1,pts(i),offsetrate)
            ReDim Preserve newpts2(i*4+2)
            newpts2(i*4+2)=offptfinder1(pts(i+1),pts(i),offsetrate)               
            ReDim Preserve newpts2(i*4+3)
            newpts2(i*4+3)=offptfinder1(pts(i+1),c1,offsetrate)
        Next
        ReDim Preserve newpts1(4*n)
        newpts1(4*n)=pts(n)
        ReDim Preserve newpts2(4*n)
        newpts2(4*n)=pts(n)
        lines(0)=rhino.Addinterpcurve(newpts1)
        If mirror="1" Then
            lines(1)=rhino.Addinterpcurve(newpts2)               
        End If   
    End If
    If linetype="SSSS" Or linetype="UUUU" Then
        For i=0 To n-1
            c1=rhino.PointScale(rhino.PointAdd(pts(i),pts(i+1)),0.5)
            c2=offptfinder1(pts(i),pts(i+1),offsetrate)   
            cc1=rhino.PointScale(rhino.PointAdd(pts(i),c2),0.5)
            cc2=rhino.PointScale(rhino.PointAdd(pts(i+1),c2),0.5)
            t1=rhino.PointScale(rhino.PointAdd(c1,cc1),0.5)
            t2=rhino.PointSubtract(rhino.PointScale(cc1,2),t1)
            t4=rhino.PointScale(rhino.PointAdd(c1,cc2),0.5)
            t3=rhino.PointSubtract(rhino.PointScale(cc2,2),t4)           
            ReDim Preserve newpts1(i*8)
            newpts1(i*8)=pts(i)           
            ReDim Preserve newpts1(i*8+1)
            newpts1(i*8+1)=t1
            ReDim Preserve newpts1(i*8+2)
            newpts1(i*8+2)=cc1               
            ReDim Preserve newpts1(i*8+3)
            newpts1(i*8+3)=t2
            ReDim Preserve newpts1(i*8+4)
            newpts1(i*8+4)=c2           
            ReDim Preserve newpts1(i*8+5)
            newpts1(i*8+5)=t3
            ReDim Preserve newpts1(i*8+6)
            newpts1(i*8+6)=cc2               
            ReDim Preserve newpts1(i*8+7)
            newpts1(i*8+7)=t4
            c3=offptfinder1(pts(i+1),pts(i),offsetrate)   
            cc3=rhino.PointScale(rhino.PointAdd(pts(i),c3),0.5)
            cc4=rhino.PointScale(rhino.PointAdd(pts(i+1),c3),0.5)
            t5=rhino.PointScale(rhino.PointAdd(c1,cc3),0.5)
            t6=rhino.PointSubtract(rhino.PointScale(cc3,2),t5)
            t8=rhino.PointScale(rhino.PointAdd(c1,cc4),0.5)
            t7=rhino.PointSubtract(rhino.PointScale(cc4,2),t8)
            ReDim Preserve newpts2(i*8)
            newpts2(i*8)=pts(i)           
            ReDim Preserve newpts2(i*8+1)
            newpts2(i*8+1)=t5
            ReDim Preserve newpts2(i*8+2)
            newpts2(i*8+2)=cc3               
            ReDim Preserve newpts2(i*8+3)
            newpts2(i*8+3)=t6
            ReDim Preserve newpts2(i*8+4)
            newpts2(i*8+4)=c3           
            ReDim Preserve newpts2(i*8+5)
            newpts2(i*8+5)=t7
            ReDim Preserve newpts2(i*8+6)
            newpts2(i*8+6)=cc4               
            ReDim Preserve newpts2(i*8+7)
            newpts2(i*8+7)=t8

        Next
        ReDim Preserve newpts1(8*n)
        newpts1(8*n)=pts(n)
        ReDim Preserve newpts2(8*n)
        newpts2(8*n)=pts(n)
        If linetype="SSSS" Then
            lines(0)=rhino.Addinterpcurve(newpts1)
            If mirror="1" Then
            lines(1)=rhino.Addinterpcurve(newpts2)               
            End If   
        Else
            lines(0)=rhino.addcurve(newpts1)
            If mirror="1" Then
                lines(1)=rhino.addcurve(newpts2)               
            End If
        End If       
    End If
    If linetype="OOOO" Then
        For i=0 To n-1
            c1=rhino.PointScale(rhino.PointAdd(pts(i),pts(i+1)),0.5)
            c2=offptfinder1(pts(i),pts(i+1),offsetrate)   
            cc1=rhino.PointScale(rhino.PointAdd(pts(i),c2),0.5)
            cc2=rhino.PointScale(rhino.PointAdd(pts(i+1),c2),0.5)
            t1=rhino.PointAdd(rhino.PointScale(c1,0.75),rhino.PointScale(c2,0.25))
            t2=rhino.PointSubtract(rhino.PointScale(cc1,2),t1)
            t3=rhino.PointSubtract(rhino.PointScale(cc2,2),t1)
            ReDim Preserve newpts1(i*6)
            newpts1(i*6)=pts(i)           
            ReDim Preserve newpts1(i*6+1)
            newpts1(i*6+1)=t1
            ReDim Preserve newpts1(i*6+2)
            newpts1(i*6+2)=t2               
            ReDim Preserve newpts1(i*6+3)
            newpts1(i*6+3)=c2
            ReDim Preserve newpts1(i*6+4)
            newpts1(i*6+4)=t3           
            ReDim Preserve newpts1(i*6+5)
            newpts1(i*6+5)=t1
            c3=offptfinder1(pts(i+1),pts(i),offsetrate)   
            cc3=rhino.PointScale(rhino.PointAdd(pts(i),c3),0.5)
            cc4=rhino.PointScale(rhino.PointAdd(pts(i+1),c3),0.5)
            t5=rhino.PointAdd(rhino.PointScale(c1,0.75),rhino.PointScale(c3,0.25))
            t6=rhino.PointSubtract(rhino.PointScale(cc3,2),t5)
            t7=rhino.PointSubtract(rhino.PointScale(cc4,2),t5)
            ReDim Preserve newpts2(i*6)
            newpts2(i*6)=pts(i)           
            ReDim Preserve newpts2(i*6+1)
            newpts2(i*6+1)=t5
            ReDim Preserve newpts2(i*6+2)
            newpts2(i*6+2)=t6               
            ReDim Preserve newpts2(i*6+3)
            newpts2(i*6+3)=c3
            ReDim Preserve newpts2(i*6+4)
            newpts2(i*6+4)=t7           
            ReDim Preserve newpts2(i*6+5)
            newpts2(i*6+5)=t5
        Next
        ReDim Preserve newpts1(6*n)
        newpts1(6*n)=pts(n)
        ReDim Preserve newpts2(6*n)
        newpts2(6*n)=pts(n)
        lines(0)=rhino.Addinterpcurve(newpts1)
        If mirror="1" Then
            lines(1)=rhino.Addinterpcurve(newpts2)       
            End If
    End If
End Function
Function offptfinder1(ByVal pt1,ByVal pt2,ByVal offsetrate)
    Dim pCenter,vCenter,vCrossUnit,vCross,dist
    pCenter=rhino.PointScale(rhino.PointAdd(pt1,pt2),0.5)
    'rhino.AddPoint pCenter
    vCenter=rhino.VectorCreate(pt1,pt2)
    dist=rhino.Distance(pt1,pt2)*offsetrate
    vCrossUnit=rhino.VectorUnitize(rhino.VectorCrossProduct(vCenter,array(0,0,1)))
    vCross=rhino.VectorScale(vCrossUnit,dist)           
    offptfinder1=rhino.PointAdd(pCenter,vCross)
    'rhino.AddLine pCenter,offptfinder1
    'rhino.AddPoint offptfinder1
End Function

没有评论:

发表评论