[REQUEST] Convert single instance components to groups
-
I was looking for quite while searching for the following problem:
Importing (Revit) DWG data into Sketchup creates a lot of single instance components. The trouble is that when copying multiple of these files together that Sketchup slows down to a crawl because it is trying to match components that are similar. Usually I end up with sketchup files with hundreds of single component instances. Sketchup works faster when these single instances are in fact groups, making copying between sketchup files way faster. Normally I explode the selected instance and group these again, but with hundreds of these, this is too cumbersome.
So what I need is the following:
- select all single instance components within my current SketchUp file (or current selection)
- convert these to standard groups
I think this is quite useful for anybody working with imported files and want to keep on working quick.
For number 2) I found the following code snippet from sdmitch (https://sketchucation.com/forums/viewtopic.php?f=323&t=17590&p=140679&hilit=single+instance+component#p623006)
@mod = Sketchup.active_model @ent = @mod.active_entities @sel = @mod.selection @vue = @mod.active_view cmps = @sel.grep(Sketchup;;ComponentInstance) cmps.each{|ci| grp = @ent.add_group(ci) grp.name = ci.definition.name ci.explode } @mod.definitions.purge_unused[highlight=#ffffbf:39rhwwt6]Does anybody know how to realize part 1?[/highlight:39rhwwt6]
Thanks in advance!
-
It's relatively straightforward...
Here's 1 AND 2 combined...
This code finds all components that have just one instance and makes a new group in place of the instance, containing its contents, using the same name/layer/material as the instance itself.
The now unused definitions are deleted.
It is one step undo-ablemodel=Sketchup.active_model defns=model.definitions model.start_operation("ci2gp",true) defns.each{|defn| next if defn.group? next if defn.image? next unless defn.instances.length==1 inst=defn.instances[0] name=defn.name layr=inst.layer matl=inst.material ents=inst.parent.entities tran=inst.transformation grp=ents.add_group() gents=grp.entities i=gents.add_instance(defn, tran) inst.erase! i.explode grp.name=name grp.layer=layr grp.material=matl } defns.purge_unused model.commit_operation -
Wow, TIG, you have saved my day(s)
Big thanks! -
@tig said:
It's relatively straightforward...
Sorry to dig up this old topic but your code above causes a bugsplat in Sketchup 2022. Any idea what could be the cause?
-
It's a snippet of old code but it should work OK.
It works for me on a PC - I don't have a suitable MAC to hand...
Nothing different in v2022 as far as I can see...What exactly are you trying to do, are there any Ruby Console messages before the failure ?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement