Grasshopper

algorithmic modeling for Rhino

Hi all,

I am a scripting neophyte, so excuse the simple question. I've been looking through the .NET SDK for Rhino and I don't understand how to use most of what I see, or whether it's even still valid now that RhinoCommon has been implemented.

Anyway, I am trying to grab objects from Rhino automatically based on the layer name. This is probably elementary code for you .NET wizards out there...

Thanks,
Marc

Views: 1394

Reply to This

Replies to This Discussion

Private Sub RunScript(ByVal layer As String, ByRef A As Object)
   Dim layer_index As Int32 = doc.Layers.Find(layer, True)
   If (layer_index < 0) Then Return

   Dim lay As Rhino.DocObjects.Layer = doc.Layers(layer_index)
   Dim objs As Rhino.DocObjects.RhinoObject() = doc.Objects.FindByLayer(lay)
   If (objs Is Nothing) Then Return

   Dim geo As New List(Of GeometryBase)
   For Each obj As Rhino.DocObjects.RhinoObject In objs
     geo.Add(obj.Geometry)
   Next

   A = geo
End Sub


Try this. A single String input for this script.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Thanks David, that works great.

One more thing... how would I also retrieve the layer color assigned to the layer (not the material but the display color)?

Thanks.

Marc
Layer has a Color property, so:

Dim layerColor As Color = lay.Color

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Wow, that was embarrassingly simple. Thanks.
how could this script be tweaked in order to assign a material then bake as opposed to assing a layer and baking
So, after some experimentation I see that the drawback of using this method is that the GH definition is not automatically updated when the gathered geometry is changed. I'm guessing that this is because of the way that Grasshopper is driven directly by referenced geometry as opposed to geometry that is gathered by the execution of a script.

Obviously the workaround is to set up a timer to continually check the geometry on the layers (executing the script on a regular basis), but then you get lag in the viewport, even with large timer intervals.

Are there any other solutions I'm missing? I'd like to have the definition rebuild every time one of the objects changes, but I don't want to force the user to reference the geometry manually in GH.

Thanks,
Marc
That is not the obvious workaround. The best solution in fact is not a workaround at all.

Instead of populating the Script node output with pure geometry, you should create actual Grasshopper Geometry data that knows it is linked to an actual object.

You can use the Kernel.GH_Convert.ToGeometricGoo() or Kernel.GH_Convert.ObjRefToGeometry() methods to convert a Rhino object ID into the appropriate Grasshopper Geometry Data instance.

This should ensure that changes to the original objects also cause updates in Grasshopper. What it won't do is update the definition if more objects are added to the layer. For this you should have event handlers that listen to layer and document changes.

Using timers is a terrible idea, almost always. Sometimes they are the only way to solve something, but you should definitely deplete any alternatives first.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
The workaround is only obvious because I had no clue how to do it properly. Anyway, if I knew how to use those methods I would definitely try it. :) (See next discussion topic.)
Event handlers listening to the layer to document changes, that sounds perfect. Is this just a couple lines of code? My first thought would be to use a handler to detect that a change has been made, and in such a case force GH to recalculate the solution. Or perhaps that is not as simple as it sounds?

Marc
btw,

When I try to use the methods you mention above, I get errors that "Convert is not a member of Kernel", and when I try to use Kernel.GH_Convert.ObjRefToGeometry() (somthing I found after some exploration), I get "Value of type 'Rhino.DocObjects.RhinoObject' cannot be converted to 'Rhino.DocObjects.ObjRef". Am I using the wrong method? I can't find just a simple Kernel.Convert in the script editor.

Thanks,
Marc
Marc,

sorry, you're right, it's supposed to be GH_Convert. I updated my original answer.

As for the second problem; RhinoObject is a different beast from ObjRef. You cannot supply a RhinoObject when an ObjRef is asked for. Luckily you can construct ObjRef from a RhinoObject:

Dim ref1 As New DocObjects.ObjRef(rhobj)
Dim ref2 As New DocObjects.ObjRef(rhobj.Id)


where rhobj is an instance of RhinoObject.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Okay, thanks for staying with me here... so far I've got the code in and I'm not getting any errors, but I'm not getting any updates when manipulating the Rhino geometry either...

Here is my code:


Dim layer_index As Int32 = doc.Layers.Find(layer, True)

If (layer_index < 0) Then Return

Dim lay As Rhino.DocObjects.Layer = doc.Layers(layer_index)
Dim objs As Rhino.DocObjects.RhinoObject() = doc.Objects.FindByLayer(lay)


If (objs Is Nothing) Then Return

Dim layerColor As Color = lay.Color
Dim geo As New List(Of GeometryBase)
For Each obj As Rhino.DocObjects.RhinoObject In objs
Dim ref1 As New DocObjects.ObjRef(obj)
Dim ref2 As New DocObjects.ObjRef(obj.Id)
Kernel.GH_Convert.ObjRefToGeometry(ref1)
geo.Add(ref1.Geometry)


Next

L = geo
col = layerColor

I'm thinking that the problem might be either in the way i'm using the convert method (probably wrong) or in the "geo.Add(ref1.Geometry)" line... Anyway, any help appreciated.

Thanks,
Marc

RSS

Translate

Search Grasshopper

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2014   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service