Python Add Linear Dimension

Hi All,

I’m fairly new to Rhino. I’m trying to create linear dimensions by specifying 3 points. In some cases, I get the desired result, but sometimes I don’t.

Below is a code sample that demonstrates the issue.
The problem is that sometimes, when specifying 3 points, it seems to interpret my inputs as being a vertical dimension, when infact, I want a horizontal dimension.

I’ve tried manually inputting the same point coordinates through the Rhino GUI, and I get the desired result, so I’m not sure what I’m doing wrong.

FYI - the sample below is compiled from my main code, so it comes with some notes.

  • The function LinearDim() is intended to replace a function from some old translated code - I’ve added this so I could reuse the existing code verbatim.
  • Basic document setup hasn’t been completed.
  • Dim style changes haven’t been implemented, so the scale etc is way off - some manual adjustment required there.

Thanks in advance.

import rhinoinside

rhinoinside.load()
import Rhino
import datetime as dt


def LinearDim(
    doc, X1, Y1, X2, Y2, offsetDistance, dimType, arrow, textFlag, textOffset
):
    dt1 = [X1, Y1]
    dt2 = [X2, Y2]
    # IF X COORDS ARE EQUAL, THEN DIM MUST BE VERTICAL, ELSE DIM MUST BE HORIZONTAL - IF BOTH ARE DIFFERENT, THEN DIM IS ALIGNED.
    if X1 == X2:
        dt3 = [X1 + offsetDistance, (Y1 + Y2) / 2]
    elif Y1 == Y2:
        dt3 = [(X1 + X2) / 2, Y1 + offsetDistance]
    # print(dt1, dt2, dt3)
    dim = AddLinearDimension(doc, "1", "default", dt1, dt2, dt3)
    return dim


def AddLinearDimension(doc, Layer: str, Style: str, PT1: list, PT2: list, PT3: list):
    ob = doc.Objects
    rg = Rhino.Geometry
    pt1 = PointListTo3dPoint(PT1)
    pt2 = PointListTo3dPoint(PT2)
    pt3 = PointListTo3dPoint(PT3)
    ld = rg.LinearDimension.FromPoints(pt1, pt2, pt3)
    print(
        "[" + str(pt1.X) + ", " + str(pt1.Y) + "]",
        "[" + str(pt2.X) + ", " + str(pt2.Y) + "]",
        "[" + str(pt3.X) + ", " + str(pt3.Y) + "]",
    )
    dim = ob.AddLinearDimension(ld)
    if Style != "":
        # ******* NEED TO SET DIMENSION STYLE LOGIC *************
        pass
    return dim


def PointListTo3dPoint(Pt: list):
    rg = Rhino.Geometry
    pt = rg.Point3d(float(Pt[0]), float(Pt[1]), 0.0)
    return pt


def CreateRhinoFile():
    doc = Rhino.RhinoDoc.CreateHeadless("")
    return doc


def SaveRhinoFile(doc, FN):
    opt = Rhino.FileIO.FileWriteOptions()
    print("File Saved: " + FN)
    doc.WriteFile(FN, opt)


Doc = CreateRhinoFile()

# INSERTS DIMENSION WITH ALL 3 POINTS VERTICALLY ALIGNED, DIMENSION VALUE = 0. 
# IMPLIES IT'S A HORIZONTAL DIMENSION
# SHOULD BE A VERTICAL DIMENSIONA
LinearDim(Doc, -3500, -3500, -3500, -935, -3300, 4, 1795, 771, 0)

# INSERTS DIMENSION WITH POINTS LOCATED CORRECTLY.
LinearDim(Doc, -3500, -3500, 3320, -3500, -3200, 4, 1795, 771, 0)

SaveRhinoFile(Doc, "TEST\\" + str(dt.datetime.now()).replace(":", "") + ".3dm")

Hi @Gerrard_Hickson,

You might try using LinearDimension.Create.

SampleCsAddAlignedDimension.cs

– Dale

1 Like

Hi Dale,

Thanks that helped. I don’t know how I missed that method.

For what its’ worth, there’s an error in the code you linked.

var p1 is defined twice - I suspect the second declaration should be p3, not p1.
yaxis is defined as p1 - p1. I suspect this should be p3 - p1

using Rhino;
using Rhino.Commands;
using Rhino.Geometry;

namespace SampleCsCommands
{
  public class SampleCsAddAlignedDimension : Command
  {
    public override string EnglishName => "SampleCsAddAlignedDimension";

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      var style = doc.DimStyles.Current;
      var plane = Plane.WorldXY;

      var p1 = new Point3d(1.0, 1.0, 0.0);
      var p2 = new Point3d(5.0, 2.0, 0.0);
      var pl = new Point3d(5.0, 4.0, 0.0);                    ###SHOULD BE p3

      var xaxis = p2 - p1;
      var yaxis = p1 - p1;                                    ###SHOULD BE p3 - p1
      if (xaxis.Unitize() && yaxis.Unitize())
      {
        var zaxis = Vector3d.CrossProduct(xaxis, yaxis);
        if (zaxis.Unitize())
        {
          plane = new Plane(p1, xaxis, yaxis);
        }
      }

      var dim = LinearDimension.Create(AnnotationType.Aligned, style, plane, Plane.WorldXY.XAxis, p1, p2, pl, 0.0);

      //string displaytext = dim.GetDistanceDisplayText(doc.ModelUnitSystem, style);

      doc.Objects.Add(dim);

      doc.Views.Redraw();
      return Result.Success;
    }
  }
}

The last character is a lowercase L, not a 1.

– Dale

1 Like

Ahh… my mistake.