Grasshopper

algorithmic modeling for Rhino

hey guys :)

here is the Problem with C#

I want to move a polyline or s.th else to each point of my point-list .

i used a loop and define a new polyline in Loop`sBody to have a different one each time  and change the vector along my Point`list and use Polyline.Transform()

But it transform all polyline with last vector :\

can i have a clone of my Moved polyline and add it to list?(consider i define new polyline each iteration but apparently it isnt Independent)

 

Views: 1955

Attachments:

Replies to This Discussion

suppress : Polyline ppl = new Polyline ();

suppress  : ppl = cr1 (I think you don't make a new object you copy a link, always the same ! )

Add :  Polyline ppl = new Polyline (cr1.ToArray());


foreach (Point3d pti in pts )
{
//define new polyline
Polyline ppl = new Polyline (cr1.ToArray());
//ppl = cr1;
// create translation vector
Vector3d vec1 = new Vector3d();
vec1 = pti - ptref;

// move new polyline ????
ppl.Transform(Rhino.Geometry.Transform.Translation(vec1));

crlis.Add(ppl);
}

// Tnx alot ;

You are welcome : 

something more generic for closed curve, it will work with rounded rectangle :

It uses AreaMassProperties for centroid calculation. So you will be able to feed every grasshopper curves.

private void RunScript(List<Point3d> pts, Curve cr1, ref object A)
{

// define refpoint for Vector Start
AreaMassProperties center = AreaMassProperties.Compute(cr1);
Point3d ptref = center.Centroid;
//create list for moved obj
List<Curve> crlis = new List<Curve> ();

foreach (Point3d pti in pts )
{
NurbsCurve ppl = new NurbsCurve(cr1.ToNurbsCurve());
Vector3d vec1 = new Vector3d();
vec1 = pti - ptref;
ppl.Transform(Rhino.Geometry.Transform.Translation(vec1));
crlis.Add(ppl);
}
//output
A = crlis;
}

Don't want to be a clever dick [...]

The compiler is a much more clever dick than you or I will ever be, but you are right that some changes will make the code both faster (fewer method invokes) and and easier to read. 

I'm a big fan of using descriptive variable names rather than cryptic ones, and always remember to check for possible nulls, it allows you to provide more useful error messages for when something goes wrong.

private void RunScript(List<Point3d> pts, Curve cr1, ref object A)
{

  if (pts.Count == 0) return;

  if (cr1 == null) return;

  AreaMassProperties area = AreaMassProperties.Compute(cr1);

  if (area == null)

    throw new ArgumentException("Area of cr1 could not be computed");

  Point3d centre = area.Centroid;
  List<Curve> curves = new List<Curve>(pts.Count);

  foreach (Point3d point in pts)
  {
    Curve duplicate = cr1.DuplicateCurve();
    duplicate .Transform(Transform.Translation(point - centre));
    curves.Add(duplicate );
  }
  A = curves;
}

why other classes like Polyline haven`t Duplicate Method? :/

is there any general method to clone an object in c#?

why other classes like Polyline haven`t Duplicate Method?

Polyline derives from List<Point3d>, so you can just new one up:

Polyline dup = new Polyline(otherPolyline);

is there any general method to clone an object in c#?

No, C# does not have a standard duplication/cloning system. There's an interface ICloneable but not all classes implement it, and at any rate it's not type-safe because it always returns a System.Object.

Classes in RhinoCommon that are difficult to duplicate (like most GeometryBase implementations) come with Duplicate methods, but that's not a particularly standard .NET approach.

Maybe the undertone good lost because of translation. Wasn't meant to be provocative

No worries, I got it :)

I know the garbage collection removes it, but that takes time either.

This incidentally is not (necessarily) true. Variables which are local to a method have well defined lifetimes and are cleaned up by the compiler. Only instances that might be shared in either a very complex or unpredictable pattern across multiple other instances need to be garbage collected.

As Eric Lippert once put it, the JIT compiler is in cahootz with the GC so they can optimize declaration and cleanup of a lot of memory allocations.

Or does the compiler knows to use only the same variable for each iteration even though your are declaring it within the loop?

Yes. The compiler may even choose to 'merge' various variables of similar type if the flow logic of the method precludes them interacting in any way.

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service