• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

AI-Created Extension for SketchUp – Issues with CTRL Modifier Functionality

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 64 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    robertWan
    last edited by robertWan 15 Aug 2024, 17:35

    Hi everyone,

    I’m having trouble with a custom SketchUp tool that was created by AI. The tool correctly highlights a selected plane and applies materials to it when clicked. Additionally, this code works for closed groups and components. However, the CTRL modifier functionality is not working as expected. Here’s a summary of the issues I’m facing:

    Plane Highlighting: The tool highlights the plane correctly when hovered over and works on closed groups and components.
    Material Application: Materials are correctly applied to the selected plane or to closed groups and components when clicked.
    CTRL Modifier: The CTRL key should trigger the application of the material to the entire object (group or component) rather than just the selected plane. This should work for both closed groups and components, but this functionality is not working.
    Here is the relevant code:

    module CustomSelectionTool
    
      class SelectPlaneTool
    
        def initialize
          @ip = Sketchup::InputPoint.new
          @view = Sketchup.active_model.active_view
          @last_face = nil
          @model = Sketchup.active_model
          @selection = @model.selection
          @materials = @model.materials
        end
    
        def onMouseMove(flags, x, y, view)
          @ip.pick(view, x, y)
          face = @ip.face
    
          if face && face.is_a?(Sketchup::Face)
            if face != @last_face
              # Clear previous selection
              @selection.clear
    
              # Select the new face
              @selection.add(face)
              @last_face = face
              view.invalidate
            end
          else
            if @last_face
              # Clear selection when there is no face
              @selection.clear
              @last_face = nil
              view.invalidate
            end
          end
    
          view.tooltip = @last_face ? "Face selected" : "No face selected"
        end
    
        def onLButtonDown(flags, x, y, view)
          if @last_face
            if (flags & MK_CONTROL) == MK_CONTROL
              # Apply material to the entire object (group or component)
              apply_material_to_object(@last_face.parent)
            else
              # Apply material to the selected face
              apply_material(@last_face)
            end
            @last_face = nil
            view.invalidate
          end
        end
    
        def apply_material(face)
          material = @model.materials.current
          if material
            face.material = material
          else
            puts "No material to apply"
          end
        end
    
        def apply_material_to_object(entity)
          material = @model.materials.current
          if material
            if entity.is_a?(Sketchup::Group) || entity.is_a?(Sketchup::ComponentInstance)
              entity.entities.grep(Sketchup::Face).each { |f| f.material = material }
            elsif entity.is_a?(Sketchup::Face)
              entity.material = material
            else
              puts "Cannot apply material to this type of object"
            end
          else
            puts "No material to apply"
          end
        end
    
      end
    
      def self.activate_select_plane_tool
        Sketchup.active_model.select_tool(SelectPlaneTool.new)
      end
    
    end
    
    if !file_loaded?(__FILE__)
      UI.menu('Plugins').add_item('Select Plane Tool') {
        CustomSelectionTool.activate_select_plane_tool
      }
      file_loaded(__FILE__)
    end
    
    

    What I’ve Tried:

    Using Sketchup::InputModifier::CTRL to check if the CTRL key is pressed.
    Ensuring the current material is selected before applying it.
    Any suggestions or insights on why the CTRL key functionality is not applying the material to the entire object (including closed groups and components) as intended would be greatly appreciated. Thank you!

    The code is written in Ruby 1.8, which is compatible with SketchUp 8.

    Robert

    T 1 Reply Last reply 16 Aug 2024, 13:52 Reply Quote 0
    • T Offline
      TIG Moderator @robertWan
      last edited by 16 Aug 2024, 13:52

      Link Preview Image
      Class: Sketchup::Tool

      Tool is the interface that you implement to create a SketchUp tool.

      favicon

      SketchUp Ruby API Documentation (ruby.sketchup.com)

      The arguments passed to the button press method are wrong, they start with key and you test if CTRL was pressed using the right VK_ constant. Also the if test needs rewriting to suit. I'm not near a computer to do more till next week...

      TIG

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by 17 Aug 2024, 15:06

        @robertWan said in AI-Created Extension for SketchUp – Issues with CTRL Modifier Functionality:

        Using Sketchup::InputModifier::CTRL to check if the CTRL key is pressed.

        Where did you get Sketchup::InputModifier::CTRL from? This is NOT a constant defined by SketchUp's Ruby API. Nor is there an InputModifier submodule. Be careful as the AIs have been known to just "invent" coding objects or functions to satisfy themselves in providing "solutions" (which are not guaranteed to work.)

        FYI, the toplevel constants defined by SketchUp's Ruby API are listed at: Top Level Namespace


        In the tool's onLButtonDown callback method, I think you should be using COPY_MODIFIER_KEY or COPY_MODIFIER_MASK instead of MK_CONTROL to test the flags passed in by the SketchUp engine. See the Sketchup::Tool abstract class overview.


        Also, your extension submodule should be wrapped within a unique top-level namespace module. Ie:

        module RobertWan # top-level namespace
          module CustomSelectionTool # extension submodule
        
            class SelectPlaneTool
              # ... the tool code ...
            end
        
            def self.activate_select_plane_tool
              Sketchup.active_model.select_tool(SelectPlaneTool.new)
            end
        
            if !file_loaded?(__FILE__)
              UI.menu('Plugins').add_item('Select Plane Tool') {
                CustomSelectionTool.activate_select_plane_tool
              }
              file_loaded(__FILE__)
            end
        
          end # extension submodule
        end # top-level namespace
        

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 1
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by Dan Rathbun 17 Aug 2024, 15:10

          If it does not work using onLButtonDown then try renaming the callback to onLButtonUp. I recall back in the v8 days there were some quirks with one or the other callback method (I think on Mac platform mostly.)

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Rich O BrienR Rich O Brien moved this topic from Extensions & Applications Discussions on 17 Aug 2024, 20:08
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement