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

    Selective component explode

    Scheduled Pinned Locked Moved Developers' Forum
    22 Posts 6 Posters 4.3k Views 6 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.
    • G Offline
      glro
      last edited by glro

      i get partial succes, or it takes a very long time for the computer to process this code:

      <span class="syntaxdefault">model&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model&nbsp;</span><span class="syntaxcomment">#&nbsp;Open&nbsp;model<br /></span><span class="syntaxdefault">entities&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities&nbsp;</span><span class="syntaxcomment">#&nbsp;All&nbsp;entities&nbsp;in&nbsp;model<br /></span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a<br /></span><span class="syntaxkeyword">for&nbsp;</span><span class="syntaxdefault">e&nbsp;in&nbsp;entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">if&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance&nbsp;</span><span class="syntaxkeyword">and&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">==</span><span class="syntaxstring">"KLC2-"<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">explode<br />&nbsp;&nbsp;&nbsp;&nbsp;end<br />end</span>
      

      the model contains 43 component definitions and 976 instances.

      Do you find it normal?

      Is there a another way to write the code to speed the process?

      1 Reply Last reply Reply Quote 0
      • G Offline
        glro
        last edited by

        @chris fullmer said:

        Well, what do you mean by "a very long time"? Is it a second, or is it 2 hours?

        about 3 or 4 minuts
        i tried your solution, and it is also about 3 or 4 minuts
        i'll be patient...

        thank you for the idea

        1 Reply Last reply Reply Quote 0
        • Chris FullmerC Offline
          Chris Fullmer
          last edited by

          Wow, that seems long. Can you uploade the model to play around with? Are the components very complex inside (their inner geometry?).

          Maybe you make an array of components that you want to explode and then explode them all at once?

          Ahh, that gives me an idea. Wrap the code inside of a

          model.start_operation("Selective Exploder", true) ---code to explde goes here--- model.commit_operation

          Putting it inside the start/commit operation will make SketchUp evaluate it as a single undo. And make sure to use the True parameter in the start_operation method, which will force the UI to not refresh, and in theory could totally speed it up.

          http://www.sketchup.com/intl/en/developer/docs/ourdoc/model#start_operation

          Try that and let us know how it goes,

          Chris

          Lately you've been tan, suspicious for the winter.
          All my Plugins I've written

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

            I'd approach it differently...

            n='KLC2-';m=Sketchup.active_model;m.start_operation('x',true);m.definitions[n].instances.each{|i|i.explode};m.commit_operation;
            

            This one-liner, copy/pasted+<enter> in the Ruby Console explodes all instances of the component definition that is named 'n', it's one step undo-able... It will explode locked instances too - to avoid that use i.explode unless i.locked? instead... If there is no matching definition named 'n' you get an error message... It ignores all other definitions and instances so will be as quick as it can.
            When exploding a container the length of time it takes depends on the number of objects in it AND in its context, since Sketchup has to decide about intersections etc with all of them mixed together...

            TIG

            1 Reply Last reply Reply Quote 0
            • Chris FullmerC Offline
              Chris Fullmer
              last edited by

              Well, what do you mean by "a very long time"? Is it a second, or is it 2 hours?

              There is a faster way though. You do not need to iterate over every entity in the model, you are only interested in the components and groups. So for that, there is:

              <span class="syntaxdefault">model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />defs </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions<br />for e in defs<br />for f in e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instances<br />puts f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name<br />if f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance and f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">==</span><span class="syntaxstring">"Component#4"<br /></span><span class="syntaxdefault">f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">explode<br />end<br />end<br />end</span>
              

              I'm not sure that actually works perfectly, so you'll need to verify that it does. But that is the general idea.

              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written

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

                How much geometry are we talking about? Explode is very slow. Adding geometry in SketchUp slows down as more geometry is added to the context you add to. Every time SketchUp performs intersections and splits edges and faces etc.

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

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

                  @glro said:

                  e.definition.name=="**KLC2-**"

                  Is " **KLC2-**" a prefix that appears at the beginning of several definition names ??

                  If so your logical test needs to use a regular expression, thus:

                  if e.definition.name =~ /\A(KLC2\-)/

                  ... which returns nil if there is no match, (and evals to false,) ... or if there is a match, it returns the zero based index of the match (which evals as true.)

                  The pattern \A matches the at the beginning of the string only, so in this case the only valid index position that can be returned (for a match,) is 0.

                  💭

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • G Offline
                    glro
                    last edited by

                    @chris fullmer said:

                    Try that and let us know how it goes,
                    Chris

                    Sorry for the delay

                    here is the model

                    the component to explode is named KLC1-

                    it contains KLC1 (that's why i need to explode it)

                    i tried the code you suggested

                    it doesn't help on my computer (it freezes the computer)

                    exploding components seems to be a dangerous pratice in sketchup


                    selective component explosion

                    1 Reply Last reply Reply Quote 0
                    • G Offline
                      glro
                      last edited by

                      @tig said:

                      I'd approach it differently...

                      n='KLC2-';m=Sketchup.active_model;m.start_operation('x',true);m.definitions[n].instances.each{|i|i.explode};m.commit_operation;
                      

                      This one-liner, copy/pasted+<enter> in the Ruby Console explodes all instances of the component definition that is named 'n', it's one step undo-able... It will explode locked instances too - to avoid that use i.explode unless i.locked? instead... If there is no matching definition named 'n' you get an error message... It ignores all other definitions and instances so will be as quick as it can.

                      When exploding a container the length of time it takes depends on the number of objects in it AND in its context, since Sketchup has to decide about intersections etc with all of them mixed together...

                      i copy/pasted the code.

                      It seemed to give a result, lines were scrolling in the ruby window
                      but it never ended, i had to force sketchup to stop

                      maybe i got something wrong somewhere...

                      thank you anyway for trying

                      1 Reply Last reply Reply Quote 0
                      • G Offline
                        glro
                        last edited by

                        @dan rathbun said:

                        @glro said:

                        e.definition.name=="**KLC2-**"

                        Is " **KLC2-**" a prefix that appears at the beginning of several definition names ??

                        If so your logical test needs to use a regular expression, thus:

                        if e.definition.name =~ /\A(KLC2\-)/

                        ... which returns nil if there is no match, (and evals to false,) ... or if there is a match, it returns the zero based index of the match (which evals as true.)

                        The pattern \A matches the at the beginning of the string only, so in this case the only valid index position that can be returned (for a match,) is 0.

                        💭

                        the component name in the example is KLC1-, it is not a prefix; just to make the difference with KLC1; the component embedded in it

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

                          Make a small SKP with a few components in it - one named 'KLC2-'.
                          Then run my one-liner [with any typos corrected - original is now fixed!].
                          It should not go into a loop...
                          Report Console errors, if any...

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • G Offline
                            glro
                            last edited by

                            this is what i typed in the ruby console

                            n='KLC1-';m=Sketchup.active_model;m.start_operation('x',true);m.definitions[n].instances.each{|i|i.explode};m.commit_operation;
                            

                            it does make a loop...

                            this a shot of the ruby console, but it never ended, i had to force sketchup out


                            loop

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

                              What does m.definitions[n] return assuming you define 'n' and 'm' as partial code beforehand...

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • Chris FullmerC Offline
                                Chris Fullmer
                                last edited by

                                The model you provided, is that the whole model that you are working with or just a portion? The first code I posted completed in 2 seconds on that model, so I'm hoping that is only a small portion of a much larger model?

                                Lately you've been tan, suspicious for the winter.
                                All my Plugins I've written

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

                                  Works fine for me too, and explodes all 122 instances in a few seconds, using my code !
                                  😕

                                  TIG

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

                                    Geo - make certain the Outliner window is closed, and try again. Then disable that SketchStruct plugin and try again.

                                    Hi

                                    1 Reply Last reply Reply Quote 0
                                    • G Offline
                                      glro
                                      last edited by

                                      @chris fullmer said:

                                      The model you provided, is that the whole model that you are working with or just a portion? The first code I posted completed in 2 seconds on that model, so I'm hoping that is only a small portion of a much larger model?

                                      no, it is the whole model

                                      the problem must be with my computer

                                      i shall try to find out

                                      1 Reply Last reply Reply Quote 0
                                      • Chris FullmerC Offline
                                        Chris Fullmer
                                        last edited by

                                        Ahh, that's good to know! Definitely try disabling other plugins like Jim mentions.

                                        Lately you've been tan, suspicious for the winter.
                                        All my Plugins I've written

                                        1 Reply Last reply Reply Quote 0
                                        • G Offline
                                          glro
                                          last edited by

                                          @jim said:

                                          Geo - make certain the Outliner window is closed, and try again. Then disable that SketchStruct plugin and try again.

                                          you solved my problem !

                                          I deleted sketchtruct and both TIG and Chris did the job in a few seconds...

                                          How did you guess it was coming from sketchtruct?

                                          This shows how much your "plugins quarantine" idea may help to solve plugin problems...

                                          Thank you

                                          1 Reply Last reply Reply Quote 0
                                          • Chris FullmerC Offline
                                            Chris Fullmer
                                            last edited by

                                            Great job Jim!

                                            Lately you've been tan, suspicious for the winter.
                                            All my Plugins I've written

                                            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