Custom display c# component with moving objects, meshes in motion

Hello, the problem is pretty simple ideed.
I made a C# component to move teapots on planes, but when I try to customize the displaying of theses meshes and wires things are getting kind of crazy :smiley:

The scene :

And when I try to move the objects…Actually kind of nice, but not exactely what I wanted :
Basically, Lines are doing trails, and meshes are flying all over the place.

only way to update the position is to delete the component and do ctrl-z to have it again.

Bonus questions :
After that main topic, I’ll have some questions that maybe some very talented people here could answer easily. The questions are market with a //???// into the script.

  • (line 110 to 132) Parsing is getting quite barbare :-o.
  • (line 154 to 187) Basically, I used a method but could’t call back the Plane inside.
  • (line 253) Converted two times the mesh instead of duplicate(); but duplicate don’t work into that context.

Also if you have some advices please let me know, because everything seems to be still in a “HardCoding fashion…”

Thanks a lot for your attention :slight_smile:

The file : (78.7 KB)

Sorry but I cannot debug this code. There’s too much of it and it’s too messy. You use identical names for class level variables and method arguments, the naming of variables is not at all self-documenting (ap1, ap2, aaa1, etc.) You’ll have to simplify your questions before I can answer them.

I suspect your problem is that you’re not duplicating your meshes enough. That your transformations somehow stack together because they are all accumulated into the same mesh. You’ll need to use DuplicateMesh() to create a copy of a mesh that you can then deform independently.

Messy yes (I tried looking into it as well, but no…).

I recall people hurting my feelings when I started programming. That pain did only good thing to me (to my programming).

@sato: I suggest you refactor the code. This is were you start your debugging:

// Rolf

Hello David, hello Rolf
First of all, thanks a lot for your answer and
Second of all ^^ Really sorry for the messy code,

But programming It’s a long way… Well you have to start somewhere. :yum:

I mean its already nice from you that you looked at it.
I re-made it completely and just enclosed the problem. It should be lot more clear now.

Still have the problem of the yellow lines that are not updating:

Creating new points to have another name than "Pts[0]" etc... Is it slower than using them directly by writing Pts[0],Pts[1],Pts[2]... ???
        for (int i = 0; i < iPoints.Count; i++)
        Point3d pt0 = Pts[0];
        Point3d pt1 = Pts[1];
        Point3d pt2 = Pts[2];

Question 2
What is the fastest way to use a copy of an embedded mesh in the same script ???

    Mesh teaPotMesh2 = teaPotMesh.Duplicate(); //Don't work :-/
    GeometryBase teaPotMesh2 = teaPotMesh.Duplicate(); //Worked but I need a mesh. And when I want to convert it I'm getting the error bellow
    teaPotMesh2 = GH_Convert.ToMesh; //Don't work either. getting : 2. Error (CS1503): cannot convert from 'Rhino.Geometry.GeometryBase' to 'Rhino.Geometry.Mesh'

I tried to be as clear as possible. It it is still messy let me know I’m learning every day :slight_smile:

Thanks again for your help.

File v2: (45.1 KB)

Copy will typically be slower when the type is a value type. But more important is that you are aware of that if you update the copy (i.e “pt0” in your code above), that doesn’t mean that you have updated the value in Pts[0]. This may be a problem if you plan to use the point in the list (Pts[0]) later in your code, and expect that it has been updated if you update the pt0.

Look at the following picture. It shows the output of Pts[0] and pt0 after all the values of pt0 are set like so: (1,1,1) while Pts[0] keeps it’s original values, which is (0,0,0):


And here’s the test code snippet so you can se for yourself what result you (may) expect, and the actual result (pictured above) :

private void RunScript(ref object OUT_Pts, ref object OUT_pt0)
    var Pts = new List<Point3d>();
    Pts.Add(new Point3d(0, 0, 0));

    // Now copy the point
    var pt0 = Pts[0];

    // ... and modify its value
    pt0.X = 1;
    pt0.Y = 1;
    pt0.Z = 1;

    // and compare the two if they still are equal
    OUT_Pts = Pts;
    OUT_pt0 = pt0;

At the moment I don’t have the time to look deeper into your code (but someone else may have an extra minute or two), but check your code if you have this bug (expecting to have updated point residing in a list while the changes were made only on the copies).

If you did this mistake I say only; welcome to the club. You’re not alone. :slight_smile:

In any case, you asked about copying values. Feel free to do so, but watch out for making copies of value types if a copy is not what you expect (Point3d is not a class, it’s a struct, which is a value type, and thus it gets copied, not referenced).

Regarding copying Meshes, I most often copy meshes like so:

var meshcopy = OriginalMesh.DuplicateMesh();

That’s equivalent to this:

Mesh meshcopy = OriginalMesh.DuplicateMesh();

… but the compiler will figure out that the DuplicateMesh() command will return a Mesh type, and so the “var” will actually become a Mesh.

Same thing with the first example above; the compiler deduces that “pt0” needs to be a Point3d.

Happy coding!

// Rolf

Thanks Rolf,

Nice exemple to make things easy to understand.
Of course the output points will be differents because :
“OUT_Pts” is obviously the Input.
“OUT_pt0” is the new Point3d.

Yes… I wasn’t able to find a solution for lines strange windowsXp stacks like ^^

But !! I replicate the problem with your exemple, than we really enclose that problem now.
Should be easy to find what is wrong.

I think the problem is the way I transform the points… Maybe I should transform.translate instead of duplicate them.
I’m getting a bit confuse with this points right now ^^
If somebody has an Idea would be nice to show up.

Thanks again :-).

The File : (5.2 KB)

In order to suppress yellow lines, I did that and it seems to works. Without Garantee !
click on SolveInstance Overrides

oO its working !!!
Merci Laurent :slight_smile:

But is this the most efficient way to do that ? it seems like… I made a mistake but I repair it with a “myUglyLines.Clear();” After to clean the mess ^^

Thanks a lot but still there is a thing that I don’t understand, is there anyway to properly transform the points and then maybe you don’t need the Clear(); method anymore ?

Thanks a lot guys for your help

Last version with Laurent’s fix : (6.8 KB)

As yellowWiresPts is a list, is not placed in RunScript and there is an add it means that at each click there was points added. So the list keep growing. If no need of list a buffer must be used to keep track of data wanted.

Thanks for the clear explanation :slight_smile:
Do you have an idea for that mesh ?
Wich is the fastest way to use a mesh two times within the same script ?
I tried duplicate() but it gives a GeometryBase wich is not a mesh that can be used into
"public override void DrawViewportMeshes(IGH_PreviewArgs args)"


You can use Mesh.DuplicateMesh() Method it outputs Mesh!
I don’t know you but I uploaded a chm file in order to have Grasshopper and Rhinocommon helps all the time at a click and without internet.

Ok strange confusion there : I was using Duplicate() it should be the same as “DuplicateMesh()”
But DuplicateMesh() work and not Duplicate().

Thanks again for the help

It is the same but as one is a derivate class it has to output a more general class. You can surely cast the output with (Mesh)Mesh.Duplicate().
Surely not all the good words for Object Oriented Programming.

I already had the Grasshopper SDK but is it possible to download the RhinoCommon SDK to have it also offline ?

See there, it seems the CHM is just for Rhino5 !