Choose option from a list of string

Hi, I’m trying in vb.net have an option to select a bloc from another file, but my option list does not work, could you help?
This is what I did:

'Check the file exist:
            Dim PathProfils As String = "C:\Users\...\Profiles.3dm"
            If My.Computer.FileSystem.FileExists(PathProfils) = False Then
                Return Result.Failure
            End If

 'Read the bloc list in the file:
            Dim TpProfilNames As New ArrayList
            RhinoApp.WriteLine("Lecture de la bibliotheque de profils")
            Dim profils As Rhino.FileIO.File3dm
            profils = Rhino.FileIO.File3dm.Read(PathProfils)
            For i = 0 To profils.InstanceDefinitions.Count - 1
                If profils.InstanceDefinitions.Item(i).Name.ToLower.Contains("cuto") Then
                    profils.InstanceDefinitions.Item(i).Name = profils.InstanceDefinitions.Item(i).Name.Replace(" ", "") 'remove spaces
                    TpProfilNames.Add(profils.InstanceDefinitions.Item(i).Name)
                End If
            Next
            Dim ProfilNames(TpProfilNames.Count - 1) As String
            For i = 0 To TpProfilNames.Count - 1
                ProfilNames(i) = TpProfilNames(i)
                RhinoApp.WriteLine(ProfilNames(i))
            Next

 'Select a point with an option to choose a bloc to the other file
            Dim SelectedPoint As Rhino.Geometry.Point3d ' point d'insertion
            Dim gp As New Rhino.Input.Custom.GetPoint
            gp.SetCommandPrompt("Sélectionner point")
            Dim boolOption As New Rhino.Input.Custom.OptionToggle(True, "Non", "Oui")
            gp.AddOptionToggle("Inverser", boolOption)
            Dim listIndex As Integer = 0
            Dim opList As Integer = gp.AddOptionList("Découpe", ProfilNames, listIndex)

            While True
                Dim get_rc As Rhino.Input.GetResult = gp.Get()
                If gp.CommandResult() <> Rhino.Commands.Result.Success Then
                    Return gp.CommandResult()
                End If
                If get_rc = Rhino.Input.GetResult.Point Then
                    Rhino.RhinoApp.WriteLine("Command line option values are")
                    Rhino.RhinoApp.WriteLine("Inverser = {0}", boolOption.CurrentValue)
                    Rhino.RhinoApp.WriteLine("Nom de la découpe = {0}", ProfilNames(listIndex))
                    SelectedPoint = gp.Point
                ElseIf get_rc = Rhino.Input.GetResult.[Option] Then
                    If gp.OptionIndex() = opList Then
                        listIndex = gp.Option().CurrentListOptionIndex
                    End If
                    Continue While
                End If
                Exit While
            End While

Also, how can I instanciante one of these blocks in my model?

Hi Matthieu,

first allow me to make some general observations:

  • Don’t use VB specific classes and methods when framework methods exist that do the same. Instead of My.Computer.FileSystem.FileExists, use System.IO.File.Exists.
  • You don’t have to compare boolean results with boolean constants in conditional statement. Instead of If (A = True) Then or If (A = False) Then use If (A) Then or If (Not A) Then.
  • Don’t use the ArrayList class. It’s a leftover type from the earliest .NET versions which did not yet support generics. It’s pretty much always better to use List(Of T) instead.
  • Don’t use the ToLower method to compare strings without case sensitivity. ToUpper is better as not all upper case characters map to unique lower case characters. Also consider the ToUpperInvariant and the String.Compare Shared method, which allows you to specifically ignore culture specific and casing constraints.

Sorry I can’t see the problem in your code, I rewrote it to make it work on my machine and it works over here.

  'Check the file exist:
  Dim profilesLocation As String = "C:\...\blocksblocksblocks.3dm"
  If (Not File.Exists(profilesLocation)) Then
    Return False
  End If

  'Read the block list in the file:
  RhinoApp.WriteLine("Lecture de la bibliotheque de profils")
  Dim profilesFile As File3dm = File3dm.Read(profilesLocation)
  Dim profileNames As New List(Of String)
  For Each instanceDefinition As InstanceDefinitionGeometry In profilesFile.InstanceDefinitions
    Dim name As String = instanceDefinition.Name
    If (name.ToUpperInvariant().Contains("CUTO")) Then
      name = name.Replace(" ", String.Empty)
      profileNames.Add(name)
      Rhino.RhinoApp.WriteLine(name)
    End If
  Next

  'Point getter with options for block names.
  Dim insertionPoint As Point3d
  Dim gp As New Rhino.Input.Custom.GetPoint
  gp.SetCommandPrompt("Pick point for block insertion")
  Dim boolOption As New Rhino.Input.Custom.OptionToggle(True, "No", "Yes")
  gp.AddOptionToggle("Invert", boolOption)

  Dim listIndex As Int32 = 0
  Dim opList As Int32 = gp.AddOptionList("Block", profileNames, listIndex)

  Do
    Dim rc As Rhino.Input.GetResult = gp.Get()
    If (gp.CommandResult() <> Rhino.Commands.Result.Success) Then
      Return False
    End If

    If (rc = Rhino.Input.GetResult.Point) Then
      Rhino.RhinoApp.WriteLine("Command line option values are")
      Rhino.RhinoApp.WriteLine("Invert = {0}", boolOption.CurrentValue)
      Rhino.RhinoApp.WriteLine("Name of block = {0}", profileNames(listIndex))
      insertionPoint = gp.Point
      Exit Do

    ElseIf (rc = Rhino.Input.GetResult.[Option]) Then
      If (gp.OptionIndex() = opList) Then
        listIndex = gp.Option().CurrentListOptionIndex
      End If
    End If
  Loop