sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Problem adding instance of child component

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 173 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.
    • K Offline
      ktkoh
      last edited by

      My script is intended to add a instance of a child component outside of the parent component. The script starts as a context selection when a child component is selected. The user needs to open the parent for editing to select the component.

      UI.add_context_menu_handler do |popup|
      
            sel = Sketchup.active_model.selection
            unless sel.empty?
              if sel.single_object?
                obj = sel[0]
                if obj.is_a?(Sketchup;;ComponentInstance) &&
                obj.parent.is_a?(Sketchup;;ComponentDefinition)
                  popup.add_item(NC2SCENE) {
                    #
                    nested_component_command(obj)
                    #
                  }
                end # component test
              end # single_object?
            end # unless empty?
            
          end # add_context_menu_handler
      

      and I have not found a method to close the parent component and then when I add an instance of the child it is added inside the parent which is not what I need. The instance needs to be added to the model outside of the parent component.

      Thanks
      Keith

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

        Assuming 'obj' is an instance... using
        obj.parent
        will return the actual 'container' of 'obj' - which might be the model or a group/component-definition].
        Use
        definition=obj.definition model.entities.add_instance(definition, transformation)
        to add an instance into the model itself using a 'transformation' - which you might get from the 'obj' and its 'container' instance...

        TIG

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

          @ktkoh said:

          ... and I have not found a method to close the parent component ...

          use: Model # close_active()

          as in:
          Sketchup.active_model.close_active()

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • K Offline
            ktkoh
            last edited by

            I used the Sketchup.active_model.close_active() and it worked as advertised.

            Thanks (I wish sometimes I could find these on my own)
            Keith

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

              @ktkoh said:

              (I wish sometimes I could find these on my own)

              The API Method Index really helps find things.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @ktkoh said:

                I used the Sketchup.active_model.close_active() and it worked as advertised.

                Note that the method is bugged and doesn't add the operation to the undo-stack - so if the user undo operations you might see some unexpected results. No workarounds atm.

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

                1 Reply Last reply Reply Quote 0
                • K Offline
                  ktkoh
                  last edited by

                  Ok I tried the other method

                  c2=@model.entities.add_instance(ent_def,insert_tran)
                  

                  when I first looked at the suggestion I thought that was how I added the instance in my origional code but closer inspection I noted that I was using

                  c2=@model.active_entities.add_instance(ent_def,insert_tran)
                  

                  Once I replaced active_entities with just entities the code worked also.

                  Thanks
                  Keith

                  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