Costruire una Brep

Ciao
Sto cercando di capire come si costruisce una Brep, data una superficie base e una curva di bordo.
Questo usando RhinoCommon.
Per ora, data la mia zucca dura, non ci riesco …
Qualcuno sa come fare o sa dove trovare un esempio da studiare ?
Io ho trovato esempi in C++, ma a quanto pare sbaglio qualcosa cercando di riprodurli in RhinoCommon…

Grazie

p.s.
questo e’ cio’ che ho provato … ma non funge:

import Rhino
def main():
# define surface
surf = Rhino.Geometry.PlaneSurface(
Rhino.Geometry.Plane.WorldXY,
Rhino.Geometry.Interval( -100, 100 ),
Rhino.Geometry.Interval( -100, 100 ) )
# draw surface
Rhino.RhinoDoc.ActiveDoc.Objects.AddSurface( surf )
# define outer curve
circ = Rhino.Geometry.Circle( 75 )
curve3d = Rhino.Geometry.ArcCurve( circ )
# draw outer curve
Rhino.RhinoDoc.ActiveDoc.Objects.AddCurve( curve3d )
Rhino.RhinoDoc.ActiveDoc.Views.Redraw()
# get curve 2D
tolr = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance
curve2d = surf.Pullback( curve3d, tolr )
vertexpnt = curve3d.PointAtStart
# surface orientation
backsurf = False
# curve orientation
backedge = False
# build brep:
rep = Rhino.Geometry.Brep()
# add vertex
vertex = rep.Vertices.Add(
vertexpnt, Rhino.RhinoMath.UnsetValue )
vertexindex = vertex.VertexIndex
# add curve 3D
cur3dindex = rep.AddEdgeCurve( curve3d )
# add edge
edge = rep.Edges.Add(
vertexindex, vertexindex, cur3dindex, Rhino.RhinoMath.UnsetValue )
# add surface
surfindex = rep.AddSurface( surf )
# add face
face = rep.Faces.Add( surfindex )
# add loop
loop = rep.Loops.Add( Rhino.Geometry.BrepLoopType.Outer, face )
# add curve 2D
cur2dindex = rep.AddTrimCurve( curve2d )
# add trim
trim = loop.Trims.Add( edge, backedge, loop, cur2dindex )
trim.IsoStatus = Rhino.Geometry.IsoStatus.None
trim.TrimType = Rhino.Geometry.BrepTrimType.Boundary
trim.SetTolerances( Rhino.RhinoMath.UnsetValue, Rhino.RhinoMath.UnsetValue )
# set surface orientation
face.OrientationIsReversed = backsurf
print 'Brep is valid: %s' % str( rep.IsValid )
main()

OK, era un problema di tolleranze.
Dopo un aggiustamento molto empirico … ora pare funzionare, almeno in questo semplice test …

import Rhino

def main():

  tolr = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance

  surf = Rhino.Geometry.PlaneSurface( 
      Rhino.Geometry.Plane.WorldXY,
      Rhino.Geometry.Interval( -100, 100 ),
      Rhino.Geometry.Interval( -100, 100 ) )

# Rhino.RhinoDoc.ActiveDoc.Objects.AddSurface( surf )

  circ = Rhino.Geometry.Circle( 75 )
  curve3d = Rhino.Geometry.ArcCurve( circ )

# Rhino.RhinoDoc.ActiveDoc.Objects.AddCurve( curve3d )

  curve2d = surf.Pullback( curve3d, tolr )
  vertexpnt = curve3d.PointAtStart

  backsurf = False
  backedge = False

  rep = Rhino.Geometry.Brep()
  vertex = rep.Vertices.Add( 
#     vertexpnt, Rhino.RhinoMath.UnsetValue )
      vertexpnt, tolr * 2 )
  vertexindex = vertex.VertexIndex
  cur3dindex = rep.AddEdgeCurve( curve3d )
# edge = rep.Edges.Add( 
#     vertexindex, vertexindex, cur3dindex, Rhino.RhinoMath.UnsetValue )
  edge = rep.Edges.Add( 
      vertexindex, vertexindex, cur3dindex, tolr * 2 )

  surfindex = rep.AddSurface( surf )
  face = rep.Faces.Add( surfindex )
  loop = rep.Loops.Add( Rhino.Geometry.BrepLoopType.Outer, face )
  cur2dindex = rep.AddTrimCurve( curve2d )

  trim = loop.Trims.Add( edge, backedge, loop, cur2dindex )
  trim.IsoStatus = Rhino.Geometry.IsoStatus.None
  trim.TrimType = Rhino.Geometry.BrepTrimType.Boundary
# trim.SetTolerances( Rhino.RhinoMath.UnsetValue, Rhino.RhinoMath.UnsetValue )
  trim.SetTolerances( tolr * 2, tolr * 2 )

  face.OrientationIsReversed = backsurf

  print 'Brep is valid: %s' % str( rep.IsValid )

  Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep( rep )
  Rhino.RhinoDoc.ActiveDoc.Views.Redraw()

main()