Batch groups are saved as a single skp file

先将群组选中,然后运行此代码即可保存为当前文件目录下,同文件名加后缀的单个skp文件

# 这段是默认代码,您可以选择使用或删除。。。
#编写者:xlindq 2021.09.19
mod = Sketchup.active_model # 打开模型
ent = mod.entities # 模型中的所有实体
sel = mod.selection # 当前选择

sel.grep(Sketchup::Group) do | grp |
      gname=grp.name
      grp1= grp.copy
      ins = grp1.to_component
      defn = ins.definition
           
      new_skp= mod.path.gsub(/.skp$/,'_' + gname + '.skp')
      puts new_skp
      defn.name = gname
      defn.save_as(new_skp)
      ent.erase_entities ins
     
end

Select the groups first, and then run this code to save it as a single SKP file with the same file name and suffix in the current file directory
增加个数字,防止群组重名
Add a number to prevent duplicate group names

# 这段是默认代码,您可以选择使用或删除。。。
mod = Sketchup.active_model # 打开模型
ent = mod.entities # 模型中的所有实体
sel = mod.selection # 当前选择
num=1
sel.grep(Sketchup::Group) do | grp |
      gname=grp.name
      grp1= grp.copy
      ins = grp1.to_component
      defn = ins.definition
          
      new_skp= mod.path.gsub(/.skp$/,'_' + gname + num.to_s + '.skp')
      puts new_skp
      defn.name = gname
      defn.save_as(new_skp)
      ent.erase_entities ins
      num=num+1
      
end

What is the question?

I think the OP was expecting the group instance names to be unique, and when they were not (or were blank) the code repeatedly oversaved the same export file.

In the 2nd post this was corrected to “difference” the files names.

1 Like

这组代码将解决总装图到零件图.也就是将模型中的图批量按群组保存为单个SKP文件.
不管你的模型有多复杂,均可按组生成的零件(SKP).从而达到批量保存的目的.
This is codes will solve the problem from general assembly drawing to part drawing, that is, save the drawings in the model in batches as a single SKP file
No matter how complex your model is, you can generate parts by group (SKP), so as to achieve the purpose of batch saving.

So this is solved?

1 Like

Yes, I think it is with the 2nd post.

分享快乐,快乐分享
Share happiness, share happiness
因为我搜索了整个网站,都没有这样的代码,于是就参照写了一个.并分享出来.
感谢各位的关注.
Because I searched the whole website without such code, I wrote one by reference and shared it
Thank you for your attention

1 Like

Please post Ruby questions in the Ruby API subcategory.