# Python Rhino: Orient Block

December 17, 2011
by admin

Our final form and unroll method was done by hand so the only script directly used was a tabbing script. The script did not auto tab for us, but instead allowed the user to select which block that represented a tab went to which side. The code is relatively simple, and since I am more interested about how to convert mathematics formulas to code, I wont go into detail with this one.
The current code uses the variable done to keep the loop going. At the end of the code the user is asked if the tab was placed in the wrong direction and they would like to flip it. If the user presses enter it automatically continues, if they press 1 it flips, and if they press 2 then done becomes 1, stopping the loop. The previous post went over how to orient a part using move and rotation, and the previous post on length matching went over how to scale a line to another one. This code uses similar concepts, with the exception of the 1D scale. Here is the full code:

```import rhinoscriptsyntax as rs
import math

done=0
while(done!=1):

part_sel=rs.GetObject("Select Part")
curve_sel=rs.GetObject("Select Curve")
curve_mid=rs.CurveMidPoint(curve_sel)
is_it=0
bound=rs.BoundingBox(part_sel)
part_start=bound
part_end=bound
part_height=bound
height_dis=part_end-part_height
height=abs(height_dis)
print height
if (height1.55):
is_it=1

curve_start=rs.CurveStartPoint(curve_sel)
curve_end=rs.CurveEndPoint(curve_sel)
#Match Length, Scale in 1 Direction (Parts must be oriented with long direction x)
curve_len=rs.CurveLength(curve_sel)
part_len=rs.Distance(part_start, part_end)
bound_x=((bound-bound)*.5)
bound_x_mid=bound+bound_x
part_mid=(bound_x_mid,bound,bound)
scale_factor=curve_len/part_len

part_scale=rs.XformScale((scale_factor,1,1),part_mid)
part_sel_2=rs.CopyObject(part_sel)

new_part=rs.TransformObject(part_sel, part_scale, False)
new_part_bound=rs.BoundingBox(new_part)
part_start=new_part_bound
part_end=new_part_bound
part_height=new_part_bound
height_dis=part_end-part_height
height=abs(height_dis)

if (is_it==1):
new_start_bound=bound+.03
new_end_bound=bound+.03
new_part_bound=new_start_bound
new_part_bound=new_end_bound
is_it=0

#Change this if orientation is facing in instead of out
#rs.OrientObject(part_sel, (part_start,part_end), (curve_start,curve_end) )
part2=rs.OrientObject(part_sel, (part_end,part_start), (curve_start,curve_end), 1)

flip=rs.GetInteger("1 For Flip, 2 For finish",0)
if (flip==1):
rs.AddPoint(part_mid)
rs.RotateObject(part2,curve_mid,180,None)
if (flip==2):
done=1

rs.DeleteObject(curve_sel)
rs.DeleteObject(part_sel)
#done=rs.GetInteger("Done? (1)", 0)

```