Skip to content

Instantly share code, notes, and snippets.

@pgolay
Last active November 25, 2023 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgolay/5dda8332e59281249442f52a6a63dac6 to your computer and use it in GitHub Desktop.
Save pgolay/5dda8332e59281249442f52a6a63dac6 to your computer and use it in GitHub Desktop.
Call out distances of user snapped points from the camera point in a viewport
#! python3
import rhinoscriptsyntax as rs
import scriptcontext as sc
import System
import Rhino
def DistanceFromCamera():
#color = System.Drawing.Color.Black
colors = [System.Drawing.Color.Black, System.Drawing.Color.AntiqueWhite]
colorList = ["Black", "White"]
def GetPointDynamicDrawFunc( sender, args ):
color = colors[colorIdx]
for i in range(len(pts)):
tempPlane = tPlane
tempPlane.Origin = pts[i]
args.Display.DrawPoint(pts[i])
args.Display.Draw3dText(str(dists[i]), color, tempPlane, labelSize, "Arial Black", True, True)
viewRC,view = Rhino.Input.RhinoGet.GetView("Select a view to test.")
if viewRC != Rhino.Commands.Result.Success:
return
vp = view.ActiveViewport
planeRC, tPlane = vp.GetCameraFrame()
cam = vp.CameraLocation
pts = []
dists = []
while True:
blnLabel=True
if 'LABEL_DIST' in sc.sticky:
blnLabel = sc.sticky['LABEL_DIST']
labelSize = 10
if 'LABEL_SIZE' in sc.sticky:
labelSize = sc.sticky['LABEL_SIZE']
colorIdx = 0
if 'COLOR_IDX' in sc.sticky:
colorIdx = sc.sticky['COLOR_IDX']
gp = Rhino.Input.Custom.GetPoint()
gp.AcceptNumber(True, True)
opLabel = Rhino.Input.Custom.OptionToggle(blnLabel, "No", "Yes")
gp.AddOptionToggle("ShowLabels", opLabel)
opLabelSize = Rhino.Input.Custom.OptionDouble(labelSize)
gp.AddOptionDouble("LabelTextHeight",opLabelSize)
gp.AddOption("ToggleColor")
if len(pts) > 0:
gp.AddOption("Undo")
if len(pts) > 0:
if blnLabel:
gp.DynamicDraw += GetPointDynamicDrawFunc
rc = gp.Get()
if( gp.CommandResult() != Rhino.Commands.Result.Success ):
break
if rc == Rhino.Input.GetResult.Point:
pt = gp.Point()
pts.append(pt)
elif rc == Rhino.Input.GetResult.Option:
idx = gp.OptionIndex()
blnLabel = opLabel.CurrentValue
sc.sticky['LABEL_DIST'] = blnLabel
labelSize = opLabelSize.CurrentValue
sc.sticky['LABEL_SIZE'] = labelSize
if idx ==3:
if colorIdx == 0:
colorIdx = 1
else:
colorIdx = 0
sc.sticky['COLOR_IDX'] = colorIdx
continue
if idx == 4:
pts.pop()
dists.pop()
continue
elif rc == Rhino.Input.GetResult.Number:
num = gp.Number()
if num > 0:
labelSize = num
sc.sticky['LABEL_SIZE'] = labelSize
continue
else:
return
if not pt:
return
dist = round(pt.DistanceTo(cam), 2)
dists.append(dist)
print("Distance from camera = " + str(dist))
DistanceFromCamera()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment