sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Uninitialized constant

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 3 Posters 2.2k Views 3 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.
    • K Offline
      Kapoue
      last edited by

      Hello,

      I'm trying to use rexml to read an XML file so I have downloaded rexml
      and I use this:
      require 'rexml/parsers/pullparser.rb'

      if I doing a mistake in the file name sketchup throw an error and with
      this line I don't have an error so I suppose that the file is loaded
      and my $PATH is ok.

      But when I try this:
      @xml = REXML::Parsers::PullParser.new( f )

      He said: uninitialized constant REXML::Parsers

      How can I resolve that?

      Thanks in advance

      Benjamin

      Here is my code
      ` require 'sketchup.rb'
      path=Sketchup.find_support_file("Plugins")
      Sketchup::require path+'/rexml/parsers/pullparser.rb'

      puts(path+'/rexml/parsers/pullparser.rb')

      class LDDFileImport

          # Initialize variables 
          @model = Sketchup.active_model 
      
          # function to  debug easilly 
          def DebugPuts(message) 
            if $show_debug_LDD_messages == true 
                  puts(message) 
            end 
          end 
      
          #load XML 
          def ParseFile( filename ) 
                  DebugPuts("Begin ParseFile") 
                  if (filename == nil) then return nil; end 
      
                  model = Sketchup.active_model 
                  model.start_operation "Import LDD File" 
                  begin 
                    DebugPuts "Parsing: "+ filename 
                    entities = model.active_entities 
      
                    f = File.new( filename, 'r' ) 
                    begin 
                          @xml = REXML::Parsers::PullParser.new( f ) 
                          model.selection.clear 
                          #model.selection.add TopLevel(entities) 
                    ensure 
                          f.close 
                    end 
      
                    model.commit_operation 
      
                  rescue => bang 
                    if (model) 
                          model.abort_operation 
                    end 
                    UI.messagebox( "Error (in script) while reading \"" + filename + 
      

      "":\n" + bang )
      end
      end
      end

      Quick Debug Code, can be called by the ruby console

      def lddParserTest()
      was = $svgImportScriptDebug
      begin
      $show_debug_LDD_messages = true
      f = LDDFileImport.new()
      f.ParseFile( 'C:/Users/Admin/Desktop/Lego/3pcslxfml.lxfml' )
      ensure
      $show_debug_LDD_messages = was
      end
      end

      def DoLDDImport
      filename = UI.openpanel "Import lxfml File",nil,"*.lxfml"
      if (filename)
      f = LDDFileImport.new()
      f.ParseFile( filename )
      end
      end

      #--------------------------------------------------------------------------

      Register within Sketchup

      if(file_loaded("ldd.rb"))
      menu = UI.menu("Plugins");
      menu.add_item("Import lxfml File...") { DoLDDImport() }
      end

      #--------------------------------------------------------------------------
      file_loaded("ldd.rb")`

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        It's possible you haven't required all the correct files.

        On a sidenote, REXML isn't the fastest XML parser as it turns out. Check out this thread: http://forums.sketchucation.com/viewtopic.php?f=180&t=20297

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

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          Try adding:
          Dir[path+'/rexml/parsers/'].each{|file|Sketchup::require file}
          This will load everything in that directory in case something else is missing...

          TIG

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            @tig said:

            Try adding:
            Dir[path+'/rexml/parsers/'].each{|file|Sketchup::require file}
            This will load everything in that directory in case something else is missing...

            Or use the require_all call.

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

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              Duh!
              Something like...
              Sketchup::require_all 'path+"/rexml/*/*.rb"'
              Which would load all '.rb' rubies in the '/parsers/' folder AND those in any othersub-folders too...

              TIG

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                Don't think you need to prefix it with Sketchup::... I've only used it once, but I think I just used require_all.

                @tig said:

                AND those in any other sub-folders too...

                Really? I haven't found any other info on the method than this page: http://code.google.com/intl/nb/apis/sketchup/docs/loading.html

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

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  I assumed the Sketchup:: was because it might get scrambled later ?
                  The * wildcards .../*/*.rb process all folders in the path, and require the .rb files found ?

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    Ah,.. I don't know anything about scrambled files.

                    I found this though on the sub folder topic: http://forums.sketchucation.com/viewtopic.php?f=15&t=16029&p=124772#p125137
                    Maybe it does include subfolders.
                    If so I need to check CityGen's code as we use it there. I thought it only took the rb and rbs files in that folder - not going deeper. I actually think that for CG we don't want it doing that either.

                    Would have been nice is sub-folders where an optional argument...

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

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      We talk at cross-purposes 😳 - I didn't know Jim had made a require_all version...
                      I thought you meant this one that takes wildcards...tarcieri-require_all-ce01fef7205f87ade3c1e7bd626f0610f01cc013.zip the require_all.rb is in the /lib/ folder...

                      Since others might not have these perhaps the
                      Dir[path+'/rexml/parsers/'].each{|file|Sketchup::require file}
                      based version is safest ?

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        But require_all is a function bundled with SU: http://code.google.com/intl/nb/apis/sketchup/docs/loading.html

                        @unknownuser said:

                        The sketchup.rb file defines a require_all function which requires all files with the extension .rb in a given folder.

                        Another good option for automatically loading Ruby scripts is to create your own folder of Ruby scripts (outside of the Plugins directory) and add that directory to the sketchup.rb file. For example, to load all scripts in the myrubyscriptsdirectory:

                        Question is, does these third-party solutions override the native method?

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

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          That's a nice snippet.
                          Though, I have to see if this other version overwrites the built-in method. I think it can cause problems for CG if suddenly loads sub-folders.

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

                          1 Reply Last reply Reply Quote 0
                          • TIGT Offline
                            TIG Moderator
                            last edited by

                            Never knew require_all was built in... 😳
                            require_all("C:\myrubyscriptsdirectory\")
                            You then could easily use it to do subfolders thus:
                            Dir.foreach("C:\myrubyscriptsdirectory\"){|entry|require_all(entry)if FileTest.directory?(entry)}

                            TIG

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

                            Advertisement