Patterned_panel in Ruby


#1

Hello fellow users of Sktechup,
who can help me with the following problem in ruby.
i have a plate and make randum holes in it
i have made an program in ruby, but it don’t work.
this is the script…
mod = Sketchup.active_model # Open model
entities = mod.active_entities # All entities in model
grp= entities.add_group # empty group
group = grp.entities

width = 36 # width of panel
n = 10 # number of circles in each direcction
s = width / (n+1).to_f # Spacing of circles

add the square base for the panel

face = group.add_face [0,0,0],[width,0,0],[width,width,0],[0,width,0]
face.back_material = “yellow”

Interate in the panel plane

(0…n-1).each { |i| (0…n-1).each { |j|

add the circles

radius = Math::sin(i/ (n-1).to_f1Math::PI)s/ 5.0 + Math::sin( j/ (n-1).to_f1*Math::PI)*s / 5.0
circle = group.add_circle [s+i*s,s+j*s,0],[0,0,1], radius
circle_face = group.add_face circle
circle_face.erase!
}
}

hope one of you give me the answer…?
thanks


#2

What specifically goes wrong? Any errors reported in the Ruby Console?


#3

when you run the script , the plate with the holes in it will not appear.
ruby says Not enough edges - at least 3 required
maybe i forget something to program…?
when you mark the last two line
circle_face = group.add_face circle
circle_face.erase!
the plate will appear , but no holes.


#4

When you post code to the forum, please either upload a file or precede and follow the code listing with triple backticks a-la:

 ```ruby
 # your listing here
Otherwise the forum display software processes your listing as Markdown code and reformats it in a way that makes it very hard to read and copy for independent testing.

I have to run some errands now, but I'll get back to this as soon as I am able.

#5

There are a couple of problems with your code.

The specific one causing the error message is that the radius you calculate at each of the four corner holes is either exactly or effectively zero. This causes group.add_circle to create only two zero-length edges, which aren’t enough to create a face in group.add_face circle. You will need to do something to trap these zero-radius cases or change your formula.

The second one is that group.add_face circle fails (you aren’t seeing this yet because the radius bug gets trapped before the code gets there) and returns nil, which can’t be erased! I’m not sure why that fails, but here’s a different way to go about it:

# instead of 
# circle_face = group.add_face circle
# circle_face.erase!

# circle is an array of Edges, Edge.faces returns an array of the Faces associated with that Edge
faces = circle[0].faces
# one of the associated Faces will be the original face, so look for a new one to erase
faces.each {|cface| next if cface == face; cface.erase!; break}

Edit: I discovered that add_face fails when the face already exists. In this case, when you draw the circle on the large rectangle face, the API also creates the face bounded by the circle and removes it from the large face. So, you have to go looking for the new Face per my code above.


#6

you’re right for For the last two lines of my script.
i study the formula i used and yes …there was someting wrong with the four corner holes.
but you gave me the solution… and i learn from such errors.
I appreciate your help
thanks


#7

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