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

    Auto-naming plugin ?

    Scheduled Pinned Locked Moved Newbie Forum
    sketchup
    11 Posts 4 Posters 581 Views 4 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.
    • H Offline
      herr_inspektor
      last edited by

      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 πŸ˜„

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        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

        Hi

        1 Reply Last reply Reply Quote 0
        • D Offline
          driven
          last edited by

          are they all pre-made groups. or do you make them?

          learn from the mistakes of others, you may not live long enough to make them all yourself...

          1 Reply Last reply Reply Quote 0
          • H Offline
            herr_inspektor
            last edited by

            Thanks to both of you for your answers!

            @jim : i'll check your links soon as I get my computer back!

            @driven : i create these groups myself, why ?

            1 Reply Last reply Reply Quote 0
            • H Offline
              herr_inspektor
              last edited by

              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 :

              1. Parses this text file
              2. Puts variables inside a "naming convention"
              3. Names the group I've selected according to this naming convention
              4. Increments the variable
              5. 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 !

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                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 use Sketchup.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 ?

                TIG

                1 Reply Last reply Reply Quote 0
                • H Offline
                  herr_inspektor
                  last edited by

                  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 !

                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    herr_inspektor
                    last edited by

                    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 with data4 += 1 and then update the dict with this variable, but it doesn't work either πŸ˜•

                    How could this work once but not twice ?

                    Thanks in advance !

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      You need to make things into say an integer - they are all strings...
                      model = Sketchup.active_model
                      Ensure data4 has a value:
                      data4 = model.get_attribute("NAME", "data4", "1").to_i
                      [that returns an integer 1 ]
                      Then ensure something is set/saved:
                      Sketchup.active_model.set_attribute("NAME", data4.to_s)
                      Then use the integers, including data4 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" πŸ˜•

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • H Offline
                        herr_inspektor
                        last edited by

                        Thanks TIG ! That's perfect !
                        I don't know why but I thought Ruby would handle it automatically !

                        1 Reply Last reply Reply Quote 0
                        • TIGT Offline
                          TIG Moderator
                          last edited by

                          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, so x="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.

                          TIG

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

                          Advertisement