Uncheck icons for each new project

Hello everyone.

As the title of the topic indicates, I’d like to uncheck icons for each new open SketchUp model.

I tried using “onNewModel” and “@@ icon.set_validation_proc {MF_UNCHECKED}”, but I did not succeed.

How would you do that?

Thank you

David

You use UI::Command instances.

But you do NOT use a hardcoded validation Proc.
Instead you access a module variable that holds a nested hash of the check states.
Outer hash has model objects as keys, inner hash has command symbols as keys.

module Author::SomePlugin

  extend self
  @@loaded ||= false

  @@command ||= {}
  @@checkstate ||= Hash::new(MF_UNCHECKED)

  def nifty_thing()
    # command code
  end

  def expectsStartupModelNotifications
    return true
  end

  def onActivateModel(model) # Mac ONLY
    unless @@checkstate.empty?
      @@checkstate.delete_if {|model_key,h| !model_key.valid? }
    end
  end

  def onNewModel(model)
    reset_checkstates(model)
  end

  def onOpenModel(model)
    reset_checkstates(model)
  end

  def reset_checkstates(model)
    if @@checkstate[model] == MF_UNCHECKED || !@@checkstate[model].is_a?(Hash)
      @@checkstate[model]= Hash::new(MF_UNCHECKED)
    end
    @@checkstate.delete_if { |model_key,h| !model_key.valid? }
    @@checkstate[model].each do |model_key,command_checkstates|
      command_checkstates.each do |command,check|
        check = MF_UNCHECKED
      end
    end
  end

  unless @@loaded

    # Declare a command called "NiftyThing":
    @@command[:NiftyThing]= UI::Command::new("NiftyThing") {
      nifty_thing() 
    }.set_validation_proc {
      @@checkstate[Sketchup::active_model][:NiftyThing] rescue MF_UNCHECKED
    }
    # set icons for @@command[:NiftyThing]

    # ... other command declarations ...

    # Attach this module itself as an AppObserver:
    Sketchup.add_observer(self)

    @@loaded = true

  end

end

Added a rescue clause in modifier position to the validation proc, just in case during startup that the active model object is not ready when the UI wants to call validation on a toolbar command button, etc.

Sorry but I can not understand your reasoning Dan.

Simple question:

In your example, does the command called “Nifty Thing” have access to the “nifty_thing ()” method?

I ask you this because the number of “end” closes the “self” class, well before calling it.

If you can demonstrate a simplified example, it would help me understand you.

cordially

David

Simple answer, … I’ll quote the code in the example, and add a # <<<---<<<< comment …

    # Declare a command called "NiftyThing":
    @@command[:NiftyThing]= UI::Command::new("NiftyThing") {
      nifty_thing() # <<<------<<<< The command's block object CALLS a ...
      # method named nifty_thing() thats has all the command's control code.
    }.set_validation_proc {
      @@checkstate[Sketchup::active_model][:NiftyThing] rescue MF_UNCHECKED
    }

Incorrect, because …

(1) there is no class in the example.

(2) It is a module, and the last line of the code closes the module edit.
(I loaded the above example into Ruby with no error except “add_observer” mispelled.)

(3) At the top of the module, there is the statement: extend self
which extends the module using itself as a mixin module.
(This allows other methods in the module to call each other without the self. qualification.)

See: Class: Object (Ruby 2.0.0)


Edited example above to correct misspelling of "add_observer" (was "add_obsever".)


It is what it is. Example code that requires extra work to be made operational.
(ie, icons, actual command code, etc.)

Take it or leave it. I do not have time to devote any more time with this.

Sorry to make you waste your time Dan.

I found a simple solution to do what I wanted:

  class MyAppObserver < Sketchup::AppObserver
    def onNewModel(model)
      A.send :unchek
      model.selection.add_observer(MyAppObserver.new)
    end
  end
  Sketchup.add_observer(MyAppObserver.new)
  class A
    class << self
      def unchek
        @@pro_01.set_validation_proc{MF_UNCHECKED}
      end
      def message
        UI.messagebox("Icon checked, open a new template to uncheck it. ;-)")
        @@pro_01.set_validation_proc{MF_CHECKED}
      end
      tb = UI::Toolbar.new(%Q(TESTE2))
      @@pro_01 = UI::Command.new("MESSAGE CHECKED"){ A.message}
      @@pro_01.set_validation_proc{MF_UNCHECKED}
      tb.add_item(@@pro_01)
      tb.restore
    end
  end

You can copy and paste the code into the ruby console to test. :wink:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.