• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

[Plugin] Scale Face to Target Area - TIG

Scheduled Pinned Locked Moved Plugins
46 Posts 11 Posters 25.3k Views
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.
  • E Offline
    EarthMover
    last edited by EarthMover 7 Aug 2012, 13:26

    Is it possible through ruby (or does something exist) to scale a face to a desired area size? Let's say I draw an irregular shape which needs to be a specific square footage. Is it possible to have a plugin which let you click on the face and set a desired square footage and the face automatically scales to that area?

    I'm drawing a bunch of different free form (lagoon style) pools for a client, but have a target square footage the shapes. I can get close with trial and error and scale tool, just thought it would be nice to do it exact through a script.

    EDIT - Plugin attached. Thanks TIG.
    EDIT: by TIG - code fixed for all unit types !
    TIG Donations==PayPalButton


    Original version


    Alternate version to work with any unit type.

    3D Artist at Clearstory 3D Imaging
    Guide Tool at Winning With Sketchup
    Content Creator at Skapeup

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 7 Aug 2012, 14:35

      <span class="syntaxdefault">module TT_ScaleToArea<br />  def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">scale_face</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> face</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> target_area </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    unit_ratio </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'1'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_l </span><span class="syntaxkeyword">**</span><span class="syntaxdefault"> 2<br />    target_area_inch </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> target_area </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> unit_ratio<br />    ratio </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> target_area_inch </span><span class="syntaxkeyword">/</span><span class="syntaxdefault"> face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">area<br />    pt </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">bounds</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">center<br />    tr </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">scaling</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> pt</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> ratio </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">parent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform_entities</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> tr</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  end<br />end<br /></span>
      

      TT_ScaleToArea.scale_face( Sketchup.active_model.selection[0], 4000 )

      target_area is numeric input in the current model units.

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

      1 Reply Last reply Reply Quote 0
      • T Online
        TIG Moderator
        last edited by 7 Aug 2012, 14:49

        Here's my version, with dialog...

        require 'sketchup.rb'
        ###
        module TIG
          ### scales a select face about its center to a new area, in current uinits.
          def self.scale_face()
           model=Sketchup.active_model
           es=model.active_entities
           ss=model.selection
           fa=nil
           fa=ss[0] if ss[0]
           unless fa && fa.is_a?(Sketchup;;Face)
        	  UI.messagebox("Scale_Face;\n\nPreSelect ONE FACE !")
        	  return nil
           end
           ar=fa.area
           mo=model.options["UnitsOptions"]
           p mo["LengthFormat"]
           case mo["LengthUnit"]
        	when 0
        	 units="in"
        	 if mo["LengthFormat"]==2 #Engineering trap ">>'
        	   units="ft"
        	   ar=ar/12.0/12.0
        	 end
        	when 1
        	 units="ft"
        	 ar=ar/12.0/12.0
        	when 2
        	 units="mm"
        	 ar=ar*25.4*25.4
        	when 3
        	 units="cm"
        	 ar=ar*25.4*25.4/10/10
        	when 4
        	 units="m"
        	 ar=ar*25.4*25.4/1000/1000
        	else
        	 units="??"
           end
           rs=inputbox(["New Area [sq "+units+"];        "], [ar], "Scale_Face")
           return nil unless rs
           na= rs[0]
           na= -na if na < 0
           unless na>0
        	  UI.beep
        	  return nil
           end
           ro=Math.sqrt(na/ar)
           pt=fa.bounds.center
           tr=Geom;;Transformation.scaling(pt, ro)
           model.start_operation("Scale_Face")
        	 es.transform_entities(tr, fa.vertices)
           model.commit_operation
          end
          ###
          UI.menu("Plugins").add_item("Scale_Face"){self.scale_face()} unless file_loaded?(__FILE__)
          file_loaded(__FILE__)
          ###
        end
        ###
        

        Copy/paste code into a script in the Plugins folder named 'TIG-scale_face.rb', and restart Sketchup. Run it from the menu item or by typing TIG.scale_face in the Ruby Console...
        EDIT: Typo fixed in menu code !!!
        EDIT: Improved Unit/Fractional/Engineering handling - see 1st post in the thread for the .rb version.

        TIG

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 7 Aug 2012, 15:14

          na = -na if na < 0
          Why not just use #abs ?
          na = na.abs

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

          1 Reply Last reply Reply Quote 0
          • T Online
            TIG Moderator
            last edited by 7 Aug 2012, 15:31

            @thomthom said:

            na = -na if na < 0
            Why not just use #abs ?
            na = na.abs

            No reason.
            I assume you meant na=results[0].abs as a one-step solution.
            I was just writing it on the fly and needed to trap for a negative or zero reply...

            Incidentally, I noted that you transformed the face while I did the face.vertices, is there any advantage in either way?
            Presumably transforming the face changes its vertices and what they are connected to anyway, and conversely transforming the vertices transforms the face and also whatever they are connected to also ?

            TIG

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 7 Aug 2012, 15:47

              @tig said:

              Incidentally, I noted that you transformed the face while I did the face.vertices, is there any advantage in either way?
              Presumably transforming the face changes its vertices and what they are connected to anyway, and conversely transforming the vertices transforms the face and also whatever they are connected to also ?

              I'd expect that under the hood, when you feed entities to #transform_entities it collects the vertices - so [face] and face.vertices should have the exact same effect. I kind of liked face.vertices instead of wrapping the face in an array - feel it reads better as the brackets isn't so easy to see immediately.

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

              1 Reply Last reply Reply Quote 0
              • T Online
                TIG Moderator
                last edited by 7 Aug 2012, 16:02

                Actually just face should work as well as [face] because the transformation is the first argument and you can then pass an array OR a series of arguments, so 'face' would be seen as the first one anyway ? ...

                TIG

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 7 Aug 2012, 16:17

                  API-doh!

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

                  1 Reply Last reply Reply Quote 0
                  • E Offline
                    EarthMover
                    last edited by 7 Aug 2012, 20:31

                    👍 You guys both rock! THANKS so much!

                    3D Artist at Clearstory 3D Imaging
                    Guide Tool at Winning With Sketchup
                    Content Creator at Skapeup

                    1 Reply Last reply Reply Quote 0
                    • J Offline
                      jorge2011
                      last edited by 8 Aug 2012, 02:20

                      You see this plugin?

                      http://rubits.com/play.php?file=areascale

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        thomthom
                        last edited by 8 Aug 2012, 07:47

                        @jorge2011 said:

                        You see this plugin?

                        http://rubits.com/play.php?file=areascale

                        That's nice looking. The VCB is a nice way to control it. But I think $10 is too much for such a trivial script. 😕

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

                        1 Reply Last reply Reply Quote 0
                        • E Offline
                          EarthMover
                          last edited by 8 Aug 2012, 13:18

                          TIG, having an issue with your script loading properly. I copied and pasted the code exactly into a notepad file and named it - TIG-scale_face.rb. Upon loading Sketchup, I get no errors, but none of my toolbars load. When I delete the script, all the toolbars load again.

                          3D Artist at Clearstory 3D Imaging
                          Guide Tool at Winning With Sketchup
                          Content Creator at Skapeup

                          1 Reply Last reply Reply Quote 0
                          • E Offline
                            EarthMover
                            last edited by 8 Aug 2012, 14:43

                            Thom, excuse my ignorance, but how would I going about testing your code?

                            3D Artist at Clearstory 3D Imaging
                            Guide Tool at Winning With Sketchup
                            Content Creator at Skapeup

                            1 Reply Last reply Reply Quote 0
                            • T Offline
                              thomthom
                              last edited by 8 Aug 2012, 15:15

                              @earthmover said:

                              Thom, excuse my ignorance, but how would I going about testing your code?

                              Sorry Adam, I typed it up and posted in a hurry. You'd put it in a .rb file and form the console invoke: TT_ScaleToArea.scale_face( Sketchup.active_model.selection[0], 4000 )

                              Didn't have the time to add UI.

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

                              1 Reply Last reply Reply Quote 0
                              • T Online
                                TIG Moderator
                                last edited by 8 Aug 2012, 15:53

                                The code is now tested and working version... I edited the original ! http://forums.sketchucation.com/viewtopic.php?p=419936#p419936
                                Stupid typo unless**.**file_loaded? instead of unless file_loaded?
                                I added the UI as an afterthought and never tested it sorry!

                                TIG

                                1 Reply Last reply Reply Quote 0
                                • E Offline
                                  EarthMover
                                  last edited by 9 Aug 2012, 14:11

                                  Thanks TIG. It works as long as I am using a decimal unit type. With fractional units, it deletes everything in the model. Not sure why, just have to remember to switch the units before using the plugin. Thanks again. I'll update the first post with the rb file.

                                  Also it is still stating inches in the dialog even when the units are feet. No big deal though. Works for what I need. Thanks again! 👍 👍


                                  Clipboard01.jpg

                                  3D Artist at Clearstory 3D Imaging
                                  Guide Tool at Winning With Sketchup
                                  Content Creator at Skapeup

                                  1 Reply Last reply Reply Quote 0
                                  • T Online
                                    TIG Moderator
                                    last edited by 9 Aug 2012, 14:21

                                    I'll adjust the code... I did type it on the fly...

                                    TIG

                                    1 Reply Last reply Reply Quote 0
                                    • E Offline
                                      EarthMover
                                      last edited by 9 Aug 2012, 15:18

                                      If you have time. If not, no worries. I just figured I'd mention the issues in case anyone else needed to use the plugin.

                                      3D Artist at Clearstory 3D Imaging
                                      Guide Tool at Winning With Sketchup
                                      Content Creator at Skapeup

                                      1 Reply Last reply Reply Quote 0
                                      • T Online
                                        TIG Moderator
                                        last edited by 9 Aug 2012, 15:26

                                        I've updated the .rb in your post and edited the post's text to match [hope you are OK with that 😉 ]
                                        It now handles all Unit-types/Fractional/Engineering etc properly.
                                        One oddity is that 'Engineering' displays on screen in 'feet', but the API unit-type [LengthUnit] is left in 'inches' [0] - unlike all other types where the 'LengthUnit' matches the displayed-unit.

                                        Anyway it now works for all combos... I hope 😕

                                        TIG

                                        1 Reply Last reply Reply Quote 0
                                        • E Offline
                                          EarthMover
                                          last edited by 9 Aug 2012, 16:12

                                          Works great now. Thanks again.

                                          3D Artist at Clearstory 3D Imaging
                                          Guide Tool at Winning With Sketchup
                                          Content Creator at Skapeup

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

                                          Advertisement