How can I create a tube with all faces being red?

I need to create a tube with all sides (also the inner part) being red. Here is my code:

model = Sketchup.active_model
entities = model.entities

tubeOuterWidth = 10.5
tubeInnerWidth = 7

#Draw smal circle
edges2 = entities.add_circle [0,0,10],[0,0,-10],(tubeInnerWidth / 2)
curve2 = edges2[0].curve
face2 = entities.add_face(curve2) 

#Draw bigger circle
edges1 = entities.add_circle [0,0,0],[0,0,10],(tubeOuterWidth / 2)
curve1 = edges1[0].curve
face1 = entities.add_face(curve1)

#Make 3D tube
face1.pushpull( -10 )
face2.pushpull( 10 ) 

#Color all faces red, also the top and inner part of the tube!!!!!!!!!!
faces = Sketchup.active_model.entities.grep(Sketchup::Face).find_all { |f| !f.deleted? }
faces.each do |face3|
face3.material = 'Red'
end

The outcome is that the bottom and round part of the outer cilinder is red, but the top and inner part not? I am new to SketchUp and Ruby and don’t know why this does not do what I want.

image

You were almost correct, but your statements were a bit out of order …

def make_tube(
    outer_width = 10.5,
    inner_width = 7
  )
  #
  model = Sketchup.active_model
  model.start_operation('Draw Tube', true)
    #
    editing_context = model.active_entities
    
    # Create a group context:
    group = editing_context.add_group
    entities = group.entities

    # Draw outer circle:
    edges_outer = entities.add_circle( ORIGIN, Z_AXIS, (outer_width / 2) )

    # Make the bottom face:
    curve_outer = edges_outer.first.curve
    entities.add_face(curve_outer)

    # Draw inner circle (which divides the bottom face into 2 faces):
    edges_inner = entities.add_circle( ORIGIN, Z_AXIS, (inner_width / 2) )

    # Get the outer face, which is that common to an outer edge and an inner edge:
    outer_face = edges_outer[0].common_face(edges_inner[0])

    # Get the faces set for the smaller circle (ie, both faces):
    both_faces = edges_inner[0].faces

    # Subtract an array of the outer face from the faces array, getting the remaining face:
    inner_face = ( both_faces - [outer_face] ).first

    # Erase the inner face:
    inner_face.erase!

    # Faces drawn with the API upon the ground plane are facing down, so ...
    outer_face.reverse!

    # Color the face:
    outer_face.material = 'Red'

    # Make 3D tube:
    outer_face.pushpull( 10 )
    #
  model.commit_operation
  #
  return group
end

(… scroll to see all code …)

Thanks for your solution, but it did not work for me. In the Ruby console I get below message and no tube:

Running the code...
Done running code. Ruby says: make_tube

But after playing with your code and changing it, I got a script that does work for me:

outer_width = 10.5
inner_width = 7
      
# Draw outer circle:
edges_outer = entities.add_circle( ORIGIN, Z_AXIS, (outer_width / 2) )

# Make the bottom face:
curve_outer = edges_outer.first.curve
entities.add_face(curve_outer)

# Draw inner circle (which divides the bottom face into 2 faces):
edges_inner = entities.add_circle( ORIGIN, Z_AXIS, (inner_width / 2) )

# Get the outer face, which is that common to an outer edge and an inner edge:
outer_face = edges_outer[0].common_face(edges_inner[0])

# Get the faces set for the smaller circle (ie, both faces):
both_faces = edges_inner[0].faces

# Subtract an array of the outer face from the faces array, getting the remaining face:
inner_face = ( both_faces - [outer_face] ).first

# Erase the inner face:
inner_face.erase!

# Faces drawn with the API upon the ground plane are facing down, so ...
outer_face.reverse!

# Color the face:
outer_face.material = 'Red'

# Make 3D tube:
outer_face.pushpull( 10 )

So thanks again for your effort and I don’t understand why your code didn’t work for me or my first code, but I am happy I have a working script now.

Perhaps you need to learn the Ruby basics? A method must be called.

The “Ruby says: make_tube” is just the interpreter returning the name of the method just defined to your console plugin.

So, after pasting the method definition (as I wrote it) into your console, you must them type the name of the method and press ENTER.

For example …

make_tube(12, 8)

… then ENTER and the method will be called using the arguments in the method call instead of the default arguments.

You should at the least go through a couple of the Ruby language primers. They are posted in my lists: