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.
    • 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
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by


                    Also..

                    1. open the Model Info dialog.

                    2. Go to the File panel.

                    3. check the "Redefine thumbnail on save" option

                    I'm not here much anymore.

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

                      See API: ComponentDefinition.refresh_thumbnail

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

                      [/quote]

                      Thanks that worked perfect. πŸ˜„

                      Frank

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

                        @frankn said:

                        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?

                        ALL Sketchup::ComponentDefinition objects are kept in the model's Sketchup::DefinitionList collection. ALL of them, regardless of what level any instances ( Sketchup::ComponentInstance,) of them are inserted.
                        The "level" can at the top (under the model,) or nested many levels deep beneath any combination of Group / ComponentDefinition "parents".

                        The Entity.parent() method is inherited (down thru Sketchup:Drawingelement,) to all the objects that can be put into a model.

                        So create all the component definitions you will use, and load them into the model.
                        To nest a component into another, insert an instance of the subcomponent, into the entities collection of another component's definition.

                        outer_comp_def.entities.add_instance( inner_comp_def, transform )

                        .. repeat to suit (ie, you can insert multiple bolt & nut component instances, in another component definition.)

                        P.S. (a Group is a "special" component, and so it also has a Sketchup::ComponentDefinition)

                        I'm not here much anymore.

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

                          Hi Dan,

                          Thanks for the explanation, I think that's what I did without realizing with just trial and error... many, many errors! πŸ˜„

                          I thought you might like to see what I've been working on and by default bothering you with πŸ˜‰ so I included the script.

                          Keep in mind this is a work in progress and my first attemp at at sort of programming so go easy on me. 😳 and please feel free to comment on what I'm doing wrong or that I could improve.

                          Also the Drawers and Doors functions/inputs aren't implemented yet... still haven't figured that part out.

                          Thanks again,
                          Frank


                          Test.rb

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

                            I fixed a few things and realized I could call the instances I added to the global component by different names. πŸ˜„ 😳

                            It does run slow but I still haven't looked into optimising scripts so hopefully I can speed it up some though I don't mind it though, it's faster then creating a cabinet from scratch. πŸ˜„

                            Next on the to do/ to learn list is...

                            • Creating door and drawer fronts, creating drawers... shouldn't be a problem.
                            • Add a user defined number of shelves and insert them evenly spaced in the cabinet... I can do this manually/individually but the math involved in doing them automatically should be fun to figure out!
                            • Same thing for door and drawer fronts.
                            • And give a choice of creating an upper or lower cabinet since the building of these is different... I could use 2 different scripts like I did when moding the cab.rb script but I think it would be slick having it all-in-one.
                            • Add grain direction
                            • And eventually learning how to export the to a csv file so that I can populate an existing excel spreadsheet that I use... yes I know there are already some cut sheet/list scripts but this is more of a hobby/learning experience and I'd like to see what I can do. πŸ˜„

                            That should be it but I'm already happy with the progress I've made in just 3 or 4 days or learning this stuff.

                            Again thanks for all the help I really appreciate your teach how to fish approach. πŸ˜„

                            Frank


                            Test.rb

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

                            Advertisement