-
Notifications
You must be signed in to change notification settings - Fork 3
/
getAllRectOutline.py
81 lines (80 loc) · 3.98 KB
/
getAllRectOutline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import rhinoscriptsyntax as rs
from returnObjectOutline import ReturnObjectOutline
from System.Drawing import Color
def GetAllRectOutline(listOfObjOutlines,rotatedAngle,objCenter,movingCenter,objectSpacing):
rectOutline = []
for obj in listOfObjOutlines:
counter = 1
rotInterval = 1
# create bounding box for rotInterval = 0
lastBoxPt = rs.BoundingBox(obj)
# CHANGE ROTCENTER TO OBJ CENTROID
rotCenter = rs.CurveAreaCentroid(obj)[0]
# append obj center into list
objCenter.append(rotCenter)
lastBoxArea = rs.Distance(lastBoxPt[1],lastBoxPt[0]) * rs.Distance(lastBoxPt[1],lastBoxPt[2])
# making sure rotating toward min area.
rs.RotateObject(obj,rotCenter,rotInterval)
nxtBoxPt = rs.BoundingBox(obj)
nxtBoxArea = rs.Distance(nxtBoxPt[0],nxtBoxPt[1]) * rs.Distance(nxtBoxPt[1],nxtBoxPt[2])
# adjust rotation angle or store rectangle information if min
if nxtBoxArea > lastBoxArea:
rotInterval = -rotInterval
rs.RotateObject(obj,rotCenter, (2*rotInterval))
nxtBoxPt = rs.BoundingBox(obj)
nxtBoxArea = rs.Distance(nxtBoxPt[0],nxtBoxPt[1]) * rs.Distance(nxtBoxPt[1],nxtBoxPt[2])
if nxtBoxArea >= lastBoxArea:
rs.RotateObject(obj,rotCenter,(-rotInterval) * counter)
lastBoxPt = lastBoxPt[:4]
lastBoxPt.append(lastBoxPt[0])
boxCenter = (lastBoxPt[0] + lastBoxPt[2]) / 2
lastBox = rs.AddPolyline(lastBoxPt)
# add safety between objects (20(mm) up and down)
offsetBox = rs.OffsetCurve(lastBox, -boxCenter, objectSpacing)[0]
rs.DeleteObject(lastBox)
lastBox = offsetBox
# check if max length is at lateral direction
if rs.Distance(lastBoxPt[0], lastBoxPt[1]) > rs.Distance(lastBoxPt[1], lastBoxPt[2]):
rs.RotateObject(lastBox, boxCenter, 90)
# update angle turn
rotatedAngle.append(90)
else:
# store angle turn without update
rotatedAngle.append(0)
# Store GUID of each rectangle created
rectOutline.append(lastBox)
movingCenter.append(boxCenter)
del lastBoxPt,lastBoxArea, nxtBoxPt, nxtBoxArea, offsetBox
continue
while nxtBoxArea <= lastBoxArea:
counter += 1
lastBoxPt = nxtBoxPt
lastBoxArea = nxtBoxArea
rs.RotateObject(obj,rotCenter,rotInterval)
nxtBoxPt = rs.BoundingBox(obj)
boxCenter = (lastBoxPt[0] + lastBoxPt[2]) / 2
nxtBoxArea = rs.Distance(nxtBoxPt[0],nxtBoxPt[1]) * rs.Distance(nxtBoxPt[1],nxtBoxPt[2])
# return curve back to original (zero) rotation
rs.RotateObject(obj,rotCenter,(-rotInterval*counter))
# Transform box pt to polyline
lastBoxPt = lastBoxPt[:4]
lastBoxPt.append(lastBoxPt[0])
lastBox = rs.AddPolyline(lastBoxPt)
# add safety between objects (20(mm) up and down)
offsetBox = rs.OffsetCurve(lastBox, -boxCenter, objectSpacing)[0]
rs.DeleteObject(lastBox)
lastBox = offsetBox
# check if max length is at lateral direction
if rs.Distance(lastBoxPt[0], lastBoxPt[1]) > rs.Distance(lastBoxPt[1], lastBoxPt[2]):
rs.RotateObject(lastBox,(lastBoxPt[0] + lastBoxPt[2]) / 2, 90)
# update angle turn
rotatedAngle.append(rotInterval * counter + 90)
else:
# store angle turn without update
rotatedAngle.append(rotInterval * counter)
# Store GUID of each rectangle created
rectOutline.append(lastBox)
movingCenter.append(boxCenter)
del lastBoxPt,lastBoxArea, nxtBoxPt, nxtBoxArea, offsetBox
# return List
return rectOutline