Grasshopper

algorithmic modeling for Rhino

I am trying to mirror a list of Point3d structs in RhinoCommon in C#. I can mirror a single Point3d struct using Point3d.Transform(mirror) which works fine. But when I try to do the same thing on a list of Point3d structs, I only get the originals. It's not mirroring them.

this works;

Point3d point = new Point3d(otherPoint);

point.Transform(mirror);

this does not:

foreach (Point3d point in pointList)

point.Transform(mirror);

There seems to be a problem when I use a loop on the list with Transform.

Please advise on what I need to change.

Thanks!

Vern

Views: 1921

Replies to This Discussion

Hi Vern,

Point3d is a ValueType, meaning that you get a copy whenever you get the point out of a list. I.e. your loop only affects copies of points in your list, which are then promptly forgotten. The code will have to look like this:

for (int i = 0; i < pointList.Count; i++)

{

  Point3d p = pointList[i];

  p.Transform(mirror);

  pointList[i] = p;

}

This is the one major drawback of value-types over reference-types, and there's no way around it.

--

David Rutten

david@mcneel.com

Another approach would be to create a second list with mirrored points:

List<Point3d> mirrored = new List<Point3d>();

foreach (Point3d point in pointList)

{

  point.Transform(mirror);

  mirrored.Add(point);

}

--

David Rutten

david@mcneel.com

Ok, I figured it had something to do with being a struct instead of a reference.

Thanks a lot David!

Hi Vernon

I have a similar problem. Is it possible that I may be allowed to see your code / grasshopper file?

Hi Mette,

try the following into a C# component:

private void RunScript(List<Point3d> x, Plane pl, ref object A)
{
Rhino.Geometry.Transform mir = Rhino.Geometry.Transform.Mirror(pl);
List<Point3d> x_mir = new List<Point3d>();
foreach (Point3d point in x)
{
point.Transform(mir);
x_mir.Add(point);
}
Print("rotated " + x_mir.Count + " points");
A = x_mir;

Best,

M.

Thank you Marios.

My problem is solved.

Hi Mette,

    Unfortunately I don't remember what project I used that code on!

Could you tell me exactly what your problem is? I'd be glad to help!

Vern

It was so difficult to debug this. But this helped. I am glad to see this post.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service