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")