Find and replace multiple components?

The ideal place for beginners to get help using SketchUp

Find and replace multiple components?

Postby nickchun » Sat Jul 24, 2010 11:26 am

Hi,
I am new to this forum and would firstly like to thank everyone putting out plugins and advice, I would be lost without it.
I am a kitchen designer and am currently trying to set up SU to perform some key functions, the main one being to change the style of door and drawer fronts in an already completed design. Obviously I could use SU's built in features to find and replace components but this gets complicated when working with frontages of varying sizes. Does anyone know of a method of doing this without repeating the process for each individual door and drawer front?

Thanks

Nick Chun
0

nickchun 
 

Re: Find and replace multiple components?

Postby TIG » Sat Jul 24, 2010 11:43 am

Follow these steps - if components are nested inside others you will have to edit the container-component...
Right-click to get the context menu shown below...
SelectAll.png
AllSelected.png
ReplaceSelected.png
AllReplaced.png
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby dedmin » Sat Jul 24, 2010 12:07 pm

If You have a lot of nested components the best way is to put them on appropriate layers and use Selection Toys
viewtopic.php?p=114380#p114380
0

dedmin 
 

Re: Find and replace multiple components?

Postby nickchun » Sun Jul 25, 2010 12:41 pm

Thanks to TIG and dedmin for your replies,
What I am trying to do is change ALL the doors and drawer fronts(in as few moves as possible) but these frontages could be of different heights and widths. I am familiar with replacing component selections, but this would mean repeating the process for each different size. I am not sure how the selection tools plugin would work that dedmin suggested. Is this a job for a plugin that doesn't exist yet?

Thanks
Nick
0

nickchun 
 

Re: Find and replace multiple components?

Postby TIG » Sun Jul 25, 2010 7:45 pm

Are your doors [etc] all separate components or scaled versions of the same one?
If they are scaled then the swap should be seamless.
If they are separate versions [which is more likely because scaling will affect rail/stile sizes] - e.g. Door-18-24-A, Door-20-24-A, Door-24-24-A etc then to use a scripted 'swapper' you at least need the replacements to follow the same pattern of 'name-coding' - e.g. Door-18-24-B, Door-20-24-B, Door-24-24-B etc - i.e. let's say the coding is Element[Door]-Width[18]-Height[24]-Code[A].
Once you have a logical way of encoding an element's name I can show you how to make a script that will go through the selected components and find their Element, Width, Height and Code and then swap for available elements with another code. A version that would run in the Ruby Console [initially at least] would take the form swap_components('-A','-B') - it would find all code-A instances in your selection and swap them with any available code-B versions - either using those already loaded into the SKP or perhaps available in a specific set of folders in the ../Components folder - e.g. a /Doors folder and a /Drawers folder... It relatively easy to find components that have a name ending in '-A' and find and the source equivalently name one, but with its name ending in '-B'. The secret is in having a logical naming convention for components that might get swapped - I used A, B etc as the 'Code' but it could be more complex - swap_components('-A-Oak-Antique','-B-Maple-Limewash') etc etc...
2
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby dedmin » Sun Jul 25, 2010 8:52 pm

I'm dealing with this problem like this - all my doors have the same axes place and orientation - upper left back corner. I'm scaling only with FredoStretch from left to right - this way component's axes doesn't move and thickness stay the same. If I want to replace some doors that have different size I firs place the component aside, change the size to be the same as the ones i want to replace and then replace

http://www.youtube.com/watch?v=Lom6aWnG7zA
0

dedmin 
 

Re: Find and replace multiple components?

Postby nickchun » Tue Jul 27, 2010 3:18 pm

Once again guys thanks for your replies,
The components are made by Fredo scaling an original, which leaves the stiles and rail sizes intact, but then they are saved as individual components into a library.
Considering the kitchens I design can have upwards of 50 frontages I think the script using codes is what I had in mind but I have no experience of writing scripts in ruby. Looks like I will have to try and learn but I struggle with some of the concepts ( the last program I wrote was in BASIC 30 yrs ago and was pretty simple). TIG, do you think it could be a fairly small script?#

Thanks
Nick
0

nickchun 
 

Re: Find and replace multiple components?

Postby TIG » Tue Jul 27, 2010 4:04 pm

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 ?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby nickchun » Tue Jul 27, 2010 9:13 pm

Thanks TIG, I get the gist of what your saying ( only the gist mind) and need to spend time learning ruby before I can make a start. I have no idea if I'm capable of doing it yet though. Changing the handles is simple enough anyway, I just use a horizontal and vertical version of each and use "replace instances".
On the subject of nesting, perhaps it would be simpler if I made libraries of the whole cabinet in different sizes, types and door styles, and swapped the whole thing.
I don't uaually show internals so it would be just a box tacked onto the door/drawer.
Thanks again
Nick
0

nickchun 
 

Re: Find and replace multiple components?

Postby TIG » Wed Jul 28, 2010 10:08 am

The secret is in the component definition's coding so that a ruby can decide which swaps with what... the Ruby code is straight forward enough.

As you say swapping handles from one type to another is easy done with 'select instances' and then 'replace selected'. You can programmatically get Ruby to that, BUT you'd have a set of 'filters' that look at the component instances' definition's name and if it matches a given pattern then swap that instance's definition for another definition with a name that matches another pattern you have given it... Just imagine doing it manually - you decide on the type of door needed to be swapped - let's say it's type 'A' - in the component browser you find one of the door components that is a type A [there might be many variants] - 'select instances' highlights them, now find the equivalent sized door type 'B' and 'replace selected'. De-select everything and start again on the next type 'A' variant... With the Ruby version all you do it tell the tool what pattern of name 'Door-*-A' you seek and what pattern to replace it with 'Door-*-B' - it then makes lists of all of the different variants and processes them in turns and swaps their definitions to match the new name pattern, if there's an equivalent component available. It reports at the end. All it's doing is what you can do manually but automatically and without the risk of error... ;)
1
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby nickchun » Fri Jul 30, 2010 4:10 pm

Thanks TIG, sorry I'm a bit slow replying. I have just started going through some ruby tutorials and will soon start experimenting. Its got to be worth the effort learning; the possibilities seem endless.......
Nick
0

nickchun 
 

Re: Find and replace multiple components?

Postby JPMays » Thu Dec 23, 2010 10:22 pm

I'm running into an odd problem with swapping components as well. I have about 200 instances of one component, that I need to swap with another one. If I select an individual component, and right-click to swap, it works fine. If I select multiple, that option is not available. If I use the Component window and right-click on the component and Select All, they are all selected, but the option to Replace Selected is grayed-out and unavailable. What gives?
0
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

JPMays 
 

Re: Find and replace multiple components?

Postby Gaieus » Fri Dec 24, 2010 5:45 am

When you selected all the instances, do not right click on the definition of this component in the component browser but on the definition of the one you want to replace these instances with. There will be the menu item available...
0
User avatar
Gaieus 
 

Re: Find and replace multiple components?

Postby xombie1912 » Tue May 05, 2015 6:59 pm

TIG wrote:Are your doors [etc] all separate components or scaled versions of the same one?
If they are scaled then the swap should be seamless.
If they are separate versions [which is more likely because scaling will affect rail/stile sizes] - e.g. Door-18-24-A, Door-20-24-A, Door-24-24-A etc then to use a scripted 'swapper' you at least need the replacements to follow the same pattern of 'name-coding' - e.g. Door-18-24-B, Door-20-24-B, Door-24-24-B etc - i.e. let's say the coding is Element[Door]-Width[18]-Height[24]-Code[A].
Once you have a logical way of encoding an element's name I can show you how to make a script that will go through the selected components and find their Element, Width, Height and Code and then swap for available elements with another code. A version that would run in the Ruby Console [initially at least] would take the form swap_components('-A','-B') - it would find all code-A instances in your selection and swap them with any available code-B versions - either using those already loaded into the SKP or perhaps available in a specific set of folders in the ../Components folder - e.g. a /Doors folder and a /Drawers folder... It relatively easy to find components that have a name ending in '-A' and find and the source equivalently name one, but with its name ending in '-B'. The secret is in having a logical naming convention for components that might get swapped - I used A, B etc as the 'Code' but it could be more complex - swap_components('-A-Oak-Antique','-B-Maple-Limewash') etc etc...


hello TIG can you help me for 1plugin i want to made i have the idea but i dont know about ruby console if you are interested please send me a private message
thanks a lot
0
User avatar
xombie1912 
 

Re: Find and replace multiple components?

Postby antioche » Tue Aug 04, 2015 8:19 am

Hi xombie1912 and TIG,

I would be interested in using the ruby script that you described, that is to say, run through the selection and swap components that match a specific name pattern with others.

More specifically in my case, I want to find modified dynamic components (automatically named by SketchUp for instance Name_of_initial_component#3 ) to revert them with the original component definition (Name_of_initial_component).

The reason for doing this is that I have create a Room dynamic component that calculates its floor area using the FACEAREA property. Inside each rooms are dynamic Windows and Door components. I need to have a separate instance of these windows/doors something else in the model to create a graphical schedule of doors and windows.

However, I need the Room dynamic components to recalculate FACEAREA() of a certain material, but this calculation is only updated when the component is redrawn (Right Click > Dynamic Components > Redraw). Unfortunately, this redraw causes all dynamic components inside them to become unique components (hence being renamed Name_of_initial_component#1 instead of Name_of_initial_component), so that they are now treated as separate definitions (although they are still identical) and the link to the schedule is broken (i.e. updating one does not update the other...). Not sure why...

The script would allow me to quickly run through the Room components and reassign the correct window/door definitions to the components it contains.

Alternatively, if you know a way to refresh a FACEAREA() calculation in a dynamic component without requiring a redraw? That would be my preferred solution...

Any suggestion or draft function of this Ruby function you described?

Cheers,

Teebs
0

antioche 
 

Re: Encontrar e substituir vários componentes?

Postby tenquin » Sun Sep 10, 2017 7:21 pm

Boa Tarde!

TIG, poderia nos auxiliar como desenvolver o plugin para substituir portas?
Desde já agradeço.
0

tenquin 
 

Re: Find and replace multiple components?

Postby TIG » Mon Sep 11, 2017 11:01 am

@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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby antioche » Mon Sep 11, 2017 11:07 am

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
0

antioche 
 

Re: Find and replace multiple components?

Postby TIG » Mon Sep 11, 2017 11:37 am

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...
Code: Select all
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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby antioche » Mon Sep 11, 2017 2:09 pm

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
0

antioche 
 

Re: Find and replace multiple components?

Postby tenquin » Mon Sep 11, 2017 8:46 pm

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.
0

tenquin 
 

Re: Find and replace multiple components?

Postby TIG » Mon Sep 11, 2017 9:15 pm

@tenquin

Yes, that's nothing like it !

You could try editing the code...
Code: Select all
###....
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
}
###...
1
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby tenquin » Tue Sep 12, 2017 12:55 am

Obrigado mais uma vez TIG!

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

:thumb:
0

tenquin 
 

Re: Find and replace multiple components?

Postby tenquin » Thu Dec 28, 2017 10:06 pm

TIG wrote: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...
Code: Select all
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...
0

tenquin 
 

Re: Find and replace multiple components?

Postby TIG » Fri Dec 29, 2017 8:05 pm

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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby tenquin » Fri Dec 29, 2017 9:43 pm

@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.
0

tenquin 
 

Re: Find and replace multiple components?

Postby TIG » Sat Dec 30, 2017 7:30 pm

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 !
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Find and replace multiple components?

Postby tenquin » Sat Dec 30, 2017 7:37 pm

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

tenquin 
 

Re: Find and replace multiple components?

Postby kostiaarh » Sat Sep 14, 2019 6:36 pm

TIG wrote: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.
0

kostiaarh 
 

Re: Find and replace multiple components?

Postby TIG » Sat Sep 14, 2019 9:04 pm

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...
0
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Newbie Forum

Who is online

Users browsing this forum: Formentin, MxmeF and 5 guests

Visit our sponsors: