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

    Find and replace multiple components?

    Scheduled Pinned Locked Moved Newbie Forum
    sketchup
    30 Posts 9 Posters 12.1k Views 9 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      @tenquin

      Why do you need a plugin to swap doors?
      Years ago I explained how to do it with the native tools...

      Por que você precisa de um plugin para trocar as portas?
      Anos atrás, expliquei como fazê-lo com as ferramentas nativas...

      TIG

      1 Reply Last reply Reply Quote 0
      • A Offline
        antioche
        last edited by

        Hi TIG,
        I suspect Tenquin may be referring to my post just above, which would involve relinking identical dynamic components that have lost their original definition due to a redraw action within nested dynamic components (see my post). Your suggested approach in your original post would not work, as the redrawn DCs get renamed as unique (although they are not).
        Any suggestion on how to achieve this?

        Cheers,

        Teebs

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

          OK

          Let's consider this...
          The original component is named [let's say] "DC" - so its unique-ified siblings are named DC#1, DC#2 etc...
          So we can search the model's definitions for matching 'patterns' and replace their instances...
          The naming has to be logical, and all instances the same...

          
          model=Sketchup.active_model
          defns=model.definitions
          name="DC" # perhaps you'd use an inputbox to get this 'name' ?
          defn=defns[name]
          dcs=defns.find_all{|d| d.name =~ /^#{name}#/ }
          # note how 'name' starts the match-pattern and has a following #
          model.start_operation("Un-unique #{name}", true) # one-step undo
          puts "Un-unique #{name}"
          dcs.each{|d|
            p d.name # to see their names listed in the Ruby Console
            d.instances.each{|i| i.definition = defn }
            # replace with original
          }
          model.commit_operation
          puts "Done"
          
          

          This works within nested components etc...
          You could check that the geometry/size etc of the instance of the original defn matches the instance of the to-be-un-uniqued definition, but that requires more convoluted code...

          TIG

          1 Reply Last reply Reply Quote 0
          • A Offline
            antioche
            last edited by

            Many thanks for the quick feedback, TIG!
            Seems to make a lot of sense. Not able to test right now (deadline looming on another project) but will test and feedback.

            Teebs

            1 Reply Last reply Reply Quote 0
            • T Offline
              tenquin
              last edited by

              Olá TIG!
              Boa tarde!
              Obrigado pelo retorno.

              Desculpe se não soube me expressar corretamente, na verdade preciso que o plugin troque todos os componentes que tem um parâmetro específico, no caso o parâmetro "modeloptions=portas inferiores"

              Desde já agradeço.


              Esses seriam os parâmetros de exemplo


              Essa seria o componente a ser trocado, sendo que teria que manter as dimensões lenx, leny, lenz e posições x,y,z....


              Essas seriam as portas de troca e estariam em uma biblioteca...

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

                @tenquin

                Yes, that's nothing like it !

                You could try editing the code...

                
                ###....
                dcs.each{|d|
                  next unless d.get_attribute("dynamic_attributes", "modeloptions", "") == "portas inferiores"
                  # this skips if it's not that type of dc ???
                  p d.name # to see their names listed in the Ruby Console
                  d.instances.each{|i| i.definition = defn }
                  # replace with original
                }
                ###...
                
                

                TIG

                1 Reply Last reply Reply Quote 0
                • T Offline
                  tenquin
                  last edited by

                  Obrigado mais uma vez TIG!

                  Vou estudar mais um pouco e tentar fazer,
                  sou novo em programação mas vou tentar até conseguir. rsrs

                  👍

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    tenquin
                    last edited by

                    @tig said:

                    OK

                    Let's consider this...
                    The original component is named [let's say] "DC" - so its unique-ified siblings are named DC#1, DC#2 etc...
                    So we can search the model's definitions for matching 'patterns' and replace their instances...
                    The naming has to be logical, and all instances the same...

                    
                    > model=Sketchup.active_model
                    > defns=model.definitions
                    > name="DC" # perhaps you'd use an inputbox to get this 'name' ?
                    > defn=defns[name]
                    > dcs=defns.find_all{|d| d.name =~ /^#{name}#/ }
                    > # note how 'name' starts the match-pattern and has a following #
                    > model.start_operation("Un-unique #{name}", true) # one-step undo
                    > puts "Un-unique #{name}"
                    > dcs.each{|d|
                    >   p d.name # to see their names listed in the Ruby Console
                    >   d.instances.each{|i| i.definition = defn }
                    >   # replace with original
                    > }
                    > model.commit_operation
                    > puts "Done"
                    > 
                    

                    This works within nested components etc...
                    You could check that the geometry/size etc of the instance of the original defn matches the instance of the to-be-un-uniqued definition, but that requires more convoluted code...

                    Olá TIG

                    Desculpe minha insistência.
                    Há alguns meses conversamos sobre o código acima, que faz um script que substitui componentes.
                    Ainda não consegui fazer com que funcionasse, criei um componente com nome DC e copiei e colei o código no console Sketchup,mas não obtive resultado.
                    Estou me dedicando a Ruby, mas mesmo depois de ler o livro Automatic Skechup, não consegui identificar o que está errado...

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

                      What results do you get ?
                      The Ruby Console will show errors and output.

                      You are looking for a component definition name, NOT a component instance name.
                      They are different things.
                      The component's name is displayed in the Components Browser.
                      Instances can have different names from their definition - this shown in Entity info - if it's selected...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        tenquin
                        last edited by

                        @TIG

                        Obrigado mais uma vez por responder.
                        Não retorna nenhum erro, porém nenhum resultado também. Segue uma imagem.

                        Script.JPG

                        Minha intenção é buscar os componentes por atributos dinâmicos ao invés de por nome, se poder me dá uma luz de como ficaria o código assim eu agradeço, se não, depois tentarei editar o código para se adequar ao que desejo.

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

                          What else do you expect ?
                          You have 1 component definition named 'DC'.
                          If you had more using that name pattern - e.g. 'DC#1', DC#2' etc.
                          Then DC would prevail !

                          The code is working just fine - but your model has got nothing in it to be processed !

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • T Offline
                            tenquin
                            last edited by

                            Perdão TIG.
                            Você tem razão, eu não estava sabendo usar o código corretamente.
                            Agora me parece que está funcionando.
                            Obrigado

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

                              @tig said:

                              IF you name your components logically then a script in the form swapcomponents("-aaa","-bbb") is straightforward - and quite simple - I can talk you through it... Once you have something working vis the Ruby Console it#s easy enough to add a menu item and dialog, even a toolbar and so on...

                              First off - make a logical component-naming strategy/
                              Then write is a simple step-sheet explaining how you'd like to use the new tool... e.g.

                              I select various cabinets where I want to swap one type of door [and drawer] for another.
                              I activate the new tool tell it what code to find and what code to replace it with... and it automatically finds equivalent doors [and drawers] in the model [or perhaps SKPs in folder[s]] in ../Components/.
                              It then uses/loads them and replaces the current component instance with the required definition.
                              A dialog reports what's been changed and/or any errors.
                              It is one step undo-able.
                              For example always name doors 'Door-WWW-HHH-FINISH-CODE' and then all we have to do is swap components with the same base but a new FINISH and/or CODE - In the input you could type Door*-Oak-A and in the output type Door*-Maple-A and all doors code A would change from 'Oak' to 'Maple'; or Door*-A >>> Door*-B would swap from code A to code B - if there were no Oak versions of code B doors then the closing dialog tells you!
                              On a more global system *-A >>> *-B tries to swap all type A 'fitments' to type B.
                              If things are 'nested' - components inside groups etc then it's more tricky but not impossible...
                              You could also swap ironmongery from Handle-*-A >>> Handle-*-B.
                              See the schema ?

                              TIG can I ask you to post an example of this code here? Thanks.

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

                                You quoted my example of a step sheet...

                                Please outline exactly what you want to do - what to find by a pattern-match and how to rename them etc...

                                This thread have been around for a decade !
                                So what you want to do needs to be specified very precisely...

                                TIG

                                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