# 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 : OBJlayercols.rb # Description : A Tool to change a Model's Entities' Colors to match the # Colors of their Layers. # Menu Item : Plugins -> "OBJ Entity Color by Layer" # OR rename as .txt file and manually load when needed... # Context Menu: N/A # Author : TIG # Usage : First you need to set the 'Color by Layer' to ON then # Export an OBJ file from the Model for it's Colors***. # You'll need the ".mtl" file. Set 'Color by Layer' OFF. # Run the Script. Select the MTL and the Model's Entities # will Color to match their Layers' Colors; as in the MTL. # Note that MTL's (unlike ACI Colors) are NOT limited to # 255 and so they are full RGB Colors. # ***Also if you set the Export Option to include Texture # Maps you'll get them used (saved renamed in sub-folder), # BUT note that scaling of the Texture is always 11"... # Note that the MTL Map names may differ from the Layer # name - the 'obj' process makes punctuation "_": So # Layers WALL-EXTL & WALL+EXTL >>> WALL_EXTL & WALL_EXTL1_ # these layer-names can both look like WALL-EXTL when # 'tested' with alternative "_" punctuation and will # therefore get that Layer's Color ! - so it's best to # avoid "ambiguous" mixed punctuation and other odd names # for these types of Layers used for coloring... # Note that the process is one-step undo-able. # Type : Tool # Version : 1.0 04/01/07 First issue. #----------------------------------------------------------------------- require 'sketchup.rb' ### class OBJlayercols def OBJlayercols::get() model=Sketchup.active_model mpath=model.path if mpath=="" UI.messagebox"You must save this 'Untitled' new model before running this !\nExiting... " return nil end#if if model.rendering_options["DisplayColorByLayer"] UI.messagebox"'Color by Layer' is ON !\nSwitching it OFF... " model.rendering_options["DisplayColorByLayer"]=false end#if ### now we open a mtl... pwd=Dir.getwd.split("/").join("\\")+"\\"### fix for SUp mtl=nil mtl=UI.openpanel"Select the MTL File...",pwd,"*.mtl" OBJlayercols::read(mtl) if mtl end#def def OBJlayercols::read(mtl) text=IO.readlines(mtl) layers=[] 0.upto(text.length-1) do |i| if text[i]=~/^newmtl */ name=text[i].slice(7..-1)### name only color=text[i+2].slice(3..-1)### rgb values map=nil if text[i+4]=~/^map_Kd /### map_Kd imap=Dir.getwd.split("/").join("\\")+"\\"+text[i+4].slice(7..-1).split("/").join("\\") imap=imap.slice(0..-2) ### fix for SUp end#if layers.push([name,color,imap]) end#if end#upto layers.uniq! ###we now have a list of all used layers and their colours ### strip out unused FrontColor and BackColor layers.each{|layr| if layr[0]=="FrontColor\n" || layr[0]=="BackColor\n" layers=layers-[layr] end#if } OBJlayercols::process(layers) if layers end#def def OBJlayercols::process(layers) ### get all model entities (face/edge/component/group) model=Sketchup.active_model entities=model.entities materials=model.materials model.start_operation"OBJ Entity Color by Layer" ### layers.each{|layer| lname=layer[0] rname=lname.slice(0..-2)### \n off rnames=[] rnames.push(rname) rnames.push(rname.slice(0..-2))if rname=~/[0123456789]_$/ ### like Layer0 it ends with number + _, _ goes... rnames.push((rname.slice(0..-2)).tr("_","-"))if rname=~/[0123456789]_$/ ### like AIA layer WALL_0001_ > WALL-0001 rnames.push((rname.slice(0..-2)).tr("_"," "))if rname=~/[0123456789]_$/ rnames.push((rname.slice(0..-2)).tr("_","."))if rname=~/[0123456789]_$/ rnames.push((rname.slice(0..-2)).tr("_","+"))if rname=~/[0123456789]_$/ rnames.push(rname.tr("_","-"))### fix for AIA layers rnames.push(rname.tr("_"," "))### fix for spaces rnames.push(rname.tr("_","."))### fix for . rnames.push(rname.tr("_","+"))### fix for + ###mixed spaces - and _ etc fail !!!! rnames.uniq! ### rgb=layer[1].split imap=layer[2] ### get their layer and match to layer_name rnames.each{|r| puts r rname=nil matches=[] entities.each{|e| ename=e.layer.name if ename==r matches.push(e) rname=r end#if }#end each e if matches[0] mat=materials.add(rname)### rname is adjusted lname col=Sketchup::Color.new(((rgb[0].to_f)*255).to_i,((rgb[1].to_f)*255).to_i,((rgb[2].to_f)*255).to_i) mat.color=col if imap if File.exist?(imap) mat.texture=imap end#if end#if matches.each{|e|e.material=mat} end#if }#end each r }#end layer model.commit_operation end#def end#class ### menu # add menu items if(not file_loaded?("OBJlayercols.rb")) UI.menu("Plugins").add_item("OBJ Entity Color by Layer"){OBJlayercols::get} end#if file_loaded("OBJlayercols.rb") ###