# Copyright 2005, John H. Aughey (jha@aughey.com) # 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. #----------------------------------------------------------------------------- require 'sketchup.rb' class Cleanup #----------------------------------------------------------------------------- # First check to see if we have already loaded this file so that we only # add the item to the menu once if( not file_loaded?("cleanup_model.rb") ) # This will add a separator to the menu, but only once add_separator_to_menu("Edit") # To add an item to a menu, you identify the menu, and then # provide a title to display and a block to execute. In this case, # the block just calls the create_box function UI.menu("Edit").add_item("Remove Unused Edges") { Cleanup.cleanup_model } end # This script cleans up unneeded edges from a model. # An unneeded edge is either an edge that doesn't form a face, or # and edge that can be removed and not effect any existing face. def self.cleanup_model model = Sketchup.active_model model.start_operation "Cleanup Operation" entities = model.active_entities # Store all of the edges to delete in a separate container to be deleted # after we have found all unneeded edges. todelete = [] entities.each do |entity| todelete.push entity if entity.kind_of?(Sketchup::Edge) and edge_can_be_removed?(entity) end # Erase everything in our todelete list entities.erase_entities todelete # Now we are done and we can end the operation model.commit_operation end private def self.edge_can_be_removed?(edge) faces = edge.faces # Delete the edge if it doesn't contribute to a face return true if faces.length == 0; # If it contributes to more than one face, it can not be removed return false if faces.length != 2; # It must contribute to only 2 faces to be a canidate. # If someone can think of a situation where an edge could # contribute to more than 2 faces and still be a canidate # for removal, please let me know. normal0 = faces[0].normal normal1 = faces[1].normal dot = normal0.dot normal1 # What should the tolerance be on the dot product # for two faces to be co-planar? # Check for two faces pointing in the same direction if dot > 0.99999 # Check that the materials are the same return true if same_side_material?(faces[0],faces[1]) end # Check for co-planar faces, but reversed normals if dot < -0.99999 # Check that the material are the same return true if opposite_side_material?(faces[0],faces[1]) end end def self.same_side_material?(face1,face2) face1.material == face2.material and face1.back_material == face2.back_material end def self.opposite_side_material?(face1,face2) face1.material == face2.back_material and face1.back_material == face2.material end end #----------------------------------------------------------------------------- file_loaded("cleanup_model.rb")