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

    Nested Attribute Dictionaries

    Scheduled Pinned Locked Moved Developers' Forum
    18 Posts 4 Posters 1.6k 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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      The other day I realized something weird, but have not yet had time to test it out thoroughly, but it does seem to work.

      An AttributeDictionary is a subclass of Entity, and so is AttributeDictionaries.

      The attribute_dictionaries() method, is a Entity method that is inherited by all subclasses.

      So it means that AN AttributeDictionary instance can have AN AttributeDictionaries collection, to which many more AttributeDictionary instances can be attached.

      Basically a tree structure of dictionaries.

      One bummer, the outliner does not list any AttributeDictionary in the model.


      Question:

      What would happen to exporters when they encounter a nested attribute dictionary structures ?

      ❓

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • brewskyB Offline
        brewsky
        last edited by

        @dan rathbun said:

        Basically a tree structure of dictionaries.

        Wow, I didn't realize this...
        Doesn't really seem intended behaviour, but you could write an entire XML structure within a single attribute!

        Sketchup BIM-Tools - http://sketchucation.com/forums/viewtopic.php?p=299107

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

          You can add an AttributeDictrionary to any object that is a sub-class of Entity. Here is a diagram I made (same as attached) of the relationships, and here is the one from the official API documentation.

          So not only can you add attributes to Drawingelements like Faces, Edges, Groups, etc, but also to things like the Layers and Materials collection.

          Just be aware that adding too many dictionaries can bloat the .skp file size tremendously.


          api_sketchup.png

          Hi

          1 Reply Last reply Reply Quote 0
          • brewskyB Offline
            brewsky
            last edited by

            @jim said:

            So not only can you add attributes to Drawingelements like Faces, Edges, Groups, etc, but also to things like the Layers and Materials collection.

            Yeah, but nesting "attribute dictionaries" under "attribute dictionaries" seems to create a perfect tree-structure for storing (for example) xml data.

            @jim said:

            Just be aware that adding too many dictionaries can bloat the .skp file size tremendously.

            That would be a problem here, because storing data like this would create a LOT of additional attributes.

            Would serializing the data(using marshall) and storing this all inside a single attribute be more filesize-efficient?
            I can't seem to find a maximum length for an attribute string...

            Sketchup BIM-Tools - http://sketchucation.com/forums/viewtopic.php?p=299107

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

              @brewsky said:

              Would serializing the data(using marshall) and storing this all inside a single attribute be more filesize-efficient?
              I can't seem to find a maximum length for an attribute string...

              These are things you would need to experiment with. I don't know the best approach to minimize the impact of using a lot of attributes. Some external database connection seems appropriate for a lot of data, but there is no persistent key for objects between SketchUp sessions, and so the data is not portable.

              Hi

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @jim said:

                So not only can you add attributes to Drawingelements like Faces, Edges, Groups, etc, but also to things like the Layers and Materials collection.

                I'm not sure if SketchUp will actually store attributes to all of these elements. I can't remmeber right now - but I think I experienced once that I wasn't able to successfully store attributes to something that I expected would. Need to dig into old code snippets..

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • brewskyB Offline
                  brewsky
                  last edited by

                  @brewsky said:

                  I can't seem to find a maximum length for an attribute string...

                  @jim said:

                  These are things you would need to experiment with.

                  It's possible to store at least a string containing 100000 characters(looping much more than that freezes my pc).
                  ` i = 0;
                  s = ""

                  while i < 100000 do
                  s = s + "a"
                  i +=1;
                  end

                  model = Sketchup.active_model
                  model.set_attribute "testdictionary", "test", s
                  v = model.get_attribute "testdictionary", "test"

                  puts s.length
                  puts v.length`

                  returns:
                  100000
                  100000

                  The file size increases substantially, storing a single string of 100000 long takes it from 8 to 200 kB.
                  But maybe a text-file would increase similarly, it's a pretty long string...

                  Sketchup BIM-Tools - http://sketchucation.com/forums/viewtopic.php?p=299107

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @brewsky said:

                    The file size increases substantially, storing a single string of 100000 long takes it from 8 to 200 kB.
                    But maybe a text-file would increase similarly, it's a pretty long string...

                    IF the skp files are encoding text in UTF-16 then there would be 2 bytes per ASCII character.

                    IF it encodes in UTF-8, there are supposed to be only 1 byte for the characters in the ASCII set.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      SKP files are for the most of it two-byte UTF-16 encoded. (Some old data blocks appear to be ASCII.) All though, the strings you get from the Ruby API is UTF-8 encoded.

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        So then is there a normal, and a backup dictionary in the file ?

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          @dan rathbun said:

                          So then is there a normal, and a backup dictionary in the file ?

                          Backup?

                          Not sure what you mean.

                          But when I have inspected .skp files I saw no duplicate of any attributes blocks - and the attributes data blocks where in UTF-16.

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • Dan RathbunD Offline
                            Dan Rathbun
                            last edited by

                            @brewsky said:

                            ` i = 0;
                            s = ""

                            while i < 100000 do
                            s = s + "a"
                            i +=1;
                            end`

                            it's easier in Ruby to do:
                            s = 'a' * 100000

                            💭

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • thomthomT Offline
                              thomthom
                              last edited by

                              @dan rathbun said:

                              @brewsky said:

                              ` i = 0;
                              s = ""

                              while i < 100000 do
                              s = s + "a"
                              i +=1;
                              end`

                              it's easier in Ruby to do:
                              s = 'a' * 100000

                              💭

                              I'm going to guess it's a bit faster as well.

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by

                                @thomthom said:

                                Backup? Not sure what you mean.

                                Yes I meant duplicate internal backups, like how a floppy disk has a backup of the master boot record, that can be restored if the primary gets corrupted. (Not that many are using floppies these days, but it's an example.)

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • brewskyB Offline
                                  brewsky
                                  last edited by

                                  @dan rathbun said:

                                  it's easier in Ruby to do:
                                  s = 'a' * 100000

                                  😄 Ruby continues to amaze me, it 'reads' so logical!
                                  Still much to learn...

                                  Sketchup BIM-Tools - http://sketchucation.com/forums/viewtopic.php?p=299107

                                  1 Reply Last reply Reply Quote 0
                                  • Dan RathbunD Offline
                                    Dan Rathbun
                                    last edited by

                                    @thomthom said:

                                    But when I have inspected .skp files I saw no duplicate of any attributes blocks - and the attributes data blocks where in UTF-16.

                                    I wonder if we could use Zlib to compress data before writing it to dictionaries ??

                                    Unzipping it on a read ?

                                    I'm not here much anymore.

                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      Another neat usage of * is for joining arrays:
                                      [1,2,3,4,5] * ' - '
                                      Returns:
                                      "1 - 2 - 3 - 4 - 5"

                                      Thomas Thomassen — SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • thomthomT Offline
                                        thomthom
                                        last edited by

                                        @dan rathbun said:

                                        @thomthom said:

                                        But when I have inspected .skp files I saw no duplicate of any attributes blocks - and the attributes data blocks where in UTF-16.

                                        I wonder if we could use Zlib to compress data before writing it to dictionaries ??

                                        I'd love to be able to use zipping in my projects. Think I looked at it some time ago and people had tried to find ways to make it work properyl within SketchUp ruby with mixed results..

                                        Thomas Thomassen — SketchUp Monkey & Coding addict
                                        List of my plugins and link to the CookieWare fund

                                        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