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.
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)
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"