Ruby code editor
-
hallo all,
i am trying to get used to ruby again - working with the ruby code editor 4.3 and sketchup pro 2016.
working with one of the script examples 'using formulas for patterned panels' i'm running into a problem when i try to modify this script slightly.
the example is about drawing circles in a plane. i'm trying to add faces to these circles and extrude them to cylinders. i'm getting errors that seem impossible to me - so i need your help.after much browsing QA articles with helpful responses for instance by TIG i noticed that possibly the error might have to do with the editor.
script:
###############################################
Script sample from Alexander C. Schreyer's book
"Architectural Design with SketchUp"
###############################################
Creates a square panel with a sinusoidal hole pattern
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selectionwidth = 36 # Width of panel
n = 10 # Number of circles in each direction
s = width / (n+1).to_f # Spacing of circles
w = 50Add the square base for the panel
face = ent.add_face [0,0,0] , [width,0,0] , [width,width,0] , [0,width,0]
Iterate in the panel plane
(0..n-1).each { |i|
(0..n-1).each { |j|
# Create a group for each circle
group = ent.add_group()
ents = group.entities
# Add the circle
centerpoint = [s+is,s+js,0]
vector = [0,0,1]
radius = Math::sin( i / (n-1).to_f * Math::PI ) * s / 5.0 + Math::sin( j / (n-1).to_f * Math::PI ) * s / 5.0
edges = ents.add_circle centerpoint , vector , radius , 24#various trial and error to get a face inside
#edges[0].find_faces
#cface = edges[0].faces[0]
cface = group.entities.add_face edgescface.back_material = [ (255 / (n-1)*i).round , (255 / (n-1)*j).round ,(255 / (n-1)*j).round ] #face.pushpull -w group.explode}
}
the add_face command is followed by error: not enough edges
if i omit the command i'll get error: undefined method 'back_materia=' for nil so a face isn't created no matter which approach i use.the ruby console tellsme:
"undefined method
back_material=' for nil:NilClass (Line 37)" Error: #<TypeError: no implicit conversion of NoMethodError into String> C:/Users/errfitt/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:346:in+'
C:/Users/errfitt/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:346:inrescue in block in initialize' C:/Users/errfitt/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:368:inblock in initialize'
SketchUp:1:in `call'can someone tell me what's wrong and what i can do to solve this?
thanks a bunch
-
The first time your nested i, j loop runs, i and j are both zero, so your radius will be zero as sin(0) = 0.
The add_circle method is called with:
add_circle([3.272727272727273, 3.272727272727273, 0], [0, 0, 1], 0.0, 24)
which has a radius of 0.0 so it will sort of fail and return 2 edges instead of 24. Then you won't be able to find faces when there's only 2 edges and you'll get the errors you saw.I haven't used that code editor or read that book. The error is nothing to do with your editor. As I don't have access to the book I don't know which parts you've changed.
I'm not entirely sure what it's supposed to look like, but here's what I've got.

I put it all in a group so it doesn't stick to existing geometry. I just erase the failed groups where add_circle fails to form a circle with 24 segments. You could check for radius being 0.0 but there's also some cases where it's extremely small e.g. i=9, j=9 where the radius is 1.6e-16. I left in some of puts statements I had for debugging to let you see what's going on.
-
thank you very much.

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement