Custom Grasshopper button error


#1

Hey everyone,

I am trying to develop a custom button for Grasshopper through the Grasshopper add-on in Visual Studio 2010. I am using Rhino 5 and Grasshopper Version 0.9.0056.

The script works in the custom VB.net function. The script also has no errors within Visual Studio. However, when I build the button and use it in Grasshopper, it error’s: invalid cast: integer>>Double. I have double checked the script and there are no instances where an integer is dim as double or used as a double.

Here’s the Script:

Imports System.Collections.Generic

Imports Grasshopper.Kernel
Imports Rhino.Geometry

Public Class Mesh
Inherits GH_Component

Public Sub New()
    MyBase.New("Mesh", "M", "Mesh Object", "Mesh", "Mesh")
End Sub


Protected Overrides Sub RegisterInputParams(ByVal pManager As GH_Component.GH_InputParamManager)
    pManager.AddMeshParameter("Mesh", "M", "Approximation Mesh", GH_ParamAccess.item)
    pManager.AddSurfaceParameter("Surface", "S", "Surface Being Approximated", GH_ParamAccess.item)
    pManager.AddIntegerParameter("Weight", "W", "Slider determining the amount of movement", GH_ParamAccess.item)
    pManager.AddIntegerParameter("Iterations", "I", "Slider determining the number of iterations", GH_ParamAccess.item)

End Sub

Protected Overrides Sub RegisterOutputParams(ByVal pManager As GH_Component.GH_OutputParamManager)

    pManager.AddMeshParameter("Mesh", "M", "Approximated Mesh", GH_ParamAccess.item)
    pManager.AddVectorParameter("MovementVector", "U", "The movement vectors of the mesh vertices", GH_ParamAccess.list)
    pManager.AddPointParameter("Vertices", "V", "Mesh Vertices", GH_ParamAccess.list)

End Sub

Protected Overrides Sub SolveInstance(ByVal DA As IGH_DataAccess)

    Dim mesh As New Rhino.Geometry.Mesh
    Dim surface As Rhino.Geometry.Surface
    Dim weight As Integer
    Dim Iterations As Integer

    If (Not DA.GetData(0, mesh)) Then Return
    If (Not DA.GetData(1, surface)) Then Return
    If (Not DA.GetData(2, weight)) Then Return
    If (Not DA.GetData(3, Iterations)) Then Return

    If (Not mesh.IsValid) Then Return
    If (Not surface.IsValid) Then Return

    Dim pts As New List(Of Point3d)
    Dim pts2 As New List(Of Point3d)
    Dim clist As New List(Of Vector3d)
    Dim new_mesh As New Rhino.Geometry.Mesh
    new_mesh = mesh

    Dim array() As Integer

    For j As Int32 = 0 To Iterations

        clist.Clear()
        pts.Clear()

        For i As Int32 = 0 To new_mesh.Vertices.Count - 1

            Dim pt1 As New Point3d
            pt1 = new_mesh.Vertices(i)

            intersection(pt1, surface, pts2)

            Dim pt2 As New Point3d
            pt2 = pts2(0)

            Dim vect1 As New Vector3d
            vect1 = (pt2 - pt1) * weight / 5

            array = new_mesh.Vertices.GetConnectedVertices(i)

            If array.Length > 3 Then

                Dim v1 As Vector3d, v2 As Vector3d, v3 As Vector3d, v4 As Vector3d
                Dim v_left As Point3d, v_right As Point3d, v_top As Point3d, v_bottom As Point3d

                v_bottom = new_mesh.Vertices(array(0))
                v_left = new_mesh.Vertices(array(1))
                v_right = new_mesh.Vertices(array(2))
                v_top = new_mesh.Vertices(array(3))

                v1 = pt2 - v_bottom
                v2 = pt2 - v_left
                v3 = pt2 - v_right
                v4 = pt2 - v_top

                If ((v1.X ^ 2 + v1.Y ^ 2 + v1.Z ^ 2) ^ 0.5 < (v2.X ^ 2 + v2.Y ^ 2 + v2.Z ^ 2) ^ 0.5) Then

                    clist.Add(v1)

                Else

                    clist.Add(v2)

                End If


                If ((clist(i).X ^ 2 + clist(i).Y ^ 2 + clist(i).Z ^ 2) ^ 0.5 > (v3.X ^ 2 + v3.Y ^ 2 + v3.Z ^ 2) ^ 0.5) Then

                    clist.RemoveAt(i)
                    clist.Add(v3)

                End If

                If ((clist(i).X ^ 2 + clist(i).Y ^ 2 + clist(i).Z ^ 2) ^ 0.5 > (v4.X ^ 2 + v4.Y ^ 2 + v4.Z ^ 2) ^ 0.5) Then

                    clist.RemoveAt(i)
                    clist.Add(v4)

                End If

            ElseIf array.Length = 3 Then

                Dim v1 As Vector3d, v2 As Vector3d, v3 As Vector3d
                Dim v_left As Point3d, v_right As Point3d, v_bottom As Point3d

                v_bottom = new_mesh.Vertices(array(0))
                v_left = new_mesh.Vertices(array(1))
                v_right = new_mesh.Vertices(array(2))

                v1 = pt2 - v_bottom
                v2 = pt2 - v_left
                v3 = pt2 - v_right

                If ((v1.X ^ 2 + v1.Y ^ 2 + v1.Z ^ 2) ^ 0.5 < (v2.X ^ 2 + v2.Y ^ 2 + v2.Z ^ 2) ^ 0.5) Then

                    clist.Add(v1)

                Else

                    clist.Add(v2)

                End If


                If ((clist(i).X ^ 2 + clist(i).Y ^ 2 + clist(i).Z ^ 2) ^ 0.5 > (v3.X ^ 2 + v3.Y ^ 2 + v3.Z ^ 2) ^ 0.5) Then

                    clist.RemoveAt(i)
                    clist.Add(v3)

                End If

            Else

                Dim v1 As Vector3d, v2 As Vector3d
                Dim v_left As Point3d, v_right As Point3d

                v_right = new_mesh.Vertices(array(0))
                v_left = new_mesh.Vertices(array(1))

                v1 = pt2 - v_right
                v2 = pt2 - v_left

                If ((v1.X ^ 2 + v1.Y ^ 2 + v1.Z ^ 2) ^ 0.5 < (v2.X ^ 2 + v2.Y ^ 2 + v2.Z ^ 2) ^ 0.5) Then

                    clist.Add(v1)

                Else

                    clist.Add(v2)

                End If

            End If

            Dim vect2 As New Vector3d

            vect2 = clist(i) * weight / 150

            Dim pt3 As New Point3d()

            pt3 = pt1 + (vect1 + vect2)

            pts.Add(pt3)

            new_mesh.Vertices.SetVertex(i, pt3.X, pt3.Y, pt3.Z)

        Next

    Next

    DA.SetData(0, new_mesh)
    DA.SetData(1, clist)
    DA.SetData(2, pts)

End Sub

Protected Overrides ReadOnly Property Icon() As System.Drawing.Bitmap
    Get

        Return Nothing
    End Get
End Property

Public Overrides ReadOnly Property ComponentGuid() As Guid
    Get
        Return New Guid("{9c70ee3d-8f13-46b7-8daa-da2eb946f138}")
    End Get
End Property

Private Sub intersection(ByVal pt1 As Point3d, ByVal y As Surface, ByVal pts2 As List(Of Point3d))
    pts2.Clear()

    Dim pt2 As New Point3d

    Dim vectl As New Vector3d(0, 0, 500000000000000)

    Dim ln As New LineCurve(pt1, vectl)

    Dim u As New Double
    Dim v As New Double

    Dim int As Intersect.CurveIntersections = Intersect.Intersection.CurveSurface(ln, y, 0.1, 0.1)

    If (int Is Nothing) Then

        y.ClosestPoint(pt1, u, v)
        pt2 = y.PointAt(u, v)
        pts2.Add(pt2)


    End If

    If (int.Count = 0) Then

        y.ClosestPoint(pt1, u, v)
        pt2 = y.PointAt(u, v)
        pts2.Add(pt2)

    Else

        Dim minX As Point3d = Point3d.Origin
        Dim minD As Double = Double.MaxValue
        For j As Int32 = 0 To int.Count - 1
            Dim ccEvent As Intersect.IntersectionEvent = int(j)

            Dim localD As Double = pt1.DistanceTo(ccEvent.PointA)

            If (localD < minD) Then
                minD = localD
                minX = ccEvent.PointA
            End If

            pts2.Add(minX)

        Next

    End If
End Sub

End Class

Thanks in advance for any feedback,


(Rajaa Issa) #2

Hi,
You have set your “weight” input to be an integer… Did you need it to be of type double?


#3

Hey Rajaa,

Thank you for your reply. I have tried making the weight input as a double. However the only option is pManager.AddNumberParameter, which I assume contains the Double Type. That did not work either, I changed everything containing Weight as doubles as well.


(Rajaa Issa) #4

Yes, pManager.AddNumberParameter is what you need to use for double.
Can you email me your project? I’ll try to debug at my end. You can email it to rajaa@mcneel.com.