Grasshopper

algorithmic modeling for Rhino

Hi

I am writing a component in Visual Studio to find a color value of an inputted object. I have it working in gh vb.script:

Dim obj As Rhino.DocObjects.RhinoObject = doc.Objects.Find(id)
If (obj Is Nothing) Then
col = Color.Transparent
Else
col = obj.Attributes.ObjectColor
End If

But while i rewrite it to Visual Studio it seems to create some problem with inputting object GUID. Can someone help me?

Views: 910

Replies to This Discussion

Can someone help me?

Not without some code to compile. What does your RegisterInputParams() method look like? What does your SolveInstance() method look like?

Generally it is not working, so i assume that there is a lot wrong there.

With inputs i tried both makin GUID and just inputting object, both without succes:

pManager.AddIntegerParameter("GUID", "ID", "GUID(ID) of an object", GH_ParamAccess.item, 10)
pManager.AddGeometryParameter("Object", "O", "Object to get color from", GH_ParamAccess.item)

With solve instance it looks like this (messy):

Protected Overrides Sub SolveInstance(DA As IGH_DataAccess)

Dim iddd As Int32 = 0
Dim objj As GeometryBase
Dim idx As Guid

If (Not DA.GetData(0, iddd)) Then Return
If (Not DA.GetData(1, objj)) Then Return

Dim obj As Rhino.DocObjects.RhinoObject



idx = obj.Id
obj = Rhino.RhinoDoc.ActiveDoc.Objects.Find(iddd)

Dim col As New Drawing.Color

If (objj Is Nothing) Then
col = Drawing.Color.Transparent
Else
col = obj.Attributes.ObjectColor
End If

DA.SetData(0, col)

End Sub

Public Overrides ReadOnly Property ComponentGuid() As System.Guid   
Get
'Don't copy this GUID, make a new one
Return New Guid("419c3a3a-cc48-4717-8cef-5f5647a5ecfc")
End Get
End Property

Have you replaced the GUID with a new GUID using a GUIDGen, which you can download from Microsoft(https://www.microsoft.com/en-us/download/confirmation.aspx?id=17252)?

Hi Aleksander,
concerning the first line only in your code: the input type for a guid should not be Integer. I think if you choose type String you'll be able to cast that succesfully to a guid.

There is also a Guid parameter. You should probably use that one.

No, scratch that. You don't need the Guid parameter at all. You only need the geometry param. I'll whip up some code.

Oookay... code is utterly untested. Hopefully it will at least tell you what the approach should be:

Public Class ColourFromObjectComponent
Inherits GH_Component

Public Sub New()
MyBase.New("Colour From Object", "Obj2Col", "Extract the colour a referenced object is drawn in", "Display", "Colour")
End Sub

Public Shared ReadOnly Id As Guid = New Guid("{9BD61B9C-C186-4CC9-8B91-C63829563997}")
Public Overrides ReadOnly Property ComponentGuid As Guid
Get
Return Id
End Get
End Property
Public Overrides ReadOnly Property Exposure As GH_Exposure
Get
Return GH_Exposure.primary
End Get
End Property

Protected Overrides Sub RegisterInputParams(pManager As GH_InputParamManager)
pManager.AddGeometryParameter("Object", "O", "Object to examine", GH_ParamAccess.item)
End Sub
Protected Overrides Sub RegisterOutputParams(pManager As GH_OutputParamManager)
pManager.AddColourParameter("Object", "O", "Object colour", GH_ParamAccess.item)
pManager.AddColourParameter("Display", "D", "Display colour", GH_ParamAccess.item)
End Sub

Protected Overrides Sub SolveInstance(DA As IGH_DataAccess)
Dim geo As IGH_GeometricGoo = Nothing
If (Not DA.GetData(0, geo)) Then Return

If (Not geo.IsReferencedGeometry) Then
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Geometry is not referenced from Rhino")
DA.SetData(0, Color.Black)
DA.SetData(1, Color.Black)
Return
End If

Dim id As Guid = geo.ReferenceID
Dim doc As Rhino.RhinoDoc = Rhino.RhinoDoc.ActiveDoc
Dim obj As RhinoObject = doc.Objects.Find(id)
If (obj Is Nothing) Then
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Object could not be found in the current Rhino document")
DA.SetData(0, Color.Black)
DA.SetData(1, Color.Black)
Return
End If

DA.SetData(0, obj.Attributes.ObjectColor)
DA.SetData(1, obj.Attributes.DrawColor(doc))
End Sub
End Class

Ah, yes, now I see, thanks David - and thanks to Dani for demonstrating how to implement that. (I was looking for pManager.AddGuidParameter or something like that...  Doh!)

There should have been an AddGuidParameter, but I never added that method.

That was my initial source of concern. I realised that there is no such thing as AddguidParameter and there I started to tweak randomly. Thank you guys! In the evening I will test all of these

Protected Overrides Sub RegisterInputParams(pManager As GH_Component.GH_InputParamManager)
pManager.AddParameter(New Grasshopper.Kernel.Parameters.Param_Guid(), "Name", "Nickname", "description", GH_ParamAccess.item)
End Sub


Protected Overrides Sub RegisterOutputParams(pManager As GH_Component.GH_OutputParamManager)
pManager.AddColourParameter("Name", "nick", "Des", GH_ParamAccess.item)
End Sub


Protected Overrides Sub SolveInstance(DA As IGH_DataAccess)
Dim id As Guid = Guid.Empty
Dim col As Color = Color.Empty

If Not (DA.GetData(0, id)) Then Exit Sub

Dim robj As Rhino.DocObjects.RhinoObject = Rhino.RhinoDoc.ActiveDoc.Objects.Find(id)
If (robj Is Nothing) Then
col = Color.Transparent
Else
col = robj.Attributes.ObjectColor
End If

DA.SetData(0, col)
End Sub

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