Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Discussions > SketchUp Ruby API > Structure in a script ???
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  17 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Keld Sørensen  
View profile  
 More options Feb 6, 9:54 pm
From: Keld Sørensen <keld.soeren...@os.dk>
Date: Mon, 6 Feb 2012 18:54:51 -0800 (PST)
Local: Mon, Feb 6 2012 9:54 pm
Subject: Structure in a script ???

I found this code in the book

Automatic SketchUp
Creating 3-D Models
in Ruby
by Matthew Scarpino

page 260 ..

I have tried to figure out the structure in this code and inserted some
comments with what I mean is what BUT ...

I have some "end"s I can't figure out and the code in the lower part
defining the toolbar - is it part of a class, def or what ????? ... and no
"end" in the end !

It works but is something left out ?

Please, can someone explain for me ?

class SphereTool

def activate
$ents = Sketchup.active_model.entities
# The points clicked by the user
@pt1 = Sketchup::InputPoint.new
@pt2 = Sketchup::InputPoint.new
# The initial state (user hasn't clicked yet)
@first_click = false
end

# If the user clicked, draw a line
def onMouseMove flags, x, y, view
if @first_click
@pt2.pick view, x, y, @pt1
view.invalidate
end

end # end class ?????????????????

# Check the state, then draw a sphere or a point
def onLButtonDown flags, x, y, view
if @first_click
if (@pt1.position.distance @pt2.position) > 0
# Remove the construction point
$ents.erase_entities $c_point
draw_sphere
end
else
@pt1.pick view, x, y
$c_point = $ents.add_cpoint @pt1.position
@first_click = true
end
end

def draw view
if @first_click && @pt2.valid?
view.set_color_from_line @pt1.position, @pt2.position
view.line_width = 3
view.draw_line @pt1.position, @pt2.position
end
end

# Draw the sphere
def draw_sphere
# Draw the circles
rad = @pt1.position.distance @pt2.position
circle = $ents.add_circle @pt1.position, [1, 0, 0], rad
path = $ents.add_circle @pt1.position, [0, 1, 0], rad + 1
circle_face = $ents.add_face circle
# Extrude the sphere and erase the extrusion path
circle_face.followme path
$ents.erase_entities path
reset
end
 # Return to original state
def reset
@pt1.clear
@pt2.clear
@first_click = false
end

# Respond when user presses Escape
def onCancel flags, view
reset
end
end     # end of what ????????????????        

# and what is this next stuff
# yeah, defines a toolbar in the end

# Create the Command object
sphere_cmd = UI::Command.new("Sphere") {
Sketchup.active_model.select_tool SphereTool.new

}

 # Configure the Command's appearance
sphere_cmd.small_icon = "sphere_small.gif"
sphere_cmd.large_icon = "sphere_large.gif"
sphere_cmd.tooltip = "Create a sphere"

# Create and configure the Toolbar
sphere_toolbar = UI::Toolbar.new "Sphere"
sphere_toolbar.add_item sphere_cmd
sphere_toolbar.show
SphereTool

# NO END ????? of what ???


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Todd Burch  
View profile  
 More options Feb 6, 10:21 pm
From: Todd Burch <toddbu...@mac.com>
Date: Mon, 06 Feb 2012 21:21:52 -0600
Local: Mon, Feb 6 2012 10:21 pm
Subject: Re: Structure in a script ???
You missed an END on an IF

On Feb 6, 2012, at 8:54 PM, Keld Sørensen wrote:

I found this code in the book
...

class SphereTool

        def activate
                $ents = Sketchup.active_model.entities
                        # The points clicked by the user
                @pt1 = Sketchup::InputPoint.new
                @pt2 = Sketchup::InputPoint.new
                        # The initial state (user hasn't clicked yet)
                @first_click = false
        end

                # If the user clicked, draw a line
        def onMouseMove flags, x, y, view
                if @first_click
                @pt2.pick view, x, y, @pt1
                view.invalidate
        end  <===  END OF  if @first_click

end             # end class ?????????????????  <=== NO, END OF def  onMouseMove
. . .

end     # end of what ????????????????         <=== END OF CLASS

                # and what is this next stuff
                # yeah, defines a toolbar in the end

                # Create the Command object
        sphere_cmd = UI::Command.new("Sphere") {
                Sketchup.active_model.select_tool SphereTool.new
        }

                # Configure the Command's appearance
        sphere_cmd.small_icon = "sphere_small.gif"
        sphere_cmd.large_icon = "sphere_large.gif"
        sphere_cmd.tooltip = "Create a sphere"

                # Create and configure the Toolbar
        sphere_toolbar = UI::Toolbar.new "Sphere"
        sphere_toolbar.add_item sphere_cmd
        sphere_toolbar.show
        SphereTool

# NO END ????? of what ???  <== NO END REQUIRED - the above code  
executes when the script is loaded.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r  
View profile  
 More options Feb 6, 10:24 pm
From: r <rt8...@gmail.com>
Date: Mon, 6 Feb 2012 19:24:31 -0800 (PST)
Local: Mon, Feb 6 2012 10:24 pm
Subject: Re: Structure in a script ???

On Feb 6, 8:54 pm, Keld Sørensen <keld.soeren...@os.dk> wrote:

> I have some "end"s I can't figure out and the code in the lower part
> defining the toolbar - is it part of a class, def or what ????? ... and no
> "end" in the end !

Even an experienced programmer would have trouble "eyeball parsing"
code that lacks indentation. Humans are not machines, and thus we
require visual clues. Consider using proper indentation and i'll bet
you won't have as much trouble locating block closures.

It is customary to use four spaces (or a single tab) for indents and
dedents. I prefer spaces myself since spaces will always be consistent
across editors. I can *sometimes* tolerate 2 space indention. But for
christ sake, NEVER use 1 or 3 space indention!

module Foo
    class Bar
        def meth
            if a
                nil
            elsif b
                nil
            end
        end
    end
end


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keld Sørensen  
View profile  
 More options Feb 6, 10:28 pm
From: Keld Sørensen <keld.soeren...@os.dk>
Date: Mon, 6 Feb 2012 19:28:52 -0800 (PST)
Local: Mon, Feb 6 2012 10:28 pm
Subject: Re: Structure in a script ???

Argh .... :-((


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keld Sørensen  
View profile  
 More options Feb 6, 10:32 pm
From: Keld Sørensen <keld.soeren...@os.dk>
Date: Mon, 6 Feb 2012 19:32:03 -0800 (PST)
Local: Mon, Feb 6 2012 10:32 pm
Subject: Re: Structure in a script ???

And my code for the modul should be ....

module Foo
    class Bar
        def meth
            if a
                nil
            elsif b
                nil
            end
        end
    end
   ... HERE, well indented ?

end


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Rathbun  
View profile  
 More options Feb 7, 2:23 am
From: Dan Rathbun <danz...@earthlink.net>
Date: Mon, 6 Feb 2012 23:23:40 -0800 (PST)
Local: Tues, Feb 7 2012 2:23 am
Subject: Re: Structure in a script ???

Actually IMHO ... Ruby (if you look at all the code for the Extended
Library files, uses 2 spaces as the normal indent.

The 4 space indent standard was established back in the day, when
programming editors ran on textmode CRTs. Today everything runs in graphics
mode (SVGA or better,) and good quality code editors will "bracket" all the
blocks, with an indent line, that matches up a "begin" with it's "end", "{"
with it's "}", and so forth.

Keld.. if you are on Microsoft Windows, I strongly suggest that you install
and use Notepad++ (the unicode version.)

<http://notepad-plus-plus.org/>

Get it here:
http://notepad-plus-plus.org/

~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Rathbun  
View profile  
 More options Feb 7, 3:06 am
From: Dan Rathbun <danz...@earthlink.net>
Date: Tue, 7 Feb 2012 00:06:34 -0800 (PST)
Local: Tues, Feb 7 2012 3:06 am
Subject: Re: Structure in a script ???

Actually the sample code should have been wrapped in the author's namespace.

He shows declaring local variables, at the toplevel within *Object*. This
can cause every other object in ruby to inherit whatever is defined there.
(Because everything in ruby is a subclass of *Object*.)
ONLY Ruby library base classes belong at the toplevel, also called the *
ObjectSpace* (see the module of the same name.)

*require('sketchup.rb')*
*
*
*module Scarpino  # <-- Author's namespace*
*  module SphereMaker # <-- plugin namespace*
*
*
*    @@cmd = nil unless defined?(@@cmd)*
*    @@tool = nil unless defined?(@@tool)*
    @@toolbar = nil unless defined?(@@toolbar)
*
    class SphereTool
        def meth
            if a
                nil
            elsif b
                nil
            end
        end # def
    end # class SphereTool
*
*
*
*    def self.use*
*      # make sure there is always only ONE instance*
*      if not @@tool*
*        **@@tool = **SphereTool.new*
*      else*
*        @@tool*
*      end*
*    end # def*
*
*
*    ### RUN ONCE*
*    #*
*    unless file_loaded?(File.basename(__FILE__))*
*
*
*      # Create the Command object
      @@cmd = UI::Command.new("Sphere") {
        Sketchup.active_model.select_tool( self.use )
      }

      # Configure the Command's appearance
      @@cmd.small_icon = "sphere_small.gif"
      @@cmd.large_icon = "sphere_large.gif"
      @@cmd.tooltip = "Create a sphere"*

*      # Create and configure the Toolbar
      @@toolbar = UI::Toolbar.new "Sphere"
      @@toolbar.add_item sphere_cmd
      @@toolbar.show*
*
*
*      **file_loaded(File.basename(__FILE__))*
*    end*
*    #*
*    ### RUN ONCE*
*
*
*  end # module SphereMaker*
*end # module Scarpino*

The global methods *file_loaded* and *file_loaded?* come from *'sketchup.rb'
*, which is in the "*Tools*" dir.
The sample plugin file (above, along with the toolbar icon files,) should
go in it's own subdir, of the author's subdir, of the "*Plugins*" dir.
"*Plugins/Scarpino/SphereMaker/spheremaker.rb*"

Then in the "*Plugins*" dir, the author puts a *SketchupExtension*registration script.
"*Plugins/Scarpino_spheremaker_ext.rb*"
I suggest prepending your extension loader scripts with your Author
namespace+"_", and appending the filename with "_ext" (so we all know that
it registers a Sketchup extension.)

See the following Google Sketchup API blog posts:

   - Turn Your Plugin Into An Extension (Thursday, July 21, 2011)<http://sketchupapi.blogspot.com/2011/07/turn-your-plugin-into-extensi...>
   - Being a Good SketchUp Ruby Citizen Wednesday, (August 3, 2011)<http://sketchupapi.blogspot.com/2011/08/being-good-sketchup-ruby-citi...>
   - RBZs in SketchUp 8M2: Distribute Your Plugin as One File! (Thursday,
   December 1, 2011)<http://sketchupapi.blogspot.com/2011/12/rbzs-in-sketchup-8m2-distribu...>

See also my sample at SCF:

   - SketchupExtension and rbs rubies (August 2, 2011)<http://forums.sketchucation.com/viewtopic.php?f=180&t=39094>

:)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Rathbun  
View profile  
 More options Feb 7, 3:16 am
From: Dan Rathbun <danz...@earthlink.net>
Date: Tue, 7 Feb 2012 00:16:11 -0800 (PST)
Local: Tues, Feb 7 2012 3:16 am
Subject: Re: Structure in a script ???

Oops a booboo, in the 10th line From the bottom:

should be:
*@@toolbar.add_item @@cmd*

dang.. hate that we cannot edit posts (oh well.)

*Keld* please come over and join SketchUcation forums. They are real
forums. Much easier to post and edit code in the "Developers" forum.
We have built up a Library of Code Snippets there, and you can download a
ton of Plugins from the "Plugins" forum.
You have much more control over notifications, and so forth.
http://forums.sketchucation.com/index.php
~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keld Sørensen  
View profile  
 More options Feb 7, 3:46 am
From: Keld Sørensen <keld.soeren...@os.dk>
Date: Tue, 7 Feb 2012 00:46:48 -0800 (PST)
Local: Tues, Feb 7 2012 3:46 am
Subject: Re: Structure in a script ???

I would love to have en editor like that .... but it's not Ruby code ... I
beleave it's C++

Can I use this outlining in Ruby code too ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keld Sørensen  
View profile  
 More options Feb 7, 3:57 am
From: Keld Sørensen <keld.soeren...@os.dk>
Date: Tue, 7 Feb 2012 00:57:09 -0800 (PST)
Local: Tues, Feb 7 2012 3:57 am
Subject: Re: Structure in a script ???

Oh .. how many of all thouse awailable downloads shall I have ?

Witch one is the unicode one ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keld Sørensen  
View profile  
 More options Feb 7, 4:13 am
From: Keld Sørensen <keld.soeren...@os.dk>
Date: Tue, 7 Feb 2012 01:13:02 -0800 (PST)
Local: Tues, Feb 7 2012 4:13 am
Subject: Re: Structure in a script ???

Oh it's SO NICE an editor - I love it !


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Rathbun  
View profile  
 More options Feb 7, 4:51 am
From: Dan Rathbun <danz...@earthlink.net>
Date: Tue, 7 Feb 2012 01:51:35 -0800 (PST)
Local: Tues, Feb 7 2012 4:51 am
Subject: Re: Structure in a script ???

Told ya' so !!  Told ya' so !! Ninny nanny poo poo !

It recognizes programming languages by their file extension, and then
hitlites them according to configure files for each individual language.

You can set up each language to have different idents, file encoding, and
EOL characters.

I suggest you set rb files to always save as ANSI encoding (required by
Ruby,) with Unix end of Line characters (because that makes them
cross-platform for both Mac and PC.)

In the "Style Configurator", I use the "Deep Space" theme (slightly
modified,) so I can see the tag matching lines more easily.
Observe the grey vertical dotted lines, that match tags for the beginning
and end of block statements:

<https://lh4.googleusercontent.com/-KKhkBsDsTZM/TzDyOc8eU-I/AAAAAAAAAA...>
If you click the cursor on the first or last line of a block, the fold
brackets, in the left "fold margin" (grey background in my theme,) will
highlight (in bright red in my theme.)

Make sure tag matching and smart hilighting is ON
Settings (menu) > Preferences ... > MISC. (dialog tab)

~ have fun programming !


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Rathbun  
View profile  
 More options Feb 7, 5:01 am
From: Dan Rathbun <danz...@earthlink.net>
Date: Tue, 7 Feb 2012 02:01:41 -0800 (PST)
Local: Tues, Feb 7 2012 5:01 am
Subject: Re: Structure in a script ???

If you wish to run Notepad++ in Danish, you'll need to d/l the Danish
language XML file:
http://websvn.tuxfamily.org/dl.php?repname=notepadplus/repository&pat...

Follow the instructions at the top of this page:
http://notepad-plus-plus.org/contribute/binary-translations.html

~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keld Sørensen  
View profile  
 More options Feb 7, 5:11 am
From: Keld Sørensen <keld.soeren...@os.dk>
Date: Tue, 7 Feb 2012 02:11:03 -0800 (PST)
Local: Tues, Feb 7 2012 5:11 am
Subject: Re: Structure in a script ???

I think I'll stick to the english language because it's a dilemma when you
are a newbee as needs help all the time !

But thx for the info anyway !


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r  
View profile  
 More options Feb 7, 9:48 am
From: r <rt8...@gmail.com>
Date: Tue, 7 Feb 2012 06:48:15 -0800 (PST)
Local: Tues, Feb 7 2012 9:48 am
Subject: Re: Structure in a script ???

On Feb 7, 2:06 am, Dan Rathbun <danz...@earthlink.net> wrote:

> [...]
> *module Scarpino  # <-- Author's namespace*
> *  module SphereMaker # <-- plugin namespace*

Your example is correct, however i need to clear up a possible
confusion.

The example you provide is correct and will work, however, if Keld
takes the example too literally, he may falsely believe that he must
write "module Scarpino"  followed by "\n....module X"  each and every
time he wants to create a new nested module UNDER "Scarpino". In fact,
all he needs to do is create the top-level module "Scarpino" once...

module Scarpino
    nil
end

If module "Scapino" is nothing more than a top-level namespace, then
the above code should be placed in a file that is read each time
SketchUp starts. Now, whenever you need to nest a new module UNDER
Scarpino you simply do:

module Scarpino::Blah
    # code here
end

...as you can see this method saves one level of indentation.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r  
View profile  
 More options Feb 7, 11:17 am
From: r <rt8...@gmail.com>
Date: Tue, 7 Feb 2012 08:17:45 -0800 (PST)
Local: Tues, Feb 7 2012 11:17 am
Subject: Re: Structure in a script ???

On Feb 7, 1:23 am, Dan Rathbun <danz...@earthlink.net> wrote:

> Actually IMHO ... Ruby (if you look at all the code for the Extended
> Library files, uses 2 spaces as the normal indent.

Well is a direct effect of one person (the lead developer) deciding to
use 2 space indention. I don't think that creates any "official" Ruby
policy.

> The 4 space indent standard was established back in the day, when
> programming editors ran on textmode CRTs. Today everything runs in graphics
> mode (SVGA or better,)

I don't see how that is relevant to the argument. I don't use four
space indent because it's historical. I use four space indent because
it's practical. As vertical space increases, lining up openers and
closures in a dense field of a two space indent becomes more and more
difficult. At the point where four space indent/dedent becomes
difficult to line up, you realize your blocks are far too long and
need restructuring :-)

> and good quality code editors will "bracket" all the
> blocks, with an indent line, that matches up a "begin" with it's "end", "{"
> with it's "}", and so forth.

Agreed. Any good editor should use some form of marking or highlight
to denote openers and closures. Bracket Matching is good enough for
me.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r  
View profile  
 More options Feb 7, 1:19 pm
From: r <rt8...@gmail.com>
Date: Tue, 7 Feb 2012 10:19:21 -0800 (PST)
Local: Tues, Feb 7 2012 1:19 pm
Subject: Re: Structure in a script ???
On Feb 6, 9:32 pm, Keld Sørensen <keld.soeren...@os.dk> wrote:

> And my code for the modul should be ....

> module Foo
>     class Bar
>         def meth
>             if a
>                 nil
>             elsif b
>                 nil
>             end
>         end
>     end
>    ... HERE, well indented ?

> end

Well, not exactly. My simplistic example was only meant to show two
general ideas:

 1. Code should always be formatted correctly using indention (2 or 4
spaces).
 2. All scripts must use module encapsulation to create safe name-
spaces.

If you look at Dan's example you'll get a better representation of
'exact' placements. Now. Whilst almost all tutorials and example code
out in the wild uses indentation, very, VERY, few use modules. There
are a few reasons for this behavior:

 * Ignorance
 * Laziness
 * Elitism

Ignorance can be excused; so long as the person does not make a habit
of ignorance! However, laziness can never be excused -- except for the
corner case of "code reuse". Reusable code not only saves you precious
time, but also is the mark of intelligence.

Some people might blubber: "You're being too pedantic about this 'r'.
People should learn to code without modules. Modules are too hard!
Wah! :-'(

On the face it that sounds like a reasonable argument. Yes it would be
nice to forget about modules until later (or maybe forever!). It's a
nice Utopian dream. But in reality it never works. The fact is; people
who don't recognize the importance of modules early on, NEVER use
adopt the practice later. Ever heard the old adage: "Can't teach an
old dog new tricks"?

Instead, they adopt the mentality of selfishness and slothfulness. Why
should THEY be burdened with such trivialities? Why should THEY care
who's identifiers THEY clobber. Why should THEY stop using global
variables just because an lowly instance or class variable would
suffice? Why should THEY care about creating a well functioning
community of developers built upon a solid foundation of style, class,
consistency, professionalism, and humility?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »