ruby code editor  Topic is solved

ruby code editor

Postby odowhacker » Sun May 24, 2020 9:14 pm

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 selection

width = 36 # Width of panel
n = 10 # Number of circles in each direction
s = width / (n+1).to_f # Spacing of circles
w = 50
# Add 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+i*s,s+j*s,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 edges

cface.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:in `rescue in block in initialize'
C:/Users/errfitt/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:368:in `block in initialize'
SketchUp:1:in `call'

can someone tell me what's wrong and what i can do to solve this?

thanks a bunch
0

odowhacker 
 

Re: ruby code editor  Topic is solved

Postby McGordon » Sun May 24, 2020 10:06 pm

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.
1

McGordon 
Premium Member
Premium Member
 

Re: ruby code editor

Postby odowhacker » Mon May 25, 2020 4:25 am

thank you very much. :thumb:
0

odowhacker 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Plugins

Who is online

Users browsing this forum: kaiomichiru and 11 guests

Visit our sponsors: