Auto-naming plugin ?
-
Hi everyone,
I'm totally new to Sketchup plugin development, and I'd like to create a plugin that automatically names groups according to a naming convention. I've already developped a first version that works fine, but there's one last feature that I don't realy know how to implement.
What I have now :
When I create a group, I right-clic on it and I select my plugin. A pannel with a textfield appears, I enter a number, press OK and the script concatenates this number with my naming convention and names the group.
What I'd like to do :
I'd like to save as much time as possible because there are possibly gonna be a looooot of groups to name. My naming convention would be something like "line1 group1, line1 group2, etc."
So, would it be possible to "autoincrement" the group number ? Maybe with something like a variable that increments each time I launch the script ? And with a possibility to set it back to 1 if I start a new "line" ? Maybe with a global variable that would be declared with SU startup or something, but then how could I set it back to zero ? With a UI button ?Sorry, lots of questions here, just to be clear I'm not asking for someone to do the job for me, just if someone could give me some leads, tips, tricks or whatever, that's great
Thanks in advance
-
Have a look at TBD's numb and/or numb e plugins.
http://rhin.crai.archi.fr/rld/plugin_details.php?id=263
http://rhin.crai.archi.fr/rld/plugin_details.php?id=261 -
are they all pre-made groups. or do you make them?
-
-
Hi all,
Sorry for updating this topic after such a long time, but I've just started again working on my project. Thanks for your help Jim, those plugins helped me understand about Sketchup scripts !
The only thing here is that I'd like to keep track of my variables between each time I launch the script. So here's what I did :
I created a first script that generates a text file with values in it, and then a second script that :
- Parses this text file
- Puts variables inside a "naming convention"
- Names the group I've selected according to this naming convention
- Increments the variable
- Updates the textfile
Thing is, I'd like to avoid using a text file : if the person using my script gives his sketchup file without this textfile, then all previous work would be lost. Is there a way I could store values somewhere in the program and use them as I want ?
I thought about creating "invisible groups" and use their names as data storage, but I don't know if it's a good practice !
If you have any idea please let me know !
Thanks !
-
It would probably be best to add a custom attribute-dictionary to the SKP itself [
Sketchup.active_model.set_attribute(dictionary_name, key, value)
].You would then use
Sketchup.active_model.get_attribute(name, key, default_value)
to read the last value saved, and then later on you would useSketchup.active_model.set_attribute(name, key, current_value)
to replace the key with the current value.Provided that the model has been saved then its SKP will contain these latest values when it is reopened later, even if it were copied onto another computer...
You can store many kinds of value as a key=value attribute, in your own named attribute-dictionary.
For example - Float/Integer/Length, String, Boolean[true/false], Array of any of these...These key+values are saved as SKP specific records, I expect that is what you'll want... but if you want to have a set of key+values that apply to ALL SKPs. then you can consider the use of
Sketchup.read_default
and.write_default
to save string data to the Registry - but remember that the data is then specific to that User on that PC, and does NOT pass on with each SKP - unless you also write those very same attributes to each SKP too, and code so that if there are no Registry entries, then the SKPs values are used instead - and the PC's Registry entries and all SKPs values are thereafter kept in sync ? -
Wow, thanks for this very clear answer TIG, didn't know it was possible to add a dictionary to the SKP itself !
I'll try this when I get back home, thank you again !
EDIT : That's perfect, it works as expected ! Thanks !
-
Sorry again for double post, but there's something strange going on with my script.
It works fine ... once
I created the dictionnary as you suggested, and it's fine, I can read from it, name my group according to it and then increment but only one time.It seems like the incrementation is not even executed the second time. Here's what I have :
` # Read data from the dict
data1 = Sketchup.active_model.get_attribute "NAME", "data1"
data2 = Sketchup.active_model.get_attribute "NAME", "data2"
data3 = Sketchup.active_model.get_attribute "NAME", "data3"
data4 = Sketchup.active_model.get_attribute "NAME", "data4"Name the selected group according to the dict
as.each {|g|
Sketchup.active_model.selection.clear
Sketchup.active_model.selection.add(g)
g.name = "#{data1} #{data2} #{data2} #{data3}"
Sketchup.active_model.selection.clear
}Write in the dict with an incremented variable
Sketchup.active_model.set_attribute "NAME", "data4", "#{data4 + 1}"`
I also tried creating a variable
newdata4
, increment it withdata4 += 1
and then update the dict with this variable, but it doesn't work eitherHow could this work once but not twice ?
Thanks in advance !
-
You need to make things into say an integer - they are all strings...
model = Sketchup.active_model
Ensuredata4
has a value:
data4 = model.get_attribute("NAME", "data4", "1").to_i
[that returns an integer1
]
Then ensure something is set/saved:
Sketchup.active_model.set_attribute("NAME", data4.to_s)
Then use the integers, includingdata4
to form the name:
g.name = "#{data1} #{data2} #{data3} #{data4}"
...and later increment the references and save them...
model.set_attribute("NAME", "data4", "#{data4 + 1}")
Incidentally
'NAME'
is not a very unique name for a registry entry... can you use your Plugin's name - like "Seb_Name" -
Thanks TIG ! That's perfect !
I don't know why but I thought Ruby would handle it automatically ! -
You see to want all integers so turning them from integers to strings and back again looks logical...
Then you can use+1
to increment the integers...BUT if you wanted 'combined references' you can use say:
data = "A01"
Then later increment the string thus:
data.next!
which gives"A02"
The Ruby method.next
increments strings, sox="A"; x.next!; x; "B"
etc....next
returns the string incremented but leaves the original reference unchanged, adding the!
as in.next!
also increments the reference itself.
Advertisement