Use rs.command for mesh


#1

Hi,

I am trying to automatise the meshing process of a large number of polysurfaces, keeping them in their original layer, if I am using the rhinoscriptsyntax I was thinking to do something like:

for i in range(0,len(layers)):
objects = rs.ObjectsByLayer(layers[i])
rs.CurrentLayer(layers[i])
rs.SelectObjects(objects)
rs.Command("mesh _enter _enter ")
rs.Command(“selnone”)

unfortunately the _enter _enter doesn’t work with the mesh dialog box that opens and I have to manually press ok, or enter.

I tried using rhino.common instead :
obj = rs.GetObject("")
obj=rs.coercebrep(obj)
Rhino.Geometry.Mesh.CreateFromBrep(obj)

but this, although not giving an error, doesn’t generate any mesh at all.

Any idea on how to sort this out?

Many thanks in advance

Best

Vincenzo


#2

You would need to use the -dash version of the command to bypass the dialog box, then you might also need to script all the command line options for mesh - there are lots…

The mesh is created (virtually) but is not added to the document - you need to do that in additional step. something like:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

objID = rs.GetObject()
obj=rs.coercebrep(objID)
mesh=Rhino.Geometry.Mesh.CreateFromBrep(obj,[meshing parameters])  #params optional
if mesh:
    sc.doc.Objects.AddMesh(mesh)

HTH, --Mitch


#3

Many thanks, this works!

However I am doing a sort of similar thing now with extrusions and it doesn’t seem to work with them, any idea why?

box = rs.BoundingBox(object)
crv = rs.AddPolyline([box[0],box[1],box[2],box[3],box[0]])
vec = rs.VectorCreate(box[4],box[0])
crv = rs.coercecurve(crv)
curve = Rhino.Geometry.Extrusion.CreateExtrusion(crv,vec)
if curve:
    sc.doc.Objects.AddExtrusion(curve)

this gives me the error “expected extursion got nurbs surface”


#4

This is tricky, as Rhino.Geometry.Extrusion.CreateExtrusion returns a NURBS surface, not an extrusion or Brep. And this method appears to be undocumented in the Extrusion class - it is actually under Rhino.Geometry.Surface.CreateExtrusion… I will complain about that later…

So you would need this instead:

extru = Rhino.Geometry.Extrusion.CreateExtrusion(crv,vec)
#or
extru = Rhino.Geometry.Surface.CreateExtrusion(crv,vec)

if extru: sc.doc.Objects.AddSurface(extru)
#don't forget to redraw your view to see the object
sc.doc.Views.Redraw()

Now, this will also fail if your object is flat, as your vector will be 0… so you might need to test for that and work around.

If you want it to actually return an extrusion object, you will need to work with Rhino.Geometry.Extrusion.Create and give it a height (not a vector).

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
object=rs.GetObject()
box = rs.BoundingBox(object)
if box:
    crv = Rhino.Geometry.PolylineCurve([box[0],box[1],box[2],box[3],box[0]])
    ht=box[0].DistanceTo(box[4])
    if ht>0:
        #True = Cap, False = No cap
        extru = Rhino.Geometry.Extrusion.Create(crv,ht,False)
        if extru:
            sc.doc.Objects.AddExtrusion(extru)
            sc.doc.Views.Redraw()
    else: print "Unable to create extrusion, 0 height"

RhinoCommon/API issue
#5

Yes, I was looking for extrusion and your Rhino.Geometry.Extrusion.Create(crv,ht,False) works perfectly, many thanks again.

Just out of curiosity, when the geometry is created “virtually” but not added to the document, does its data disappear when the script finishes?

Like closing a Grasshopper definition without baking an object?


#6

Yep, exactly.