Using _Inset with RhinoCommon?

Greetings,

Can’t seem to find _Inset tool in RhinoScriptSyntax

Is it possible to call it using RhinoCommon ?

Note: _Inset is found under Mesh Tools, and it works with both Meshes and Surfaces. It creates a border of a specified distance around a mesh face or surface.

Hi @Bogdan_Chipara, for surfaces there is this method since Rhino 8 SR8

_
c.

Hi @clement this is good to hear,

But it turns out I’m a bad programmer and I don’t know how to setup inputs for RhinoCommon methods.
Coud you please write a simple tuturial on how to use this method ?
Thank you!

Hi @Bogdan_Chipara, try below on a surface and a box:

#! python 2
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    brep_id = rs.GetObject("Brep to inset", 16, True, True)
    if not brep_id: return
    
    brep = rs.coercebrep(brep_id, True)
    faces = range(brep.Faces.Count)
    new_brep = Rhino.Geometry.Brep.InsetFaces(
                                            brep,
                                            faceIndices=faces,
                                            distance=1.0,
                                            loose=True,
                                            ignoreSeams=True,
                                            creaseCorners=False,
                                            tolerance=0.001,
                                            angleTolerance=0.1
                                            )
    
    if isinstance(new_brep, Rhino.Geometry.Brep):
        scriptcontext.doc.Objects.Replace(brep_id, new_brep)
        scriptcontext.doc.Views.Redraw()
    
DoSomething()

_
c.

1 Like

Thank you @clement ,works perfect!

I don’t understand the following lines:

#converts the object id directly to a brep Geometry which is what the InsetFaces method requires as first argument
brep = rs.coercebrep(brep_id, True)
# python function to check if new_brep is a type of Rhino.Geometry.Brep
isinstance(new_brep, Rhino.Geometry.Brep):
scriptcontext.doc.Objects.Replace(brep_id, new_brep)
# one can also use Add
scriptcontext.doc.Objects.Add(new_brep)

and

scriptcontext.doc.Views.Redraw()

Hi @Bogdan_Chipara,

the rs.coercebrep converts the object id directly to a brep Geometry which is what the InsetFaces method requires as first argument.

isinstance is a build in function of python. It basically ensures that the returned value of InsetFaces method is of the required type Brep. You could also write like below but it just checks if the returned value is not None:

if new_brep is not None:
    # do something here

The Replace method is documented in the RhinoCommon docs. It replaces the brep you picked with the one which now has inset faces.

Redraw() just refreshes the views. In RhinoScriptSyntax this usually happens automatically but in RhinoCommon it is only done when requested.

btw. one way to understand how rs works under the hood is to look into the python files located here:

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

_
c.

1 Like

I see,
I’m confused because RhinoCommon methods require Geometry while rhinoscriptsyntax works with object_id. Am I correct?
And where’s the documentation for scriptcontext ?

Yes.

The methods for eg. scriptcontext.doc are found under Rhino.RhinoDoc in RhinoCommon. You might read this thread too.

_
c.

hmm, this is confusing.
On Rhino.RhinoDoc i cannot find scriptcontext.doc , not even if I use Search.

I think I’m doing somethig wrong.

Hi @Bogdan_Chipara, compare the methods you get when you type a dot after

scriptcontext.doc.

with the methods you see when you type

Rhino.RhinoDoc.

here is what is see:

_
c.

with Rhino.RhinoDoc, nothing pops after .Objects.

Hi @Bogdan_Chipara, that is why you use scriptcontext.doc.Objects.

please read the thread i’ve linked to above “Understanding scriptcontext”

When using a script, you use scriptcontext.doc.Objects to access the ObjectTable

_
c.

Ok, so for example, the Add method is this one, under Object Table and I use it by scriptcontext
like this : scriptcontext.doc.Objects.Add(new_brep)

Yes, and when you know it is a brep which you like to add you better write this:

scriptcontext.doc.Objects.AddBrep(new_brep)

if you scroll down you’ll find this method on the same page. Btw. if it worked, this method returns something which you are used to:

brep_id = scriptcontext.doc.Objects.AddBrep(new_brep)

_
c.

1 Like

Ok, thanks a lot for explaining all this.
The workflow is not really explained anywhere very clearly, and for someone new it might seem confusing.

While in fact it is quite clear.

  1. rhinoscriptsyntax methods use object_id
  2. object_id is translated to geometry using coercebrep
  3. The brep then can be manipulated with Rhino.Geometry.Brep. methods
  4. scriptcontext.doc.Objects. methods are used to add the new brep on the scene and get the new id

Note: scriptcontext.doc.Objects methods are explained under:
https://developer.rhino3d.com/api/rhinocommon/rhino.docobjects.tables.objecttable#methods

2 Likes

object_id is translated to geometry using coercebrep

Just to clarify, object ids can be coerced into various things. So you want to be sure you’re coercing into the correct type of thing.