Ruby how to make face into component

In sketchup, we can select a face, click rightly to display a menu, in a menu, we can select “create component”, it can convert a face to a component instance.

but if i use ruby , how do i implement the function?

SketchUp doesn’t convert the face into a component, but create a component with the face (or whatever selection) inside and deletes the face outside.

In Ruby, you create a component definition first, then add a new face to its entities collection (entities.add_face(…)) and then add an instance to the model.

To transfer an existing face, it is easier to create a group and turn it into a component:

group = model.entities.add_group(existing_face)
component_instance = group.to_component
# = "…"

One caveat…

group = model.entities.add_group(existing_face)

will Bugsplat it model.entities are not the current ‘active_entities’ and ‘existing_face’ is not in that active context !
So use

group = model.active_entities.add_group(existing_face)

AND also check that


If your code works on a selected face then it’s likely that it is in the active context.
The new group/component-instance will be in the same context.
You can always add another instance of that definition into another context and delete the original, if your plans require that ?

If it isn’t already in the active context, then you need to get the face’s geometry data etc, and replicate it inside a new definition [group or component]…


you know, some face is 2d, and some face is 3d. 3d can convert to component, how do i judge a face is 3d or 2d?

Although a face is planar (2d) it is in SketchUp always embedded in 3d space (and enclosed by edges). Can you try to explain again what you mean, maybe you are thinking of a concept and using words that we understood differently?

  • Do you mean faces are planar (a region on an imaginary plane)?
  • Do you mean curved surfaces (not planar, thus “3d”, but they consist of many faces) vs. a single face?
  • Do you mean faces parallel to the ground plane (like when using SketchUp for 2d drawings)?

yes, many faces vs a single face.

[quote=“TIG, post:3, topic:46667”]
group = model.active_entities.add_group(existing_face)
[/quote] Make an array of the wanted existing_faces in the current active_entities context…
Then use:
group = model.active_entities.add_group(existing_faces)

The faces’ edges should come with them…

In Ruby you either have one <#Sketchup::Face> entity or many and that should be trivial to distinguish. It depends on the context of your Ruby code. If you obtain the face(s) from a selection, then

faces = model.selection.grep(Sketchup::Face)
faces.length == 1 # true if only a single face
faces.length >  1 # true if many faces selected
faces.empty?      # true if no face selected
!faces.empty? && (faces - faces.first.all_connected.grep(Sketchup::Face)).empty? # true if faces are all connected with an arbitrarily chosen faces.first

thank you. i’ll hava a try. you answer will give me great help.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.