"""Sample script used in HowTo: Work with the Selection Buffer. LICENSE AGREEMENT The Original Code is "reportarea.py". The Initial Developer of the Original Code is Leica Geosystems. Portions created by Leica Geosystems are Copyright (C) 2014 year. Leica Geosystems. All Rights Reserved. """ import sys from grail.ms3d import mssys # Import statements we will use to read the selection buffer and # generate an simple area report. from grail.ms3d import selectionbuffer from grail.ms3d import element from grail.ms3d import elementop from grail import gsys from grail import messages # Re-direct print statements to the MS-3D Message Window # sys.stdout = mssys.stdout def gmain(msg, data): """Main entry point.""" if msg == messages.gPRE_RUN: pass # do any pre-processing prior to running a script. elif msg == messages.gRUN: run_code() # run your script. elif msg == messages.gPOST_RUN: pass # do any post processing on your script. else: return gsys.grailmain(msg, data) def run_code(): # (1) Check that there is a selection buffer. if selectionbuffer.is_selection(): report_area() else: # generates an error message string in the message window. mssys.stderr.write("No objects selected.") def report_area(): # (2) Retrieve all elements from the selection buffer. els = selectionbuffer.get_elements() # (3) Traverse the selected elements and generate # a report to the message window. for el in els: name = el.get_name() # (4) If the element is unnamed give it a name. if not bool(name): name = "" # (5) Only compute areas for elements that are polygons # or shells. type = el.get_type() if type in [element.PolygonType, element.ShellType]: area = elementop.calc_area(el) print "%s : %.2f" % (name, area) else: print "%s : n/a" % (name)