Hey everyone,
I’m trying to automate some modeling but I can’t get the syntax for the functions to work in Sketchup. Here’s the code that I have right now
def wrud (width, running_vertical)
point1 = Geom::Point3d.new(running_vertical+width,$counter_overhangF,$kickplate_height)
point2 = Geom::Point3d.new(running_vertical+width,$counter_overhangF,$countertop_height - $countertop_thickness)
line = entities.add_line(point1, point2)
running_vertical = running_vertical + width
return running_vertical
end
wrud (10, running_vertical)
wrud (20, running_vertical)
I defined the variable running_vertical at the top and want it to increase every time I call the function (eg. if running_vertical = 0 at the beginning, I want it to equal 30 by the end).
The function essentially draws a line a “width” unit away from the original running_vertical.
Can someone let me know if my syntax is correct or if there is a mistake in my logic? Thank you so much!
-
ALL code must be within a module. Usually a top level namespace module and then inside a specific extension submodule.
(Ie, the toplevel of Ruby is Object, which is an ancestor of everything else, so you should not define methods at the top level as everyone else’s classes and modules will inherit them.)
-
Do not ever use global variables (despite older poor examples that do.)
Instead use @vars or @@vars within your modules | submodules for persistent references.
Otherwise pass values as method parameters.
-
Do not put a space between method name and the ( of it’s parameter list.
This will cause warnings. (In a future Ruby version it mat result in a SyntaxError
exception.)
-
Please post code correctly in forum with tabs replaced with 2 space characters.
Something more like …
# encoding: UTF-8
module GardenLiving
module Test
extend self
@counter_overhangF ||= 2.0
@countertop_height ||= 34.0
@countertop_thickness ||= 1.5
@kickplate_height ||= 3.0
@start_verticle ||= 0
def wrud(width, running_vertical)
point1 = Geom::Point3d.new(
running_vertical+width, @counter_overhangF, @kickplate_height
)
point2 = Geom::Point3d.new(
running_vertical+width, @counter_overhangF,
@countertop_height - @countertop_thickness
)
entities.add_line(point1, point2)
end
def test_command
for running_vertical in @start_verticle..30
wrud(width, running_vertical)
end
end
if !defined?(@loaded)
UI.menu("Plugins").add_item("GardenLiving - Test") {
test_command()
}
@loaded = true
end
end # submodule
end # namespace module
for
… in
looping statement is only one of the iterative staements. See …
File: control_expressions.rdoc [Ruby 2.7.1]
There are also many built-in iterator methods for the Ruby Array
class (which also has the Enumerable
module mixed in.)