download the package for this article at here
Hello Fabrication Experts!
We all know that unroll meshes to prepare for laser cutting is always a bit tricky. Although there are third-party plugins like IVY or FOX that can help us, learning the new workflows and install plugins are still not the best idea.
Therefore, I wrote a pretty straight forward code for people that want to unroll the mesh geometry and properly arrange on the XY plane. You may find the GH file at the beginning of the article.
Following, I will explain the idea behind the code. The code is not perfect and can be more concise. However, it is working and be able to use during the GH introduction workshop that I conducted last week.
The code was composed of three major parts.
- turn the mesh into brep
- unroll the brep
- arrange the flatten surface according to the original tree structure of the data on the XY plane
It is essential that remaining the original data structure can help us properly index and arrange the individual pieces.
Step00/ import the required library
__author__ = "YU" __version__ = "2019.07.30" import rhinoscriptsyntax as rs import Rhino.Geometry as rg import Grasshopper as gh import Grasshopper.Kernel.Data as gkd def pt2plane(pts_tree): planes_tree = gh.DataTree[object]() branchnum = pts_tree.BranchCount for i in range(branchnum): pts_list = pts_tree.Branch(i) planes_list = [] for pt in pts_list: planes_list.append(rg.Plane(pt, rg.Vector3d(0,0,01))) planes_tree.AddRange(planes_list, pts_tree.Path(i)) return planes_tree
Step01/ turn meshes into breps and unroll the breps
pathnum = mesh.BranchCount breptree = gh.DataTree[object]() center_of_breptree = gh.DataTree[object]() points_tree = gh.DataTree[object]() </pre><pre>[python] #access the branches for i in range(pathnum): meshlist = mesh.Branch(i) breplist = [] center_of_breplist = [] #access the items for m in meshlist: brep2mesh = rg.Brep.CreateFromMesh(m, True) unroller = rg.Unroller(brep2mesh) unroll_brep = rg.Unroller.PerformUnroll(unroller) brep = unroll_brep[0][0] # output of perform unroll is 2d array brep_areaprop = rg.AreaMassProperties.Compute(brep) center_of_brep = rg.AreaMassProperties.Centroid.GetValue(brep_areaprop) breplist.append(brep) center_of_breplist.append(center_of_brep) breptree.AddRange(breplist, mesh.Path(i)) #addrange for adding list in datatree center_of_breptree.AddRange( center_of_breplist, mesh.Path(i))
step02/ create the target planes based on the tree structure
for i in range(pathnum): breps = breptree.Branch(i) points_list = [] for j in range(len(breps)): point = rg.Point3d(i*x_dis, j*y_dis,0) points_list.append(point) points_tree.AddRange(points_list, mesh.Path(i)) tar_planes = pt2plane(points_tree) org_planes = pt2plane(center_of_breptree)
step03/ move the surfaces from original position to the target position
for i in range(pathnum): breps = breptree.Branch(i) org_plane = org_planes.Branch(i) tar_plane = tar_planes.Branch(i) for j, element in enumerate(breps): trans = rg.Transform.PlaneToPlane(org_plane[j], tar_plane[j]) breps[j].Transform(trans) output_geo = breptree output_center = points_tree
If you have more questions or suggestions, feel free to leave the message or write me the email!