• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Require 'sketchup'

Scheduled Pinned Locked Moved Developers' Forum
24 Posts 5 Posters 1.5k Views 5 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    thomthom
    last edited by 7 Dec 2010, 19:08

    @jim said:

    Also, I question the practice of scripts sneakily loading DC's when I explicitly disabled them. Seems like it would cause greater confusion - especially if I'm working on the assumption that I have disabled DC's.

    Ditto.

    Thomas Thomassen — SketchUp Monkey & Coding addict
    List of my plugins and link to the CookieWare fund

    1 Reply Last reply Reply Quote 0
    • H Offline
      honoluludesktop
      last edited by 7 Dec 2010, 21:00

      I looked up sketchup.rb. Is it correct to assume that none of that stuff is covered in the api, or required by the methods therein? If so, and if I do not call any of the sketchup.rb methods, should I still "require sketchup.rb" in my script?

      Is is also correct to assume that other sketchup .rb's may require it, and that they would call it?

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 7 Dec 2010, 21:32

        A require 'sketchup.rb' does no harm.
        Using if when you don't need to might delay SUp's startup by a nanosecond... ❓

        TIG

        1 Reply Last reply Reply Quote 0
        • H Offline
          honoluludesktop
          last edited by 7 Dec 2010, 21:41

          OK, just curious.

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 7 Dec 2010, 21:49

            require is like load, with the difference that it only loads the file if it has not been loaded before. It ensures the file is loaded only once.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • H Offline
              honoluludesktop
              last edited by 7 Dec 2010, 22:20

              Does that mean that I can add require "drive\path\my_folder\my_method" to the top of my code, and call my_method without the "drive\path" from any of my plugins?

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 8 Dec 2010, 07:47

                @honoluludesktop said:

                Does that mean that I can add require "drive\path\my_folder\my_method" to the top of my code, and call my_method without the "drive\path" from any of my plugins?

                Yes [but with 'forward-slashes'=/] BUT you could just load "drive/path/my_folder/my_method" if it's only needed once - or even load it from a separate 'loading ruby' in Plugins... The 'require' will 'load' it if it isn't already loaded, whereas a 'load' will re-load it even if it is already loaded...
                You can even set Ruby to load all of the files in you folder at startup by changing the $LOAD_PATH array.
                I use this code below to allow my students to load their own Plugins at uni where they don't have access rights to the C:../Plugins folder on the system. Adjust the 'path' to suit your folder needs...

                ### This file goes in MAIN ..Sketchup../Plugins/ folder - named 
                ### "ZZ_LoadPathUpdater.rb" so that it loads near [or at] the end.
                ### This code sets the 'path' to the User's "Sketchup_Plugins" folder on the H; drive.
                path = "H;/Sketchup_Plugins"
                ###
                ### This next code extends the Ruby search paths to include 'path' 
                ### therefore any subsequent use of "load 'xxx.rb'" or "load 'xxx.txt'" 
                ### commands in the Ruby Console etc will also search/load from the 'path' 
                ### in addition to the more usual paths like ../Tools/ and ../Plugins/
                $LOAD_PATH << path; $LOAD_PATH.uniq!
                ###
                ### If the User has a folder that matches this 'path' then 
                ### any Ruby scripts in 'path' will auto-loaded into Sketchup...
                Dir.entries(path).each{|file| load(File.join(path, file)) if File.extname(file)==".rb" or File.extname(file)==".rbs"} if File.exist?(path) 
                ###
                
                

                TIG

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Dan Rathbun
                  last edited by 8 Dec 2010, 10:39

                  @honoluludesktop said:

                  Does that mean that I can add require "drive\path\my_folder\my_method" to the top of my code, and call my_method without the "drive\path" from any of my plugins?

                  Just to clarify.. load and require both open and read FILES not methods.

                  Your file could have the same name as the method within it, or the file could contain a module with many methods.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 8 Dec 2010, 10:58

                    @honoluludesktop said:

                    I looked up sketchup.rb. Is it correct to assume that none of that stuff is covered in the api, or required by the methods therein? If so, and if I do not call any of the sketchup.rb methods, should I still "require sketchup.rb" in my script?

                    @tig said:

                    A require 'sketchup.rb' does no harm.
                    Using if when you don't need to might delay SUp's startup by a nanosecond...

                    What TIG says is true.. BUT

                    There is no point, if your script does not "require" the services of a script to put a require 'suchandsuch.rb' statement at the top of your code.

                    There are ruby utilities the use the require statements to "map" out dependencies for scripts. If you insert bogus require statements, your really lying to Ruby and any person that reads your code. Your declaring a false dependency.

                    So in the end, YOU decide whether you wish to be a precise programmer or a sloppy one ... (but sloppy is OK for a quick and dirty test, or a script that isn't used that often, or one for your own use.)

                    @honoluludesktop said:

                    Is is also correct to assume that other sketchup .rb's may require it, and that they would call it?

                    TRUE

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      thomthom
                      last edited by 8 Dec 2010, 11:18

                      @dan rathbun said:

                      @honoluludesktop said:

                      Is is also correct to assume that other sketchup .rb's may require it, and that they would call it?

                      TRUE

                      But one can't rely on other scripts to do so. Your plugin can be the only one to be loaded - or even the first.

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • H Offline
                        honoluludesktop
                        last edited by 8 Dec 2010, 12:50

                        Thanks All, for the help.

                        1 Reply Last reply Reply Quote 0
                        • D Offline
                          Dan Rathbun
                          last edited by 8 Dec 2010, 14:02

                          @Jim and ThomThom: I am speaking of general use of Sketchup, not Coders doing Debugging or Testing of plugins, etc.

                          @jim said:

                          @dan rathbun said:

                          I also advise that the first extension loaded (and that it always be loaded,) is dynamiccomponents.rb (because there are miscellaneous extra API methods for various classes within the DC extension, that need to be loaded. In addition when the DC extension is not loaded, some of the AttributeDictionary methods no longer work.)

                          dynamiccomponents.rb is always loaded, anyway - it's in the Tools folder. Requiring it first or again does nothing about these "miscellaneous extra API methods" and the "AttributeDictionary methods" (which are these, anyway?) because the file only registers DC's as a SketchupExtension. The DC loader, which defines the various DC classes, is not called if the user has DC's disabled, regardless if a script re-requires dynamiccomponents.rb.

                          Right?

                          TRUE Which is why I suggested in parens "that they be always loaded".
                          (I could edit & clarify the first post.)

                          @jim said:

                          Also, I question the practice of scripts sneakily loading DC's when I explicitly disabled them. Seems like it would cause greater confusion - especially if I'm working on the assumption that I have disabled DC's.

                          Perhaps the issue of DCs clouds the idea?

                          I never said anything about creating "scripts sneakily loading DCs".

                          I said (and I clarify,) that YOU (an experienced Ruby coder,) can create a master load order rb file, for YOUR OWN use, in which YOU control what scripts load and in what order they load, on YOUR OWN computer.

                          This would be in the normal use of Sketchup for modeling, not when testing or debugging plugins.

                          But forget about DCs for a moment, .. pretend I never said anything about DCs. Instead imagine the !autoload.rb file as having it's first line be:
                          require 'sketchup'

                          ... the idea is the same.

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            Dan Rathbun
                            last edited by 10 Dec 2010, 21:09

                            @jim said:

                            Requiring it first or again does nothing about these "miscellaneous extra API methods" and the "AttributeDictionary methods" (which are these, anyway?) ....

                            See this API topic post for info on AttributeDictionary class methods that need the DC extension.

                            Extended API Methods defined by the DC Extension

                            ` ::Geom::Transformation.rotx()
                            ::Geom::Transformation.roty()
                            ::Geom::Transformation.rotz()
                            ::Geom::Transformation.xscale()
                            ::Geom::Transformation.yscale()
                            ::Geom::Transformation.zscale()

                            ::Sketchup::ComponentInstance.copy()
                            ::Sketchup::ComponentInstance.description()

                            ::Sketchup::Drawingelement.last_scaling_factors()
                            ::Sketchup::Drawingelement.local_transformation()
                            ::Sketchup::Drawingelement.scaled_size()
                            ::Sketchup::Drawingelement.set_last_size( lenx,leny,lenz )
                            ::Sketchup::Drawingelement.unscaled_size()

                            ::Sketchup::Model.delete_attribute( dictionary_name, key )
                            ::Sketchup::Model.entityID()
                            ::Sketchup::Model.layer()
                            ::Sketchup::Model.typename()

                            ::UI::WebDialog.last_height()
                            ::UI::WebDialog.last_height=()
                            ::UI::WebDialog.last_width()
                            ::UI::WebDialog.last_width=()`

                            .

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • T Offline
                              thomthom
                              last edited by 11 Dec 2010, 01:58

                              ... why didn't they just make this part of the API? ❓

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • D Offline
                                Dan Rathbun
                                last edited by 12 Dec 2010, 02:43

                                @thomthom said:

                                ... why didn't they just make this part of the API? ❓

                                A good question for Scott and Tyler.

                                My guess is, it was faster for development for Scott to write the extensions in Ruby to "prove the concept." They may be thinking to "bake them into" the C side API code at a later date.

                                Likely this is low priority, which means it is always (and probably will remain,) at the bottom of the to do list. Bug fixes to the application code come first.

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • D Offline
                                  Dan Rathbun
                                  last edited by 12 Dec 2010, 17:36

                                  @Jim

                                  I think if you wished to have the DC extension OFF, but still have the extended methods defined, you could issue the following call at the Console:

                                  Sketchup::require( 'DynamicComponents/ruby/dcutils' )

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • D Offline
                                    Dan Rathbun
                                    last edited by 12 Dec 2010, 18:23

                                    You could do this automatically by adding the above statement to the end of the Tools/dynamiccomponents.rb file (after the statement that registers the DC extension.)

                                    IF the extension is OFF then the extra class methods would be defined.

                                    IF the extension is ON, the file may get reloaded, but no harm should be done.

                                    I'm not here much anymore.

                                    1 Reply Last reply Reply Quote 0
                                    • 1
                                    • 2
                                    • 1 / 2
                                    1 / 2
                                    • First post
                                      17/24
                                      Last post
                                    Buy SketchPlus
                                    Buy SUbD
                                    Buy WrapR
                                    Buy eBook
                                    Buy Modelur
                                    Buy Vertex Tools
                                    Buy SketchCuisine
                                    Buy FormFonts

                                    Advertisement