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

    Sketchup.active_model.set_attribute

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 3 Posters 923 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

      I'm trying to keep track of some objects via "set_attribute"
      But when I retrive the objecs via "get_attribute" I get NIL 😞

      Is'nt possible to keep track of objects in set_attribute ?

      Thanks
      Jorgensen

      This is muy short test script:
      def tmTest
      model = Sketchup.active_model
      test = model.get_attribute "TM_area", "test", 'false'
      if(test == "false")
      test = Array.new
      end
      a = model.selection
      for object in a
      test.push object.typename
      end
      Sketchup.active_model.set_attribute "TM_area", "test", test

      for object in test
      puts object
      end
      end

      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've fixed a few errors. This is a complete test case that works.

        Todd

        def tmTest
        	model = Sketchup.active_model
        	test = model.get_attribute "TM_area", "test" ; 
        	puts "test = #{test}" ; 
        	if(test == "false") 
        		test = Array.new
        	end
        	a = model.selection
        	for object in a
        		test.push object.typename
        	end
        
        	for object in test
        		puts object
        	end
        end
        
        rc = Sketchup.active_model.set_attribute("TM_area", "test", "false") ; 
        puts "rc = #{rc}" ; 
        tmTest 
        
        
        1 Reply Last reply Reply Quote 0
        • 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