Batch Exporting Groups to AutoCAD?


I’m on an assignment to export roughly 3000 Groups from SKP files to their respective DWG file. Is there a way of automating this using Ruby API/SDK or should I just stick to the old fashioned way of copypasting to a new file and then exporting the groups one-by-one? Thanks in advance!

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.


There is an old snippet here, by @TIG :

It is an obj export, but theoretically - as far as I see, with a short check- you “just need to change the obj to dwg…”

Perhaps you need to set some option,
for the Model #export method


Yesterday, I whacked out a simple example batch export extension. In testing it, it crashed SU 2023.0.2 at least 3 times, I think when ComponentDefinition#save_as was called.

I fiddled around with it, and wrapped the call within a beginrescue block and I haven’t gotten it to crash again doing about 4 or 5 batch exports for simple models with simple groups.

I need to have someone look up these crash reports:

Crash #124574
Crash #124615
Crash #124618

@tt_su @colin ?

The crashes all happen at this point:

mfc140u!CWnd::GetStyle 206. D:\a01_work\2\s\src\vctools\VC7Libs\Ship\ATLMFC\Src\MFC\winocc.cpp

Don’t know what winocc.cpp is, but I couldn’t find it in any of the SketchUp source code.

I will send you the debugger output, in the hope that you can make some sense of it!

1 Like

It is a part of the Microsoft Foundation Classes C++ libraries.

winscp/libs/mfc/source/winocc.cpp at master · mirror/winscp · GitHub

It has a bunch of window interface functions.

I assumed it was happening upon the calls to save out the definitions, but that happens directly after using the UI::select_directory window. So it might be triggered by that window use and a combination of my dark mode Windows theme.

It might also have been memory corruption where the runtime was residing in memory.