# SketchUp to KML Converter (mesh) v0.2 # Last hack: 07/28/05 # Author: Michael Ashbridge (Google Inc.) # Copyright: None. If you want to take this and improve on it, go nuts. # # Drop this file in your plugins directory and you will be able to # export your model to Google Earth. # # Unlike the other exporter, this one does cope with holes in objects # because it first meshes the faces. This doesn't produce very elegant # KML, but it's better than nowt. It also copies all exported faces to # a new layer. # def export_kml_mesh model = Sketchup.active_model ss = model.selection entities = model.entities faces_array = [] model.start_operation "export_kml_mesh" $ui_prompts = ["Lat offset", "Lon offset", "Alt offset", "Lat scale", "Lon scale", "Alt scale"] $ui_values = [0.0, 0.0, 0.0, 0.00001, 0.00001, 1.0] $ui_results = inputbox $ui_prompts, $ui_values, "Offsets and Scale" return if not $ui_results $lat_offset, $lon_offset, $alt_offset, $lat_scale, $lon_scale, $alt_scale = $ui_results if ss.empty? answer = UI.messagebox("No objects selected. Export entire model?", MB_YESNOCANCEL) else export_ents = Sketchup.active_model.selection end if( answer == 6 ) export_ents = model.entities end if( answer == 6 ) or (not ss.empty?) (erase_mesh) out_name = UI.openpanel( "KML file location", "." , "#{File.basename(model.path).split(".")[0]}.kml" ) kml_file = File.new( out_name , "w" ) others = 0 i = 0 0.upto(export_ents.length - 1) do |e| element = export_ents[i] if( element.typename != "Face") others = others + 1 else faces_array.push element end i = i + 1 end model.layers.add("KML_data") model.active_layer = ("KML_data") model.layers[0].visible=false model.layers[1].visible=true faces_array.each do |face| mesh=face.mesh(7) normal = mesh.normal_at 1 entities.add_faces_from_mesh mesh, 9 end (mesh_bucket) (export_mesh kml_file) UI.messagebox( faces_array.length.to_s + " faces converted to " + $triangles_array.length.to_s + " triangles \n" + others.to_s + " objects ignored" ) $triangles_array = [] end model.commit_operation end def mesh_bucket model = Sketchup.active_model $triangles_array = [] entities = model.entities i = 0 entities.each do |f| element = entities[i] if(element.typename == "Face" and element.layer.name == "KML_data") $triangles_array.push element end i = i + 1 end end def erase_mesh model = Sketchup.active_model ent_arr=[] model.entities.each do |element| if(element.layer.name == "KML_data") ent_arr.push element end end ent_arr.each {|e| e.erase! if not e.deleted?} end def export_mesh( kml_file ) model = Sketchup.active_model model_filename = File.basename(model.path) if( model_filename == "" ) model_filename = "no_name" end model_name = model_filename.split(".")[0] kml_file.puts( "") kml_file.puts( "") kml_file.puts( "") kml_file.puts( " SketchUp!]]>") kml_file.puts( "") kml_file.puts( "") kml_file.puts( " SketchUp " + model_name + "") i = 0 $triangles_array.each do |tri| normal = tri.normal vertices = tri.vertices kml_file.puts( " ") kml_file.puts( " Face #{i}") kml_file.puts( " \#sketchupStyle01") kml_file.puts( " Debug info

Normal " + tri.normal[0].to_s + " " + tri.normal[1].to_s + " " + tri.normal[2].to_s + "]]>
") kml_file.puts( " ") kml_file.puts( " 0") kml_file.puts( " relativeToGround") kml_file.puts( " ") kml_file.puts( " ") kml_file.puts( " ") kml_file.puts( ((vertices[0].position.x.to_f * $lon_scale) + $lon_offset).to_s + "," + ((vertices[0].position.y.to_f * $lat_scale) + $lat_offset).to_s + "," + ((vertices[0].position.z.to_f * $alt_scale) + $alt_offset).to_s) kml_file.puts( ((vertices[1].position.x.to_f * $lon_scale) + $lon_offset).to_s + "," + ((vertices[1].position.y.to_f * $lat_scale) + $lat_offset).to_s + "," + ((vertices[1].position.z.to_f * $alt_scale) + $alt_offset).to_s) kml_file.puts( ((vertices[2].position.x.to_f * $lon_scale) + $lon_offset).to_s + "," + ((vertices[2].position.y.to_f * $lat_scale) + $lat_offset).to_s + "," + ((vertices[2].position.z.to_f * $alt_scale) + $alt_offset).to_s) kml_file.puts( ((vertices[0].position.x.to_f * $lon_scale) + $lon_offset).to_s + "," + ((vertices[0].position.y.to_f * $lat_scale) + $lat_offset).to_s + "," + ((vertices[0].position.z.to_f * $alt_scale) + $alt_offset).to_s) kml_file.puts( " ") kml_file.puts( " ") kml_file.puts( " ") kml_file.puts( " ") kml_file.puts( "
") i = i + 1 done = ((i * 100.0) / $triangles_array.length).to_i Sketchup.set_status_text("Exporting mesh: " + done.to_s + "%") end kml_file.puts( "
") kml_file.puts( "
") kml_file.puts( "
") kml_file.close end if( not file_loaded?("kml_mesh_exporter.rb") ) add_separator_to_menu("Plugins") UI.menu("Plugins").add_item("Export KML (mesh)") { export_kml_mesh } end file_loaded("kml_mesh_exporter.rb")