A group is a special kind of component instance, and so therefore has a component definition with the #group?
flag set true
.
Component definitions can be written out to standalone SKP files using the ComponentDefinition#save_as
method.
Possible “gotcha” here are:
If the instance has been scaled, the definition will not be scaled. Likewise the definition will not have an instance only properties such as material
painted to the instance as a whole. (If you instead entered into the group edit context and painted the faces, then the definition will also have those faces painted.)
Groups are usually treated as a singleton object, but can be copied so that the definition has more than 1 instance. If this is so and the instances have unique name properties then the definition will not have these names. (Ie, the definition has it’s own name property.)
Other instance properties not part of the definition are cast_shadows
, receives_shadows
, it’s layer
(tag) assignment, locked
and whether it’s been set visible
/ hidden
.
Any attribute dictionaries
attached to instances are not exported with the definition.
So, anyway, you can iterate the model’s DefinitionList
collection choosing those that have the #group?
flag set true
and writing them out to a folder you’ve selected.
Then batch open each file in that folder and export each in turn to DWG using the export options as appropriate. (This implies you’ve saved each filename or path of the SKP files in an array as you wrote them out. Otherwise you’d need to use the core Dir
class to get the array of files in your export folder.)
The trick here is to be sure that the export is done, before moving on to the next file. (I’m not sure if the export method is synchronous or if it returns early. A delay might be needed.)
The other option of using Selection#add
, then Sketchup.send_action("copy:")
, Sketchup.file_new()
, then Sketchup.send_action("paste:")
, followed by opening the source model using Sketchup.open_file
also has asynchronous issues.
It is complex to try to know when the new model is ready for pasting, and when the source model is done reloading each loop through the batch iteration. It might be done with a temporary AppObserver
object.