Rhino.LastCreatedObjects to return a string?

Hello Everyone

Is it possible to make the command Rhino.LastCreatedObjects return a string?

I’m importing a file and then need to select it as a string for other commands
But I keep getting the error needs string

anyone has any ideas?

Hi There,

Rhino.LastCreatedObjects does return an Array of Strings, meaning also if you`re only selecting one object it will still return an Array.

strObject=Rhino.LastCreatedObjects(optional Arguments) (0)

This will only return the first Element (if you only have one, also the only one) of the Array. You actually need this for quite a few Rhinoscript Methods as some of them always return arrays also if they only consist of a single element.

Hope this helps
K.

You also might want to be a bit careful when doing this:

strObject=Rhino.LastCreatedObjects(optional Arguments) (0)

If no objects have been created by the previous command - due to an error or something else - Rhino.LastCreatedObjects will be Null and the line above will cause the script to error out at that point. I find it better to separate the procedure into two separate moves:

arrObjs=Rhino.LastCreatedObjects()
If IsArray(arrObjs) Then 
    strObj=arrObjs(0)
Else
    Rhino.Print "Error -no objects created"
End If

–Mitch

You can also check for success in the last command-

If Rhino.LastCommandResult() = 0 Then
arrObjs=Rhino.LastCreatedObjects()
strObj=arrObjs(0)
Else
Rhino.Print "Error -no objects created"
End If

-Pascal

How can i get the name of of last created object in VB.net using script object ??

Public obj As Object = RhUtil.RhinoApp.GetRhinoScriptInterface()
Public RScript As RhinoScript4.IRhinoScript = CType(obj, RhinoScript4.IRhinoScript)

objPatch = RScript.LastCreatedObjects

this command does not returns the array.

if i try to delete the last created object using following command it gives an error:

RScript.DeleteObject(objPatch)

how can i get the result as we get in RVB ??

I’m not a programmer but what type of data is variable objPatch ?
Dim objPatch() As String

objPatch = RScript.LastCreatedObjects() // If successful objPatch is array of strings

if i try to delete the last created object using following command it gives an error:

DeleteObject works only with one element, or not ???

RScript.DeleteObject(objPatch(0)) // Deletes first stringelement in array ....

No eddi,

In VB.Net the return of the RScript.LastCreatedObjects is not an array it shows Object and that is the problem

Sorry, I have no idea
Why do not you post the entire code snippet
So it is not clear, where error is.

Best regards

Keep in mind that the RhinoScript object is a COM object. Thus, all RhinoScript methods return some kind of COM data type. usually a Variant.

This function (and similar functions) returns a Variant that contains a SafeArray of Variants. Each of these Variants holds a string which represents the id (Guid) of the object.

To obtain the data in .NET form, you should be able to do something like this:

var object_ids = rs.AllObjects();
if (null != object_ids)
{
  foreach (var id in object_ids)
  {
    var str = id as string;
    // TODO...
  }
}

Here is the Project Code.

Basically i am not able to handle the result of the command which is in array. Because in VB.Net using RhinoScript Object the result of the command is shows as system.object.

In this project, i am trying to count the objects in particular layer using the command
ObjCount = RScript.ObjectsByLayer(lyrName)

but not able to get the result of this command in array

pls take a look at the attached project files, and suggest me the way.

Thank you
MyPlugIn1.zip (151.5 KB)

Guys Any solution ??

My solution is not to call RhinoScript from a Rhino.NET plug-in. Rather, use the Rhino.NET SDK to solve the problem.

For example:

Thanx Dale,

But i am making an Rhino Automating plugin and have used the RhinoScript. The plugin is almost done and just stuck because of this problem. is there any way to solve this using RhinoScipt ??

meta - please delete my reply, thanks.

What you can try is get all objects inside of your drawing (list1). Run the command. Loop through them again (list2). and compare them. If an object in list2 does not exist in list1 it is your new object…

Yes Jordy that is one solution but looping every time is little slower when there are so many objects.

Again, something like this will work:

Dim object_ids = rs.LastCreatedObjects()
If object_ids IsNot Nothing Then
  For Each id As var In object_ids
    Dim str = TryCast(id, String)
    ' TODO...
  Next
End If

Hi Dale,

Thank you for your reply

But it still not working

If i use this :

        Dim object_ids = RScript.LastCreatedObjects()
        If object_ids IsNot Nothing Then
            For Each id As var In object_ids
                Dim str = TryCast(id, String)
                ' TODO...
            Next
        End If

Its says - Type ‘var’ is not defined.

If i use VariantType instead of var :

        Dim object_ids = RScript.LastCreatedObjects()
        If object_ids IsNot Nothing Then
            For Each id As VariantType In object_ids
                Dim str = TryCast(id, String)
                ' TODO...
            Next
        End If

it gives following errors :

Error 1 Implicit conversion from ‘Object’ to ‘System.Collections.IEnumerable’. ( this is for ‘object_ids’ )
Error 2 Warning treated as error : Implicit conversion from ‘Object’ to ‘System.Collections.IEnumerable’. ( this is for ‘object_ids’ )
Error 3 Value of type ‘Microsoft.VisualBasic.VariantType’ cannot be converted to ‘String’. (this is for ‘id’ )

Try to change VariantType to Guid. Does that work?

This works for me:

rs.Command("_Line 0,0,0 1,1,0", 1)

Dim results = rs.LastCreatedObjects()
If IsArray(results) Then
  For Each res As Object In results
    If res IsNot Nothing Then
      Dim str = TryCast(res, String)
      If str IsNot Nothing Then
        RhinoApp.WriteLine(str)
      End If
    End If
  Next
End If