【范例】选取视窗中显示在最前面的物件(Python & RhinoCommon)

这个问题是前段时间通过 E-Mail 为用户解答的问题,拿来给大家参考:

因为 Rhino 中组织管理物件是通过物件类型和物件 GUID,没有哪个现成的函数能够通过显示顺序来抓取物件,所以这就需要自己写这样的功能。

思路很简单,求取正在使用中工作视窗中所有显示的物件最近点和这个工作视窗摄影机位置点的距离,距离最近的物件必然就是显示在最前面的物件。

我用 Python 调用 RhinoCommon 的函数写了一个简化版的例子,只是求解了工作视窗中物件边框方块中心点和摄影机位置的距离,用最近点的话要区分物件类型分别对待

例子如下:


import Rhino
import scriptcontext

viewport=scriptcontext.doc.Views.ActiveView.MainViewport
settings = Rhino.DocObjects.ObjectEnumeratorSettings()
settings.HiddenObjects = False
settings.DeletedObjects = False
settings.IncludeGrips = False
settings.IncludePhantoms = True
settings.IncludeLights = False
settings.ViewportFilter = scriptcontext.doc.Views.ActiveView.MainViewport
settings.VisibleFilter = True
settings.ReferenceObjects = True
objects = scriptcontext.doc.Objects.GetObjectList(settings)
object_ids = []
vlocation=scriptcontext.doc.Views.ActiveView.MainViewport.CameraLocation
i=0
for object in objects:
i=i+1
bbox = object.Geometry.GetBoundingBox(True)
if viewport.IsVisible(bbox):
bboxc=bbox.Center
td=vlocation-bboxc
if i==1:d=td.Length
if td.Length<=d:
scriptcontext.doc.Objects.UnselectAll()
d=td.Length
object.Select(True)
object_ids.append(object.Id)
scriptcontext.doc.Views.Redraw()

厉害啊