Group Color Changing

Dear friends,
In following codes, we make 2 red box in different ways, we can change color of one of them by clicking on it. How can I change color of both of these boxes by clicking on them?

=begin
 encoding: UTF-8
{ =========================================================
  Documentation block ...
} =========================================================
=end

require 'sketchup.rb'
module MajidMahmoudi
  module BoxTest
    $ent = Sketchup.active_model.entities
    $lay = Sketchup.active_model.layers 
    class Box
      def init()
        @original_mat = Sketchup::Material.new
        @st = 0
      end  
      def activate
        init()
        Sketchup.active_model.active_layer = $lay.add('Untagged')
        # Box1
        pt1 = [0, 10, 0]
        pt2 = [2, 10, 0]
        pt3 = [2, 20, 0]
        pt4 = [0, 20, 0]
        grp = Sketchup.active_model.active_entities.add_group
        face1 = grp.entities.add_face [pt1, pt2, pt3, pt4]    
        face1.pushpull -10
        grp.material = "red"
        # Box2
        pt1 = [0, -10, 0]
        pt2 = [2, -10, 0]
        pt3 = [2, -20, 0]
        pt4 = [0, -20, 0]
        grp = Sketchup.active_model.active_entities.add_group
        face1 = grp.entities.add_face [pt1, pt2, pt3, pt4]
        face1.material = "red"
        face1.back_material = "red"
        face1.pushpull -10

        
      end # activate
      def deactivate(view)
        view.invalidate
        Sketchup.status_text = ""
      end

      def suspend(view)
        view.invalidate
        Sketchup.status_text = ""
      end

      def resume(view)
        view.invalidate
        Sketchup.status_text = ""
      end

      def onLButtonDown(flags, x, y, view)
        if @st == 0
          @st = 1
          input = Sketchup::InputPoint.new
          input.pick view, x, y
          face1 = input.face
          if face1
            mats = Sketchup.active_model.materials
            mat = mats.add("green") unless mat=mats["green"]
            path = input.instance_path.to_a
            grp = path.grep(Sketchup::Group).first
            @original_mat = grp.material
            grp.material = mat if grp
          else
            @st = 0
          end    
          view.model.active_path = input.instance_path.valid? ? input.instance_path : nil
        else

          @st = 0
          input = Sketchup::InputPoint.new
          input.pick view, x, y
          face1 = input.face
          if face1  
            path = input.instance_path.to_a
            grp = path.grep(Sketchup::Group).first
            grp.material = @original_mat
            Sketchup.vcb_value = "Choose again"
          end  
        end
      end # OnLBD

    end # class Box
#    unless file_loaded?(__FILE__)
      UI.menu("Plugins").add_item("Box") {
      Sketchup.active_model.select_tool(Box.new)
      }
#      file_loaded(__FILE__)
#    end
  end # BoxTest
end # MajidMahmoudi

The “magic comment” for encoding must be the very first line in a file, or the second line if the first line is a “shebang”. (Ruby embedded script files do not use nor need “shebang” lines because they are not run outside of SketchUp.)


DO NOT add the "Untagged" layer-tag.
It is ALWAYS in the Sketchup::Layers collection.

Use simple reasoning. To find objects to affect with code, your code will search for theses objects.
To find the objects your code wants, give them a name. Then read this prior topic …


I will not comment further on your code as it uses $var global variables, which you have been told repeatedly not to use.

Use @var or @@var references inside your modules and classes.

2 Likes

I gave a solution to both versions in a different topic.
Unfortunately, there isn’t much chance that copying the code rows here and there will work.
First, try to understand both versions. Separately. It certainly won’t work in a matter of seconds.
Take as much time as you need. Check line by line, letter by letter until you understand.

As Dan mentioned, the biggest problem is that you don’t pay attention and don’t even accept suggestions written in bold. Even if we describe it several times.

If you ignore us, we will ignore you.

1 Like

I am trying to follow your advice and many times I review them but sometimes I cannot understand it. Even I cannot understand I keep them on my mind and think about it. Dan advice me for file format and I tried it seriously. Give me time and keep advice me, I will improve. I know you gave me solution to both versions in different topic and I can use both but I think maybe there is another way that can work with both versions. If I have to use 2 ways, just let me know.

Sorry for my mistake. You told me before but I forget to delete them in all of my codes. Now I deleted all and this mistake will never happen.

For “magic comment” I thought simply we can ignore gray codes and I just use it to follow your advice to write plugins code. Now I know it is important.

Still I have problem with file_loaded code. This is why I deactivate it.

# encoding: UTF-8
#{ =========================================================
#  Documentation block ...
#} =========================================================

require 'sketchup.rb'
module MajidMahmoudi
  module BoxTest
    class MajBox
      def init()
        @ent = Sketchup.active_model.entities
        @lay = Sketchup.active_model.layers 
        @original_mat = Sketchup::Material.new
        @st = 0
      end  
      def activate
        init()
        Sketchup.active_model.active_layer = ('Untagged')
        # Box1
        pt1 = [0, 10, 0]
        pt2 = [2, 10, 0]
        pt3 = [2, 20, 0]
        pt4 = [0, 20, 0]
        grp = Sketchup.active_model.active_entities.add_group
        face1 = grp.entities.add_face [pt1, pt2, pt3, pt4]    
        face1.pushpull -10
        grp.material = "red"
        # Box2
        pt1 = [0, -10, 0]
        pt2 = [2, -10, 0]
        pt3 = [2, -20, 0]
        pt4 = [0, -20, 0]
        grp = Sketchup.active_model.active_entities.add_group
        face1 = grp.entities.add_face [pt1, pt2, pt3, pt4]
        face1.material = "red"
        face1.back_material = "red"
        face1.pushpull -10

        
      end # activate
      def deactivate(view)
        view.invalidate
        Sketchup.status_text = ""
      end

      def suspend(view)
        view.invalidate
        Sketchup.status_text = ""
      end

      def resume(view)
        view.invalidate
        Sketchup.status_text = ""
      end

      def onLButtonDown(flags, x, y, view)
        if @st == 0
          @st = 1
          input = Sketchup::InputPoint.new
          input.pick view, x, y
          face1 = input.face
          if face1
            mats = Sketchup.active_model.materials
            mat = mats.add("green") unless mat=mats["green"]
            path = input.instance_path.to_a
            grp = path.grep(Sketchup::Group).first
            @original_mat = grp.material
            grp.material = mat if grp
          else
            @st = 0
          end    
          view.model.active_path = input.instance_path.valid? ? input.instance_path : nil
        else

          @st = 0
          input = Sketchup::InputPoint.new
          input.pick view, x, y
          face1 = input.face
          if face1  
            path = input.instance_path.to_a
            grp = path.grep(Sketchup::Group).first
            grp.material = @original_mat
            Sketchup.vcb_value = "Choose again"
          end  
        end
      end # OnLBD

    end # class MajBox
#    unless file_loaded?(__FILE__)
      UI.menu("Plugins").add_item("MajBox") {
      Sketchup.active_model.select_tool(MajBox.new)
      }
#      file_loaded(__FILE__)
#    end
  end # BoxTest
end # MajidMahmoudi

I know, I know, I should not send so many codes and just something important but let me do it for this topic only.

The __FILE__ interpreter function only works when you load the Ruby file from disk.
It returns "<main>" if you copy and paste the code into a Ruby console.

All the block does is prevent multiple menu items being created when the file is reloaded during testing.

You can put that part in a separate file by itself if it gives you problems during development.


And I just tested it on my machine, and it loads fine.

There are other runtime exceptions happening that are not related to this.

1 Like

I activate codes and put it in plugine directory, it works well as you said and again, ruby code editor cannot run it. Also it prevent multiple menu and every time I have to reload sketchup program. If you agree, I will activate codes after I finish testing/ learning. As Dezmo said, I need better understanding each line of codes.

What does “activate codes” mean ?

You should not put code files directly into the "Plugins" directory.
Each extension should be in it’s own subfolder that uses the correct unique name.

Ie … "MajidMahmoudi_BoxTest/Boxtest.rb"

And then in the "Plugins" directory you have an extension registrar script that allows the user to switch the extension on or off using the Extension Manager.

# encoding: UTF-8
# file: "Plugins/MajidMahmoudi_BoxTest.rb"
require 'sketchup.rb' unless defined? file_loaded
require 'extensions.rb' unless defined? SketchupExtension

module MajidMahmoudi
  module BoxTest

    # Create an entry in the Extension Manager list that loads a script called:
    # "MajidMahmoudi_BoxTest/Boxtest.rb"
    EXTENSION ||= SketchupExtension.new(
      "Majid Mahmoudi: BoxTest", "MajidMahmoudi_BoxTest/Boxtest.rb"
    )
    EXTENSION.instance_eval {
      self.version     = "1.0.0"
      self.description = "A Tool to draw boxes."
      self.author      = "Majid Mahmoudi"
      self.copyright   = "©2020, by author."
      Sketchup.register_extension(self, true)
    }

  end
end
1 Like

Dear Dan, I know it is old topic but I have to back to this topic. For plugin usually we download an main “rbz” file from Extension Wearhouse then in Extension Manager install it. When we install it, automatically a directory and a rb file will make. As I understand rb file have some basic information and use an rbz file in made directory. Would you please let me know how can we make main rbz file from our own rb file?

1 Like

Do some research yourself on creating Zip archive files.

Either using Windows built-in “Send to > Compressed (zipped) folder” …
… or a Zip Utility like WinZip or 7-Zip.

1 Like

Dear Dezmo, I finish my first plugin and plan to distribute it for free. Without you and Dan help I could never do it and I really wish to have especial thanks to you somewhere in software if you let me. Before Aug 1st I never heard about Ruby and from 30 years ago that I wrote assembly codes I never write any code. To be honest I scare send my plugin to SketchUp team and wish to know your idea about it before.
Attached you can find it in “rbz” format.maj_group_wall_maker_v0.0.1.rbz (5.6 KB)

1 Like

Dear Dan, I finish my first plugin and plan to distribute it for free. Without you and Dezmo help I could never do it and I really wish to have especial thanks to you somewhere in software. I wish to know your idea about it before I distribute it I wish to know your idea. Thank you in advance.
maj_group_wall_maker_v0.0.1.rbz (5.6 KB)

1 Like

Sorry, I am too busy to read and critique a whole plugin.
The Warehouse submission process has an automatic code checker. Try it and see if it passes.

I can’t make full and proper check but some notes:

  1. Bad habit of using Layers (Tags)
    The proper way is described here: Controlling-visibility-tags
  2. You must use Tooll#getExtents-instance_method otherwise : “If you don’t implement this method, you may find that part of what the tool is drawing gets clipped to the extents of the rest of the model.”
  3. Extremely long lines in the code. I can’t follow.
  4. Variable names should be more self-explanatory. I can’t follow.
  5. There is no explanation about the first Wall Information window. I have not been able to evaluate what is meaning what?
  6. The “copyright notice” s are not clear. I’m not a lawyer but for sure something are wrong with these…
    .
    .
    .
1 Like

Thank you for advice. Any idea about plugin?

Beside that your plugin two times “bugsplat”-ed SU I cannot judge, I just removed and deleted.
I’m not using any other wall plugin yet, but there are plenty: