• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

Menu.add_item algorithm problem

Scheduled Pinned Locked Moved Developers' Forum
11 Posts 3 Posters 573 Views 3 Watching
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.
  • P Offline
    Pauline01
    last edited by 22 Jan 2010, 07:34

    Hello,

    I don't find a solution, see in my code after "#####menu of the friend list to delete them", i have explain the problem.

    It's an algorithm problem.

    Thx.

    require 'sketchup.rb'
    
    def delete_friend(NameFriend)
    
     UI.messagebox (NameFriend)
    
    end
    
    #Menu
    if (not file_loaded?("zListFriend_config2.rb"))
     
        UI.add_context_menu_handler do |menu|
          if (Sketchup.active_model.selection.length == 1)
    	    menu.add_separator
    	    if ((Sketchup.active_model.selection[0].get_attribute("MyDico", "Name")) != nil)
    	
    			  i=0
    			  a="x"
    			
    			  while (a != "" )
    			      
    		             NameFriends="NameFriends"+i.to_s
    	                     a=Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s
    				  if( a !="")
    		
    #####menu of the friend list to delete them
    #####My problem; i don't arrive to pass the good name in the function "delete_friend", when i click on sketchup.
    ###i want to pass the name corresponding example; if i have the menu "Delete a Friend (Jim), i want to pass Jim in the function delete_friend.
    				  
                                     menu.add_item("Delete a Friend(" + NameF=Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s + ")") { delete_friend(NameF)} 
    
    ###NameF is the good name on the menu title but don't pass the good name in my function
     
    				  end
    				  i += 1
    		          
    			  end  
         
    	    end
          end
        end
    
      file_loaded("zListFriend_config2.rb")
    end
    
    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 22 Jan 2010, 08:09

      Menu items get made as SUp launches, you can add menu items with code as you continue to use it BUT you cannot remove or modify existing items - therefore you need to have a general 'Delete friend..' item that opens a dialog populated with the required name(s) etc...

      TIG

      1 Reply Last reply Reply Quote 0
      • P Offline
        Pauline01
        last edited by 22 Jan 2010, 09:16

        We can use the extension gtk2? i don't test for the moment.

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 22 Jan 2010, 09:30

          @tig said:

          Menu items get made as SUp launches, you can add menu items with code as you continue to use it BUT you cannot remove or modify existing items - therefore you need to have a general 'Delete friend..' item that opens a dialog populated with the required name(s) etc...

          But that's a context menu. It gets created every time - so you can make menu items with different titles.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 22 Jan 2010, 09:38

            Pauline, you want NameF to contain a user friendly name - but instead you're getting something else? What value is it NameF is getting?

            Also; "NameF" - in Ruby, variables start with lower case letters, and constants starts with upper case. "NameF" is a constant as you start with an upper case, while you use it as a variable.

            Another note:
            At some point you typed: a=Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s
            and after that point you don't need to refer to Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s as that has been refereed to by your variable a.

            Example:
            menu.add_item("Delete a Friend(" + NameF=Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s + ")") { delete_friend(NameF)}
            can simply be:
            menu.add_item("Delete a Friend(" + a + ")") { a }

            You see? You already have a variable a referring to Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s, so you don't need to create the variable NameF that also refer to the same thing.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 22 Jan 2010, 09:40

              I apologize - I quickly scanned the code and didn't realize it was a context-menu, which as thomthom points out is capable of changes to suit the current circumstances and is NOT fixed...
              I don't think it is a menu issue... you have used capital letters to start your def's array and variable names - this is not allowed in Ruby for local variables - they must start with a lowercase letter e.g. nameFriend NOT NameFriend...
              This is a better version..

              
              require 'sketchup.rb'
              
              def delete_friend(nameFriend)
              
              UI.messagebox (nameFriend)
              
              end
              
              #Menu
              if (not file_loaded?("zListFriend_config2.rb"))
              
                  UI.add_context_menu_handler do |menu|
                    if (Sketchup.active_model.selection.length == 1)
                     menu.add_separator
                     if ((Sketchup.active_model.selection[0].get_attribute("MyDico", "Name")) != nil)
                 
                         i=0
                         a="x"
                       
                         while (a != "" )
                            
                                 nameFriends="NameFriends"+i.to_s
                                      a=Sketchup.active_model.selection[0].get_attribute("MyDico", nameFriends).to_s
                            if( a !="")
                    
              #####menu of the friend list to delete them
              #####My problem; i don't arrive to pass the good name in the function "delete_friend", when i click on sketchup.
              ###i want to pass the name corresponding example; if i have the menu "Delete a Friend (Jim), i want to pass Jim in the function delete_friend.
                           
                                               menu.add_item("Delete a Friend(" + nameF=Sketchup.active_model.selection[0].get_attribute("MyDico", nameFriends).to_s + ")") { delete_friend(nameF)}
              
              ###nameF is the good name on the menu title but don't pass the good name in my function
              
                            end
                            i += 1
                             
                         end 
                   
                     end
                    end
                  end
              
                file_loaded("zListFriend_config2.rb")
              end
              

              ...
              EDIT: thomthom beat me to it !!!!!!!!!! 👊 with a better analysis too... 💚

              TIG

              1 Reply Last reply Reply Quote 0
              • P Offline
                Pauline01
                last edited by 22 Jan 2010, 10:30

                ok thx, i test your solution. I don't know what I'll do without you...really...😄

                Pauline, you want NameF to contain a user friendly name - but instead you're getting something else? What value is it NameF is getting?

                NameF contained Just a person, and this person have a lot of friends (NameFriend)
                I created a function that associates an object to other objects. Each object is a person.

                So a person(NameF) conatained friends ("NameFriend"+i.to_s)

                Exemple: Jim (NameF) contained too Adrian(NameFriend1),Jaine(NameFriend2),Sylvain(NameFriend3)

                My menu is a list to delete a friend. I want to pass the NameFriend corresponding in the function "delete_friend" to know what a friend I delete.

                And in my menu, when i click on my menu, NameFriend is not the good variable, because:

                NameFriends="NameFriends"+i.to_s
                
                menu.add_item("Delete a Friend(" + NameF=Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s + ")") { delete_friend(NameF)} 
                

                When i click on my menu NameF i have always the last NameF where i is max. So...it's good for the last friend but no for other.

                An other question? I do not know if I'm clear...

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 22 Jan 2010, 11:26

                  1.) Change all your variables to start with a lower case.

                  2.) menu.add_item("Delete a Friend(" + NameF=Sketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s + ")") { delete_friend(NameF)}
                  Does the menu say "Delete a Friend (Jim)"?

                  3.) You method delete_friend(): what is the content of the argument you pass to it? Have you tried to output the argument to the console?
                  Like

                  def delete_friend(name)
                    puts name
                  
                    # Rest of your code...
                  end
                  

                  ?

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • P Offline
                    Pauline01
                    last edited by 22 Jan 2010, 17:28

                    menu.add_item("Delete a Friend(" + namef=Sketchup.active_model.selection[0].get_attribute("MyDico", namefriends).to_s + ")") { delete_friend(namefriends)}
                    

                    I want to pass the attribute corresponding, after in my function:

                    def delete_friend(name)
                    
                    puts name
                    
                    a=Sketchup.active_model.selection[0].set_attribute("MyDico",name,nil)
                    
                    end
                    

                    the problem, if i have for my object the attributes: name="Pauline"; namefriends1="Jim"; namefriends2="John" ect ect namefriends+i="a name" (pauline it's a person and Jim, john ect ect are friends, if i want delete a friend of the list i must say "namefriends corresponding to the friend is nil".)

                    in skectchup i have my menu with a list of: Delete a friend(Jim)
                    Delete a friend(john)
                    Delete a friend(anna)

                    when i click on one menu the namefriends is always anna because it's namefriends+i.to_s and so namefriends3 for all menu...

                    1 Reply Last reply Reply Quote 0
                    • P Offline
                      Pauline01
                      last edited by 26 Jan 2010, 09:39

                      up

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        thomthom
                        last edited by 28 Jan 2010, 10:28

                        Pauline: I split off the last posts Dan and I made. It turned into a separate discussion.

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        1 / 1
                        • First post
                          1/11
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement