Wednesday, November 29, 2023
Home3D ModelingRely all teams and used element relying of the layer - Ruby...

Rely all teams and used element relying of the layer – Ruby API


Hello

I’m attempting to rely all teams and used element relying of the layer they’ve.

For exemple, I wish to know what number of “door” taged group and element i’ve and i would like it to rely for every definitions.

Eg. :
5 element White door
2 element Entrance door
1 group Again door

I attempted various things. I’m in a position to get the nb of element used with definition.count_used_instances however i’m not in a position to rely used occasion if tagged

I’m in a position to rely tagged occasion, entity.is_a?(Sketchup::ComponentInstance) #&& entity.layer.identify == "Door"

However it additionally rely doorways that are in not used element …

I’m king of caught

Hello,

With this code, I’m in a position to checklist parts of my lively entities

	mannequin = Sketchup.active_model
	entities = mannequin.active_entities
	
	entities.every{ |entity|  
	if entity.is_a? Sketchup::ComponentInstance
		places "sure it's (#{entity.definition.identify}) (#{entity.identify}) (#{entity.layer.identify}) "
	finish}
	

However to not checklist element in nested entites, and that’s type of my purpose :slight_smile:

Do that. It really works for me in a small check

mannequin = Sketchup.active_model
entities = mannequin.active_entities

def analyze(entity)
  places "sure it's (#{entity.definition.identify}) (#{entity.identify}) (#{entity.layer.identify}) "
  entity.definition.entities.every
    if subentity.is_a? Sketchup::ComponentInstance
      analyze(subentity)
    finish
  
finish

entities.everyentity



1 Like

That’s so good !

Thanks.

I depart the thread open in case somebody know a Sketchup perform to do it.

Thank’s quite a bit



1 Like

You may create an empty hash with key (layer.identify) and worth (quantity of parts) each merchandise test if key exist and replace the worth…
I’ll do that weekend…



1 Like

[menu]. File > Generate report will be grouped by Tag (Layer)
You’ll need to edit the usual report, although.

Hello Mike,

I used to work with “Studies,” however even after exporting, I nonetheless needed to edit the CSV file to make it suitable with the software program that requires the CSV knowledge. By creating my very own plugin, I can tailor this system to my particular wants and make it simpler to make use of.

This script is simply step one of a bigger challenge. :smiley:

I plan so as to add the flexibility to see at any time what number of parts I’ve in my challenge realtime.

Thank’s

That is higher than earlier. It counts all teams and parts and report portions and names

mannequin = Sketchup.active_model
entities = mannequin.active_entities

@counter = {}

def analyze(entity)
  places "sure it's (#{entity.definition.identify}) (#{entity.identify}) (#{entity.layer.identify}) "
  updateDict(entity)
  entity.definition.entities.every do |subentity|
    analyze(subentity) if subentity.respond_to?(:definition)
  finish
finish
def updateDict(entity)
  if @counter[entity.layer.name]
    previousCount = @counter[entity.layer.name][0]
    previousEntitiesNames =  @counter[entity.layer.name][1]
    @counter[entity.layer.name] = [ previousCount + 1 , previousEntitiesNames + '|' + entity.definition.name ]
  else
    @counter[entity.layer.name] = [ 1 , entity.definition.name ]
  finish
finish

entities.every do |entity|  
  analyze(entity) if entity.respond_to?(:definition)
finish

@counter.every do |key, worth|
  textToReport = ""
  allNames = worth[1].break up("|")
  uniqNames = allNames.uniq
  uniqNames.every do |n|
    textToReport = textToReport + n.to_s + " (" + allNames.rely(n).to_s + ") "
  finish
  places "#{worth[0]} group or parts in #{key} named as: #{textToReport}"
finish

(edited by Writer acording @DanRathbun recomendations)

Do not place an area between technique names and their argument checklist. This will confuse the Ruby interpreter.


Reasonably than “onerous” class typing, you should utilize “duck typing”.
(If it walks, talks and acts like a duck, then it may be handled as a duck.)

:duck:

Ie …

… can develop into …

  entity.definition.entities.every 
    analyze(subentity) if subentity.respond_to?(:definition)
  

REF: Object#respond_to?

Edited!
Thanks once more @DanRathbun ! Cleaner and quicker… Your assistance is all the time welcome… (all geese reported… :wink:)

You’re nonetheless inserting an area earlier than an argument checklist …

This will “come again to chunk you” sooner or later.

Ie, Ruby can get confused between what is meant to be an argument checklist and what’s an expression enclosed in parenthesis.


Additionally, for readability sake, the { for a block needs to be spaced the identical as in case you used dofinish as an alternative of {}.

Ie, not

entities.every  
  analyze(entity) if entity.respond_to?(:definition)

however

entities.every   
  analyze(entity) if entity.respond_to?(:definition)

… simply as you’d if utilizing …

entities.every do |entity|  
  analyze(entity) if entity.respond_to?(:definition)
finish



1 Like

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments