Centroid for line or polyline

The object I want to trim is a closed extrusion. However, after using trim or split, the new object turns to a open polysurface.
Why?
How can I turn the remain part into a closed extrusion again.
Thanksslice.3dm (39.1 KB)

Use BooleanSplit or BooleanDifference instead of Split or Trim.

A solid in Rhino is a closed polysurface. Split or Trim just split or trim the polysurface and don’t take account that it is closed and considered as a surface. The Boolean commands are used when a solid needs to remain a solid.

1 Like

Please start a new topic with this stuff…

As Mitch says, please start a new topic when you have an unrelated question.

Since David has answered your question, I suppose this discussion has now ended in this thread. I would just like to add a final thing though… :stuck_out_tongue:

You’ll have to read up on nomenclature for Rhino. An extrusion is a very specific object type in Rhino that was added to Rhino 5 to allow light-weight objects to be used. An extrusion is only defined by a cross section and an extrusion length. When you trim with a surface that is at an angle to the cross section plane, you will not be able to get an extrusion. Extrusion objects are converted on-the-fly into regular Breps when needed but out-of-the-box Rhino does not convert Breps back into extrusion objects.

  • wim out…

Really interesting reading @davidcockey posts in this thread and then got to @wim post ……so had to search for what a Brep was and LOL found a thread from July 13 with @davidcockey asking the same question.

1 Like

for clarity (just in case ; ) )

that’s july **'**13 …as in a couple years ago… instead of july 13 as in a few months ago.

Thanks @jeff_hammond. Yes July 2013….and Pascals explanation of what a Brep is in that thread is excellent.

Also just to clarify in case it came across the wrong way…I found it amusing because it reminded me we all learn from asking questions….even someone who is obviously as intelligent and educated as @davidcockey is.

1 Like

Hi all

I have tried to test the two algorithms explained here, plus a third algorithm that, after dividing the curve, discards even points and only uses odd points …

It’s only a quick test, but I get pretty different iteration count ( and the points seem a little apart from each other )
( I tried it onto a half circle )

… But I suspect that the script be somewhat buggy … particularly on the second algorithm, maybe it’s not written correctly …

First algorithm is the one from Mitch’s script
Second is the one explained by David
Third is the one that uses odd points only

Should someone happen to see some bug here, please tell me about it, thanks … :smiley:

BTW Thanks Mitch for teaching me about reduce() ! :smiley:

import Rhino
import scriptcontext
import System

def cent1( cur, cnt ):
  '''
  algorithm 1
  '''
  pars = cur.DivideByCount( cnt, True )
  pts = [ cur.PointAt( par ) for par in pars ]
  cen = reduce( lambda a, b : a + b, pts ) / len( pts )
  return cen

def cent2( cur, cnt ):
  '''
  algorithm 2
  '''
  pars = cur.DivideByCount( cnt, True )
  pts = [ cur.PointAt( par ) for par in pars ]
  pts[ 0 ] /= 2
  pts[ -1 ] /= 2
  cen = reduce( lambda a, b : a + b, pts ) / len( pts )
  return cen

def cent3( cur, cnt ):
  '''
  algorithm 3
  '''
  pars = cur.DivideByCount( cnt, True )
  pars = pars[ 1 : : 2 ]
  pts = [ cur.PointAt( par ) for par in pars ]
  cen = reduce( lambda a, b : a + b, pts ) / len( pts )
  return cen

def main():
  gob = Rhino.Input.Custom.GetObject()
  gob.AcceptNothing( True )
  gob.SetCommandPrompt( 'Curve for centroid ?' )
  gob.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
  gob.Get()
  res = gob.Result()
  if res == Rhino.Input.GetResult.Cancel:
    return
  elif res == Rhino.Input.GetResult.Object:
    obref = gob.Object( 0 )
    cur = obref.Curve()
  else:
    return
  tolr = 0.001
 
  cnt = 4
  prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
  itr = 0
  att = Rhino.DocObjects.ObjectAttributes()
  att.ObjectColor = System.Drawing.Color.Red
  att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
  while True:
    scriptcontext.escape_test()
    itr += 1
    cen = cent1( cur, cnt )
    if cen.DistanceTo( prev ) < tolr:
      print 'Algorithm 1: %d iterations' % itr
      Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
      break
    prev = cen
    cnt += 2
 
  cnt = 4
  prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
  itr = 0
  att = Rhino.DocObjects.ObjectAttributes()
  att.ObjectColor = System.Drawing.Color.Green
  att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
  while True:
    scriptcontext.escape_test()
    itr += 1
    cen = cent2( cur, cnt )
    if cen.DistanceTo( prev ) < tolr:
      print 'Algorithm 2: %d iterations' % itr
      Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
      break
    prev = cen
    cnt += 2
 
  cnt = 4
  prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
  itr = 0
  att = Rhino.DocObjects.ObjectAttributes()
  att.ObjectColor = System.Drawing.Color.Blue
  att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
  while True:
    scriptcontext.escape_test()
    itr += 1
    cen = cent3( cur, cnt )
    if cen.DistanceTo( prev ) < tolr:
      print 'Algorithm 3: %d iterations' % itr
      Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
      break
    prev = cen
    cnt += 2
      
# Rhino.RhinoDoc.ActiveDoc.Views.Redraw()

main()


I got that one from Steve a long time ago… :smile:

About the centroid point differences, I only compared mine to the area centroid of a tiny diameter pipe as suggested in one of the posts - it ended up pretty close to that in my quick tests…

–Mitch

OK, I’ve found my error. :blush:
The results look somewhat OK now and David’s algorithm should be the fastest. :smiley:
( But I think that my exit test comparing the result against the previous one makes no sense … :frowning: )
Anyway … interesting stuff.

import Rhino
import scriptcontext
import System

def cent1( cur, cnt ):
  '''
  algorithm 1
  '''
  pars = cur.DivideByCount( cnt, True )
  pts = [ cur.PointAt( par ) for par in pars ]
  cen = reduce( lambda a, b : a + b, pts ) / len( pts )
  return cen

def cent2( cur, cnt ):
  '''
  algorithm 2
  '''
  pars = cur.DivideByCount( cnt, True )
  pts = [ cur.PointAt( par ) for par in pars ]
  pts[ 0 ] = ( pts[ 0 ] + pts[ -1 ] ) / 2.0
  pts.pop()
  cen = reduce( lambda a, b : a + b, pts ) / len( pts )
  return cen

def cent3( cur, cnt ):
  '''
  algorithm 3
  '''
  pars = cur.DivideByCount( cnt, True )
  pars = pars[ 1 : : 2 ]
  pts = [ cur.PointAt( par ) for par in pars ]
  cen = reduce( lambda a, b : a + b, pts ) / len( pts )
  return cen

def main():
  gob = Rhino.Input.Custom.GetObject()
  gob.AcceptNothing( True )
  gob.SetCommandPrompt( 'Curve for centroid ?' )
  gob.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
  gob.Get()
  res = gob.Result()
  if res == Rhino.Input.GetResult.Cancel:
    return
  elif res == Rhino.Input.GetResult.Object:
    obref = gob.Object( 0 )
    cur = obref.Curve()
  else:
    return
  tolr = 0.001
 
  cnt = 4
  prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
  itr = 0
  att = Rhino.DocObjects.ObjectAttributes()
  att.ObjectColor = System.Drawing.Color.Red
  att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
  while True:
    scriptcontext.escape_test()
    itr += 1
    cen = cent1( cur, cnt )
    if cen.DistanceTo( prev ) < tolr:
      print 'Algorithm 1: %d iterations' % itr
      Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
      break
    prev = cen
    cnt += 2
 
  cnt = 4
  prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
  itr = 0
  att = Rhino.DocObjects.ObjectAttributes()
  att.ObjectColor = System.Drawing.Color.Green
  att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
  while True:
    scriptcontext.escape_test()
    itr += 1
    cen = cent2( cur, cnt )
    if cen.DistanceTo( prev ) < tolr:
      print 'Algorithm 2: %d iterations' % itr
      Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
      break
    prev = cen
    cnt += 2
 
  cnt = 4
  prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
  itr = 0
  att = Rhino.DocObjects.ObjectAttributes()
  att.ObjectColor = System.Drawing.Color.Blue
  att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
  while True:
    scriptcontext.escape_test()
    itr += 1
    cen = cent3( cur, cnt )
    if cen.DistanceTo( prev ) < tolr:
      print 'Algorithm 3: %d iterations' % itr
      Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
      break
    prev = cen
    cnt += 2
      
# Rhino.RhinoDoc.ActiveDoc.Views.Redraw()

main()