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")