Created
December 13, 2016 15:03
-
-
Save Farfarer/31148a78f392a831239d9b018b90330c to your computer and use it in GitHub Desktop.
MODO pyAPI - Get the polygon IDs of each "island" of polygons in the active layers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import lx | |
import lxu.command | |
import lxifc | |
class SetMarks (lxifc.Visitor): | |
def __init__ (self, acc, mark): | |
self.acc = acc | |
self.mark = mark | |
def vis_Evaluate (self): | |
self.acc.SetMarks (self.mark) | |
class PolysByIsland (lxifc.Visitor): | |
def __init__ (self, polygon, point, mark): | |
self.polygon = polygon | |
self.point = point | |
self.mark = mark | |
self.islands = [] | |
def vis_Evaluate (self): | |
inner = set () | |
outer = set () | |
outer.add (self.polygon.ID ()) | |
while len(outer) > 0: | |
polygon_ID = outer.pop () | |
self.polygon.Select (polygon_ID) | |
self.polygon.SetMarks (self.mark) | |
inner.add (polygon_ID) | |
num_points = self.polygon.VertexCount () | |
for v in xrange (num_points): | |
self.point.Select (self.polygon.VertexByIndex (v)) | |
num_polys = self.point.PolygonCount () | |
for p in xrange (num_polys): | |
vert_polygon_ID = self.point.PolygonByIndex (p) | |
if vert_polygon_ID not in inner: | |
outer.add (vert_polygon_ID) | |
self.islands.append (inner) | |
class PolysByIsland_Cmd(lxu.command.BasicCommand): | |
def __init__(self): | |
lxu.command.BasicCommand.__init__ (self) | |
def cmd_Flags(self): | |
return lx.symbol.fCMD_UNDO | |
def basic_Enable(self, msg): | |
return True | |
def basic_Execute(self, msg, flags): | |
layer_svc = lx.service.Layer () | |
layer_scan = lx.object.LayerScan (layer_svc.ScanAllocate (lx.symbol.f_LAYERSCAN_EDIT_POLYS)) | |
if not layer_scan.test (): | |
return | |
# Sort out mark modes we'll need. | |
mesh_svc = lx.service.Mesh () | |
mark_mode_checked = mesh_svc.ModeCompose ('user0', None) | |
mark_mode_unchecked = mesh_svc.ModeCompose (None, 'user0') | |
for n in xrange (layer_scan.Count ()): | |
mesh = lx.object.Mesh (layer_scan.MeshBase (n)) | |
if not mesh.test (): | |
continue | |
polygon_count = mesh.PolygonCount () | |
if polygon_count == 0: | |
continue | |
polygon = lx.object.Polygon (mesh.PolygonAccessor ()) | |
point = lx.object.Point (mesh.PointAccessor ()) | |
if not polygon.test () or not point.test (): | |
continue | |
# Clear the checked marks on any polygons that are marked as checked. | |
visClear = SetMarks (polygon, mark_mode_unchecked) | |
polygon.Enumerate (mark_mode_checked, visClear, 0) | |
# Now grab the island of any polygon that's marked as unchecked. | |
# All polygons added to an island get marked as checked as we go. | |
visIslands = PolysByIsland (polygon, point, mark_mode_checked) | |
polygon.Enumerate (mark_mode_unchecked, visIslands, 0) | |
lx.out ('%s islands' % len(visIslands.islands)) | |
for island in visIslands.islands: | |
lx.out ('%s polys' % len(island)) | |
layer_scan.Apply () | |
lx.bless (PolysByIsland_Cmd, 'ffr.polysByIsland') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment