Wish for rhinoscript: distance annotation

Hi,

dimensions are great, but sometimes a quick distance annotation is all one needs. I tried to use rhinoscript to make the following:
Pick point A,
pick point B,
draw line AB
apply arrowheads
place a textdot in the middle with the distance between points

somehow my script didn’t work :rofl: - has someone an idea how to achieve this?
thanks

Haven’t tested this but it appears there’s an example here:

thanks-
the script creates a radial dimension not distance , but maybe I can modify the code

1 Like

Pick point A, :white_check_mark:
pick point B, :white_check_mark:
draw line AB :white_check_mark:
calc distance :white_check_mark:
calc mid-point :white_check_mark:
apply arrowheads :question:
place a textdot in the middle with the distance between points :question:

! _NoEcho _-Runscript (
    Option Explicit

Call Distbetween()
Sub Distbetween()
Dim pt,pt2,dist,units,prec,msg
pt = Rhino.GetPoint("Pick point on object")
If Not IsArray(pt) Then Exit Sub
            pt2 = Rhino.GetPoint("Pick point on object")
If Not IsArray(pt) Then Exit Sub
dist = Rhino.Distance(pt,pt2)
Rhino.AddLine pt, pt2
            units = Rhino.UnitSystemName(,, True)
prec = Rhino.UnitDistanceDisplayPrecision()
msg = "Distance between points is "
Rhino.TextDotPoint Round(dist, prec), Rhino.PointDivide (Rhino.PointAdd(pt, pt2), 2)

             Call Rhino.Print(msg & Cstr(Round(dist, prec)) & " " & units)
End Sub
)

do you particularly need that text dot?

Funny thing about this RadialDimension example was that you copy paste it directly. Run it it asks you to select a curve. You comply, and then it throws an error. :smiley:
How reliable should the examples in the API be?

I just realized you use VB. I can’t help you with VB.

VB is the German Language of all programming languages.

well I am german, but still I don’t have an idea what I am doing :blush:

haha, ironic.

Well have you tried Python instead of VB. Much easier to comprehend. Not my expression, many people on the internet say Python is the English of the programming languages.

Although, this is a bad python example:

from Rhino import *
from Rhino.DocObjects import *
from Rhino.Commands import *
from Rhino.Geometry import *
from Rhino.Input import *
from scriptcontext import doc

Hey,hey

success! programing something i dont have a clue of … well

but still missing the arrowheads


! _NoEcho _-Runscript (
Option Explicit

Call Distbetween()
Sub Distbetween()
Dim pt,pt2,dist,units,prec,msg
pt = Rhino.GetPoint(“Pick point on object”)
If Not IsArray(pt) Then Exit Sub
pt2 = Rhino.GetPoint(“Pick point on object”)
If Not IsArray(pt) Then Exit Sub
dist = Rhino.Distance(pt,pt2)
Rhino.AddLine pt, pt2
units = Rhino.UnitSystemName(, True)
prec = Rhino.UnitDistanceDisplayPrecision()
msg = "Distance between points is "
Rhino.AddTextDot Cstr(Round(dist, prec)), Rhino.PointDivide (Rhino.PointAdd(pt, pt2), 2)

Call Rhino.Print(msg & Cstr(Round(dist, prec)) & " " & units)
End Sub
)

1 Like

Hi,

Try something like this :

Call Distbetween()
Sub Distbetween()
	Dim pt,pt2,dist,units,prec,msg, arrLine
	pt = Rhino.GetPoint("Pick point On object")
	If Not IsArray(pt) Then Exit Sub
	pt2 = Rhino.GetPoint("Pick point On object")
	If Not IsArray(pt) Then Exit Sub
	dist = Rhino.Distance(pt, pt2)
	arrLine = Rhino.AddLine(pt, pt2)
	Call Rhino.CurveArrows(arrLine, 3)
	units = Rhino.UnitSystemName(, True)
	prec = Rhino.UnitDistanceDisplayPrecision()
	msg = "Distance between points is "
	Rhino.AddTextDot Cstr(Round(dist, prec)), Rhino.PointDivide(Rhino.PointAdd(pt, pt2), 2)

	Call Rhino.Print(msg & Cstr(Round(dist, prec)) & " " & units)
End Sub

1 Like

many thanks! :bowing_man:

Hi @Konrad.Kobayashi, you might also take care if the picked points are far enough from eachother so a line can be added, otherwise you’ll get an error eg. if both points are identical.

Option Explicit

Call Main()
Sub Main()

        Dim arrPt0, arrPt1, dblTolerance, dblDistance, strLine, arrMidPt
	
	arrPt0 = Rhino.GetPoint("First point")
	If IsNull(arrPt0) Then Exit Sub
	
	arrPt1 = Rhino.GetPoint("Second point")
	If IsNull(arrPt1) Then Exit Sub
	
	' exit if the distance is to small to add a line
	dblDistance = Rhino.Distance(arrPt0, arrPt1)
	dblTolerance = Rhino.UnitAbsoluteTolerance()
	If dblDistance < dblTolerance Then Exit Sub
	
	' create line with arrowheads
	strLine = Rhino.AddLine(arrPt0, arrPt1)
	If IsNull(strLine) Then Exit Sub
	Call Rhino.CurveArrows(strLine, 3)
	
	' add annotation dot to line mid point
	arrMidPt = Rhino.PointDivide(Rhino.PointAdd(arrPt0, arrPt1), 2.0)
	Call Rhino.AddTextDot(Round(dblDistance, 3), arrMidPt)
	
End Sub

_
c.

1 Like

thanks – nice, even with comments!

beautiful! :

interesting tool, python version anybody?

hey clement,

could I ask for a little help once more?
I tried to change the color of the dot to white
i just added this line after addTextDot:

       Call.Rhino.ObjectColor (Rhino.LastCreatedObjects(), (255,255,255))

Hi @Konrad.Kobayashi, you might try this:

strDot = Rhino.AddTextDot(Round(dblDistance, 3), arrMidPt)
Call Rhino.ObjectColor(strDot, vbWhite) 

_
c.

1 Like
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    pt0 = rs.GetPoint("First Point")
    if not pt0: return
    
    pt1 = rs.GetPoint("Second Point", pt0)
    if not pt1: return
    
    distance = pt0.DistanceTo(pt1)
    
    if distance > scriptcontext.doc.ModelAbsoluteTolerance:
        line_id = rs.AddLine(pt0, pt1)
        rs.CurveArrows(line_id, 3)
        mid_pt = (pt0 + pt1) * 0.5
        dot_id = rs.AddTextDot(round(distance, 3), mid_pt)
        scriptcontext.doc.Views.Redraw()
    
DoSomething()

_
c.

2 Likes

:bowing_man::bowing_man::grinning::star_struck:
thanks, time for me to learn some rs myself :wink:

just to share the script with everyone:
I’ve changed the input method to getLine for more feedback and grouped the output

use at your own risk :wink:

distance

! _NoEcho _-Runscript (
Call Distbetween()
Sub Distbetween()
  Dim arrPt0, arrPt1, dblTolerance, dblDistance, strLine, arrMidPt,  strDot, strGroup, strLineArr 
' create line with arrowheads
strLineArr = Rhino.GetLine 
            If IsNull(strLineArr) Then Exit Sub
            If IsNull(strLineArr(0)) Then Exit Sub
            If IsNull( strLineArr(1)) Then Exit Sub
            strLine = Rhino.AddLine( strLineArr(0), strLineArr(1))
            If IsNull(strLine) Then Exit Sub
            
            
            ' exit if the distance is to small to add a line
dblDistance = Rhino.Distance(strLineArr(0), strLineArr(1))
dblTolerance = Rhino.UnitAbsoluteTolerance()
If dblDistance < dblTolerance Then Exit Sub
            Call Rhino.CurveArrows(strLine, 3)

' add annotation dot to line mid point
arrMidPt = Rhino.PointDivide(Rhino.PointAdd(strLineArr(0), strLineArr(1)), 2.0)

           strDot = Rhino.AddTextDot(Round(dblDistance, 3), arrMidPt)
            Call Rhino.ObjectColor(strDot, vbWhite) 

            strGroup = Rhino.AddGroup()
            Call Rhino.AddObjectToGroup( strDot, strGroup)
           Call Rhino.AddObjectToGroup( strLine, strGroup)
           
End Sub
)
1 Like