Problem with offset a rectangle

Hello everybody,
I have a problem when I try to offset a rectangle.

I use the following method:

Curve curvaOffset = ((Curve)geometria).Offset(Plane.WorldXY, herramientaCorte.Diametro / 2, 0.01,CurveOffsetCornerStyle.Sharp)[0]; 

Geometria contains a rectangle. After doing this, I want to explode curvaOffset with the following method:

curvaOffset .TryGetPolyline(out polylinea)

But polylinea has 12 segments instead 4 because each increment of the offset is a segment.

Are there any way to offset a rectangle and get a polyline of 4 segments?

Thank you in advance.

Yes, this is on the list to fix. To work around this, you can clean up the offset curve by doing something like this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var src_points = new Point3d[5];
  src_points[0] = new Point3d(1, 1, 0);
  src_points[1] = new Point3d(9, 1, 0);
  src_points[2] = new Point3d(9, 9, 0);
  src_points[3] = new Point3d(1, 9, 0);
  src_points[4] = new Point3d(1, 1, 0);

  var src_curve = new PolylineCurve(src_points);
  doc.Objects.AddCurve(src_curve);

  var offsets = src_curve.Offset(Plane.WorldXY, 1.0, doc.ModelAbsoluteTolerance, CurveOffsetCornerStyle.Sharp);
  if (null == offsets || 0 == offsets.Length)
    return Result.Failure;

  for (var i = 0; i < offsets.Length; i++)
  {
    var curve = offsets[i];
    if (null != curve)
    {
      if (curve.IsPolyline())
      {
        var polyline = new Polyline();
        var dom = curve.Domain;
        var t = dom.Min;
        while (curve.GetNextDiscontinuity(Continuity.G1_continuous, t, dom.Max, out t))
          polyline.Add(curve.PointAt(t));

        if (curve.IsClosed)
        {
          var p0 = polyline[0];
          var pn = polyline[polyline.Count - 1];
          var v = pn - p0;
          if (v.Length > Rhino.RhinoMath.SqrtEpsilon)
            polyline.Add(p0);
        }

        if (curve.IsValid)
          curve = new PolylineCurve(polyline);
      }

      doc.Objects.AddCurve(curve);
    }
  }

  doc.Views.Redraw();

  return Result.Success;
}
1 Like

It works,

Thank you very much.