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

    [code] ComponentDefinition-delete (another version!)

    Scheduled Pinned Locked Moved Developers' Forum
    22 Posts 5 Posters 4.0k 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.
    • A Offline
      AlexMozg
      last edited by

      You can use a not method (ComponentDefinition.delete), but idea:

      definition.entities.erase_entities definition.entities.to_a
      

      between
      model.start_operation ("")
      and
      model.commit_operation

      Sometimes so simpler...

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

        @alexmozg said:

        You can use a not method (ComponentDefinition.delete), but idea:

        definition.entities.erase_entities definition.entities.to_a
        

        between
        model.start_operation ("")
        and
        model.commit_operation

        Sometimes so simpler...

        So that's the core of it? The commit operation initiates SU to purge the empty definition? I couldn't really work out how your code worked. So I suppose that something in my code interferes with this?

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

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

          @thomthom said:

          Another thought: Not too sure if it's good that there's two different methods available with the same name. What if one plugin implements one method and another plugin implements the other. Since the method integrates with the SU native class there's no way to avoid conflict.

          Can we decide on one?

          I'll remove mine from the forum now...
          As AlexM points out using the:

          model.start_operation("Delete Definition")
            definition.entities.erase_entities(definition.entities.to_a)
          model.commit_operation
          

          it has been discovered how to do what we want anyway without a new method at all...

          TIG

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

            @tig said:

            it has been discovered how to do what we want anyway without a new method at all...

            Sorry to bring back a very old post/topic but I'm trying to delete definitions and I tried using the code AlexMozg wrote but I'm having problems with it and to be honest I don't really understand how the code works so I'm not sure what's going wrong.

            The error I get is... Error: #<TypeError: reference to deleted ComponentDefinition>

            Is there another way to do this if not what should I be looking for in regards to the problem I'm having?

            Thanks.

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

              If you want to delete ALL definitions in a model [use with care!]
              A one-liner like:

              m=Sketchup.active_model;m.start_operation('!');m.definitions.each{|d|d.entities.clear!};m.commit_operation
              

              does it.

              BUT if you want to delete just the definitions of Selected component-instances [and of course the instances will vanish too] use:

              m=Sketchup.active_model;m.start_operation('!');ds=[];m.selection.grep(Sketchup;;ComponentInstance).each{|i|ds<<i.definition};ds.uniq.each{|d|d.entities.clear!};m.commit_operation
              

              this way you make an array of all affected definitions and process those just once each - otherwise, if you have two instances selected of the same definition, then the second instance processed has an invalid 'deleted definition' so it will fail etc...

              TIG

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

                Thanks TIG,

                The second code is what I'm looking to do but I'm getting this error...
                Error: #<NoMethodError: undefined method `confirm_operation' for #Sketchup::Model:0x8e9a4b4>

                Also I tried selecting the definition I want to delete with this but it doesn't seem to work

                @model.selection.add(#<Sketchup;;ComponentDefinition;0xbcd77a0>)
                

                I forgot to mention that if I manually select the instance it does delete it and the parent from the components viewer but leaves the subcomponents.

                I'm still trying to figure out what your code does, this is a little over my pay grade! πŸ˜•

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

                  Ok so after a beer break I figured out why I couldn't select the instance... I was trying to select the definition! 😳

                  When I comment out m.confirm_operation everything seems to work but obviously there's something wrong with the code or I wouldn't need to comment it out.

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

                    Here's something interesting/weird that's happening...

                    After I delete the definition I iterate through model.definitions just to make sure everything was getting deleted but I found that the parent instance isn't getting deleted and the names of the subcomponents aren't getting deleted either... which causes the new component being created to be renamed with #1 at the end...

                    Any idea why?

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

                      It's ' commit_operation' NOT ' confirm_operation' - my typo 😳 - it's now corrected in my earlier code...
                      You can manually select objects including instances, and just the instances' definitions will be processed.
                      You can only add 'entities' to a Selection [like geometry or instances] in code.
                      NOT a 'definition' - which is NOT an 'entity'... πŸ˜•

                      TIG

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

                        @tig said:

                        It's ' commit_operation' NOT ' confirm_operation' - my typo 😳 - it's now corrected in my earlier code...
                        You can manually select objects including instances, and just the instances' definitions will be processed.
                        You can only add 'entities' to a Selection [like geometry or instances] in code.
                        NOT a 'definition' - which is NOT an 'entity'... πŸ˜•

                        Thanks you sir! That works perfectly! I should of caught that but I just figured it was a command I didn't know of yet! 😳 πŸ˜„

                        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