by Tuğrul Yazar | July 25, 2017 11:27
This python code proves how much effort it takes to create a simple hexagonal tessellation[1]. There are, of course, much easier and faster methods than this. But here you see a code that introduces students to Rhino Python[2]. Using this code, a new Rhino command can be generated, and for the first time in Rhino, we can have a command that creates a hexagonal grid. I followed this tutorial to turn this Python script into a Rhino command. This is also educational. Because I sometimes want my design mathematics students to mimic a Rhino command in Python. Then, I ask them to improve one of the commands. This is a nice example of further improvements. A student can follow this and lead the hexagonal grid code to a hexagonal array command, for example.
The below Python code uses the rhinoscriptsyntax module to draw the hexagonal grid. As you can read, it draws the hexagons by dividing a circle. Because Rhino Python in 2017 doesn’t have a polygon method. I hope in the future, this might be solved. However, it is a good exercise to include a polygon method in this script. After defining a function that draws the polygon, the script uses it iteratively to create the grid.
I will try to publish and explain this as soon as possible. Also, I think that this and some other commands can be a plug-in in the future. There are many lunchbox plug-ins out there. But why not I shouldn’t code my own version.
# Draw centered hexagonal grid
# 25.07.2017 www.designcoding.net - Tugrul Yazar
import rhinoscriptsyntax as rs
import math
hexGridCenter = rs.GetPoint("Specify center point")
hexGridExtend = rs.GetInteger("Enter the number of radial levels", 3, 2)
hexGridClSize = rs.GetReal("Edge size of hexagons", 1.0)
hexGridCAngle = rs.GetReal("Rotation angle of hexagons", 0.0)
edges = 6
rs.AddPoint(hexGridCenter)
hexGrid = []
pythagoras = 2 * math.sqrt((hexGridClSize * hexGridClSize) - ((hexGridClSize / 2) * (hexGridClSize / 2)))
def polygon(center,edges,size,angle):
tempCircle = rs.AddCircle(center, size)
tempPoints = rs.DivideCurve(tempCircle, edges, create_points=False, return_points=True)
tempPoints.append(tempPoints[0])
tempHexago = rs.AddPolyline(tempPoints)
tempReturn = rs.RotateObject(tempHexago, center, angle, axis=None, copy=False)
rs.DeleteObject(tempCircle)
return tempReturn
for i in range(0, hexGridExtend):
tempSize = pythagoras * (i + 1)
dut = polygon(hexGridCenter, edges, tempSize, 30 + hexGridCAngle)
temp = rs.DivideCurve(dut, (i+1) * edges, create_points=False, return_points=True)
rs.DeleteObject(dut)
hexGrid.extend(temp)
for i in range(0, len(hexGrid)):
polygon(hexGrid[i], edges, hexGridClSize, hexGridCAngle)
rs.AddPoint(hexGrid[i])
Source URL: https://www.designcoding.net/hexagonal-grid-with-rhino-python/
Copyright ©2024 designcoding unless otherwise noted.