Explode in ruby script

Hi I’m new here. I have imported a sketchup file with ruby scritp. next I want to do the explode in the same ruby script, please help. The script code is as follows:

SKETCHUP_CONSOLE.clear
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection

url = “https:\u002F\u002F3dwarehouse.sketchup.com\u002Fwarehouse\u002Fv1.0\u002Fpubliccontent\u002F42bda7c5-ae87-40a6-9f49-5ed72c9c02e7”
loadm = mod.definitions.load_from_url(url)
return unless loadm # good practice

could be “next unless loadm” inside a loop

point = Geom::Point3d::new( 0, 0, 0 )
myobject = mod.active_entities.add_instance(
loadm,
Geom::Transformation::new( point )
)

Welcome!
Please read this and edit your post accordingly: [How to] Post correctly formatted and colorized code on the forum?

Be aware that Ruby script can not be run in SU web version as your indicated in your profile. Please complete your profile with valid data. :innocent:

1 Like

ComponentInstance#explode-instance_method

thank very much. I added the line of code cinst.explode and it does not work for me.
RUBY CODE:
SKETCHUP_CONSOLE.clear
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
url = “https:\u002F\u002F3dwarehouse.sketchup.com\u002Fwarehouse\u002Fv1.0\u002Fpubliccontent\u002F42bda7c5-ae87-40a6-9f49-5ed72c9c02e7”
cdef = mod.definitions.load_from_url(url)
return unless cdef # good practice

could be “next unless cdef” inside a loop

point = Geom::Point3d::new( 0, 0, 0 )
cinst = mod.active_entities.add_instance(
cdef,
Geom::Transformation::new( point )
)

Assuming ‘cinst’ is a ComponentInstance object

array = cinst.explode
if array
UI.messagebox “Exploded the component instance”
else
UI.messagebox “Failure”
end

Please, per the link @dezmo gave earlier, tell the forum’s formatter how to handle your code by putting
```ruby
on the line before the code, and
```
on the line after the code. As is, it is very hard to read and impossible to cut and paste for trying it out.

1 Like

You have a return statement in your code. This must be used from within a method definition.

File: keywords.rdoc [Ruby 2.7.1]

Exits a method. See methods syntax. If met in top-level scope, immediately stops interpretation of the current file.


The following worked fine for me in both SU2020 and SU2021 …

def load_and_explode(url, xform = IDENTITY)
  mod = Sketchup.active_model
  cdef = mod.definitions.load_from_url(url)
  unless cdef # good practice
    puts "Failure to load definition from 3DW"
    return false
  end

  # Insert an instance:
  cinst = mod.active_entities.add_instance(cdef, xform)

  # Explode the ComponentInstance object:
  array = cinst.explode
  if array
    puts "Exploded the component instance."
    return array
  else
    puts "Failure to explode the component instance."
    return false
  end
end

url = "https:\u002F\u002F3dwarehouse.sketchup.com\u002Fwarehouse\u002Fv1.0\u002Fpubliccontent\u002F42bda7c5-ae87-40a6-9f49-5ed72c9c02e7"

load_and_explode(url)
#=> Exploded the component instance.
#=> [#<Sketchup::Group:0x0000020156cfac48>]

The bare table profile group was the result in both tests.


I would suggest however that the method take the 3DW model ID …
… as the first part of the URL could change as Trimble makes changes to 3DW.
(This would mean you only have 1 place in the code to fix future changes.)

def load_and_explode(model_id, xform = IDENTITY)
  mod = Sketchup.active_model
  url = "https:\u002F\u002F3dwarehouse.sketchup.com\u002Fwarehouse"<<
  "\u002Fv1.0\u002Fpubliccontent\u002F"<< model_id
  cdef = mod.definitions.load_from_url(url)
  unless cdef # good practice
    puts "Failure to load definition from 3DW"
    return false
  end

  # Insert an instance:
  cinst = mod.active_entities.add_instance(cdef, xform)

  # Explode the ComponentInstance object:
  array = cinst.explode
  if array
    puts "Exploded the component instance."
    return array
  else
    puts "Failure to explode the component instance."
    return false
  end
end

Thanks, but it don’t worked for me in SU2020. Please watch the video

The video tells me nothing. I don’t know where these balloons come from.

I used the native Ruby Console (and never use any of the web browser based consoles. They introduce problems.)

The outputs to STDOUT in your image indicate that the return value from the method is correctly returning an array with the bare group inside it.

So retry using the native Ruby Console.