<commit_before># TODO: Performance improvements. I'm sure the recursion I'm doing here isn't 
# needed. Get material changes working faster for large groups of entities.


class SU_ChangeMaterial
  
  # Intialize the class by creating instance variables that we will use later.
  def initialize
    @model = Sketchup.active_model
    @selection = @model.selection
    @materials = @model.materials.sort # Sort materials alphabetically.
    @material_names = @materials.collect { |m| m.name }
    @material_display_names = @materials.collect { |m| m.display_name }
    @new_face_material = nil
    @new_back_material = nil
    @debug = MATERIAL_CHANGER_DEBUG
  end
  
  # Method to change the material of a given entity, depending on whether or 
  # not it is a face and what the value of the new back material and face 
  # material are.
  def change_material(entity)
    
    # Define the typename of the entity (e.g. "Face", "Group", etc...) so 
    # we can do different things based on the type.
    etype = entity.typename
    
    puts "\nentity: #{entity}" if @debug
    
    # See if the entity is a face, group, component or something else. If the 
    # entity is a face we will change it's face and back material. If it is a 
    # group/component, lets loop through each entity in the group/component and 
    # recurse through this method to find all the faces within it.
    case etype
      
      when "Face"
          
        # Check to see if there is a new face material for the entity, if so 
        # change the material.
        if @new_face_material
          entity.material = @new_face_material
          puts "Face face material changed to #{@new_face_material}" if @debug
        end
        
        # Check to see if there is a new back material for the entity, if so 
        # change the material.
        if @new_back_material
          entity.back_material = @new_back_material
          puts "Face (#{entity}) back material changed to #{@new_face_material}" if @debug
        end
        
      when "Edge"
        
        # Check to see if there is a new face material for the entity, if so 
        # change the material.
        if @new_face_material
          # Edges can have a face material but not a back material, so let's 
          # just give the edge the face material.
          entity.material = @new_face_material
          puts "Edge material changed to #{@new_face_material}" if @debug
        end
        
      when "Group"
        
        # Check to see if there is a new face material for the entity, if so 
        # change the material.
        if @new_face_material
          # Groups can have a face material but not a back material, so let's 
          # just give the group the face material.
          entity.material = @new_face_material
          puts "Group material changed to #{@new_face_material}" if @debug
        end
        
        # Since this is a group, we need to now recurse through it to get all 
        # the entities within it.
        entity.entities.each { |e| change_material(e) }
        
      when "ComponentInstance"
        
        # Check to see if there is a new face material for the entity, if so 
        # change the material.
        if @new_face_material
          # Components can have a face material but not a back material, so let's 
          # just give the component the face material.
          entity.material = @new_face_material
          puts "Component material changed to #{@new_face_material}" if @debug
        end
        
        # Since this is a component, we need to now recurse through it to get all 
        # the entities within it.
        entity.entities.each { |e| change_material(e) }
      
      else
        
        # Another entity was found that does not have a 
        # material property, do nothing...
        puts "An entity was found that cannot have a material property..." if @debug
      
    end
    
  end
  
  # The main function for the class. This is what you would call when you 
  # want to show the change material dialog.
  def run
    
    # If there are no materials in the model, then prompt the user of this 
    # problem and close out of the action because we can't assign materials 
    # if they don't exist.
    if @materials.length == 0
      
      result = UI.messagebox "You have no materials in your model. For this plugin to work, you need to have a material in your model.", MB_OK
      
      if result == 1
        puts "User has no materials in their model, cancel the action." if @debug
        return
      end
      
    end
    
    # If the selection is empty, prompt the user if they want us to select 
    # everything in the model.<commit_msg>Removing use of typename checking as it inefficient.<commit_after># TODO: Performance improvements. I'm sure the recursion I'm doing here isn't 
# needed. Get material changes working faster for large groups of entities.


class SU_ChangeMaterial
  
  # Intialize the class by creating instance variables that we will use later.
  def initialize
    @model = Sketchup.active_model
    @selection = @model.selection
    @materials = @model.materials.sort # Sort materials alphabetically.
    @material_names = @materials.collect { |m| m.name }
    @material_display_names = @materials.collect { |m| m.display_name }
    @new_face_material = nil
    @new_back_material = nil
    @debug = MATERIAL_CHANGER_DEBUG
  end
  
  # Method to change the material of a given entity, depending on whether or 
  # not it is a face and what the value of the new back material and face 
  # material are.
  def change_material(entity)
    
    puts "\nentity: #{entity}" if @debug
    
    # start_time = Time.new()
    
    # See if the entity is a face, group, component or something else. If the 
    # entity is a face we will change it's face and back material. If it is a 
    # group/component, lets loop through each entity in the group/component and 
    # recurse through this method to find all the faces within it.
    if entity.is_a? Sketchup::Face
          
      # Check to see if there is a new face material for the entity, if so 
      # change the material.
      if @new_face_material
        entity.material = @new_face_material
        puts "Face face material changed to #{@new_face_material}" if @debug
      end
      
      # Check to see if there is a new back material for the entity, if so 
      # change the material.
      if @new_back_material
        entity.back_material = @new_back_material
        puts "Face (#{entity}) back material changed to #{@new_face_material}" if @debug
      end
      
    elsif entity.is_a? Sketchup::Edge
        
      # Check to see if there is a new face material for the entity, if so 
      # change the material.
      if @new_face_material
        # Edges can have a face material but not a back material, so let's 
        # just give the edge the face material.
        entity.material = @new_face_material
        puts "Edge material changed to #{@new_face_material}" if @debug
      end
        
    elsif entity.is_a? Sketchup::Group
        
      # Check to see if there is a new face material for the entity, if so 
      # change the material.
      if @new_face_material
        # Groups can have a face material but not a back material, so let's 
        # just give the group the face material.
        entity.material = @new_face_material
        puts "Group material changed to #{@new_face_material}" if @debug
      end
      
      # Since this is a group, we need to now recurse through it to get all 
      # the entities within it.
      entity.entities.each { |e| change_material(e) }
      
    elsif entity.is_a? Sketchup::ComponentInstance
        
      # Check to see if there is a new face material for the entity, if so 
      # change the material.
      if @new_face_material
        # Components can have a face material but not a back material, so let's 
        # just give the component the face material.
        entity.material = @new_face_material
        puts "Component material changed to #{@new_face_material}" if @debug
      end
      
      # Since this is a component, we need to now recurse through it to get all 
      # the entities within it.
      entity.definition.entities.each { |e| change_material(e) }
    
    else
        
      # Another entity was found that does not have a 
      # material property, do nothing...
      puts "An entity was found that cannot have a material property..." if @debug
    
    end
    
    # end_time = Time.new()
    # 
    # puts "Elapsed: #{end_time - start_time}"
    
  end
  
  # The main function for the class. This is what you would call when you 
  # want to show the change material dialog.
  def run
    
    # If there are no materials in the model, then prompt the user of this 
    # problem and close out of the action because we can't assign materials 
    # if they don't exist.
    if @materials.length == 0
      
      result = UI.messagebox "You have no materials in your model. For this plugin to work, you need to have a material in your model.", MB_OK
      
      if result == 1
        puts "User has no materials in their model, cancel the action." if @debug
        return
      end
      
    end
    
    # If the selection is empty, prompt the user if they want us to select 
    # everything in the model.
{'fraction_non_alphanumeric': 0.05876102433322707, 'fraction_numerical': 0.00042503453405589204, 'mean_word_length': 2.256747404844291, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7470427', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2484, 'n_words': 1180}