sketchucation logo sketchucation
    • Login
    1. Home
    2. piranesi
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download
    P
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 3
    • Groups 1

    piranesi

    @piranesi

    10
    Reputation
    1
    Profile views
    3
    Posts
    0
    Followers
    0
    Following
    Joined
    Last Online

    piranesi Unfollow Follow
    registered-users

    Latest posts made by piranesi

    • RE: Web dialog problem

      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

      posted in Developers' Forum
      P
      piranesi
    • RE: Web dialog problem

      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:186

      I 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

      posted in Developers' Forum
      P
      piranesi
    • 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")
      
      
      posted in Developers' Forum
      P
      piranesi