Grasshopper

algorithmic modeling for Rhino

Hi all,

sorry if it's a stupid question ... I'm confused.

Is there a clean, generic way to duplicate RhinoCommon structure instances ?

In particular I'm talking about Rhino.Geometry structures.

Since in Python assigning a structure instance to a variable does not create a new instance, I'm looking for an easy way to duplicate that instance.

Some structures have what I might call copy-constructors:

I can build a new Point3d from another Point3d, same for Plane etc ...

Some other structures lack that. There is no constructor to build a Line from a Line, or an Arc from an Arc, etc.   Nor are there Duplicate() methods for them.

What should I do to duplicate an Arc ?

Thanks

emilio

Views: 2213

Replies to This Discussion

Hi Emilio,
this is not a stupid question. You're talking about a so called deep-copy of an object. You can call it a copy-constructor but Point3d and Plane are an Exception for this type of constructors.
All GeometryBase based classes:
have inherited the method  Duplicate(), which makes a full (deep) copy of that object and returns a GeometryBase object. For all other types like Line, Arc, Circle, there are no comparing methods.
But you can cast a line/arc/circle/ellipse to a NurbsCurve duplicate that one. Later on you can convert the obtained GeometryBase object back to your desired type with these Methods:

TryGetArc(Arc, Double), TryGetCircle(Circle), TryGetEllipse(Ellipse).

I hope this answers your question.

Cordiali saluti,

FF

Hi Florian,

many thanks for your clear explanation !

Indeed I copied the arc by an instruction like:

    newarc = Rhino.Geometry.ArcCurve( oldarc ).Arc

I'm glad to see that it's also what you suggest.  :)   ( I wasn't sure about it )

But I'm not glad to learn that there is no better way ...  ;)

... Having to create a class instance to copy a structure instance feels somewhat strange ...

OK, problem solved.

Thanks again !

Saluti

emilio

Emilio,

if oldarc is of type Rhino.Geometry.Arc, you can just assign it directly and a copy will be made:

Arc newArc = oldArc;

oldArc.Radius = 5;

at this point newArc will still have the old radius.

--

David Rutten

david@mcneel.com

Tirol, Austria

Hi David,

please correct me if I'm wrong.

I think that copy by assignment for structures works fine in VB and C#, but I just learned (the hard way ...) that Python works differently.

It seems that structure instances are copied by reference, just like class instances ...

Thanks for replying !

emilio

I suppose that a generic Python specific approach would be to use the copy module. See attached file. Also, really great post Florian.

Attachments:

That's awesome...these kind of things are the reason people should do python instead of vb/c# ;-)

Thanks Anders

Careful now, no worries ;)

Hi Anders,

nice idea ! Works fine.  :)

Thanks a lot !

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service