Trying to setup a cloud notebook to import SystemModeler (WSM) libraries by CloudImport[] I noted that it is not really straight forward to do so currently. In WSM as of v13.1using File>Save Total Model to arrive at a convenient system model archive (.sma) file seems to break references (for example for files located in a Resources directory.
In this notebook you will find a little workaround as we can achieve a convenient .sma export using the Wolfram Language and a bit of tweaking. The function ExportSMA[dest, class, cat] will export a single library or user class (using the given category cat) as dest.sma file, which can then be uploaded to the Cloud. Dependent libraries need to be set up in the same fashion, it is a deliberate design choice of this solution to allow exports without any dependencies.
In this notebook you will find a little workaround as we can achieve a convenient .sma export using the Wolfram Language and a bit of tweaking. The function ExportSMA[dest, class, cat] will export a single library or user class (using the given category cat) as dest.sma file, which can then be uploaded to the Cloud. Dependent libraries need to be set up in the same fashion, it is a deliberate design choice of this solution to allow exports without any dependencies.
ExportSMA[ ]
ExportSMA[ ]
Note that Pause[ n ] is used as some operations seem to not work unless a previous is finished.
In[]:=
ClearAll[ExportSMA];Options[ExportSMA]={"MinVersion"->"13.0"};ExportSMA::fargs="False arguments.";ExportSMA::usage="ExportSMA[dest, class, cat] will export a SystemModeler class to dest.sma using the category cat.";ExportSMA[dest_String,class_SystemModel,cat_String,opts:OptionsPattern[]]:=With[{systemModelerCategories={"Libraries","User Classes","Examples"},tempDir=FileNameJoin[{$TemporaryDirectory,"smaTempFiles"}],minVersion=OptionValue["MinVersion"]},Module[{category,sourceDirectory,destDirectory,packageDir,destFileBaseName,destFile,archive,manifestFile,manifest},(*Useexactcategoryclosesttouserinputandassignsourceanddestinationvalues*)category=First@*NearestTo[cat,1,DistanceFunction->(-SmithWatermanSimilarity[#1,#2,IgnoreCase->True]&)]@systemModelerCategories;sourceDirectory=DirectoryName@class["SourceFile"];{destDirectory,destFileBaseName}=Through[{DirectoryName,FileBaseName}@dest];destFile=FileNameJoin[{destDirectory,destFileBaseName<>".sma"}];packageDir=FileNameJoin[{tempDir,sourceDirectory//FileNameSplit/*Last}];(*Createfirstversionof.smafileusingcurrentlyimplementedmethods,renameto.zipandextracttodestinationdirectory*)Export[destFile,class,"SMA","IncludedLibraries"->None,OverwriteTarget->True];RenameFile[destFile,archive=FileNameJoin[{destDirectory,destFileBaseName<>".zip"}],OverwriteTarget->True];ExtractArchive[archive,tempDir,OverwriteTarget->True];(*replace_totaldirectorywithfilesfromtheactualinstallation*)Pause[10];DeleteDirectory[FileNameJoin[{tempDir,destFileBaseName<>"_total"}],DeleteContents->True];DeleteFile[archive];(*adaptmanifest.xmlandreplaceit*)manifest=Import[manifestFile=FileNameJoin[{tempDir,"manifest.xml"}]]//RightComposition[ReplaceAll[XMLElement["title",a_,{title_String}]:>XMLElement["title",a,{StringDelete[title,Whitespace~~"(Total Model)"]}]],ReplaceAll[XMLElement["library",{"id"->id_String,"file"->file_String,rule___},rest___]:>XMLElement["library",{"id"->StringDelete[id,"_total"],"file"->StringDelete[file,"_total"],rule},rest]],ReplaceAll[XMLElement["category",a_,{"Total Models"}]:>XMLElement["category",a,{category}]],ReplaceAll[XMLElement["tool",{"name"->"SystemModeler","minVersion"->v_},rest___]:>XMLElement["tool",{"name"->"SystemModeler","minVersion"->minVersion},rest]]];Export[manifestFile,manifest,"XML",OverwriteTarget->True];(*almostdone...nowcreateanarchiveandrenameitto.sma*)CopyDirectory[sourceDirectory,packageDir];(*nowwejustneedtocreateanarchivefromthefilesintempDirandrenameitto.sma*)Pause[10];CreateArchive[FileNames[All,tempDir],archive,OverwriteTarget->True];Pause[10];DeleteDirectory[tempDir,DeleteContents->True];RenameFile[archive,destFile,OverwriteTarget->True];]]ExportSMA[___]/;Message[ExportSMA::fargs]:="Never happens"