# Copyright 2007, TIG # Permission to use, copy, modify, and distribute this software for # any purpose and without fee is hereby granted, provided the above # copyright notice appear in all copies. # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #----------------------------------------------------------------------- # Name : ListLayerColors.rb # Description : A Tool to make a List of a Model's Layer Colorss. # Menu Item : Plugins -> "List Layer Colors" # Author : TIG # Usage : First it sets the 'Color by Layer' to be ON then # Exports an OBJ file from the Model to give colors***. # It needs its assocaited "MTL" file. # Then it resets 'Color by Layer' back as it was. # It parses the MTL file into acsv file, # It tidies up by deleting the tmp_ files # Note that MTL colors (unlike ACI) are NOT limited to # 255 and are full RGB Colors. # With OBJ layer names containing punctuation get an '_', # so WALL-EXTL & WALL+EXTL >>> WALL_EXTL & WALL_EXTL1_ ! # Version : 1.0 01/11/07 First issue. #----------------------------------------------------------------------- require 'sketchup.rb' ### class ListLayerColors def ListLayerColors::get() model=Sketchup.active_model mpath=File.dirname(model.path) mname=model.title rname=mname+"_tmp_"+Time::now.to_i.to_s if mpath=="." UI.messagebox"You must save this 'Untitled' new model before running this !\nExiting... " return nil end#if if model.rendering_options["DisplayColorByLayer"]==false UI.messagebox"'Color by Layer' is OFF !\nSwitching it ON briefly to compile list..." model.rendering_options["DisplayColorByLayer"]=true already_on=false else already_on=true end#if ### export obj and mtl model.export(mpath+"/"+rname+".obj",false) ### read mtl and parse into a csv list mtl=mpath+"/"+rname+".mtl" text=IO.readlines(mtl) layers=[] 0.upto(text.length-1) do |i| if text[i]=~/^newmtl */ name=text[i].slice(7..-2)### layer name only rgbR=((text[i+2].slice(3..11).to_f)*255).to_i.to_s### color rgb values rgbG=((text[i+2].slice(12..20).to_f)*255).to_i.to_s### color rgb values rgbB=((text[i+2].slice(21..29).to_f)*255).to_i.to_s### color rgb values layers.push([name+","+rgbR+","+rgbG+","+rgbB]) end#if end#upto ### we now have a list of all used layers and their colors, ### and now we write it to csv file... filep=mpath+"/"+mname+"-LayerColors.csv" begin file=File.new(filep,"w") rescue### trap if open UI.messagebox("Layer Colors List:\n\n"+filep+"\n\nCannot be written to - it's probably already open.\nClose it and try making it again...\n\nExiting...") ###erase temp tiles File.delete(mpath+"/"+rname+".obj") File.delete(mtl) model.rendering_options["DisplayColorByLayer"]=false if already_on==false return nil end layers.each{|e|file.puts(e)} file.close ### erase temp tiles File.delete(mpath+"/"+rname+".obj") File.delete(mtl) ### model.rendering_options["DisplayColorByLayer"]=false if already_on==false ### UI.messagebox("Layer Colors List:\n\n"+filep+"\n\nCompleted.") end#def end#class ### menu # add menu items if(not file_loaded?("ListLayerColors.rb")) UI.menu("Plugins").add_item("List Layer Colors"){ListLayerColors::get} end#if file_loaded("ListLayerColors.rb") ###