Uninitialized constant
-
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
endQuick 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
enddef 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")` -
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
-
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 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. -
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... -
Don't think you need to prefix it with
Sketchup::
... I've only used it once, but I think I just usedrequire_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
-
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 ? -
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...
-
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 therequire_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 ? -
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?
-
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. -
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)}
Advertisement