Saturday, January 27, 2007

DrawLineFromCurve










The following is pretty basic script to take a curve to generate a straight line that has the same length with the original curve.



Option Explicit
'this script is created to unroll sphere
'so that hopefully I chould unroll little bit advanced shape.


UnrollSphere()
Sub UnrollSphere()

'Shpere should be ready
' -Circumference Max is 8'
' -Trimmed with 0 and 45 degree lines
' -Rebuilt Vertical Surf to U2, Vt
' -Extract wireframe



'Number of Division
Dim t
t = 8

Dim arrOrigin
arrOrigin = array(0,0,0)



'Get Length
Dim halfSphereCircumference1, dblLength

halfSphereCircumference1 = Rhino.GetObject("Select a circumference curve", 4)
If IsNull(halfSphereCircumference1) Then Exit Sub

dblLength = Rhino.CurveLength(halfSphereCircumference1)
Rhino.Print "Curve length: " & CStr(dblLength)




'Draw Line and Divide
Dim strStraight, arrCircumferenceCurvePoints

strStraight = Rhino.AddLine (arrOrigin, array(0,dblLength,0))



End Sub



Friday, January 26, 2007

Script Samples

If you are trying to veryfy whether or not the selected object is curve, don't use IsCurve.

Rhino.IsCurve (strObject [, intIndex])

It would be much faster if you use GetObject instead.

Rhino.GetObject ([strMessage [, intType [, blnPreSelect [, blnSelect [, arrObjects ]]]]])

The Second option [, intType lets you limit the type of object you can select.
The object types are the following

0
1
2
4
8
16
32
256
512
4096

Monday, January 15, 2007

Digital Project




Hope the Rhinoscripting is going good. I just sat through a presentation of Digital project. The program seems amazing and is totally compatible with visual basic, which is good for all of us that started learning. From what Nick Pisca said the plus of Digital Project is its stability, and of course real time parametric updating that rhino doesn't have. Also turns out a lot of other programs can be accessed through windows platform. I don't know the specifics of it but really its this real-time, network, cross-program workflow that is fascinating.

All Things BIM has a post about Digital Project.

Friday, January 12, 2007

minimal surface script

// minimum surface creator.
// copyright 2005 Theo Calvin
// free to use for educational purposes.
// for any other use please contact theo@mediumlite.com


minSurfUI;

/////////////////////////////////////////////////////////////////////////////////////

proc string[] parseName (string $input) {
string $return[];
string $next;
string $currentString = "";
int $strCount = 0;
for ($i=2; $i<=`size($input)`; $i++) { $next = `substring ($input) $i $i `; if ($next == "|") { $return[$strCount] = $currentString; $currentString = ""; $strCount++; } else { $currentString = $currentString + $next; } if ($i == `size($input)`) { $return[`size($return)`] = $currentString; } } return $return; } ///////////////////////////////////////////////////////////////////////////////////// proc vector getLocV(string $one){ float $currentpos[] = `xform -ws -q -t ($one)`; return <<$currentpos[0],$currentpos[1],$currentpos[2]>>;
}


// ================================================================================
//
// minSurf
//
// ================================================================================

global proc minSurf() {
waitCursor -state on;
float $curveMin;
float $curveMax;
float $curveSpan;
int $updatePercentage ;
float $smoothLoops;
float $updateAmmount;

progressWindow
-title "Creating Surface"
-status ("Complete: "+$updatePercentage+"%")
-isInterruptable true;
;

//........................
// get selection
//........................
string $curves[] = `ls -sl -l`;
string $path[] = parseName ($curves[0]);

//........................
// create polySurface
//........................
int $minSXarray[] = `intFieldGrp -q -v polySubX_field`;
int $minSYarray[] = `intFieldGrp -q -v polySubY_field`;
int $minSX = $minSXarray[0];
int $minSY = $minSYarray[0];
string $minSholder[] = `polyPlane -w 1 -h 1 -sx $minSX -sy $minSY -ax 0 1 0 -tx 1 -ch 0 -n "minSurface"`;
string $minS = $minSholder[0];

//...............................
// place edges on borders (x1)
//...............................
$curveMin = `getAttr ($curves[0]+".min")`;
$curveMax = `getAttr ($curves[0]+".max")`;
$curveSpan = `abs $curveMax`-`abs $curveMin`;
float $subSpan = $curveSpan/$minSX;


float $currentU;
float $pointLoc[];
for ($i=0; $i<=$minSX; $i++) { $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[0]`; select -r ($minS+ ".vtx["+$i+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //............................... // place edges on borders (y1) //............................... $curveMin = `getAttr ($curves[1]+".min")`; $curveMax = `getAttr ($curves[1]+".max")`; $curveSpan = `abs $curveMax`-`abs $curveMin`; float $subSpan = $curveSpan/$minSY; int $vtx; for ($i=1; $i<$minSY; $i++) { $vtx = $minSX +($i*($minSX+1)); //print ("vtx: " + $vtx + "\n"); $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[1]`; select -r ($minS+ ".vtx["+$vtx+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //............................... // place edges on borders (x2) //............................... $curveMin = `getAttr ($curves[2]+".min")`; $curveMax = `getAttr ($curves[2]+".max")`; $curveSpan = `abs $curveMax`-`abs $curveMin`; float $subSpan = $curveSpan/$minSX; for ($i=0; $i<=$minSX; $i++) { $vtx = ((($minSX+1)*($minSY+1))-1)-$i; $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[2]`; select -r ($minS+ ".vtx["+$vtx+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //............................... // place edges on borders (y2) //............................... $curveMin = `getAttr ($curves[3]+".min")`; $curveMax = `getAttr ($curves[3]+".max")`; $curveSpan = `abs $curveMax`-`abs $curveMin`; float $subSpan = $curveSpan/$minSY; for ($i=1; $i<$minSY; $i++) { $vtx = ((($minSX+1)*($minSY)))-($i*($minSX+1)); $currentU = $curveMin + ( $subSpan*$i); $pointLoc = `pointOnCurve -pr $currentU $curves[3]`; select -r ($minS+ ".vtx["+$vtx+"]") ; move $pointLoc[0] $pointLoc[1] $pointLoc[2] ; //currentTime 0; } //...................................... // place vertices into initial position //...................................... vector $v1; vector $v2; vector $v3; vector $v4; vector $new1; vector $new2; vector $pos; int $row; for ($i=($minSX+2); $i< ((($minSX+1)*$minSY)-1); $i++) { int $vMod = $i%($minSX+1); if ($vMod>0 && $vMod < $minSX) { $v1 = getLocV($minS+ ".vtx["+($i-$vMod)+"]"); $v2 = getLocV($minS+ ".vtx["+($i-$vMod+$minSX)+"]"); $v3 = getLocV($minS+ ".vtx["+($vMod)+"]"); $v4 = getLocV($minS+ ".vtx["+($vMod+(($minSX+1)*$minSY))+"]"); $new1 = (($v1*($minSX-$vMod))+($v2*$vMod))/$minSX; $row = $i/($minSX+1); $new2 = (($v3*($minSY-$row))+($v4*$row))/$minSY; $pos = ($new1 + $new2)*.5; select -r ($minS+ ".vtx["+$i+"]") ; move ($pos.x) ($pos.y) ($pos.z); //currentTime 0; } } int $vMod; $smoothLoops = `intSliderGrp -q -v maxIterationsField`; for ($loop=0; $loop<$smoothLoops ; $loop++) { for ($i=($minSX+2); $i< ((($minSX+1)*$minSY)-1); $i++) { $vMod = $i%($minSX+1); if ($vMod>0 && $vMod < $minSX) { //print (" " + $i); $v1 = getLocV($minS+ ".vtx["+($i-1)+"]"); $v2 = getLocV($minS+ ".vtx["+($i+1)+"]"); $v3 = getLocV($minS+ ".vtx["+($i-1-$minSX)+"]"); $v4 = getLocV($minS+ ".vtx["+($i+1+$minSX)+"]"); $pos = ($v1 + $v2 + $v3 + $v4)/4; select -r ($minS+ ".vtx["+$i+"]") ; move ($pos.x) ($pos.y) ($pos.z); } } select -r $minS; //print ("$loop: "+$loop+"\n"); if ( `progressWindow -query -isCancelled` ) break; $updateAmmount = ($loop/$smoothLoops )*100; $updatePercentage= $updateAmmount; //print ("$updatePercentage: " + ($updateAmmount) + "\n"); progressWindow -edit -progress ($updatePercentage) -status ("Complete: "+$updatePercentage+"%") ; if (`checkBox -q -v UIshowSurface`) { refresh -cv; } } progressWindow -endProgress; //print ("curveSpan: " + $curveSpan); select -r $minS; xform -cp; if (`size($path)`>2) {
parent ($minS) ($path[0]+"|"+$path[1]+"|"+$path[2]) ;
}
waitCursor -state off;
}

// ================================================================================
//
// minSurfUI
//
// ================================================================================

global proc minSurfUI () {
if (`window -exists minSurfUIWin` == 1) {
deleteUI minSurfUIWin;
}

string $minSurfUIWin = `window
-title "Minimum Surface Builder"
-menuBar 0
-mxb 0
-rtf 1
minSurfUIWin`;

//scrollLayout;
columnLayout;
separator -h 15 -w 400 -st "none";// whitespace
intFieldGrp -numberOfFields 1
-label "PolyDivisions X" -value1 15
polySubX_field; //`intFieldGrp -q -v polySubX_field`

intFieldGrp -numberOfFields 1
-label "PolyDivisions Y" -value1 15
polySubY_field; //`intFieldGrp -q -v polySubY_field`

// smoothing ITERATIONS
intSliderGrp -label "Smooth Iterations" -field true
-minValue 0 -maxValue 20
-fieldMinValue 0 -fieldMaxValue 100
-value 20
maxIterationsField; //`intSliderGrp -q -v maxIterationsField`


// show updates
separator -h 15 -w 400 -st "none";// whitespace
checkBox -label "Show Surface During Creation" -v 0 UIshowSurface;
/*
separator -h 15 -w 400 -st "in";// whitespace

text -l "0% Complete" UIupdateText;
*/

separator -h 15 -w 400 -st "in";// whitespace
// BUTTONS
//--------------
rowLayout
-nc 2
-cat 1 "both" 0
-cat 2 "both" 0
-columnWidth 1 100
-columnWidth 2 100
;

button -label "Create" -align "center" -command "minSurf";
button -label "Cancel" -align "center" -command "deleteUI minSurfUIWin";

showWindow $minSurfUIWin;
}


Friday, January 5, 2007

FitCurvetoLength Modified

























Here's Yuki's earlier script modified to identify the centerpoint of the curve with the method Rhino.BoundingBox



Option Explicit
'title: FitCurveToLength
'this script is to loop scaling a curve until it fits certain length

FitCurveToLength ()
Sub FitCurveToLength ()
Dim strCurveID
strcurveID = Rhino.GetObject("Select a curve to fit to length", 4, True, True)
If IsNull(strCurveID) Then Exit Sub

Dim arrBoundingBox
arrBoundingBox = Rhino.BoundingBox(strCurveID)

Dim arrCtr
Dim dblx, dbly, dblz
dblx =0.5*(arrBoundingBox(0)(0)+arrBoundingBox(1)(0))
dbly =0.5*(arrBoundingBox(1)(1)+arrBoundingBox(2)(1))
dblz =0.5*(arrBoundingBox(1)(2)+arrBoundingBox(5)(2))

arrCtr = array(dblx, dbly, dblz)

Dim dblLength
dblLength = Rhino.CurveLength(strCurveID)

Dim dblLengthLimit
dblLengthLimit = Rhino.GetReal("Length limit", 0.5*dblLength, 0.01*dblLength, dblLength)
If IsNull(dblLengthLimit) Then Exit Sub

Do
If Rhino.CurveLength(strCurveID) <= dblLengthLimit Then Exit Do
strCurveID = Rhino.ScaleObject(strCurveID, arrCtr, Array(0.95,0.95,0.95),True)
If IsNull(strCurveID) Then
Rhino.Print "Something went wrong..."
Exit Sub
End If
Loop

Rhino.Print "New curve length: " & Rhino.CurveLength(strCurveID)

End Sub

Thursday, January 4, 2007

Architectural Critique: Scripting Misuse

I found some Rhinoscripting studies while surfing the web that I was particularly disheartened with. The website shows a studio from Rensselaer Polytechnic Institute and what appears to be student projects in which math functions are the basis for structure/home design. Here are examples of what I DON'T want to do.

















Here we have math functions in their purity and complexity making singular forms that get transformed into buildings that simply don't work very well or would work much better if they were developed in different ways. Not to be too harsh but the above example simply works against the very premise of wild parametric design by including distinctly uniform and standard floor slabs. In addition, despite the variations that each student shows faded into the background the math functions aren't flexible for local conditions. What it amounts to is form exploration for its own sake, not for better design.

Granted this is all my superficial reaction to this work in that I haven't taken the time to read the studio premise or any individuals goal. But this is a blog and I get to say what I like. So step off.

Monday, January 1, 2007

DivideCurve ( ) _yuki















Here is my first script I wrote from scratch. User can pick two curves, divide into the number s/he wants, and draw lines accordingly. Array was complicated, but I'm getting hang of it.



Option Explicit
'S.S.S.S. sciarcsmac first challenge #2

DivideCurve()
Sub DivideCurve()
Dim strFirstCurve
strFirstCurve = Rhino.GetObject("Select the first curve", 4)
If IsNull(strFirstCurve) Then Exit Sub

Dim strSecondCurve
strSecondCurve = Rhino.GetObject("Select the second curve", 4)
If IsNull(strSecondCurve) Then Exit Sub


Dim t
t = Rhino.GetInteger("Number of division?", 10, 2, 100)
If IsNull (t) Then Exit Sub


Dim arrFirstCurvePoints
arrFirstCurvePoints = Rhino.DivideCurve(strFirstCurve, t, True)

Dim arrSecoudCurvePoints
arrSecoudCurvePoints = Rhino.DivideCurve(strSecondCurve, t, True)


Dim strAddedLines
Dim i


For i = 0 To t Step 1
strAddedLines = Rhino.AddLine(arrFirstCurvePoints(i), arrSecoudCurvePoints(i))

Next

Dim strSayYes
strSayYes = Rhino.GetString ("Continue? ", "Y")
If Not strSayYes = "Y" Then Exit Sub

Rhino.Print "Well done! "

End Sub

FitCurveToLength ( ) _yuki















So far, I just copied tutorial, but my challenge is to scale down the curve within the original. The example on the right is my goal. Now, the scale origin is set to (0,0,0). I have to change the scale origin to the center of gravity. I don't even know if there is a way to specify it.




Option Explicit
'title: FitCurveToLength
'this script is to loop scaling a curve until it fits certain length

FitCurveToLength ()
Sub FitCurveToLength ()
Dim strCurveID
strcurveID = Rhino.GetObject("Select a curve to fit to length", 4, True, True)
If IsNull(strCurveID) Then Exit Sub

Dim dblLength
dblLength = Rhino.CurveLength(strCurveID)

Dim dblLengthLimit
dblLengthLimit = Rhino.GetReal("Length limit", 0.5*dblLength, 0.01*dblLength, dblLength)
If IsNull(dblLengthLimit) Then Exit Sub

Do
If Rhino.CurveLength(strCurveID) <= dblLengthLimit Then Exit Do strCurveID = Rhino.ScaleObject(strCurveID, Array(0,0,0), Array(0.95,0.95,0.95),True)
If IsNull(strCurveID) Then
Rhino.Print "Something went wrong..."
Exit Sub
End If
Loop

Rhino.Print "New curve length: " & Rhino.CurveLength(strCurveID)

End Sub