Selecting a Curved Surface with Ruby API


#1

Hello,

I have imported a solid part from another software as STL. In Sketchup, the triangles making up the surfaces are obviously shown. AFter softening the edges, all these triangles disappeared but something wierd is going on:
I clicked on one of the curved surfaces that already had many triangles initially before smoothening the edges, but in Ruby, when I try to get the selection by typing:
sel = Sketchup.active_model.selection
the whole surface could not be retrieved and only a triangle at which I clicked the mouse on has been selected. So the question, how can I get a curved surface as an object in ruby API?

Acually, I’m trying to color a curved surface from ruby. That’s what I want to achieve eventually.

Regards


#2

A Surface is not an entity type, and smoothing the edges would not convert the triangles into a new Surface entity. Knowing this, the expected result in the Ruby API is that the selection contains an array of Sketchup::Face.

Any action you want to apply to the complete surface needs to be applied to each face.


#3

Thanks Aerilius,

Actually, I tried to color each face of the surface, but they are a lot (thousands). So it is taking too much time to color the surface if I pass through each face independently and give its material a color.
Note that if I want to give the surface material a color manually from the Sketchup interface, it is done in a second! That’s why I thought there’s some other faster way.


#4

It is normal for a Ruby script to take longer to complete a task than the compiled code behind the GUI. For complex processing, sometimes a lot longer. But it is also possible that your Ruby code is not efficient, either due to the algorithm you are using or due to the specific Ruby API methods you are employing. If you want to share your code, some of the gurus here might be able to help improve performance.


#5

Simply, I selected a surface which I already smoothed its edges. The number of faces is more than 3000. And I want to color them all. Here’s the code:

color = Sketchup::Color.new("green")
selection.each {  |entity|
    	entity.material = color if entity.is_a?(Sketchup::Face)
}

#6

Try this one-liner - it skips any testing of non-faces - e.g. edges…
Select the surface and run it…

Sketchup.active_model.selection.grep(Sketchup::Face).each{|e|e.material="Green"}


#7

Thanks TIG,

Unfortunately, it took too much time also.


#8

If you are running it from the Ruby Console, try adding some nils to prevent output to the Ruby Console, which can bog down execution:

Sketchup.active_model.selection.grep(Sketchup::Face).each{|e|e.material="Green";nil};nil

#9

No I did it from within a plugin that I am developing currently. I’m not doing it from the ruby console


#10

What about grouping all the faces and common edges, then applying the color to the group ?


#11

Hi Dan,
What do you mean by applying the color to the group? It is not possible as far as I know to apply a color to group as color can only be applied to face.


#12

Oh Dan … You are right!!! It worked

Thanks Thanks :slight_smile:


#13

Not actually true.

You can apply a “parent” material to a group or component instance, and any child faces that have nil assigned as their material property (called the default material,) will render with the parent’s material.

See the User Guide:

You can apply materials to a group or component as whole or to specific geometry within the group or component:

  • To paint a whole group or component, select it and apply a material. When you select a group or component and apply a material, only faces in the default material are painted.
  • To paint geometry within the group or component, double-click it to open its context. Then select the faces you want to paint with a material.

There is this embedded tutorial video in the guide just after the above statement:


#14

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