Menu.add_item algorithm problem
-
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 -
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...
-
We can use the extension gtk2? i don't test for the moment.
-
@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.
-
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 toSketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_sas that has been refereed to by your variablea.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
areferring toSketchup.active_model.selection[0].get_attribute("MyDico", NameFriends).to_s, so you don't need to create the variableNameFthat also refer to the same thing. -
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.nameFriendNOTNameFriend...
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... 
-
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.) 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?
Likedef delete_friend(name) puts name # Rest of your code... end?
-
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) endthe 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...
-
up
-
Pauline: I split off the last posts Dan and I made. It turned into a separate discussion.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement