Odd ID created


#1

Just ran across an odd phenomenon - don’t know if it should be classified as a bug or not… Run the following:

import Rhino
import scriptcontext as sc

ptA=Rhino.Geometry.Point3d(1,1,1)
pl=Rhino.Geometry.Polyline([ptA,ptA])
ID=sc.doc.Objects.AddPolyline(pl)
print ID
findMyObj=sc.doc.Objects.Find(ID)
print findMyObj

So, first, I was able to create an invalid polyline definition (just two coincident points). Then in trying to add that invalid geometry to the document, it actually output an object ID - all 0’s… And of course, this ID isn’t valid and nothing was really added to the document…

Since I assumed that if an ID was produced, the AddPolyline function had succeeded, later on, when I tried to get that object back out of the document by finding its ID, the script failed…

So, the question is: if adding the object to the document fails - shouldn’t the function return None and not a bogus ID? Or is this considered to be normal behavior?

And, no, I’m not deliberately trying to add bad objects, it’s just that one method I am using - Polyline.ReduceSegments - actually does return invalid polyline definitions like above when the input polyline is very small…

Thanks,
–Mitch


(Menno Deij - van Rijswijk) #2

The GUID returned is a value type, not a reference type. It is not possible to return None (or NULL) for a value type, therefore the returned id is equal to Guid.Empty (which has all zero’s).

You are supposed to test if something else is returned then Guid.Empty to judge success of the Add operation. But, imho that is a last resort. Prior to adding geometry, you can test if it is valid using the IsValid property. If this returns false you know something is wrong.

Also, for curves, there is an IsShort(tolerance) function to test if curves are shorter than some given tolerance.

HTH,
Menno


#3

Hey Menno,

Thanks, as soon as I got into the car to drive home I said to myself - "I’m sure there is an Is.Valid method I forgot about… The GUID.Empty also makes sense.

I was also pretty certain that I would have an answer from you by the time I got home… :smile:

Thanks for your help as always,
–Mitch


(Menno Deij - van Rijswijk) #4

Thanks, you’re very welcome :smile: