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

    Creating Components in Cab.rb

    Scheduled Pinned Locked Moved Developers' Forum
    26 Posts 5 Posters 1.3k Views 5 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.
    • M Offline
      mac1
      last edited by

      While you are waiting for plugin update consider using http://forums.sketchucation.com/viewtopic.php?f=323&t=14975&hilit=selection+toys%E2%80%A6. This allows for much quicker selection.

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

        In SketchUp, drawing elements are added to an Entities collection. The Model has an Entities collection, and also Groups and ComponentDefinitions.

        So if before each line that has entities.add_[something], if you create a new ComponentDefinition and add the drawing elements to that Entities collection.
        An example:

        ls=[] #left panel ls[0] = [0, depth, kick] ls[1] = [0, depth, height-thick] ls[2] = [0, thick, height-thick] ls[3] = [0, thick, kick] new_definition=Sketchup.active_model.definitions.add("Left Panel") cdef_entities = new_definition.entities ls_base = cdef_entities.add_face(ls) ls_base.pushpull(thick)

        Hi

        1 Reply Last reply Reply Quote 0
        • B Offline
          Brendon5374
          last edited by

          Thanks Jim,

          but I dropped that into the script, and when I ran the plugin it came back with nothing. Nothing appeared. I tried doing just the example you gave - that didn't work - and then each new element the extra lines. I made sure each one was different. ie:

          ls=[] #left panel
          ls[0] = [0, depth, kick]
          ls[1] = [0, depth, height-thick]
          ls[2] = [0, thick, height-thick]
          ls[3] = [0, thick, kick]
          new_definition=Sketchup.active_model.definitions.add("Left Panel")
          cdef_entities = new_definition.entities
          ls_base = cdef_entities.add_face(ls)
          ls_base.pushpull(thick)

          and..

          btm=[] #bottom shelf
          btm[0] = [thick, thick, kick+thick]
          btm[1] = [thick, depth, kick+thick]
          btm[2] = [thick, depth, kick+0]
          btm[3] = [thick, thick, kick+0]
          new_definition=Sketchup.active_model.definitions.add("Bottom Shelf")
          cdef_entities = new_definition.entities
          btm_base = cdef_entities.add_face(btm)
          btm_base.pushpull(width-thick-thick)

          Didn't work.

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

            Sorry - it worked, but I seem to have left out the part where you insert the ComponentInstance into the model.

            # Not the cdef_entities... but the model.entities or model.active_entities. entities.add_instance(new_definition, transformation)

            Hi

            1 Reply Last reply Reply Quote 0
            • B Offline
              Brendon5374
              last edited by

              Jim, I know you must be right. But still, I can't get it to work my end.

              Here is the file. If I cut and paste even just your first lines to replace the first piece, nothing comes up when I run the plugin. I put the old lines back, and it works.


              Cabinet

              1 Reply Last reply Reply Quote 0
              • B Offline
                Brendon5374
                last edited by

                Well, in light of not getting an answer I can use (at least with my knowledge base), I have figured out an inelegant solution for my problem. I have moved each piece .01mm from each other. The side panel is .01mm away from the bottom shelf and .01mm away from the table top, and the back panel...etc. So now I can point the cursor at each piece without the whole assembly being selected, select all, and make a component.

                Slow, but by making up a plugin, at least all the lines are already drawn.

                I did have a look at the suggestions, but they just wouldn't work for me, no matter where I put them in the script. And I did try. I tried to look for references and examples, but it was a labynrith on google, and quite often the only link was back here in this thread. LOL I thought it would be easier, since it is a basic action. But this experience for me here has been like asking what the motto says on a school blazer, only to be told to learn the latin language and find out that way.

                1 Reply Last reply Reply Quote 0
                • F Offline
                  Frankn
                  last edited by

                  Hi,

                  Sorry for digging up an old post but this is exactly the file I'm trying to modify to add components to. As per Dan Rathbun's advice I've read, searched tried to emulate other scripts and I just can't get it to work.

                  I just don't graspe the inner workings of how to create components and the more I search the more I get confused because there seems to be a number of different ways to do this and obviously none of which my ruby newbie brain is understanding.

                  Any help would be greatly appreciated,
                  Frank

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

                    Frank,

                    First, add a new ComponentDefinition:

                    model = Sketchup.active_model
                    definitions = model.definitions
                    new_component_definition = definitions.add("Definition Name")
                    

                    Then add geometry to the Definition's Entities:

                    new_component_entities = new_component_definition.entities
                    new_component_entities.add_line([0, 0, 0], [10, 10, 10])
                    

                    Finally, add an Instance of the Definition to the model (or current context)

                    model.active_entities.add_instance(new_component_definition, Transformation.new)
                    
                    

                    Hi

                    1 Reply Last reply Reply Quote 0
                    • F Offline
                      Frankn
                      last edited by

                      Thank you so much Jim for taking the time to answer such a seemingly simple thing to do. πŸ˜„

                      I still get confused with this part...

                      entities = new_component_definition.entities
                      entities.add_line([0, 0, 0], [10, 10, 10])

                      Is the second line equivelent to the following?

                      ls=[] #left panel
                      ls[0] = [0, depth, kick]
                      ls[1] = [0, depth, height-thick]
                      ls[2] = [0, thick, height-thick]
                      ls[3] = [0, thick, kick]
                      ls_base = entities.add_face ls
                      ls_base.pushpull thick

                      It's starting to make more sense. πŸ˜„

                      Out of curiosity and in the name of learning how to do things why is this code different then what you gave before, which is what I've been trying to make work?

                      Frank

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

                        For Brenden (and Frank, and whomever...)

                        Get the Cutlister plugin from Dana Woodman (ishboo)
                        ver 1.2: http://www.extendsketchup.com/plugins/cutlister/

                        Note: The one here on SCF is ver 1.1

                        I'm not here much anymore.

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

                          @frankn said:

                          I still get confused with this part...

                          entities = new_component_definition.entities
                          > entities.add_line([0, 0, 0], [10, 10, 10])
                          

                          Yes it can be and I (personally,) try not to use method names as reference (ie, variable,) identifiers (although Ruby allows it,) it is very confusing for newbies. I would use a identifier that you would KNOW is a reference, and write the example like:

                          ents_ref = new_component_definition.entities
                          ents_ref.add_line([0, 0, 0], [10, 10, 10])
                          

                          @frankn said:

                          Is the second line equivelent to the following?

                          ls=[] #left panel
                          > ls[0] = [0, depth, kick]
                          > ls[1] = [0, depth, height-thick]
                          > ls[2] = [0, thick, height-thick]
                          > ls[3] = [0, thick, kick]
                          > ls_base = entities.add_face ls
                          > ls_base.pushpull thick
                          

                          NO. The former produces a single Skecthup::Edge object (adding it to the model, into the current editing context: the Model entities, a Group or Component entities collection, or a sub-Group of a Group... etc.)
                          The latter, adds 12 Edges and 6 Faces.

                          @frankn said:

                          It's starting to make more sense. πŸ˜„

                          Well don't feel bad.. because Components are in the second semester course. 🀣

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • F Offline
                            Frankn
                            last edited by

                            Thanks for the link Dan, I'll give it a try and see how it compares to CutList 4.1

                            So is it better to use... entities.add_line([0, 0, 0], [10, 10, 10]) ...which is something new I'll have to learn πŸ‘Š then what this script is using?

                            @unknownuser said:

                            Well don't feel bad.. because Components are in the second semester course.

                            So what you're saying is I'm trying to run before I can walk... πŸ˜†

                            If there's a simple tutorial on components somewhere please point me to it, I'd really like to understand this aspect of scripting because it seems like something I'd use a lot of.

                            Well back to the books/web for me... wish me luck I need it so far what you and Jim have given me causes Sketchup to crash πŸ‘Š 🀣 and I know it isn't the code you've given me but I think it's where I'm placing it into the script...

                            Serenity now!!!

                            Thanks,
                            Frank

                            1 Reply Last reply Reply Quote 0
                            • F Offline
                              Frankn
                              last edited by

                              The more I read about how to create entities (I think I'm using the correct term) the more I realize that this script (cab.rb) probably isn't the best one to learn to script with.

                              Using entries like entities.add_line([0, 0, 0], [10, 10, 10]) seem to be much more useful, also more powerful and maybe even simpler to use when you get how they work.

                              Even if this script does hwta I want it to do, seems like building off of it to get the end result I want is more work then making something from scratch...

                              Back to the reading!

                              p.s. Mods if this is taking this thread off topic let me know and I'll stop posting here. πŸ˜„

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

                                @dan rathbun said:

                                Yes it can be and I (personally,) try not to use method names as reference (ie, variable,) identifiers (although Ruby allows it,) it is very confusing for newbies. I would use a identifier that you would KNOW is a reference, and write the example like:

                                Good point, I've modified my example to avoid the use of a method name as a reference.

                                Hi

                                1 Reply Last reply Reply Quote 0
                                • F Offline
                                  Frankn
                                  last edited by

                                  Hi just wanted to thank you both for taking the time to help me learn something new. I was able to get it to work but I couldn't get this line to work...

                                  model.active_entities.add_instance(new_def_component, Transformation.new) I keep getting this error not sure why.
                                  Error: #<NameError: uninitialized constant Transformation>

                                  But I am able to get it to work with this and it does what I need so far. πŸ˜„
                                  inst = ent.add_instance def_shelf, [0, 0, 0]

                                  I do have a question. I know how to create multiple instances of the same component and even make them unique if I need to... but how can I make the number of instances needed a variable?

                                  Example... user is prompted for the number of boxes he wants to add, he enters 3, the scripts creates 3 boxes in different locations.

                                  Frank

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

                                    @frankn said:

                                    ... but I couldn't get this line to work...

                                    model.active_entities.add_instance(new_def_component, Transformation.new) I keep getting this error not sure why.
                                    Error: #<NameError: uninitialized constant Transformation>

                                    Because the Transformation class is wrapped in the Geom module, it is qualified as Geom::Transformation, and you cannot refer to it as simply Transformation unless your code is executing within module Geom (which is a no-no.)

                                    So.. from another namespace (module,) you must qualify the classes, so the interpreter can find them. It's like id'g me as:
                                    Earth::UnitedStates::Florida::Brevard::Dan

                                    @frankn said:

                                    But I am able to get it to work with this and it does what I need so far. πŸ˜„
                                    inst = ent.add_instance def_shelf, [0, 0, 0]

                                    Because the 2nd arg to add_instance() takes a Geom::Point3d object, and the API extended the base Array class to be Comparable with both the API Geom::Point3d and Geom::Vector3d classes.

                                    I'm not here much anymore.

                                    1 Reply Last reply Reply Quote 0
                                    • F Offline
                                      Frankn
                                      last edited by

                                      Again thank you for the explaination Dan... it almost makes sense to me! πŸ˜„

                                      Something strange is happening with the way the components are being displayed in the Components window in Sketchup. They're all there but there is no preview picture to the components. But if I create a component manually it shows up... an idea why?

                                      I've also been looking into adding components to other components and all I found so far is some refernences to subcomponents and parent components but nowhere did I find how those work?

                                      This has been a greta learning experience and I find half the battle is actually using the correct search terms but I'm getting there!!

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

                                        @frankn said:

                                        Something strange is happening with the way the components are being displayed in the Components window in Sketchup. They're all there but there is no preview picture to the components. But if I create a component manually it shows up... an idea why?

                                        Try refreshing the thumbnails, thru the thumbnail menu (the left hand button with the drop-down.)

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • F Offline
                                          Frankn
                                          last edited by

                                          I had already tried that and it doesn't work, the only way to get them to show up is to edit the component then the preview shows up. Not a big deal but was just curious if I was doing something wrong with the script/code.

                                          Thanks again sir,
                                          Frank

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

                                            @frankn said:

                                            I had already tried that and it doesn't work, the only way to get them to show up is to edit the component then the preview shows up. Not a big deal but was just curious if I was doing something wrong with the script/code.

                                            Oh are these YOUR components ?? (ie, NOT Google supplied components,) that your having problems with?

                                            See API: ComponentDefinition.refresh_thumbnail

                                            cdefs = Sketchup.active_model.definitions
                                             cdefs.each {|comp| comp.refresh_thumbnail }
                                            

                                            I'm not here much anymore.

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

                                            Advertisement