Web dialog problem
-
Hi All,
I'm a complete newbie to Ruby and programming for Sketchup. I use Autocad and Sketchup to model my models. I saw some great plugins to assign materials to layers. (Useful when you work in both Autocad and Sketchup). Still I wanted to write my own plugin and learn Ruby by combining a lot of snippets from different, brilliant plugins.
So this is my problem. When I use webdialog.set_file ('somehtml.html') everything runs smooth. But when I add a variable e.g. htmlstr = "here I put all the contents from the somehtml.html file" and use webdialog.set_html I get an error.
I tried several things but nothing seems to work. Please help me. What am I doing wrong?
Thank you!
Jeroen
I added the script:
The info assigned to html variable is the same as the info from the mbl.html file used in the wb.set_file command. Therefore I did not add the HTML file.# mlb.rb # Material By Layer v0.4 # jeroenverdonschot@gmail.com require 'sketchup.rb' def mbl_main html = <<DATA <html><head><title>Material by Layer</title><meta http-equiv="Content-Type" content="text/html" charset="utf-8"/> <style type="text/css"> body { font-family; Helvetica, Geneva, Arial, SunSans-Regular, sans-serif; font-size; 12px; } h4 { font-family; Helvetica, Geneva, Arial, SunSans-Regular, sans-serif; font-size; 12px; } select { background; transparent; width; 275px; padding; 0px; font-size; 14px; border; 0px solid #ccc; height; 34px; overflow; hidden; } </style> </head> <body> <h4>Choose Layer;</h4><select name="combo" width="200"></select><br> <h4>Choose Material;</h4><select name="combo2"></select> <form name="checkboxes"> <input type="checkbox" checked="yes" name="include" value="faces" /> include faces<br /> <input type="checkbox" checked="yes" name="include" value="groups" /> include groups<br /> <input type="checkbox" checked="yes" name="include" value="components" /> include components </form> <script type="text/javascript"> function rubyCalled( callback_name, string ) { if ( (typeof string) == 'undefined' ) string = ''; window.location.href = 'skp;' + callback_name + '@' + string; } function call_ruby( msg ) { location = 'skp;catch_data@' + msg; } function from_ruby( data, namecombo ) { var names; eval( 'names = ' + data ); for ( var x in names) { var optn = document.createElement("OPTION"); var combo = document.getElementById( namecombo ); optn.text = names[x]; combo.options.add(optn); } } function closer( button_text ) { if ( button_text == 'cancel' ) { call_ruby( 'cancel' ); //return; } if ( button_text == 'ok' ) { var i = new Array(5); i[0] = document.getElementById("combo").selectedIndex; i[1] = document.getElementById("combo2").selectedIndex; for (x=0; x<document.checkboxes.include.length; x++){ i[x+2]=document.checkboxes.include[x].checked; } call_ruby(i); } return; } // end of closer() rubyCalled( 'layers' ) rubyCalled( 'materials' ) </script> <center> <button type="button" style="width;80;" onclick = "closer( 'ok' )">OK</button> <button type="button" style="width;80;" onclick = "closer( 'cancel' )">Cancel</button> </center> </body> </html> DATA model = Sketchup.active_model selection = model.selection view = model.active_view lays=model.layers layNstmp=[] lays.each{|l|layNstmp.push(l.name)} layList = "['#{layNstmp.join('\',\'')}']" mats=model.materials matNstmp=[] mats.each{|m|matNstmp.push(m.name)} matNames= matNstmp matList= "['#{matNames.join('\',\'')}']" wd = UI;;WebDialog.new("Material by Layer", false, "MaterialByLayer", 310, 320, 100, 200, true) wd.add_action_callback( "layers" ) do |dlg, msg| script = 'from_ruby("' + layList + '", "'+ "combo" + '");' dlg.execute_script( script ) end wd.add_action_callback( "materials" ) do |dlg, msg| script = 'from_ruby("' + matList + '", "'+ "combo2" + '");' dlg.execute_script( script ) end wd.add_action_callback( 'catch_data' ) do |another_wd, msg| if msg == 'cancel' wd.close() else ac_Array = msg.split(',') # convert from javascript to Ruby pickedLayer = layNstmp[ac_Array[0].to_i] pickedMaterial = matNames[ac_Array[1].to_i] includeFaces = ac_Array[2] includeGroups = ac_Array[3] includeComponents = ac_Array[4] selection.each { | entity| putCurrentRecursion(entity, pickedMaterial, pickedLayer) } view.refresh end wd.close() end # wd.set_file( '/mbl/mbl.html' ) THIS WORKS! # wb.set_html(html) # THIS DOESN'T wd.show() end def putCurrentRecursion(ents,selmat,sellayer) etype = ents.typename case etype when "Face" ents.material=selmat if ents.layer.name == sellayer when "Group" ents.material=selmat if ents.layer.name == sellayer ents.entities.each do |ent| putCurrentRecursion(ent,selmat,sellayer) end when "ComponentInstance" ents.material=selmat if ents.layer.name == sellayer begin ents.definition.entities.each do |ent| putCurrentRecursion(ent,selmat,sellayer) end rescue => err puts "EXCEPTION; #{err}" end end end if( not file_loaded?("mbl.rb") ) UI.add_context_menu_handler do |menu| menu.add_separator menu.add_item("Material by Layer") { mbl_main } end end file_loaded("mbl.rb")
-
The problem may be due to coding.
Try to remove this.(and in html)<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
-
Thanks Sahi for your quick reply!
I removed the <meta>-tag and contents. Still the same problem.
Still the webdialog.Set_file works, the webdialog.set_html doesn't.I get this error from the console:
Error: #<NameError: undefined local variable or method
wb' for main:Object> C:/Program Files/Google/Google SketchUp 8/Plugins/mbl.rb:149:in
mbl_main'
C:/Program Files/Google/Google SketchUp 8/Plugins/mbl.rb:186I can't find the variable. I do not understand why this error is reported with the same html file/string?
Please help, thanks in advance!
Jeroen
-
string 133 ? split(",") ?
which string gets the variable msg ???
puts msg.to_s
-
Hi
It's the JS that's falling down in ruby...
if you put it in a simple WD, that works without it, you get js parsing errors from ruby with it.
some are whitespace and some are case[Case] issues
It's basically because the 'browser' parses the JS differently to ruby [which is either stricter or simply different],
you need to manually parse it for ruby, then it works for both...here's the simple WD I tested the html in...
dlg_html='put your html in here... I deleted the DATA tags first' dlg = UI;;WebDialog.new('My Dialog', true,'MyDialog', 400, 400, 150, 150, true) dlg.navigation_buttons_enabled = false dlg.set_html(dlg_html) dlg.show
john
-
Thanks for your replies!
@sahi JS returns msg with a string ("cancel") or with information about the selected items. To use the JS array I had to convert it to a Ruby array. So I used the split method.
@driven. I think I understand the problem. I unfortunately have not enough experience/knowledge to rewrite the JS part so Ruby can understand it. So I think I'll use the separate html file and wb.set_file method.
Thank you for your replies!
Jeroen
-
I'm a bit late to this discussion. Have you worked out your issue now?
In general I recommend separating the HTML into a separate file as it's easier to manage.
If you are new to WebDialogs in SketchUp I can suggest a thread I started that tries to summarize many pitfalls: http://forums.sketchucation.com/viewtopic.php?f=180&t=23445#p198883
A side note on your code snippet, I see you use
.typename
to compare entity types, something I strongly recommend you avoid. It's a big performance drain. I was just finishing up a small article on that topic: http://www.thomthom.net/thoughts/2011/12/never-ever-use-typename/ -
The error is a simple typo
Did anyone read his error message ?
line 149 he has
wb
as the webdialog object reference, instead ofwd
!!PS: this thread belongs in the Developers forum.
-
@dan rathbun said:
The error is a simple typo
It may well have the typo, but when I ran his 'working bits' in the clean wd, it also has JS issues...
@unknownuser said:
PS: this thread belongs in the Developers forum.
who can move these types of post? -
@driven said:
It may well have the typo, but when I ran his 'working bits' in the clean wd, it also has JS issues...
@unknownuser said:
PS: this thread belongs in the Developers forum.
who can move these types of post?I can. Already moved. See breadcrumb navigation over the topic title.
-
Advertisement