# Make several components unique to one definition - in Ruby

**URL:** <https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660>\
**Category:** Ruby API\
**Created:** [January 2, 2017, 10:29am UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660 "2017-01-02T10:29:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoni](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/yoni/32/47842_2.png) [@Yoni](https://forums.sketchup.com/u/Yoni)\
**Post date:** [January 2, 2017, 10:29am UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/1 "2017-01-02T10:29:47Z")

</div>

I have for example 4 component instances (all from the same component definition).  
In Sketchup, if I select 2 components, right click, and make them unique. I will end up with 2 component definitions with 2 instances per each definition.  
That’s good! It’s what I want.  
How can I replicate this behavior with a selection of components in ruby?

What I really want to do, is loop over all entities within a component. And make them all unique from their instances in other components, but not unique from each other…

Just using `instance.make_unique` within the loop, makes each and every one of them unique… not what I want.

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [January 2, 2017, 3:30pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/2 "2017-01-02T15:30:27Z")

</div>

Let’s assume you have a reference to some entities context - like:

`ents = component_definition.entities`

Then find any instances in that component.

`instances = ents.grep(Sketchup::ComponentInstance)`

Now make other instances [if any] of their definitions unique - UNLESS they are in `ents`

```
instances.each{|i|
  next unless i.definition.instances[1]
  i.make_unique unless i.parent.entities == ents
}

```

So now if you have two instances in `ents` they are still referencing the same definition, but all other instances in other entities contexts will now be unique…

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [January 2, 2017, 7:00pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/4 "2017-01-02T19:00:38Z")

</div>

Or if you want to make each set of components unique but the same for all instances with the same parent, you could do this.

```ruby
instances = subcomponent.definition.instances

parents = Hash.new

instances.each{|i|
  if parents.include? i.parent
    i.definition = parents[i.parent]
  else   
    i.make_unique 
    parents[i.parent] = i.definition
  end
}

```

---

<div class="post-metadata">

**Author:** ![Yoni](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/yoni/32/47842_2.png) [@Yoni](https://forums.sketchup.com/u/Yoni)\
**Post date:** [January 5, 2017, 7:40am UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/5 "2017-01-05T07:40:51Z")

</div>

Great!  
What does this line mean though?

`next unless i.definition.instances[1]`

---

<div class="post-metadata">

**Author:** ![Yoni](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/yoni/32/47842_2.png) [@Yoni](https://forums.sketchup.com/u/Yoni)\
**Post date:** [January 5, 2017, 10:32am UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/6 "2017-01-05T10:32:09Z")

</div>

Hi Neil,  
That’s an interesting one. In what practical situations would you use that?  
If all the instances have the same parent… It’s like they are all in the same component? so it wouldn’t make a difference… would it!? Unless the parent is maybe a… group? (this stuff is confusing :))

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [January 5, 2017, 1:42pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/7 "2017-01-05T13:42:18Z")

</div>

[quote=“Yoni, post:5, topic:36660”]  
next unless i.definition.instances[1]  
[/quote] If the instance’s definition has only one instance, then there is no need to make it unique [there’s only one already], so we skip it with the ‘next’.  
The likelihood is that the API will itself skip over the unnecessary make\_unique anyway, but in case it doesn’t [to avoid hiccups], and also to save a nano-second of processing time, you might as well skip processing it yourself…

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [January 5, 2017, 2:10pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/8 "2017-01-05T14:10:07Z")

</div>

I thought that is what you wanted. Here is def that allows you to make all instances unique, or make instances unique to parent.

```ruby
def make_unique(inst, unique_to_parent = true)
  if unique_to_parent 
    Sketchup.active_model.start_operation "Make Unique to Parent"
  else
    Sketchup.active_model.start_operation "Make Unique"
  end
  instances = inst.definition.instances
  parents = Hash.new 
  instances.each{|i| 
    if parents.include? i.parent and unique_to_parent
      i.definition = parents[i.parent]
    else 
      i.make_unique 
      parents[i.parent] = i.definition 
    end 
  }
  Sketchup.active_model.commit_operation
end

```

 ![](https://global.discourse-cdn.com/sketchup/original/3X/8/e/8efc99e674f4939632fee8d546b35fa666b1ca07.gif)

---

<div class="post-metadata">

**Author:** ![Yoni](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/yoni/32/47842_2.png) [@Yoni](https://forums.sketchup.com/u/Yoni)\
**Post date:** [January 5, 2017, 4:19pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/9 "2017-01-05T16:19:18Z")

</div>

Sorry, I misread one of your lines Neil, and thought you meant something else (I’m not even sure anymore what it was I was imagining ;))  
It seems like both your code and TIGs are doing what I asked. I’ve adapted it to a little plugin I’m working on. Hope to release it soon.

Thank you both very much!

---

<div class="post-metadata">

**Author:** ![Yoni](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/yoni/32/47842_2.png) [@Yoni](https://forums.sketchup.com/u/Yoni)\
**Post date:** [January 10, 2017, 4:35pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/10 "2017-01-10T16:35:25Z")

</div>

Looking at the code again, in both suggestions, the scripts make all ‘unrelated’ instances unique instead of making only the selected instances or their sub-components unique.

In TIG’s code, all instances in other parents are made unique of each other (or at least that’s what happens in my adaptation).

In the animation that Neil posted, in each ‘Big Box’ the small boxes are becoming unique (small\_box#1 x 6, small\_box#2 x 6, small\_box#3 x 6… etc.).

I wouldn’t want my operation on one Big Box and it’s sub-boxes affecting the similarities of the small boxes in the other Big Boxes…

If I were to ‘select’ manually (with the mouse) one Big Box, ‘right click’, ‘make unique’. Go into it, and select 6 small boxes, ‘right click’, ‘make unique’, it still only makes those selected unique, without touching anything else outside of context. I wish to replicate that exact function in Ruby without messing with other components “by accident”.

Or, if I were ‘forced’ to mess with all other components (I’d rather not), I would want to maintain all similarities outside my selection. (including in multiple nested levels).

---

<div class="post-metadata">

**Author:** ![Neil\_Burkholder](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Neil\_Burkholder](https://forums.sketchup.com/u/Neil_Burkholder)\
**Post date:** [January 10, 2017, 10:30pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/11 "2017-01-10T22:30:25Z")

</div>

You would do that like this.

```ruby
def make_selection_unique
  inst = Sketchup.active_model.selection.grep(Sketchup::ComponentInstance)
  if inst.count == 0 then end
  inst[0].make_unique
  pdef = inst[0].definition
  inst.each do |i| 
    i.definition = pdef
  end
end

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [January 11, 2017, 3:41am UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/12 "2017-01-11T03:41:40Z")

</div>

> **(Ruby coding style nitpick)**
>
> > [@Neil\_Burkholder](#):
> >
> > ```ruby
> > if inst.count == 0 then end
> > 
> > ```
> 
> Most coding style guides (and I) would prefer:
> 
> ```ruby
> return if inst.empty?
> 
> ```
> 
> a. `inst.empty?` is one method call that does not use a literal, whilst `inst.count == 0` is two method calls. The `count` method is called returning a reference to an integer, whose `==` method is called passing the literal `0` by reference into it (where a comparison is made.)
> 
> b. I am always wary of having too many `end` keywords in the file.  
> (Have gotten too many `SyntaxError`s on files, with messages like “unexpected end” or “missing end”, etc.)

---

<div class="post-metadata">

**Author:** ![Yoni](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/yoni/32/47842_2.png) [@Yoni](https://forums.sketchup.com/u/Yoni)\
**Post date:** [January 19, 2017, 2:49pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/13 "2017-01-19T14:49:05Z")

</div>

Thanks Dan. Neil, in your last suggestion, if the selection had multiple instances, with different definitions, it would turn them all into instances of one definition (the first) - which is unlike how the ‘right click’ - ‘make unique’ works.  
With the tips from all of you, I think I managed to get what I want. 🙂

The following code works almost like the ‘right click’ - ‘make unique’, except when making a selection of ALL similar instances (all in selection are of the same definition) - It still makes them all unique (adding a new definition), instead of ignoring them. (why add new definitions if no need to).

```
def make_sel_unique
  def_hash = Hash.new
  sel = Sketchup.active_model.selection.grep(Sketchup::ComponentInstance)
  return if sel.empty?
  sel.each { |i| 
    # if the instant's definition exists in the hash
    # assign to it the new definition value (new_def). 
    if def_hash.key?(i.definition) 
      i.definition = def_hash[i.definition]  
    else
    # If not in the hash - make unique while adding to the hash the 
    # definitions - before & after making unique - {old_def -> new_def}
      old_def = i.definition
      i.make_unique
      new_def = i.definition
      def_hash[old_def] = new_def
    end 
  }
  return nil  
end

```

---

<div class="post-metadata">

**Author:** ![Yoni](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/yoni/32/47842_2.png) [@Yoni](https://forums.sketchup.com/u/Yoni)\
**Post date:** [January 19, 2017, 2:53pm UTC](https://forums.sketchup.com/t/make-several-components-unique-to-one-definition-in-ruby/36660/14 "2017-01-19T14:53:56Z")

</div>

(Next step I’ll combine it with making unique to parent… )
