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

    'Dynamic' array

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

      Hi

      I'm not sure if this is the right forum for asking ruby questions, so please feel free to move my topic.

      I'm trying to create a simple function that collects the areas of the face inside groups and puts a text note that shows the totals.

      But I'm having some problems figure out how to store the areas.

      I have some groups. Every group consist of one face. The face has a name eg. Office home etc.

      I then would like to collect the areas as:
      Office 50.000 m2
      Home 24.000 m2
      Disp. 3.000 m2
      Total 77.000 m2

      I thought I could use something like AreaTotal[name] = AreaTotal[name] + object.area, and then run through the array and list the name of the key and value, but it seems arrays don't accept variables as keys.

      Can anyone please help me to find a solution?

      Ps. I know there might be a plugin that does the job, but I find it somehow exciting to create it myself.

      Thanks
      Jorgensen
      Denmark

      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
      • TIGT Offline
        TIG Moderator
        last edited by

        You need a hash {} to store the space as the key and the value.
        You can then get an array [] of the key-names, sort that in order and then get the hash key value that matches each sorted array element.
        The names need to be unrepeated.
        To get the total at the end simply add up the values and assign it to a hash key 'Total'...
        areas={} groups.each{|group| faces=group.entities.to_a.find_all{|e|e.class==Sketchup::Face} areas[group.name]=faces[0].area }### assuming only ever one face and its area is in sqm already names=[] areas.each_key{|k|names << k} names.sort! tot=0 names.each{|name| tot+=areas[name] puts name+" "+areas[name].to_s } puts puts "Total "+tot.to_s
        😕
        You can sort hashes etc but I felt you might be happier just remembering everything in a hash and using an array to process it 😉

        TIG

        1 Reply Last reply Reply Quote 0
        • sdmitchS Offline
          sdmitch
          last edited by

          The API says that face.area returns the area in current units but my experience is that it always returns square inches. So area would need to be divided by inches/meter squared.

          Nothing is worthless, it can always be used as a bad example.

          http://sdmitch.blogspot.com/

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

            You are right about face.area arriving in sq".
            But it's easy enough to convert to sqm and then to fix it at say 3dp using

            area_text = sprintf("%.3f", face.area*0.00064516)

            I was concentrating on the array[] versus hash{} issue... 😒

            TIG

            1 Reply Last reply Reply Quote 0
            • sdmitchS Offline
              sdmitch
              last edited by

              Not to step on TIG's toes but to find the groups

              groups=Sketchup.active_model.active_entities.find_all{|e| e.class==Sketchup;;Group}
              

              and to total area of multiple faces

               areas[group.name]=0 if !areas[group.name]
               areas[group.name] += faces[0].area
              
              

              Nothing is worthless, it can always be used as a bad example.

              http://sdmitch.blogspot.com/

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

                And this:
                names=[] areas.each_key{|k|names << k} names.sort!

                can be replaced with:
                names = areas.keys.sort!

                I'm not here much anymore.

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

                  BTW.. the following thread has a code example for getting the volumes of multiple groups.
                  You can copy it rename the outer and inner modules, to something like: Jorgensen::MultiArea and modify it to return areas instead of volumes.
                  Volume of Multiple Groups

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • sdmitchS Offline
                    sdmitch
                    last edited by

                    If you look back at my previous post, the second code snippit has the answer to your problem.

                    Nothing is worthless, it can always be used as a bad example.

                    http://sdmitch.blogspot.com/

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

                      Hi all

                      I think I'm a bit lost here.

                      Could this part somehow be split up? I think that could help me to understand whats going on 😄
                      groups.each{|group|faces=group.entities.to_a.find_all{|e|e.class==Sketchup::Face} areas[group.name]=faces[0].area

                      Because I can't figure out where to place this part areas[group.name]=0 if !areas[group.name], and when I add the += I get an error saying Error: #<NoMethodError: undefined method+' for nil:NilClass>`.

                      Right now my (your) code looks like this:

                      <span class="syntaxdefault">def tmArea<br /><br />    groups</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find_all</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.class==</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">    areas </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{}<br /></span><span class="syntaxdefault">    groups</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">faces</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find_all</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.class==</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">    areas</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+=</span><span class="syntaxdefault"> faces</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">area    </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">names </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> areas</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">keys</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">sort</span><span class="syntaxkeyword">!<br /><br /></span><span class="syntaxdefault">tot</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">0<br />names</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">tot</span><span class="syntaxkeyword">+=</span><span class="syntaxdefault">areas</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">puts name</span><span class="syntaxkeyword">+</span><span class="syntaxstring">" "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">areas</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_s<br /></span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">puts<br />puts </span><span class="syntaxstring">"Total "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">tot</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /><br />end<br /><br /></span><span class="syntaxkeyword">if(</span><span class="syntaxdefault">not file_loaded</span><span class="syntaxkeyword">?(</span><span class="syntaxstring">"TM_Area.rb"</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">  Mejeriet_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Plugins"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"TM"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Arealer"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  Mejeriet_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"On"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault">  tmArea </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">end<br />file_loaded</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"TM_Area.rb"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span>
                      

                      Sorry for my noob questions.

                      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

                        First thanks to your both for the fast reply.

                        @TIG
                        the code looks far more professional than my code would look like, I'm not sure if coding in Ruby/SU has change a lot since my last attemt three years ago.

                        As I understand your code, if two groups has the same name e.g. Office - it won't be saved as a total, but only the area of the last object will be saved.

                        Do I need something like areas[group.name] += areas[group.name] + faces[0].area to sum the areas?

                        I have added
                        model = Sketchup.active_model groups = model.active_entities

                        Is this still the right way of getting the groups?

                        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
                        • J Offline
                          Jorgensen
                          last edited by

                          Hi sdmitch

                          How do you fit in 'areas[group.name]=0 if !areas[group.name]
                          areas[group.name] += faces[0].area' into

                          groups.each{|group|faces=group.entities.to_a.find_all{|e|e.class==Sketchup::Face}
                          areas[group.name]=faces[0].area

                          ?

                          Sorry I'm a noob 😕 but I'll try to learn

                          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
                          • sdmitchS Offline
                            sdmitch
                            last edited by

                            by simply adding the line above the areas[group.name] += faces[0].area line like this

                            groups.each{|group|
                             faces=group.entities.to_a.find_all{|e|e.class==Sketchup;;Face}
                             areas[group.name]=0 if !areas[group.name]
                             areas[group.name]+=faces[0].area
                            }### assuming only one face in group
                            
                            

                            Nothing is worthless, it can always be used as a bad example.

                            http://sdmitch.blogspot.com/

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

                              Works like a charm now 😄

                              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
                              • 1 / 1
                              • First post
                                Last post
                              Buy SketchPlus
                              Buy SUbD
                              Buy WrapR
                              Buy eBook
                              Buy Modelur
                              Buy Vertex Tools
                              Buy SketchCuisine
                              Buy FormFonts

                              Advertisement