Create transparent operations with the observer


#1

Hello everyone,

After submitting my new plugins to the extension bank, he asks me to fix some things.

Here is their message:

Here are the notes from the reviewer:
Observers should create transparent operations.
entity.model.start_operation('add texture', true)

Here is the code in question:

class MyEntitiesObserver < Sketchup::EntitiesObserver

  def onElementAdded(entities, entity)

    return unless entity.is_a?(Sketchup::ComponentInstance)
    
    mod = Sketchup.active_model
    mats = mod.materials
    entity.model.start_operation('add texture', true) 
    matatt = [] 
    add_mat(entity, matatt)
    matatt.compact!
    matatt.uniq!
    matskt = []
    mod.materials.each{ |m| matskt << m.name }
    matadd = matatt - matskt  
    plans_jpg = ['PLAN']

    path_plans = File.join(Sketchup.find_support_file('plugins'), "TNT_ClickChange2/Materials/PLAN/")
      
    last02 = mod.get_attribute("Plan","Last","27")
      
    all = []
    all << plans_jpg
      
    matadd.each do |m|
      unless all.include?(m)
        
        matnew = mats.add(m)
          
        if plans_jpg.include?(m)
          matnew.texture = path_plans + "#{last02}-#{m}.jpg"
        else
          matnew = "RED"
        end
      end
    end
    entity.model.commit_operation
    onElementAdded2(entities, entity)
    entities.remove_observer(self)
  end      

  def add_mat(entity, matatt)
    entity.definition.entities.grep(Sketchup::ComponentInstance).each do |e|  
      attribut = e.get_attribute 'dynamic_attributes', 'material'
      unless attribut.nil?
        unless attribut == "#{ ""}"
          matatt << attribut
        end 
      end
      add_mat(e, matatt)
    end
  end 

TNT_COMPONENT_PATTERNS = [/TABLE SUR PIED CLC2/,/PLAN BAR/,/PLAN DE TRAVAIL DYNAMIQUE/]  
  
  def onElementAdded2(entities, entity)
    return unless entity.is_a?(Sketchup::ComponentInstance)
    return unless TNT_COMPONENT_PATTERNS.any?{ |pattern| entity.definition.name =~ pattern }
    
    $dc_observers.get_latest_class.redraw_with_undo(entity)
     
    entity.model.start_operation('Color Faces', true)
    meubels_verven(entity)
    entity.model.commit_operation
	  entities.remove_observer(self)
  end
	
  PAINT_COMPONENT_PATTERNS = [/PLATEAU BAR/,/PLAN/]
  DONT_PAINT_COMPONENT_PATTERNS = [/POIGNEE/]
    
  def meubels_verven(entity, paint_subcomponent = false)
    entity.definition.entities.grep(Sketchup::ComponentInstance).each do |instance|
      
    next unless (paint_subcomponent) || PAINT_COMPONENT_PATTERNS.any?{ |pattern| instance.definition.name =~ pattern }
    next if DONT_PAINT_COMPONENT_PATTERNS.any?{ |pattern| instance.definition.name =~ pattern }
    
      if instance.material
        instance.definition.entities.grep(Sketchup::Face).each do |f|
          f.material = instance.material
          instances_parent = instance.parent.name
          stop = []
          ["V40","V60","V80","V100"].each do |lst|
            stop << "STOP" if instances_parent.include?(lst)
          end
          if stop.empty?
            f.back_material = instance.material 
          end
        end
      end
      meubels_verven(instance, true)
    end
  end   
end

This code is active when components with the definition “PLATEAU BAR” or “PLAN” are imported into SketchUp.

The goal is to create the material “PLAN” and apply it to the premitives of the components.

The code works well and I’m not sure I understand their expectations.

Can you help me understand that he is not going?

Thank you


#2

Nobody to an idea of the problem?

Who can I contact in the SketchUp team to find a solution?

Thank you

David


#3

You’re supposed to set the transparency option to true when you’re inside an observer, like the example in the docs:

class MyDefinitionsObserver < Sketchup::DefinitionObserver
  def onComponentAdded(definitions, definition)
    return if definition.deleted?
    # The operation name won't be displayed when the fourth argument is
    # +true+. It will absorb into the previous operation.
    definition.model.start_operation('Tag It', true, false, true)
    definition.set_attribute('MyExtension', 'Tag', 'You are it')
    definition.model.commit_operation
  end
end

observer = MyDefinitionsObserver.new
model = Sketchup.active_model
model.definitions.add_observer(observer)
#  Change this:
entity.model.start_operation('add texture', true)
#  To this
entity.model.start_operation('add texture', true, false, true)

#4

Thank you very much Gordon McIntyre!
I will submit again Click-Kitchen 2 and Click-Change 2. :slight_smile: