private void performInROTransaction(IMgaProject project, voidDelegate d) { IMgaTerritory territory = project.BeginTransactionInNewTerr(transactiontype_enum.TRANSACTION_READ_ONLY); try { d(); } finally { try { project.AbortTransaction(); } catch { } try { territory.Destroy(); } catch { } } }
private void CleanUpTempMgaProject(string outputFile) { var newProject = new MgaProject(); bool ro_mode; // open the copied temporary mga file newProject.Open("MGA=" + outputFile, out ro_mode); IMgaTerritory terr = null; try { // create a new transaction terr = newProject.BeginTransactionInNewTerr(); // get the sot object on which the job manager will operate var sot = newProject.GetObjectByID(sotConfig.SoTID); if (sot != null) { // if the sot object exists in the project GME.MGA.Meta.objtype_enum objtype; MgaObject parent; // get the parent folder of which this SoT belongs to. sot.GetParent(out parent, out objtype); if (objtype == GME.MGA.Meta.objtype_enum.OBJTYPE_FOLDER) { // if it is really a folder cast it var parentSoTFolder = parent as MgaFolder; // get all SoT objects in the folder except the one we operate on // mark them for deletion var sotsToDelete = parentSoTFolder .ChildFCOs .Cast <MgaFCO>() .Where(x => x.ID != sotConfig.SoTID) .ToList(); try { // destroy all SoTs that are not used by this sot run sotsToDelete.ForEach(x => x.DestroyObject()); } catch { // ok } // get all not null test bench objects that this sot run uses. var referredTBsInSot = sot .ChildObjects .OfType <MgaReference>() .Where(x => x.Meta.Name == typeof(CyPhy.TestBenchRef).Name && x.Referred != null) .Select(x => x.Referred) .Cast <MgaModel>() .ToList(); foreach (var tb in referredTBsInSot) { GME.MGA.Meta.objtype_enum tbParentObjtype; MgaObject tbParent; // get the parent folder where the referenced test bench located tb.GetParent(out tbParent, out tbParentObjtype); if (tbParentObjtype == GME.MGA.Meta.objtype_enum.OBJTYPE_FOLDER) { var parentTBFolder = tbParent as MgaFolder; // get all test benches that are not used by the sot // WARNING: sometimes COM object comparison can fail this code is not the safest. var tbsToDelete = parentTBFolder .ChildFCOs .OfType <MgaModel>() .Where(x => referredTBsInSot.Contains(x) == false) .ToList(); try { // delete test benches that sot does not use tbsToDelete.ForEach(x => x.DestroyObject()); } catch { // ok } } } } } // commit changes if everything is successful newProject.CommitTransaction(); } catch { // abort all changes if exception occured newProject.AbortTransaction(); } finally { if (terr != null) { // destroy the territory we were working in if it is not null terr.Destroy(); } } // close the project AND save all changes newProject.Close(); }
// Generator Entry Point public static string main(string projectPathFull, string targetPath, string namespaceName) { Clear(); Generator.Path = targetPath; IMgaProject project = new MgaProject(); IMgaTerritory territory = null; try { bool ro_mode = true; project.Open("MGA=" + projectPathFull, out ro_mode); territory = project.BeginTransactionInNewTerr(transactiontype_enum.TRANSACTION_READ_ONLY); GME.MGA.IMgaFolder root = project.RootFolder as GME.MGA.IMgaFolder; Generator.ClassName = root.Name; Generator.NamespaceName = namespaceName; foreach (GME.MGA.IMgaFCO fco in root.ChildFCOs) { ProcessParadigmSheet(fco); } foreach (GME.MGA.IMgaFolder fol in root.ChildFolders) { foreach (GME.MGA.IMgaFCO fco in fol.ChildFCOs) { ProcessParadigmSheet(fco); } } RootFolder rootFolder = new RootFolder(); rootFolder.Save(); foreach (DSM.Generators.Object elem in DSM.Generators.Object.ElementsByName.Values) { elem.Save(); } DSM.Generators.Enum.Save(); DSM.Generators.Generator.Save(); if (Errors.Count > 0) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Errors during generation process:"); foreach (string item in Errors) { sb.AppendLine(item); } } return(Generator.ClassName); } catch (Exception e) { MessageBox.Show("Error generationg domain-specific interface: " + e.Message); return(null); } finally { if (territory != null) { territory.Destroy(); project.AbortTransaction(); } project.Close(true); } }