Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:rhinocommonsamples:findobjectsbyname [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Find Objects by Name =====
 +
 +===== C# =====
 +<code c#>
 +public static Rhino.Commands.Result FindObjectsByName(Rhino.RhinoDoc doc)
 +{
 +  const string name = "​abc";​
 +  Rhino.DocObjects.ObjectEnumeratorSettings settings = new Rhino.DocObjects.ObjectEnumeratorSettings();​
 +  settings.NameFilter = name;
 +  System.Collections.Generic.List<​Guid>​ ids = new System.Collections.Generic.List<​Guid>​();​
 +  foreach (Rhino.DocObjects.RhinoObject rhObj in doc.Objects.GetObjectList(settings))
 +    ids.Add(rhObj.Id);​
 +
 +  if (ids.Count == 0)
 +  {
 +    Rhino.RhinoApp.WriteLine("​No objects with the name " + name);
 +    return Rhino.Commands.Result.Failure;​
 +  }
 +
 +  Rhino.RhinoApp.WriteLine("​Found {0} objects",​ ids.Count);
 +  for (int i = 0; i < ids.Count; i++)
 +    Rhino.RhinoApp.WriteLine(" ​ {0}", ids[i]);
 +
 +  return Rhino.Commands.Result.Success;​
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Shared Function FindObjectsByName(ByVal doc As Rhino.RhinoDoc) As Rhino.Commands.Result
 +  Const name As String = "​abc"​
 +  Dim settings As New Rhino.DocObjects.ObjectEnumeratorSettings()
 +  settings.NameFilter = name
 +  Dim ids As New System.Collections.Generic.List(Of Guid)()
 +  For Each rhObj As Rhino.DocObjects.RhinoObject In doc.Objects.GetObjectList(settings)
 +    ids.Add(rhObj.Id)
 +  Next
 +
 +  If ids.Count = 0 Then
 +    Rhino.RhinoApp.WriteLine("​No objects with the name " & name)
 +    Return Rhino.Commands.Result.Failure
 +  Else
 +    Rhino.RhinoApp.WriteLine("​Found {0} objects",​ ids.Count)
 +    For i As Integer = 0 To ids.Count - 1
 +      Rhino.RhinoApp.WriteLine(" ​ {0}", ids(i))
 +    Next
 +  End If
 +
 +  Return Rhino.Commands.Result.Success
 +End Function
 +</​code>​
 +===== Python =====
 +<code python>
 +import Rhino
 +import scriptcontext
 +import System.Guid
 +
 +def FindObjectsByName():​
 +    name = "​abc"​
 +    settings = Rhino.DocObjects.ObjectEnumeratorSettings()
 +    settings.NameFilter = name
 +    ids = [rhobj.Id for rhobj in scriptcontext.doc.Objects.GetObjectList(settings)]
 +    if not ids:
 +        print "No objects with the name", name
 +        return Rhino.Commands.Result.Failure
 +    else:
 +        print "​Found",​ len(ids), "​objects"​
 +        for id in ids: print " ​ ", id
 +    return Rhino.Commands.Result.Success
 +
 +if __name__ == "​__main__":​
 +    FindObjectsByName()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/findobjectsbyname.txt ยท Last modified: 2015/09/14 (external edit)