Grasshopper

algorithmic modeling for Rhino

Following this post
I could use the functions of a pluggin in rhino grasshopper components.

Only the method used does not allow me to manage one object at a time.
If there are several objects then the command is repeated as many times as object.

I wish to send by this method a list of objects.

Exemple:

Private Sub RunScript(ByVal x As Mesh, ByVal y As Mesh, ByRef A As Object) 
'Add the objects to Rhino
Dim idMeshA As Guid = doc.Objects.AddMesh(x)
Dim idMeshB As Guid = doc.Objects.AddMesh(y)
Dim runtimeEvent As UInteger = New Rhino.DocObjects.ObjRef(idMeshB).RuntimeSerialNumber

'Create the command macro
Dim command As String = "MeshBooleanDifference"
Dim macro As String = String.Format("!_-{0} _SelID ""{1}"" _Enter _SelID ""{2}"" _Enter", command, idMeshA, idMeshB)

'Run the macro
Rhino.RhinoApp.RunScript("_SelNone", False)
Rhino.RhinoApp.RunScript(macro, True)

'Harvest new meshes
Dim objs As Rhino.DocObjects.RhinoObject() = doc.Objects.AllObjectsSince(runtimeEvent)
Dim rc As New List(Of Mesh)
For Each obj As Rhino.DocObjects.RhinoObject In objs
Dim m As Mesh = Nothing
GH_Convert.ToMesh(obj.Geometry, m, GH_Conversion.Both)

If (m Is Nothing) Then Continue For
rc.Add(m.DuplicateMesh())

doc.Objects.Delete(obj.Id, True)
Next

A = rc
End Sub

Views: 1511

Replies to This Discussion

Then X and/or Y need to be set to List access in the menu. This will turn them from ByVal x As Mesh into ByVal x As List(Of Mesh) which then allows you to iterate over them.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Hi David,

In searching the forum, I found this code that works well with a list of objects or a logic tree object.

I tried to adapt your code, but I did not succeed.

I do not know if the Guid class can be called by a list.

Thanks

David and you can return to this subject?
Thank you in advance.

David, thank you for answered, I was afraid that the post goes forgot.
I'm Passing a list or tree access but the component not work.
Would it be possible to modify the code that you wrote, with the y as the list access Mesh.
If you want write explain, it would be very nice.


I think it would be more useful to use the access tree.
Regards

Can you post the file with the component, ideally cut down to the simplest case that does not work?

 

Unfortunately I've totally broken the VB/C# components at present but I might be able to get them semi-working in a few hours.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thank you for your reply david.


Here is a simple example that I have done with the command mesh union.

You will quickly understand my expectations.

When there are multiple objects, the VB component performs the function for the first, second and so on. But he does not understand that he must perform the function for all objects at the same time.

I hope to be clear.

This simple example should be replicated to a component using curves and points (Delaunay mesh).
Besides, I do not know if I pass by a component with a single parameter of type "system.objects" or a component with two parameters (first: point, second: curve).

I can help as well?

Attachments:

Since you want to operate on multiple meshes, you have to set the input to List instead of Item. The following source assumes a single input called M with List access and Mesh typehint:

'Iterate over all meshes and add them all to the document.
Dim ids As New List(Of Guid)
For Each mesh As Mesh In M
ids.Add(doc.Objects.AddMesh(mesh))
Next

'Get the runtime number of the mesh that was added last.
Dim runtimeNumber As UInt32 = New ObjRef(ids(ids.Count - 1)).RuntimeSerialNumber

'Create a collection of SelID commands.
Dim selID As New System.Text.StringBuilder()
For Each id As Guid In ids
selID.AppendFormat("_SelID ""{0}"" ", id)
Next

'First make sure everything is deselected.
Rhino.RhinoApp.RunScript("-_SelNone", False)
Rhino.RhinoApp.RunScript(String.Format("-_MeshBooleanUnion {0}_Enter", selID), False)

'Harvest all new meshes
Dim objs As RhinoObject() = doc.Objects.AllObjectsSince(runtimeNumber)
Dim result As New List(Of Mesh)
For Each obj As RhinoObject In objs
Dim mesh As Mesh = TryCast(obj.Geometry, Mesh)
If (mesh IsNot Nothing) Then result.Add(mesh.DuplicateMesh())
Next

'Delete all objects that were added.
doc.Objects.Delete(ids, True)
For Each obj As RhinoObject In objs
doc.Objects.Delete(obj, True)
Next

A = result

Unfortunately I haven't been able to get the script components working again, so the above code is untested.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks David,

I thought that he had to go through a list.
I have 4 errors due to the type objRef which he said is not defined.

Should I declare it as a variable?

No you'll need to prefix those classes with the namespaces they are in as it seems they are not imported.

So convert ObjRef to Rhino.DocObjects.ObjRef and convert RhinoObject to Rhino.DocObjects.RhinoObject.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Sorry David,

That's my correction:


'Iterate over all meshes and add them all to the document.
Dim ids As New List(Of Guid)

For Each mesh As Mesh In M
ids.Add(doc.Objects.AddMesh(mesh))
Next

'Get the runtime number of the mesh that was added last.
Dim runtimeNumber As UInt32 = New Rhino.DocObjects.ObjRef(ids(ids.Count - 1)).RuntimeSerialNumber

'Create a collection of SelID commands.
Dim selID As New System.Text.StringBuilder()
For Each id As Guid In ids
selID.AppendFormat("_SelID ""{0}"" ", id)
Next

'First make sure everything is deselected.
Rhino.RhinoApp.RunScript("-_SelNone", False)
Rhino.RhinoApp.RunScript(String.Format("-_MeshBooleanUnion {0}_Enter", selID), False)

'Harvest all new meshes
Dim objs As Rhino.DocObjects.RhinoObject() = doc.Objects.AllObjectsSince(runtimeNumber)
Dim result As New List(Of Mesh)
For Each obj As Rhino.DocObjects.RhinoObject() In objs
Dim mesh As Mesh = TryCast(obj.Geometry, Mesh)
If (mesh IsNot Nothing) Then result.Add(mesh.DuplicateMesh())
Next

'Delete all objects that were added.
doc.Objects.Delete(ids, True)
For Each obj As Rhino.DocObjects.RhinoObject() In objs
doc.Objects.Delete(obj, True)
Next

A = result

Return:

For Each obj As Rhino.DocObjects.RhinoObject() In objs

I didn't have those parenthesis after RhinoObject(). You turned a regular class into an array.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

The final code:

'Iterate over all meshes and add them all to the document.
Dim ids As New List(Of Guid)
For Each mesh As Mesh In M
ids.Add(doc.Objects.AddMesh(mesh))
Next

'Get the runtime number of the mesh that was added last.
Dim runtimeNumber As UInt32 = New Rhino.DocObjects.ObjRef(ids(ids.Count - 1)).RuntimeSerialNumber

'Create a collection of SelID commands.
Dim selID As New System.Text.StringBuilder()
For Each id As Guid In ids
selID.AppendFormat("_SelID ""{0}"" ", id)
Next

'First make sure everything is deselected.
Rhino.RhinoApp.RunScript("-_SelNone", False)
Rhino.RhinoApp.RunScript(String.Format("-_MeshBooleanUnion {0}_Enter", selID), False)

'Harvest all new meshes
Dim objs As Rhino.DocObjects.RhinoObject() = doc.Objects.AllObjectsSince(runtimeNumber)
Dim result As New List(Of Mesh)
For Each obj As Rhino.DocObjects.RhinoObject In objs
Dim mesh As Mesh = TryCast(obj.Geometry, Mesh)
If (mesh IsNot Nothing) Then result.Add(mesh.DuplicateMesh())
Next

'Delete all objects that were added.
doc.Objects.Delete(ids, True)
For Each obj As Rhino.DocObjects.RhinoObject In objs
doc.Objects.Delete(obj, True)
Next
A = result

It's good. Thanks david.

Can you tell me why in the case of a rhino file including linkages, they are included in the list of final component VB?

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