Grasshopper

algorithmic modeling for Rhino

Hi All:

I've been trying to figure how to translate a Bake w/Color button that I picked up somewhere along the way to Rhinocommon and I'm having some difficulties. Any ideas would be greatly appreciated.

Three inputs:
blnBake, boolean
obj, Breps
color, corresponding colors for each Brep

Code:
If blnBake Then

Dim att As New Rhino.DocObjects.ObjectAttributes()
att.ObjectColor = color

Dim mObj As Guid
mObj = doc.Objects.AddBrep(obj, att)

End If

It bakes the geometry but does not include the colors. The script I was trying to tweak is below. It changes the Breps' colors after they've been baked but I couldn't get any results trying to translate. The real problem I'm having seems to be finding equivalent properties/methods.

Old Code:

If blnBake Then


Dim mObj As New MRhinoBrepObject
mObj = doc.AddBrepObject(obj)

Dim att As New MRhinoObjectAttributes(mObj.Attributes())
att.SetColorSource(1)
Dim rColor As New OnColor (color) 
 att.m_color = rColor              

Dim objref As New MRhinoObjRef(mObj.Attributes.m_uuid)
doc.ModifyObjectAttributes(objref, att)

End If

Any help is greatly appreciated!
Cheers,

Views: 3574

Replies to This Discussion

Your first code snippet is like 99% correct. The reason why your not seeing the color is because the attributes still have the color source set to layer. Add the following line before you set the color and you should be fine.

att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
Wow, I was closer than I thought.

Thanks a lot for the help!
Hi everybody -

Thanks to everybody who has commented on these VB posts regarding baking... at long last I've cobbled together bits and pieces of code from the various forum posts, and after digging into the RhinoCommon SDK, I have developed a working Bake Tool including layer names. If the layer exists, the components bakes to that layer, if it doesn't exist, it creates it, and if the value is blank, it bakes to the current layer. Also outputs a string telling you what type of geometry was baked.

See working code below, let me know if you see any errors, or if there are any ways to improve or condense it. I know it's pretty basic for experienced coders, but it was a milestone for me, at least. I think I'm starting to grasp basic RhinoCommon code... until the next project. :)


----

Private Sub RunScript(ByVal blnBake As Boolean, ByVal obj As Object, ByVal color As Color, ByVal layer As String, ByRef A As Object, ByRef type As Object)
If blnBake Then


Dim att As New Rhino.DocObjects.ObjectAttributes()
Dim t As String
att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
att.ObjectColor = color

If String.IsNullOrEmpty(layer) Then
att.LayerIndex = doc.Layers.CurrentLayerIndex

Else

Dim layer_index As Integer = doc.Layers.Find(layer, True)
If layer_index >= 0 Then
att.LayerIndex = layer_index

Else
layer_index = doc.Layers.Add(layer, System.Drawing.Color.Black)
att.LayerIndex = layer_index

End If

End If

If TypeOf obj Is Brep Then
Dim mObj As Guid
mObj = doc.Objects.AddBRep(obj, att)
t = "BRep"
type = t
End If

If TypeOf obj Is Curve Then
Dim mObj As Guid
mObj = doc.Objects.AddCurve(obj, att)
t = "Curve"
type = t
End If

If TypeOf obj Is Line Then
Dim mObj As Guid
mObj = doc.Objects.AddLine(obj, att)
t = "Line"
type = t
End If


If TypeOf obj Is Point3d Then
Dim mObj As Guid
mObj = doc.Objects.AddPoint(DirectCast(obj, Rhino.Geometry.Point).Location, att)
t = "Point"
type = t
End If



End If

End Sub
Incidentally, I find that the following code did not work for 3d points:

mObj = doc.Objects.AddPoint(DirectCast(obj, Rhino.Geometry.Point).Location, att)

Instead, I simplified to:

mObj = doc.Objects.AddPoint(obj, att)

I had made the change because of someone having a problem with this simple line of code in another thread... but it turns out it works for me in this case.

Marc
thanks for sharing your work

congrats!

Hi Marc,

 

I am trying to understand your code. The logic of the steps are clear to me, but there is a -somehow- technical detail that i cant resolve:

 

When you use doc.objects.addpoints(...) i understand that "Doc" is the name of a class, but i cannot find this class anywhere in rhinocommon sdk (I limit my search to those namespaces that the vb componets imports by default, assuming  this -hypothetical- class should be there:

Imports Rhino
Imports Rhino.Geometry
Imports Rhino.Collections

 

but nothing, cant find "doc" class anywhere. Where is the mistake?

 

thanks

leceta

 

doc is declared inside the Grasshopper Script component. Have a look at the Members section in the Script_Instance class. 

 

I basically put it there so you don't have to call RhinoDoc.ActiveDoc all the time.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

i see...

My missunderstood was that doc it´s not the actual name of a class, but of an instance of the property (¿shouldnt be a class?) ActiveDoc inside the class RhinoDoc under the namespace rhino. Is this right?

Should i considere that a property of a class can be in turn a class itself.

 

Effectively i´m just starting with OOP

 

I also get into account that this was explained -implictly- in Rajaa´s introduction to vb.net. (pg.99)

 

thanks for the clarification.

leceta

This is correct. Rhino is the main Namespace in our SDK. There's other namespaces inside Rhino, for example Rhino.Geometry and Rhino.Collections. Every type in .NET is part of some namespace. Namespaces only have one raison d'être, which is to group types into logical chunks.

 

A Type is the definition of a class or a structure. Classes and structures are very similar entities (they can both contain any number of methods, properties and other types), but because they behave differently, you need to know which is which.

 

An Instance of a type is a single object that actually exists. For example, the class Human is the definition of what it means to be human. You are an instance of this class. I am a different instance of this class. Damien is yet another instance of this class. Every instance can assign unique values to all its properties and fields. If for example the type Human had a property for height, then in my case it would say 1.97m. In Damien's case it would be set to something much less, as Damien isn't anywhere near as handsome and tall as I am.

 

A Reference is a piece of information that tells you where a certain instance is. Think of a reference as an address. When you mail a letter (or e-mail), you must specify a valid and unambiguous address or the letter won't arrive. Similarly, every piece of data stored in the memory of your computer also has a unique address, but instead of "3670, Woodland Park Avenue, Seattle", it looks like this 0x12345678. Trying to access an instance through an invalid reference is like sending a letter without an address.

 

References are sometimes also called "variables", though sometimes the word variable is only used to indicate primitive types, such as Booleans, Integers and such. A reference is not the instance itself. Take the following code:

 

1. Dim crv As Rhino.Geometry.Curve

2. Dim dup As Rhino.Geometry.Curve

3. crv = GetACurveFromSomewhere()

4. dup = crv

5. If (dup IsNot Nothing) Then dup = dup.DuplicateCurve()

 

One lines (1) and (2) I declare two references, one is called "crv", the other "dup". Because Visual Basic is a strongly typed language, I can assign limitations to what sort of data crv and dup are allowed to reference. In this case they can only point to instances of the Rhino.Geometry.Curve class or any class which derives from Rhino.Geometry.Curve (such as Rhino.Geometry.NurbsCurve, or Rhino.Geometry.LineCurve).

 

On line (3) I assign an actual curve instance to the crv reference/variable. At least, assuming the GetACurveFromSomewhere() function actually returns a proper instance. If it doesn't, crv will remain a "null reference".

 

Line (4) is interesting, because both crv and dup will now point to the same curve instance. So even though there is only 1 curve in memory, both crv and dup provide access to it. This means that when we change the curve via crv, then dup will notice that change.

 

On line (5) two things happen. I want to duplicate my curve data so I can change the data via dup, without affecting the data available via crv. The best way to duplicate a curve (i.e. create a second curve in memory, that has the same shape as the first curve) is to use the DuplicateCurve() method on the Rhino.Geometry.Curve class. However I cannot call a method on an instance that doesn't exist, so before I do that, I need to check whether or not dup actually points to a real curve object, or whether it is a null reference.

 

Finally, it is also possible to have an instance of a class in memory, without any references to it. In this case nobody can reach that curve any more and it is essentially dead weight, taking up pointless memory. In C++ this is called a Memory Leak and it's considered a serious bug. In VB.NET and C# this memory will automatically be cleaned up by the .NET Garbage Collector. In my opinion the Garbage Collector is the single most important feature in .NET. It's what turns VB and C# from frustration central into a friendly and flexible coding platform.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

The bake color component I have still works in 8.0050.
It's the bakemostattributes by Giulio

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service