# Copyright 2005, Didier Bur # Permission to use, copy, modify, and distribute this software for # any purpose and without fee is hereby granted, provided that 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 : su2iv.rb 1.0 # Description : Exports selection or whole model to IV format external file # Author : Didier Bur # Usage : Select objects exports the selection, no selection exports whole # model. Select Plugins/Export to STL. Groups and components not supported. # # Date : 27.June.2005 # Type : Exporter # History: 1.0 (30.June.2005) - first version #--------------------------------------------------------------------------------- def export_model_to_iv #Init model = Sketchup.active_model ss = Sketchup.active_model.selection model.start_operation "Export to IV" $faces_array = [] entities = model.entities # Selection checking if ss.empty? answer = UI.messagebox("Nothing selected. Export whole model ?", MB_YESNOCANCEL) else entities_to_export = Sketchup.active_model.selection end if( answer == 6 ) entities_to_export = model.entities end # Output file out_name = UI.openpanel( "Select file to export to", "." , "*.iv" ) export_file = File.new( out_name , "w" ) # Error checking, put valid elements in $faces_array others = 0 i = 0 0.upto(entities_to_export.length - 1) do |something| element = entities_to_export[i] if( element.typename != "Face") others = others + 1 else $faces_array.push element end i = i + 1 end #of upto # Layer management model.layers.add("IV_triangles") model.active_layer = ("IV_triangles") #export triangles (export_iv_bymaterial export_file) #Report message UI.messagebox( $faces_array.length.to_s + " faces to export.\n" + others.to_s + " objects ignored.\n") # Undo model.commit_operation end #-------------------------------------------------------- Export layers (materials) one by one def export_iv_bymaterial( export_file ) model = Sketchup.active_model #File Header model_filename = File.basename(model.path) if( model_filename == "" ) model_filename = "no_name" end model_name = model_filename.split(".")[0] # Header export_file.puts( "#Inventor V2.1 ascii") export_file.puts( "#Exported from Sketchup file: " + model_name) export_file.puts( "") export_file.puts( "ShapeHints {") export_file.puts( " vertexOrdering COUNTERCLOCKWISE") export_file.puts( " shapeType SOLID") export_file.puts( " faceType UNKNOWN_FACE_TYPE") export_file.puts( " }") export_file.puts( "") #Export faces, layer by layer model.materials.each {|mat| (export_material mat, export_file)} # Close the export file: export_file.close end def export_material( m, export_file ) model = Sketchup.active_model model.active_layer = ("IV_triangles") #Start Group export_file.puts( "#SketchUp material: " + m.display_name ) export_file.puts( "Group {") # Material for this group r = m.color.red / 255.0 g = m.color.green / 255.0 b = m.color.blue / 255.0 export_file.puts( "Material { diffuseColor " + r.to_s + " " + g.to_s + " " + b.to_s + " }" ) #Check if face belong to layer lay, triangulate it and export group $faces_array.each do |face| if( face.material == m) (export_iv_face face, export_file) end end # End Group export_file.puts( "}" ) end def export_iv_face( f, export_file ) normal = f.normal vertices = f.vertices export_file.puts( "IndexedFaceSet {") export_file.puts( "vertexProperty VertexProperty {") export_file.puts( "vertex [ ") i = 0 0.upto(vertices.length - 1) do |that| export_file.puts( vertices[i].position.x.to_f.to_s + " " + vertices[i].position.y.to_f.to_s + " " + vertices[i].position.z.to_f.to_s + ",") i = i + 1 end export_file.puts( " ]") # Normal export_file.puts( "normal " + normal[0].to_s + " " + normal[1].to_s + " " + normal[2].to_s) export_file.puts( "normalBinding OVERALL") export_file.puts( "}") #Indexes for coordinates line = "coordIndex [ " 0.upto(vertices.length - 1) do |that| if( that != (vertices.length - 1)) line = line + that.to_s + " ," else line = line + that.to_s + " ]" end end export_file.puts( line ) export_file.puts( "materialIndex -1") export_file.puts( "normalIndex 0" ) export_file.puts( "}") end #--------------------------------------------------------------------------------- # Menu items if( not file_loaded?("su2iv_sans_triangles.rb") ) add_separator_to_menu("Plugins") UI.menu("Plugins").add_item("Export to IV (sans triangulation)") { export_model_to_iv } end #----------------------------------------------------------------------------- file_loaded("su2iv_sans_triangles.rb")