• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

I'm getting there - but need some help.

Scheduled Pinned Locked Moved Developers' Forum
14 Posts 2 Posters 1.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
    ezt
    last edited by 22 Mar 2008, 15:38

    I am not versed in Ruby, but I needed a script to create a cabinet based on dimensions entered in a GUI - so I have tried to figure out how to accommodate my needs by studying other scripts to see if I could figure out how they work.

    The cabinet needs to be placed as a 'group' named by a Cabinet Code entered in the GUI, and each part of the cabinet needs to be a 'sub-group' named with the Cabinet code prefixing the part name.

    For example the cabinet group may be called 'Cabinet1' and the panel parts are groups called 'Cabinet1 - SideL', 'Cabinet1 - Bottom','Cabinet1 - Top' etc.

    I have managed to creat the first script which is for drawing the cabinet.
    Each cabinet part is created as its own 'group'. (No rocket science for most of you, but I was well pleased that I got it to work).

    However I am stuck on the next 2 steps.

    As I need the group for each part to be called something different for every cabinet created I have added a prompt for the cabinet code to be entered in the 'Cabinet Code' box.

    • My first question is how do I specify the default value for this to show text (eg 'Code Req'd') instead of a dimension (in this case I have it at 0.mm )?

    My second question is how do I use this code to prefix the group.name (for example if the Cabinet Code is 'Cabinet1' the group name for the left side should be 'Cabinet1 - SideL').

    My final question is, how do I create a global 'Group' for the whole cabinet before it is placed using the 'Cabinet Code' such that each of the part groups are secondary level.

    Here is the ruby code I have used

    @unknownuser said:

    require 'sketchup.rb'
    def wallcab1

    prompts = ["Width ", "Height ", "Depth", "Side Thickness", "Top-Bottom Thickness", "Back thickness ", "Cabinet Code"]
    values = [500.mm , 720.mm , 300.mm , 18.mm , 18.mm , 18.mm , 0.mm ]

      results = inputbox prompts, values, "Cabinet Dimensions"
      return if not results
      xx, zz, yy, d1, z1, r1, n1 = results
    

    Make Left Side

    model = Sketchup.active_model
    model.start_operation "SideL"
    entities = model.active_entities
    group = entities.add_group
    entities = group.entities

        pts = []
    pts[0] = [0.mm, 0.mm, 0.mm]
    pts[1] = [d1, 0.mm, 0.mm]
    pts[2] = [d1, yy, 0.mm]
    pts[3] = [0.mm, yy, 0.mm]
    
    
    	face = entities.add_face pts
    

    status = face.pushpull -zz, true
    group.name = "SideL"

    Make Bottom Panel

    model = Sketchup.active_model
    model.start_operation "Bottom"
    entities = model.active_entities
    group = entities.add_group
    entities = group.entities

        pts = []
    pts[0] = [0.mm+d1, 0.mm, 0.mm]
    pts[1] = [xx-d1, 0.mm,  0.mm]
    pts[2] = [xx-d1, yy,  0.mm]
    pts[3] = [0.mm+d1, yy,  0.mm]
    
    
      face = entities.add_face pts
    

    status = face.pushpull -z1, true
    group.name = "Bottom"

    Make Top Panel

    model = Sketchup.active_model
    model.start_operation "Top"
    entities = model.active_entities
    group = entities.add_group
    entities = group.entities

        pts = []
    pts[0] = [0.mm+d1, 0.mm, 0.mm+zz]
    pts[1] = [0.mm+xx-d1, 0.mm, 0.mm+zz]
    pts[2] = [0.mm+xx-d1, yy, 0.mm+zz]
    pts[3] = [0.mm+d1, yy, 0.mm+zz]
    
    
    	face = entities.add_face pts
    

    status = face.pushpull -z1, true
    group.name = "Top"

    Make Right Side

    model = Sketchup.active_model
    model.start_operation "SideR"
    entities = model.active_entities
    group = entities.add_group
    entities = group.entities

        pts = []
    pts[0] = [xx-d1, 0.mm , 0.mm]
    pts[1] = [xx, 0.mm, 0.mm]
    pts[2] = [xx, yy, 0.mm]
    pts[3] = [xx-d1, yy, 0.mm]
    

    face = entities.add_face pts
    status = face.pushpull -zz, true
    group.name = "SideR"

    Make Back Panel

    model = Sketchup.active_model
    model.start_operation "Back"
    entities = model.active_entities
    group = entities.add_group
    entities = group.entities

        pts = []
    pts[0] = [0.mm, 0.mm+yy, 0.mm+zz]
    pts[1] = [0.mm+xx, 0.mm+yy, 0.mm+zz]
    pts[2] = [0.mm+xx, 0.mm+yy+r1, 0.mm+zz]
    pts[3] = [0.mm, 0.mm+yy+r1, 0.mm+zz]
    

    face = entities.add_face pts
    status = face.pushpull -zz, true
    group.name = "Back"

    end

    if( not file_loaded?("wallcab1.rb") )

    # This will add a separator to the menu, but only once
    #add_separator_to_menu("Plugins")
    # To add an item to a menu, you identify the menu, and then
    # provide a title to display and a block to execute.  In this case,
    # the block just calls the create_box function
    UI.menu("Plugins").add_item("Wall Cabinet1") { wallcab1 }
    

    end

    #-----------------------------------------------------------------------------
    file_loaded("wallcab1.rb")

    Any help would be greatly appreciated.

    1 Reply Last reply Reply Quote 0
    • T Offline
      todd burch
      last edited by 22 Mar 2008, 17:40

      To set a name default name, just do this:
      default_name = "Cabinet1"
      values = [500.mm , 720.mm , 300.mm , 18.mm , 18.mm , 18.mm , default_name]

      To make a longer name, take the returned value, which is n1, and append to it, like this:
      group.name = n1 + " - SideL" ;

      To create an empty group, code:
      new_empty_group = Sketchup.active_model.entities.add_group() ;

      To create a group with stuff in it, code:
      stuff = an_array_of_entities_andor_groups_andor_components_you_have_already_created ;
      new_populated_group = Sketchup.active_model.entities.add_group(stuff) ;

      Todd

      1 Reply Last reply Reply Quote 0
      • E Offline
        ezt
        last edited by 22 Mar 2008, 18:48

        Thanks very much Todd.

        The first part works a treat - excellent.

        I am not sure I fully understand what to put in the last bit about creating the Global group, but I'll experiment with that this evening.

        Thanks again.

        1 Reply Last reply Reply Quote 0
        • E Offline
          ezt
          last edited by 23 Mar 2008, 00:54

          @unknownuser said:

          To create a group with stuff in it, code:
          stuff = an_array_of_entities_andor_groups_andor_components_you_have_already_created ;
          new_populated_group = Sketchup.active_model.entities.add_group(stuff) ;

          I have searched around and can't find anything that really helps with this.

          I assume that 'stuff' is the name that will be applied to the new group, but don't understand what to add after the = sign to include the other groups I have created.

          Also, 'stuff' (the new group) needs to be called the same as the 'default_name' (eg Cabinet1) entered in the Gui.

          Any further guidance would be appreciated.

          1 Reply Last reply Reply Quote 0
          • T Offline
            todd burch
            last edited by 23 Mar 2008, 01:02

            OK.

            In your code, every time you need a new group, for the left side, right side, bottom, whatever, you use the same variable name... "group". Doing that, you lose "addressability" to any group you've already created.

            Therefore, change the names from "group" to "left_side_group" and "right_side_group" and "bottom_group", etc.

            Then, you when are ready to create your big overall group, code this:

            overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group )

            Todd

            1 Reply Last reply Reply Quote 0
            • T Offline
              todd burch
              last edited by 23 Mar 2008, 01:12

              A few other helpful pointers...

              Within your method, you only need to do this once:

              
              model = Sketchup.active_model
              
              

              Same with this:

              
              entities = model.active_entities
              
              

              It's bad form to redefine entities like you are doing.

              
              entities = model.active_entities
              group = entities.add_group
              entities = group.entities
              
              

              I would not even set the first one. And for as little use you are getting out of it, not even the second one.

              While you are creating your cabinet, you perform several "start_operations". You really only need one for the whole cabinet, and you should close it off at the end by an explicit "commit_operation".

              Todd

              1 Reply Last reply Reply Quote 0
              • E Offline
                ezt
                last edited by 23 Mar 2008, 14:53

                Thanks again Todd.

                You have saved me a lot of time trying to get this far.

                @unknownuser said:

                A few other helpful pointers...

                I quickly tried to modify the script to take these comments in to account, and I somehow ended up breaking the script. I'll have another look at this later when I have some time, but I have about 15 other scripts I have done, similar to the one I posted here, for other types of cabinets, and I need to press on an incorporate the changes for grouping in those first.

                One question though.

                In the global grouping you said to use

                @unknownuser said:

                overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group )

                This works well enough, but can you just clarify what name is given to this group?
                From what I can figure, it has no name. If that's the case, can I somehow give the overall group the same name as was input for the Cabinet Code?

                1 Reply Last reply Reply Quote 0
                • T Offline
                  todd burch
                  last edited by 23 Mar 2008, 15:41

                  Sure.

                  overall_group.name = n1

                  Todd

                  1 Reply Last reply Reply Quote 0
                  • E Offline
                    ezt
                    last edited by 23 Mar 2008, 16:24

                    It's so easy when you know how. πŸ˜‰

                    Thanks again Todd.

                    1 Reply Last reply Reply Quote 0
                    • E Offline
                      ezt
                      last edited by 26 Mar 2008, 16:27

                      @unknownuser said:

                      Then, you when are ready to create your big overall group, code this:

                      overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group )

                      This works for most of my scripts except:

                      Some of the scripts include items which may or may not be created depending on the input.
                      For example the script allows for there to be none, one, two or three drawers, and the overall_group call would be:

                      overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group, drawer1_group, drawer2_group, drawer3_group)

                      This works if both drawers groups are created from the input, but if the option for the second drawer is not to have one, drawer2_group is not created so the overall_group is not created either.

                      My thoughts are that I need to write a couple of statements to check if the group has been created - something like this:

                      if drawer_group3 exists then
                      overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group, drawer1_group, drawer2_group, drawer3_group)

                      else

                      if drawer_group2 exists then
                      overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group, drawer1_group, drawer2_group)

                      else

                      if drawer_group1 exists then
                      overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group, drawer1_group)

                      else

                      overall_group = Sketchup.active_model.entities.add_group( left_side_group, right_side_group, bottom_group, drawer1_group)

                      What I am not certain about is what the statements shown in red should be for checking if the group was previously created.

                      Can anyone assist.

                      Thanks.

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        todd burch
                        last edited by 26 Mar 2008, 17:56

                        Don't do that - too much redundant code.

                        Do something like this. As you create a new group, add it to the array of groups that will eventually be created...

                        all_groups = []

                        all_groups << left_side_group
                        ...
                        all_groups << right_side_group

                        etc...

                        Then, if you create 3 drawers, append them all to your array like
                        if drawer_group1 exists then all_groups << drawer_group1 end
                        and so on.

                        If you skip a drawer, no biggy.

                        Then, at the end,

                        overall_group = .....add_group(all_groups)

                        Todd

                        1 Reply Last reply Reply Quote 0
                        • E Offline
                          ezt
                          last edited by 26 Mar 2008, 23:59

                          Thanks again Todd.

                          I have the script working, but not quite the way you suggested.

                          I used your advice and created the all_groups array, and added the groups using 'all_groups<<'.

                          However I kept getting a script error if I tried to use the
                          'if drawer1_group exists then all_groups << drawer1_group end '

                          The error read:

                          @unknownuser said:

                          single_door_rt_base
                          Error: #<NameError: C:/Program Files/Google/Google SketchUp 6/Plugins/sdrtbase.rb:348:in single_door_rt_base': undefined local variable or method exists' for main:Object>
                          C:/Program Files/Google/Google SketchUp 6/Plugins/sdrtbase.rb:348

                          Line 348 is where the first 'if' line is.

                          As it happens, I already had some 'if' statements where the drawers were created, so I added the 'all_groups<<drawer1_group' before the end of the statement and it works fine.

                          However if you have any thoughts on the error I'd be interested to hear. (I wondered if there should be some other syntax like [ or ' or something)

                          Thanks again.

                          1 Reply Last reply Reply Quote 0
                          • T Offline
                            todd burch
                            last edited by 27 Mar 2008, 00:02

                            I was hoping you weren't going to type my "code" just as I typed, because it wasn't really "code", per se, as it was pseudo code. You're assignment is to work out the proper syntax. I was merely illustrating the concept. πŸ˜‰

                            Todd

                            1 Reply Last reply Reply Quote 0
                            • E Offline
                              ezt
                              last edited by 27 Mar 2008, 01:01

                              As I said in the heading of my topic - 'I'm getting there' πŸ˜‰

                              I also said - 'but need some help.' and you have certainly given me that.

                              My problem is that I have no experience of programming/scripting, and I am short of time in developing a major project (the scripts for SU are only a part of the project) and have limited time to 'study' something new, particularly something as involved as Ruby - but I will persevere as time allows.

                              Thanks again for you patience and assistance so far - it has helped no end.

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

                              Advertisement