sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Sketchup.active_model.set_attribute

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 3 Posters 985 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.
    • J Offline
      Jorgensen
      last edited by

      Thanks Todd

      Actualy, I need this to keep track of some add_text objects I create - so that I can run through the 'test' array and delete the objects that I have created, even if the file has been saved and Sketchup restarted. Will this be possible ?

      Is this right ?
      "rc = Sketchup.active_model.set_attribute("TM_area", "test", "false") ; "
      do you save the test array ?

      As you (and surely many others) can se, I'm not a programmer, just an architect, that have a need for a special function, and tries to create this by looking in others scripts and in this forum 😳

      Maybe there should be two groups under Ruby, one for programming ruby/sketchup and one for using plugins ?

      Thanks
      Jorgensen

      Thanks

      sketchup pro 2016 16.1.1449 64 bit | windows 10 pro | i7-3770k @3.5 GHz | 16gb ram | gtx 780 ti / gtx 980 ti | nvidia driver 368.39

      1 Reply Last reply Reply Quote 0
      • T Offline
        todd burch
        last edited by

        No, that doesn't save a test array at all. that saves a key of "test", and it's value" of "false".

        If you want to save an array worth of data, and then get it later, here is the code to do that:

        def save_array(data) 
        	model = Sketchup.active_model
        	dictionary_name = "TM_area" ; 
        	i = 1 ; 
        	data.each {|value| 
        		model.set_attribute(dictionary_name, "var"+i.to_s , value ) ; 
        		i+= 1 ; 
        	}		
        end ; 
        
        def get_array() 
        	model = Sketchup.active_model
        
        	any_array = [] ; 
        	dictionary_name = "TM_area" ; 
        	i = 1 ; 
        	while true  
        		value = model.get_attribute(dictionary_name, "var"+i.to_s ) ; 
        		break if !value
        		any_array.push(value) 
        		i += 1 ; 
        	end
        	return any_array ; 
        end ; 
        
        
        myarray = [] ; 
        
        myarray.push("This is the first entry") ; 
        myarray.push("Entry #2...") ; 
        myarray.push("3rd and final entry.") ; 
        
        save_array(myarray) ; 
        
        myarray = []  ;   # reset array to empty 
        
        myarray = get_array() ; 
        i = 1 ; 
        myarray.each {|value| 
        	puts "Value #{i} is #{value}." ; 
        	i+=1 ; 
        } 
        
        1 Reply Last reply Reply Quote 0
        • J Offline
          Jorgensen
          last edited by

          Hi Todd

          Thanks for your input - it works well πŸ˜„

          I have one problem though:
          I use the get_array to recive an array (logic πŸ˜„ )
          I then run through this array - witch contains some entityIDs and if an object, with this ID exist, I delete it from the model and from the array. I then save the array again.

          The problem is, that even if I delete some keys from the Array - so lets say the original array has an length of 10 and I then delete five objects, the length of the array is 5. I then save the array - from i=1 to i=6 - but the old ones from 6-11 still exits in the dictionaryname - how do I delete the var1 - var10 before updating the dictionaryname ?

          Thanks
          Jorgensen

          sketchup pro 2016 16.1.1449 64 bit | windows 10 pro | i7-3770k @3.5 GHz | 16gb ram | gtx 780 ti / gtx 980 ti | nvidia driver 368.39

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

            I guess it could be solved via delete_key - but how ?

            sketchup pro 2016 16.1.1449 64 bit | windows 10 pro | i7-3770k @3.5 GHz | 16gb ram | gtx 780 ti / gtx 980 ti | nvidia driver 368.39

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

              I have just tried to save
              model.set_attribute("TM_area", "objects", data)

              where data is an array - I can close sketchup and open gain, reload the file and the array can be read again as model.get_attribute("TM_area", "objects")

              Works fine - BUT

              why the #€"#€%€%) have the entityID for the objects I created change when I reload the file ? now I can't keep track of them anymore 😞

              a guite tired
              Jorgensen

              sketchup pro 2016 16.1.1449 64 bit | windows 10 pro | i7-3770k @3.5 GHz | 16gb ram | gtx 780 ti / gtx 980 ti | nvidia driver 368.39

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

                I guess this has something to do with it
                "The entityID is not persistent between sessions."

                But how can I then recognise objects when a file is reloaded ?

                Thanks
                Jorgensen

                sketchup pro 2016 16.1.1449 64 bit | windows 10 pro | i7-3770k @3.5 GHz | 16gb ram | gtx 780 ti / gtx 980 ti | nvidia driver 368.39

                1 Reply Last reply Reply Quote 0
                • T Offline
                  todd burch
                  last edited by

                  I'm certain there is a delete method to get rid of a dictionary key. Before saving the array, delete all existing keys. It's a brainless (simple) way to do it.

                  How? Figure it out! Use the Ruby console to experiment. http://download.sketchup.com/sketchuphelp/gsu6_ruby/Docs/Ruby-AttributeDictionary.html#delete_key

                  You can't use entityID across SketchUp session. Assign them your own ID as a key/value. "Jorg1", "Jorg2", "Jorg_Cabinet", "Jorg_Oil_Platform", etc. Whatever you want to call them. Use some Dictionary name that you expect will be unique in the bog scheme of things. Perhaps a dictionary names like "Jorgensen" or something unique to you / about you. I use "Smustard".

                  Todd

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

                    YES YES YES YES
                    I found the solution 😍

                    I add a entity.set_attribute ("TM_area", "TM_ID", "TM_ID " + target.entityID.to_s)
                    This is kept and can be used to recognise the objects - finally the script can be finshed 🀣

                    I won't disturb anymore - I did that quite alot - sorry

                    Thanks Todd (thanks all)

                    Jorgensen
                    😍

                    Here it is in action. This will be very useful for me when sketching projects with focus on areas


                    TM_Area.jpg

                    sketchup pro 2016 16.1.1449 64 bit | windows 10 pro | i7-3770k @3.5 GHz | 16gb ram | gtx 780 ti / gtx 980 ti | nvidia driver 368.39

                    1 Reply Last reply Reply Quote 0
                    • C Offline
                      CPhillips
                      last edited by

                      @jorgensen said:

                      YES YES YES YES
                      I found the solution 😍

                      I add a entity.set_attribute ("TM_area", "TM_ID", "TM_ID " + target.entityID.to_s)
                      This is kept and can be used to recognise the objects - finally the script can be finshed 🀣

                      I won't disturb anymore - I did that quite alot - sorry

                      Thanks Todd (thanks all)

                      Jorgensen
                      😍

                      Here it is in action. This will be very useful for me when sketching projects with focus on areas

                      Be advised that entityID can change when a file is saved and loaded. I hope that isnt a problem for your application.

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

                        That should not be a problem, because I'm adding my own attribute:
                        entity.set_attribute ("TM_area", "TM_ID", "TM_ID " + target.entityID.to_s)

                        that I use to keep track of objects.

                        Thanks
                        Jorgensen

                        sketchup pro 2016 16.1.1449 64 bit | windows 10 pro | i7-3770k @3.5 GHz | 16gb ram | gtx 780 ti / gtx 980 ti | nvidia driver 368.39

                        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