Convert a vb script to c#


#1

HI,
I tried to convert a panelling script from vb to c#,I cant find the error, it should be already translated completly.

 Private Sub RunScript(ByVal Srf As Surface, ByVal U As Integer, ByVal V As Integer, ByRef Panels As Object) 
    Dim p As New List (Of NurbsSurface)
    Dim ustep As Double = 1 / u
    Dim vstep As Double = 1 / v

    For i As Integer = 0 To u - 1
      Dim j As Integer = 0
      While j <= V - 1
        Dim myRandom As New Random(i + j)
        Dim varlength As Integer = myRandom.Next(1, 10)

        If (j + varlength) > V Then
          varlength = V - j
        End If

        Dim panelpts As New List (Of Point3d)

        For k As Integer = 0 To varlength
          Dim ptA As Point3d = Srf.PointAt(i * ustep, (j + k) * vstep)
          Dim ptB As Point3d = Srf.PointAt((i + 1) * ustep, (j + k) * vstep)
          panelpts.Add(ptA)
          panelpts.Add(ptB)
        Next

        Dim mysurface As NurbsSurface = NurbsSurface.CreateFromPoints(panelpts, varlength + 1, 2, 3, 3)
        p.Add(mysurface)

        j = j + (varlength)
      End While
    Next

    Panels = p
  End Sub

Its from this page: http://wiki.theprovingground.org/scripts-paneling

my attempt so far:

 private void RunScript(Surface S, int U, int V, ref object A)
  {
    var panels = new List<NurbsSurface>();

    for(int i = 0; i < U - 1; i++)
    {
      int j = 0;
      while (j <= V - 1)
      {
        Random myRandom = new Random(i + j);
        int varlength = myRandom.Next(1, 10);
        if ((j + varlength) > V )
          varlength = V - j;

        var panelPts = new List<Point3d>();

        for (int k = 0; k < varlength; i++)
        {
          Point3d ptA = S.PointAt(i * U, (j + k) * V);
          Point3d ptB = S.PointAt((i + 1) * U, (j + k) * V);
          panelPts.Add(ptA);
          panelPts.Add(ptB);

          NurbsSurface ns = NurbsSurface.CreateFromPoints(panelPts, varlength + 1, 2, 3, 3);
          panels.Add(ns);

          j = j + (varlength);
        }
      }
    }
    A = panels;
  }

Thanks for any hint!

Regards!

file : RandomPanels.gh (4.0 KB)


(David Rutten) #2

Note that for loops in VB and C# work differently. In VB the loop runs up to and including the terminating value. So: For i As Integer = 0 To u-1 would translate to for (int i = 0; i < u; i++)


(David Rutten) #3

I’d recommend creating a single Random object before the loop and just calling Next within the loop.


#4

HI David,

thanks again!
I applied the changes you offered( if I understood well), but still something goes wrong,in the loop, I assume.
My code now:

private void RunScript(Surface S, int U, int V, ref object A)
{
  var panels = new List<NurbsSurface>();
  Random myRandom = new Random();
  double uStep = S.Domain(0).Length / U;
  double vStep = S.Domain(1).Length / V;

  for(int i = 0; i < U; i++)
  {
    int j = 0;
    while (j <= V)
    {
      int varLength = myRandom.Next(1, 10);
      if ((j + varLength) > V )
        varLength = V - j;
      var panelPts = new List<Point3d>();

      for (int k = 0; k < varLength; k++)
      {
        Point3d ptA = S.PointAt(i * uStep, (j + k) * vStep);
        Point3d ptB = S.PointAt((i + 1) * uStep, (j + k) * vStep);
        panelPts.Add(ptA);
        panelPts.Add(ptB);

        NurbsSurface ns = NurbsSurface.CreateFromPoints(panelPts, varLength + 1, 2, 3, 3);
        panels.Add(ns);
        j = j + (varLength);
      }
    }
  }
  A = panels;
}

Maybe you or someone could give hint.

Really apreciated.
Thanks!

file:RandomPanels.gh (6.5 KB)


(David Rutten) #5

I did a straight translation from the original vb code into C#: vb2cs.gh (8.4 KB)

The only thing I changed was move the random constructor to the outside of the loop.


#6

Hi,

thanks a lot!
Now I can keep on fooling around.

Thanks !