使用 SolidWorks API 将装配体另存为零件时出现 swGenericSaveError (C++)

swGenericSaveError when using SolidWorks API to save assembly as part (C++)

我正在尝试使用 C++ API 实现将程序集 (.SLDASM) 保存为零件文档 (.SLDPRT),但我不断收到 swGenericSaveError。

使用以下代码:

void guardarModelo()  
{  
  //Guardar como part  
  wchar_t exportedfilename[MAX_PATH], assemblyname[MAX_PATH], *pext, *pname;  
  long error = 0, warning = 0;  
  VARIANT_BOOL result = 0;  
  CComPtr<IModelDocExtension> swExtension; //Extension (formato de fichero)  
  swUserPreferenceIntegerValue_e part = swSaveAssemblyAsPartOptions; //Opciones de part  

  //Arreglamos la extension y la ubicacion del fichero  
  wcscpy(exportedfilename, filename);  
  pext = wcsrchr(exportedfilename, L'\') + 1;  
  wcscpy(pext, L"-modelo\");  
  CreateDirectory(exportedfilename, NULL);  

  wcscpy(assemblyname, filename);  
  pname = wcsrchr(assemblyname, L'\') + 1;  
  pext = wcsrchr(assemblyname, L'.');  
  wcsncpy(pext, L".SLDPRT[=11=]", 8);  
  wcscat(exportedfilename, pname);  

  CComBSTR sFileName(exportedfilename);  

  swModel->get_Extension(&swExtension);  

  //swApp->SetUserPreferenceIntegerValue(part, swSaveAsmAsPart_AllComponents, &result);  
  swExtension->SetUserPreferenceInteger(swSaveAssemblyAsPartOptions, swDetailingNoOptionSpecified, swSaveAsmAsPart_AllComponents, &result);  

  swExtension->SaveAs(sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, NULL, &error, &warning, &result);  

  if (error == 0)  
  wcout << "Modelo completo guardado como part." << endl;  
  else  
  wcout << "ERROR " << error << " guardando modelo completo como part." << endl;  

}  

将程序集导出到 Parasolid 二进制 (.x_b) 时代码工作正常:

void exportarModelo()  
{  
  //Exportar a parasolid  
  wchar_t exportedfilename[MAX_PATH], assemblyname[MAX_PATH], *pext, *pname;  
  long error = 0, warning = 0;   
  VARIANT_BOOL result = 0;  
  CComPtr<IModelDocExtension> swExtension; //Extension (formato de fichero)  
  swUserPreferenceIntegerValue_e parasolid = swParasolidOutputVersion; //Opciones de Parasolid  


  //Arreglamos la extension y la ubicacion del fichero  
  wcscpy(exportedfilename, filename);  
  pext = wcsrchr(exportedfilename, L'\') + 1;  
  wcscpy(pext, L"-modelo\");  
  CreateDirectory(exportedfilename, NULL);  


  wcscpy(assemblyname, filename);  
  pname = wcsrchr(assemblyname, L'\') + 1;  
  pext = wcsrchr(assemblyname, L'.');  
  wcsncpy(pext, L".x_b[=12=]", 5);  
  wcscat(exportedfilename, pname);  


  CComBSTR sFileName(exportedfilename);  

  swModel->get_Extension(&swExtension);  


  //Elegir version de Parasolid y guardar  
  swApp->SetUserPreferenceIntegerValue(parasolid, swParasolidOutputVersion_150, &result);  


  swExtension->SaveAs(sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, NULL, &error, &warning, &result);  


  if (error == 0)  
  wcout << "Modelo completo exportado a Parasolid." << endl;  
  else  
  wcout << "ERROR " << error << " exportando modelo completo a Parasolid." << endl;  


}  

有人知道为什么这不起作用吗?有没有其他方法可以将装配体的所有零部件保存在单个零件文档中?

找到解决方案。关键是在保存文档之前使用ActivateDoc3方法激活加载的程序集。这会处理 swGenericSaveError 并且效果很好。