"Specified cast is not valid" Error


#1

Hi there,

In testing some tutorial code, I got an error message and did not find the cause. Appreciate any hints.

The error was with the line-

rs.AddCurve(newverts, 5)

–Max

Code:

import rhinoscriptsyntax as rs

def blendcorners():
	polyline_id = rs.GetObject("Polyline to blend", 4, True, True)
	if not polyline_id: return
	vertices = rs.PolylineVertices(polyline_id)
	if not vertices: return
	radius = rs.GetReal("Blend radius", 1.0, 0.0)
	if radius is None: return
	between = lambda a,b: (a+b)/2.0
	newverts = []
	for i in range(len(vertices)-1):
		a = vertices[i]
		b = vertices[i+1]
		segmentlength = rs.Distance(a, b)
		vec_segment = rs.PointSubtract(b, a)
		vec_segment = rs.VectorUnitize(vec_segment)
		if radius<(0.5*segmentlength):
			vec_segment = rs.VectorScale(vec_segment, radius)
		else:
			vec_segment = rs.VectorScale(vec_segment, 0.5*segmentlength)
		w1 = rs.VectorAdd(a, vec_segment)
		w2 = rs.VectorSubtract(b, vec_segment)
		newverts.append(a)
		newverts.append(between(a,w1))
		newverts.append(w1)
		newverts.append(between(w1,w2))
		newverts.append(w2)
		newverts.append(between(w2,b))
		
	newverts.append(vertices[len(vertices)-1])
	rs.AddCurve(newverts, 5)
	print(newverts)
	rs.DeleteObject(polyline_id)

if __name__ == "__main__":
    blendcorners()

(Menno Deij - van Rijswijk) #2

Use rs.AddPolyline(newverts) instead


#3

Thanks. But how about what I needed was a curve (NURBS of degree 5) instead of a polyline? --Max


(Menno Deij - van Rijswijk) #4

TAh, ok.
The code for AddCurve is hereand it’s

def AddCurve(points, degree=3):
    """Adds a control points curve object to the document
    Parameters:
      points = a list of points
      degree[opt] = degree of the curve
    Returns:
      id of the new curve object
    Example:
      import rhinoscriptsyntax as rs
      points = rs.GetPoints(True, message1="Pick curve point")
      if points: rs.AddCurve(points)
    See Also:
      AddInterpCurve
      IsCurve
    """
    points = rhutil.coerce3dpointlist(points, True)
    curve = Rhino.Geometry.Curve.CreateControlPointCurve(points, degree)
    if not curve: raise Exception("unable to create control point curve from given points")
    rc = scriptcontext.doc.Objects.AddCurve(curve)
    if rc==System.Guid.Empty: raise Exception("Unable to add curve to document")
    scriptcontext.doc.Views.Redraw()
    return rc

Now if you use that code, where exactly is the error?


#5

Hi Max

printing the types of the items in the list shows that there are both points and vectors

import rhinoscriptsyntax as rs

def blendcorners():
	polyline_id = rs.GetObject("Polyline to blend", 4, True, True)
	if not polyline_id: return
	vertices = rs.PolylineVertices(polyline_id)
	if not vertices: return
	radius = rs.GetReal("Blend radius", 1.0, 0.0)
	if radius is None: return
	between = lambda a,b: (a+b)/2.0
	newverts = []
	for i in range(len(vertices)-1):
		a = vertices[i]
		b = vertices[i+1]
		segmentlength = rs.Distance(a, b)
		vec_segment = rs.PointSubtract(b, a)
		vec_segment = rs.VectorUnitize(vec_segment)
		if radius<(0.5*segmentlength):
			vec_segment = rs.VectorScale(vec_segment, radius)
		else:
			vec_segment = rs.VectorScale(vec_segment, 0.5*segmentlength)
		w1 = rs.VectorAdd(a, vec_segment)
		w2 = rs.VectorSubtract(b, vec_segment)
		newverts.append(a)
		newverts.append(between(a,w1))
		newverts.append(w1)
		newverts.append(between(w1,w2))
		newverts.append(w2)
		newverts.append(between(w2,b))
		
	newverts.append(vertices[len(vertices)-1])
	for p in newverts:
	    print type(p)
	rs.AddCurve(newverts, 5)
	print(newverts)
	rs.DeleteObject(polyline_id)

blendcorners()

I think w1 and w2 might be vectors …

Regards


#6

Thank you very much emilio. You are right - some obj’s are vectors. I ever thought they may be automatically implicitly convertible but seems like I was wrong. How can I explicitly convert a vector to a Point3D?

Thanks. --Max


#7

Thank you Menno.

I assume I should use
import rhutil
at the beginning.

But I got this -

Message: No module named rhutil

Thanks – Max


#8

I would not know how to do that using rhinoscriptsyntax … sorry.
But it’s straightforward in RhinoCommon

Here are the docs for RhinoCommon:
http://4.rhino3d.com/5/rhinocommon/
You can see there that the Rhino.Geometry.Point3d class contains a constructor to build a point from a vector.
Something like this should work

import Rhino

myvector = # ...
mypoint = Rhino.Geometry.Point3d( myvector )

You have to import ‘Rhino’ in your script to be able to use RhinoCommon
----------------------------------------- (sorry, Discourse does not let me start a new line here ) ---------------------------------------------------------------------

BTW, to use ‘rhutil’ , you’ll need this line:

import utility as rhutil

JFYI, in this PC, with Windows, the rhhinoscriptsyntax code is found in:

C:\Users\computer\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

There you can find the code Menno posted and all the other rhinoscriptsyntax stuff

( Don’t know where to look for that in a Mac … )

Regards


#9

hi @max.wwwang i cant recall where exactly i have seen it but i believe that somewhere on the mcneel wiki it was written that rhinoscript sooner or later becomes obsolete in future versions of rhino due to the rewriting and alining towards a mac windows platform independency and that python will get the substitute of choice. maybe think about getting into rhinoscript too much in those matters.


#10

I’ve heard no such thing, on the contrary, I’ve heard it said that vb Rhinoscript will be maintained along with rhinoscriptsyntax in python. That being said, if you want to script for cross-platform, the only choice currently is python (+ rhinoscriptsyntax and/or RhinoCommon).

–Mitch


#12

Hi Richard

We’re talking about (Iron) Python here …
Or am I missing something ?


#14

OK, I’m missing something


#15

We all are…


#16

Hi Emilio, Thank you very much for your information.
I tried this
import utility as rhutil
but got this
Message: No module named utility
–Max


#17

Hi Richard,

Thank you. That sort of info would have major implication on whether I should invest time on any particular solution. But as I have quite limited understanding of the Rhino ecosystem at the moment, I am still not sure about the difference between rhinoscript and rhinoscriptsyntax?
Thanks --Max


#18

I tried this way. But it did not work either.

	newverts.append(a)
	newverts.append((between(a,w1)[0], between(a,w1)[1], between(a,w1)[2]))
	newverts.append(w1)
	newverts.append((between(w1,w2)[0], between(w1,w2)[1], between(w1,w2)[2]))
	newverts.append(w2)
	newverts.append((between(w2,b)[0], between(w2,b)[1], between(w2,b)[2]))

#19

on mac, the path is:


(in case anyone is wondering ; )


#20

There should be no need to do this. If you import rhinoscriptsyntax, all the methods in rhutil are also imported. For example if you are trying to coerce vectors (or something else) into points, you can simply use rhinoscriptsyntax.coerce3dpoint().[quote=“max.wwwang, post:17, topic:38218”]
I am still not sure about the difference between rhinoscript and rhinoscriptsyntax?
[/quote]

Rhinoscript is based on VBscript and pre-exists the introduction of python for Rhino. Rhinoscript are higher-level calls into base Rhino functions that allow you to script Rhino operations with simplified, more plain-language programming. I think in this case Rhinoscript methods call Rhino C++ functions directly.

Rhinoscriptsyntax mimics the functions of Rhinoscript using Python instead of VBscript. The basic structure of the methods are the same - with some minor variations due to the differences between how VB and Python work. Rhinoscriptsyntax methods call into RhinoCommon - in fact all rhinoscriptsyntax is are a set of “wrapper” functions that make it easier to script Rhino just like its VB counterpart. The advantage of working in Python is that you can use either Rhinoscriptsyntax, Rhinocommon, or a mixture of both. And it’s cross-platform, whereas VB based Rhinoscript is not.

RhinoCommon is a “common” set of programming classes and methods that can be called with VB.net, C# and Python.
–Mitch


#21

Hi Mitch,

At last it works.

between = lambda a,b: rs.coerce3dpoint((a+b)/2.0)

for i in range(len(vertices)-1):

	newverts.append(a)
	newverts.append(between(a,w1))
	newverts.append(rs.coerce3dpoint(w1))
	newverts.append(between(w1,w2))
	newverts.append(rs.coerce3dpoint(w2))
	newverts.append(between(w2,b))

Thank you very much for all the explanations! Sounds like it’s worthwhile to go further down this path with Rhino Python (Rhinoscriptsyntax, etc.) One day I may also use Rhinocommon with C#.

Thank you again. --Max


#22

Hi Max

Sorry for the confused advice about rhutil
Mitch is right (obviously), importing rhinoscriptsyntax already imports all those functions.

Just in case I were not clear ( I proved to be very good at doing that …:smile: ) …
You can use RhinoCommon directly in Iron Python if you like, you only have to import ‘Rhino’ in your script …

Good luck for your scripts ! :slight_smile: