algorithmic modeling for Rhino
Sorry if these are VB 101 kinds of questions, but how do I access an object's properties? I would like to extract an object's user-defined name, for instance. Here are a few examples of things that don't work (where Obj is my input of objects):
Dim objName As String = Obj.Name
Dim ObjName As String = Rhino.DocObjects.RhinoObject.Name(Obj)
I'm having the same problem with Materials. If I use Obj as my input for Objects, how do I access the material that has been assigned in the Rhino environment?
Still working through VB.NET School on Programmer's Heaven. Unfortunately it seems even the most basic things elude me...
The ObjectAttributes class is what you're looking for.
Anyone with a basic knowledge of .NET would probably be more than satisfied with your answer... unfortunately, this horse is not smart enough to know when it's being led to water.
I try things like:
Dim S As String = Rhino.DocObjects.RhinoObject(Obj).Attributes.Name
Dim att As Rhino.DocObjects.RhinoObject(Obj)
Dim S As String = att.Name
and get nowhere... I feel like a guest on J-Pop America Fun Time Now. VB.NET-uru! :P
Provided you've already got your object (this is actually the hardest part), the following code will work:
Dim S = obj.Attributes.Name
If you are having trouble getting at an object, try the methods doc.Objects.Find, doc.Objects.FindByLayer, or doc.Objects.FindByFilter (there are also a few others).
I am more comfortable with C#, so this may not be a totally ideal way, but a working VB.net script to get the names of a set of objects on a layer would look like this:
Dim objs() As Rhino.DocObjects.RhinoObject = doc.Objects.FindByLayer("Layer Name")
Dim names(objs.Length - 1) As String
For i As Integer = 0 To objs.Length - 1
names(i) = objs(i).Attributes.Name
A = names
I guess the getting of the objects into the right type is really the issue. I am simply passing the objects into an input, I'd rather not call by layer. I tried casting as compatible type but that didn't work:
Private Sub RunScript(ByVal Obj As Object, ByRef Name As Object)
Dim objs As CType(Obj, Rhino.DocObjects.RhinoObject)
Dim names As String
names = objs.Attributes.Name
Name = names
Not sure how I use the Find methods using the geometry as input...
Remember that the GH types are not necessarily the types in Rhino, and that an object in GH doesn't necessarily exist in Rhino. The best, most reliable, and fastest way to access a Rhino Object from GH is to do so by Guid. Trying to do it by geometry is not recommended, and Rhino wasn't really designed to find an object based on its geometry (although it could).
Assuming you have a Guid, the following will get you your Rhino object
Dim myRhObj as RhinoObject = doc.Objects.Find(aGuid)
From there you'll be able to access the Attributes property on it and extract the name similar to how I wrote before.
Dim myObjName As String = myRhObj.Attributes.Name
I would advise against trying to cast any object that you get into your GH scripting component into something like a RhinoObject as it simply won't work. Its an indication that you're probably not working with the right information to start with as casting isn't all that necessary in the majority of situations, especially one that seems simple and straightforward (because it probably is).
Remember that the DocObjects table (Rhino.DocObjects) is a collection (/list/array) of the rhino objects that are in the document. When you deal with this table, the main idea is to add objects into or pull them out of it. Its best to use one of the Find methods to pull objects out, and depending on which method you get back, you may get a single RhinoObject or an array of RhinoObjects.
Thanks guys... I think I'm finally getting it. I recently read a thread where David explained that geometry is converted to pure geometry before entering the script component, so I decided to add another input for Guid and use that to call my geometry (rather than trying to cast). The Guid type hint does the conversion automatically and I can encapsulate in a User Object to get a nice clean component.
Thanks for the help... toughing it out to do simple things like this is the only way I'm going to learn...
Private Sub RunScript(ByVal obj As String, ByVal id As Guid, ByRef name As Object)
Dim nObj As Rhino.DocObjects.RhinoObject = Doc.Objects.Find(id)
Dim oName As String = nObj.Name
name = oName