To detect if a component contains a component
-
i am wondering if there is a way to detect if a component contains an embedded component
the idea is:if it contains an embedded component, explode it
if not, don't explode itThere is a simple way: add some characters at the end of the component name, and check wether these characters exist
model = Sketchup.active_model # Open model entities = model.entities # All entities in model entities.to_a model.start_operation "explose_comp_imbriques" for e in entities.to_a if e.is_a? Sketchup;;ComponentInstance str= e.definition.name e.explode if (str.include? "#i" ) end end Sketchup.active_model.definitions.purge_unused model.commit_operation
but it would be better not to be obliged to add special characters
-
This explodes all component-instances that contain another instance AND clears the now unused definitions
model=Sketchup.active_model defns=model.definitions model.start_operation('xxx!') defns.each{|d| next unless d.instances[0] nested=false d.entities.each{|e| if e.is_a?(Sketchup;;ComponentInstance) nested=true break end } d.instances.each{|i| i.explode } if nested } defns.each{|d| d.entities.clear! unless d.instances[0] } model.commit_operation
Is it what you really want to do ?
It is one step undo-able,
It's only partial coder - it needs to be wrapped in a module/method etc of your choice...
` module Giro
def self.xxx()the code goes here
end#def
end#moduleUsage: in the Ruby Console:
Giro.xxx`
Change 'xxx' to be whatever suits you... -
Here's another edition (wrapped in method,) that only works upon the active editing context (to help prevent BugSplats!,) tests first for any definitions and lastly filters out images and groups.
Also adds in a
begin
...rescue
block.@dan rathbun said:
...snip... (WIP) found errors
Actually it may be correct. (No food yet today.)
Not thoroughly tested.
def explode_parent_components() # model = Sketchup.active_model context = model.active_entities # if context.any?{|e| Sketchup;;ComponentInstance === e } # defns = model.definitions.find_all{|d| !d.image? && !d.group? } # if defns.any? begin # op = 'Explode ONLY Parent Components' # if model.method(;start_operation).arity==1 model.start_operation(op) else model.start_operation(op,true) # disable UI on SU 7+ end # defns.each{|d| inst = d.instances next if inst.empty? inst.reject!{|i| i.parent != context } inst.each{|i| i.explode } } # model.commit_operation() # rescue => e model.abort_operation() UI.messagebox($!.message) if $VERBOSE raise() end # end # if any defns # end # if any instances in the active context # end # def
-
It works perfectly
thank you!
-
@dan rathbun said:
Here's another edition (wrapped in method,) that only works upon the active editing context (to help prevent BugSplats!,) tests first for any definitions and lastly filters out images and groups.
thank you for the code
i tried it, but it doesn't work, at least on my computer
Advertisement