AN_Clay cracks

Option Explicit
‘Script written by Adolfo Nadal
‘Script copyrighted by Archiologics
‘Script version Thursday, October 30, 2008 1:27:08 PM

Call Main()
Sub Main()
 
 Dim intU,intV, uDom, vDom, strSrf
 strSrf = Rhino.GetObject(“srf”,8,True)
 
 intU = Rhino.GetInteger(“u”,10)
 intV = Rhino.GetInteger(“v”,5)
 uDom = Rhino.SurfaceDomain(strSrf,0)
 vDom = Rhino.SurfaceDomain(strSrf,1)
 
 Dim uStep : uStep = (UDom(1) – UDom(0)) / intU
 Dim vStep : vStep = (VDom(1) – VDom(0)) / intV
 
 Dim t,s, u, v
 ReDim arrFrame(intU,intV)
 Dim n,m, aPt, iRnd, p
 
 
 Call rhino.EnableRedraw(False)
 For t = 0 To intU
  For s = 0 To intV
   
   u = uDom(0) + uStep * t
   v = vDom(0) + vStep * s
   
   arrFrame(t,s) = Rhino.SurfaceFrame (strSrf, Array(u,v))
   ‘Rhino.AddPoint(arrFrame(t,s)(0))
   
   ‘If t >= 1 And s >= 1 Then
   ‘ Call rhino.addline(arrFrame(t,s)(0),arrFrame(t-1,s-1)(0)) 
   ‘End If
   
   ‘If t >= 1 And s >= 1 Then
   ‘ Call rhino.addline(arrFrame(t-1,s)(0),arrFrame(t,s-1)(0)) 
   ‘End If
   
   If t >= 1 And s >= 1 Then

    Dim aPtsQuad : aPtsQuad = array(arrFrame(t,s)(0),arrFrame(t,s-1)(0), arrFrame(t-1,s-1)(0),arrFrame(t-1,s)(0))
    
    iRnd = rnd
    
    If iRnd > 0.6 Then
     Rhino.Print “rnd>.6”
     Call rhino.addSrfpt(aPtsQuad)
    End If
    
    If iRnd > 0.3 And iRnd <= 0.6   Then
     aPt = rndCPtWithin(aPtsQuad)
     For n = 1 To UBound(aPtsQuad)
      Call rhino.addSrfpt(array(aPtsQuad(n), aPtsQuad(n-1), aPt))
      If n = UBound(aPtsQuad) Then
       Call rhino.addSrfpt(array(aPtsQuad(n), aPtsQuad(0), aPt))
      End If
     Next
    End If
    
    If iRnd <= 0.3   Then
     Rhino.AddPolyline(aPtsQuad)
     aPt = rndCPtWithin(aPtsQuad)
     For n = 1 To UBound(aPtsQuad)
      Dim aPtsTri : aPtsTri = array(aPtsQuad(n), aPtsQuad(n-1), aPt)
      Dim width : width = 0.02
      Dim NraPtsTri : NraPtsTri = array(Rhino.VectorAdd(aPtsQuad(n),Rhino.VectorReverse(Rhino.VectorScale(Rhino.VectorUnitize(Rhino.SurfaceNormal(strSrf,Rhino.SurfaceClosestPoint(strSrf,aPtsQuad(n)))),width))),_
       Rhino.VectorAdd(aPtsQuad(n-1),Rhino.VectorReverse(Rhino.VectorScale(Rhino.VectorUnitize(Rhino.SurfaceNormal(strSrf,Rhino.SurfaceClosestPoint(strSrf,aPtsQuad(n-1)))),width))),_
       Rhino.VectorAdd(aPt,Rhino.VectorReverse(Rhino.VectorScale(Rhino.VectorUnitize(Rhino.SurfaceNormal(strSrf,Rhino.SurfaceClosestPoint(strSrf,aPt))),width))))
      
      Dim aPt2: aPt2 = rndCPtWithin(aPtsTri)
      
      ‘CALCULATE NORMAL POINT BASED ON TRIANGLE RND PT WITHIN
      Dim scalefactor : scalefactor = Rnd*1.25
      Dim aPt2Normal : aPt2Normal = Rhino.SurfaceNormal(strSrf,Rhino.SurfaceClosestPoint(strSrf,aPt2))
      aPt2Normal = Rhino.VectorReverse(Rhino.VectorScale(Rhino.VectorUnitize (aPt2Normal),scalefactor))
      aPt2Normal = Rhino.PointAdd (aPt2,aPt2Normal)
      
      ‘CALCULATE THE OTHER NORMAL TO HAVE THICK ELEMENTS (TO OBTAIN THE SECOND PATCH SURFACE TO JOIN TO THE PRECIOUS ONE)
      Dim NraPt2 : NraPt2 = Rhino.VectorAdd(aPt2,Rhino.VectorReverse(Rhino.VectorScale(Rhino.VectorUnitize(Rhino.SurfaceNormal(strSrf,Rhino.SurfaceClosestPoint(strSrf,aPt))),(scalefactor-width))))
      
      For m = 1 To UBound(aPtsTri)
       
       Call rhino.addSrfpt(array(aPtsTri(m), aPtsTri(m-1), aPt2))
       
       ‘DEFINE POINTS FOR STRPATCH
       Dim Line1 : Line1 = Rhino.AddLine(aPtsTri(m-1), aPtsTri(m))
       Dim L1Ptsdiv : L1PtsDiv = Rhino.DivideCurve(Line1, 3)
       Dim Line2 : Line2 = Rhino.AddLine(aPtsTri(m), aPt2)
       Dim L2Ptsdiv : L2PtsDiv = Rhino.DivideCurve(Line2, 3)
       Dim Line3 : Line3 = Rhino.AddLine(aPt2, aPtsTri(m-1))
       Dim L3Ptsdiv : L3PtsDiv = Rhino.DivideCurve(Line3, 3)
       Dim strPatchCrv : strPatchCrv = Rhino.AddCurve (array(aPtsTri(m-1),L1Ptsdiv(2),L1Ptsdiv(1),aPtsTri(m),L2Ptsdiv(1),L2Ptsdiv(2),aPt2Normal,L3Ptsdiv(1),L3Ptsdiv(2),aPtsTri(m-1)))
       
       Rhino.SelectObject StrPatchCrv
       Rhino.Command “-patch p=1 u=10 v=10 s=1 a=yes t=yes enter”
       Dim strPatch : strPatch = Rhino.FirstObject
       Rhino.UnselectAllObjects
       
       ‘DEFINE POINTS FOR STRPATCHNORMAL
       
       Dim NrLine1 : NrLine1 = Rhino.AddLine(NraPtsTri(m-1), NraPtsTri(m))
       Dim NrL1Ptsdiv : NrL1PtsDiv = Rhino.DivideCurve(NrLine1, 3)
       Dim NrLine2 : NrLine2 = Rhino.AddLine(NraPtsTri(m), NraPt2)
       Dim NrL2Ptsdiv : NrL2PtsDiv = Rhino.DivideCurve(NrLine2, 3)
       Dim NrLine3 : NrLine3 = Rhino.AddLine(NraPt2, NraPtsTri(m-1))
       Dim NrL3Ptsdiv : NrL3PtsDiv = Rhino.DivideCurve(NrLine3, 3)
       Dim NrstrPatchCrv : NrstrPatchCrv = Rhino.AddCurve (array(NraPtsTri(m-1),NrL1Ptsdiv(2),NrL1Ptsdiv(1),NraPtsTri(m),NrL2Ptsdiv(1),NrL2Ptsdiv(2),NraPt2,NrL3Ptsdiv(1),NrL3Ptsdiv(2),NraPtsTri(m-1)))
       
       Rhino.SelectObject NrStrPatchCrv
       Rhino.Command “-patch p=1 u=10 v=10 s=1 a=yes t=yes enter”
       Dim NrstrPatch : NrstrPatch = Rhino.FirstObject
       Rhino.UnselectAllObjects       
       
       If Rhino.IsSurfaceTrimmed(strPatch) And Rhino.IsSurfaceTrimmed(NrstrPatch)Then
        Dim strLoftSrf : strLoftSrf = Rhino.AddLoftSrf(array(strPatchCrv,NrstrPatchCrv))
        Rhino.JoinSurfaces(array(strPatch,strLoftSrf(0),NrstrPatch))
       Else
        Rhino.DeleteObject(strPatch)
        Rhino.DeleteObject(NrstrPatch)
       End If
              
       If m = Ubound(aPtsTri) Then
        Call rhino.addSrfpt(array(aPtsTri(m), aPtsTri(0), aPt2))
        Line1 = Rhino.AddLine (aPtsTri(m),aPtsTri(0))
        L1PtsDiv = Rhino.DivideCurve(Line1, 3)
        Line2 = Rhino.AddLine (aPtsTri(0),aPt2)
        L2PtsDiv = Rhino.DivideCurve(Line2, 3)
        Line3 = Rhino.AddLine (aPt2,aPtsTri(m))
        L3PtsDiv = Rhino.DivideCurve(Line3, 3)
        Rhino.AddCurve array(aPtsTri(m),L1Ptsdiv(1),L1Ptsdiv(2),aPtsTri(0),L2Ptsdiv(1),L2Ptsdiv(2),aPt2,L3Ptsdiv(1),L3Ptsdiv(2),aPtsTri(m))
       End If       
      Next
      If n = Ubound(aPtsQuad) Then
       Call rhino.addSrfpt(array(aPtsQuad(n),aPtsQuad(0),aPt))
      End If
     Next
    End If
   End If
  Next
 Next
 Rhino.HideObject strSrf
 Call rhino.EnableRedraw(True)  
End Sub

Function rndCPtWithin(aPts)
 
 Dim i, x, y, z
 x = aPts(0)(0) : y = aPts(0)(1) : z = aPts(0)(2)
 For i = 1 To Ubound(aPts)
  x = x + aPts(i)(0)
  y = y + aPts(i)(1)
  z = z + aPts(i)(2)
 Next
 x = x / (UBound(aPts)+1)
 y = y / (UBound(aPts)+1)
 z = z / (UBound(aPts)+1)
 Dim aPtCenter : aPtCenter = array(x,y,z)
 
 Dim sPt : sPt = Rhino.AddPoint(aPtCenter)
 Call rhino.ObjectColor(sPt, vbred)
 
 Dim scrv : sCrv = rhino.AddPolyline(aPts)
 
 If Not Rhino.IsCurveClosed(sCrv) And Rhino.IsCurveClosable(sCrv) Then
  sCrv = Rhino.CloseCurve (scrv)
 End If
 
 Rhino.ObjectColor scrv,vbBlue
 Dim aPtsdiv : aPtsDiv = rhino.DivideCurve(sCrv, 30)
 Dim aPtDir : aPtDir = aPtsdiv(int(UBound(aPtsDiv)* rnd))
 Dim vDir : vDir = rhino.VectorCreate(aptDir, aPtcenter)
 vDir = rhino.VectorScale(vDir, rnd)
 rndCPtWithin = rhino.PointAdd(aPtcenter, vDir)
 
 Call rhino.DeleteObject(scrv)
  
End Function

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s