Counting ComponentInstances

Is it a method that returns all of ComponentInstance included in parent ComponentInstance in all levels of nesting?

ComponentDefinition.count_used_instances [>=v2016]

I think, It gives me only number of instances, or not? I need handle to each.

ComponentDefinition.instances will give you handles to all the instances.

You asked how to count them !

But as was said:


returns an array of all of its instances, but you then need to work out the ‚Äėparent‚Äô of each of them in turn‚Ķ

Actually, he did not. Reread the original question.

[quote=‚ÄúDanRathbun, post:6, topic:28421‚ÄĚ]
Actually, he did not. Reread the original question.
[/quote] Actually he did, reread the title:
"Counting ComponentInstances"
NOT ‚ÄúCollecting ComponentInstances‚Ä̂Ķ
Although in the text itself I admit the OP does indeed ask for a method to ‚Äėreturn‚Äô all instances‚Ķ
But given the title itself it could have meant to ‚Äėreturn a count of‚Ķ‚Äô ‚Ķ

1 Like

I‚Äôll give it to you, but IMO, the title is not the question, it‚Äôs the ‚Äúhook‚ÄĚ.

Gentlemen, thank you for your suggestions. Next time I’ll try to accurately describe my problems in content and topic.
But back to access to the instance. I checked ComponentDefinition.instances. It returns only instances from the first level. Do you have any ideas, suggestiosn where I should start writing method that supports these issues? How to write a loop (or something else) running through all (of all levels of nesting) instances?

thank you in advance


Ok, I made it in this way:

def get_instance(instance)
inst_array =
instance.definition.entities.each {|inst| inst_array << [inst,1] if inst.typename == ‚ÄúComponentInstance‚ÄĚ}
until not inst_array.find {|inst| inst[1] == 1}
inst_array.each do |inst|
inst[0].definition.entities.each {|inst| inst_array << [inst,1] if inst.typename == ‚ÄúComponentInstance‚ÄĚ}
inst[1] = 0
tablica =
inst_array.each {|inst| tablica << inst[0]}

Do you think is there a easier way?

(1) An instance of a component cannot be inserted into it’s own definition’s entities collection. It would create a circular reference.

(2) You should check the number of used instances and bail out of the method if the count is 0.

This statement makes no sense.

ALL of a definition’s instances will be a member of it’s .instances collection.