Need help: compute centerline through a 3D model

Anyone have an idea how to find the center point through a model from start to end?
I can’t seem to find an API that supports this.

Alternative (a bit clearer / more technical):
Does anyone know a good way to compute a model’s centerline (center point path) from one end to the other?
I haven’t found an API that can do this out of the box.

I wonder, for example, where is the centerline of this model?
Where this model start and where is the end?
On what basis would anyone generate a centerline? How many of centerlines are there? Which ones are you talking about?

Are you talking about Ruby API or SDK?

The API is there, but I don’t think there’s a method like “create_a_centerline_of_model”. You need to define more precisely what you want.. and create your own method.

Do you think it is something like this: Hobby mihai.s - #462 by mihai.s

1 Like

ehm, i want to find the centerpoint trueout the pipe from start to finish, like the exact route, so i can reuse it in civil3d. so i have the centerline of the pipeline from a single click, i understand?

No, there is no such method. Basically, everything in Sketchup consists of edges and the faces they bound. So it will be quite difficult to determine which edges make up the tube and in what order…

Some ideas:
Edges can be part of (defined by) ArcCurves, for which there is a method for determining the center, for example. So you have to examine the model and search edge by edge based on certain properties..
Or you can also observe the bounding boxes of entities, or collection of entities.
I linked to another method in my previous post.

So i have been trying prob 10 different ways to get this done, ray tracing, edges, wireframe and what not. But i cant seem to get happy with the results, dosnt really work.. i think im might fucking it up myself haha

one of my approches way like, creating guide points from the ray trace, then making those “point clysters” in to one group, making a senterpoint from each selected groupes and then creating a line form start to finish from the points.. but i dosnt work on all models…

I’m maybe this can help:

Well, I posted that already…

I guess you want to use Ruby API, so I moved the topic to that category…

yeah, the api. Just cant wrap my head around what would accualy work on all models, not matter what. tried to diffent codes in one, like first finding all edges then doing the ray trace arpudn the model with x-sections.. but cant seem to make it work properly

There are multible selections like this, for this to work the best on pipes u can use the 3 point selection to accualy get the middle point of a circle.

But i want to automate this process from the begnning.

Cool. Didn’t see that you did. You are clearly a god among mere mortals here.

Yes, 3 point can define a circle. The only “minor” issue is how the algorithm will select that 3 point.

Good luck with it. I cant do it for sure.

Thanks for the input, no the less!

yeah, i agree… thats the hassle, its like an ai task haha..

But i have manged to get a way to work on “straight” pipes thats all together.. but i have some weeknesses..

model = Sketchup.active_model
bounds = model.bounds #-> a Geom::BoundingBox
center_pt = bounds.center

This would be a center line. Bounding boxes are aligned with the axes.
In 3D there would be 3 centerlines. Example for the one parallel to X axis:

# Adds a center line parallel to the X axis.
def center_line_x
  model = Sketchup.active_model
  bounds = model.bounds #-> a Geom::BoundingBox
  origin = bounds.corner(0)
  x_dim = bounds.width
  y_dim = bounds.height
  z_dim = bounds.depth
  # Add the center line:
  pt1 = origin.transform( [0, y_dim/2.0, z_dim/2.0] )
  pt2 = origin.transform( [x_dim, y_dim/2.0, z_dim/2.0] )
  ents = model.active_entities
  ents.add_cline(pt1, pt2)
end
1 Like

If the pipe is a group or component instance, and is selected this works.
(Assumes that the points are in end to end order.)

# Adds a center line through a pipe object.
def center_line_pipe(pipe = Sketchup.active_model.selection.first)
  #puts pipe
  return false unless pipe && pipe.respond_to?(:definition)
  ents = pipe.definition.entities
  #puts ents.inspect
  curve_edges = ents.grep(Sketchup::Edge).select(&:curve)
  curves = curve_edges.map(&:curve)
  curves.uniq!
  circles = curves.select { |curve|
    curve.respond_to?(:center) && curve.circular?
  }
  #puts circles.inspect
  centers = circles.map(&:center)
  #puts centers.inspect
  unique = []
  # Compare using the API's Geom::Point2d#== method:
  centers.each do |pt|
    unique << pt unless unique.any? { |u| u == pt }
  end
  #puts unique.inspect
  clines = []
  # Add the center line(s):
  while unique.size > 1
    pt1 = unique.shift
    pt2 = unique.first
    clines << ents.add_cline(pt1, pt2)
  end
  return clines
end
1 Like

Appreciate all the input. Just to reset the scope: my goal is programmatic reconstruction of a pipe centerline in SketchUp Ruby, not manual modeling steps.
The pipe is typically imported/triangulated and can be made of multiple sections with bends/offsets, so bounding-box center or a single centroid/COM isn’t usable.
What I’m looking for is an approach like:

  • detect circular/near-circular cross-section “rings” along the tube (edge loops or face loops),

  • fit circles in 3D (least squares) to get ring centers + local axis,

  • connect those centers into a clean polyline (then optionally smooth).
    Alternative approaches (ray casting to find local interior center, or any skeletonization/medial-axis approximation that’s feasible in Ruby) are also welcome.
    If you’ve got sample code or known patterns for (a) ring detection, (b) 3D circle fitting, or (c) robust centerline tracing through mesh tubes in SketchUp’s API, that would be super helpful. I’ll share what I end up implementing once tested.

    Added 2 .skp files with 2 different types of creation of the pipes, for example

pipe - electrical.skp (9.0 MB)

pipe - Water and sewers.skp (3.0 MB)

Please add this to the initial post as this changes the game drastically.

  • What format is the imported file?

  • Are you using Model#import with the :merge_coplanar_faces set to true ?

  • If so, are the “rings” converted into SketchUp curve objects?
    If not, then this really should be a bug in the importer.


For now, I’ll let my previous code snippets stand as they should work uponwell formed SketchUp geometry.

1 Like