static int Main(string[] args) { if (args.Length < 2) { usage(); return(1); } MgaProject mainMgaProject = GetProject(args[0]); if (mainMgaProject == null) { return(2); } int retval = 0; MgaGateway mgaGateway = new MgaGateway(mainMgaProject); mgaGateway.PerformInTransaction(delegate { for (int ix = 1; ix < args.Length; ++ix) { string[] mergeInfo = args[ix].Split(new string[] { ".mga/" }, 2, StringSplitOptions.None); string filename = mergeInfo[0] + ".mga"; string path = "/" + mergeInfo[1]; MgaFCO currentObject = mainMgaProject.get_ObjectByPath(path) as MgaFCO; if (currentObject == null) { Console.Error.WriteLine("Error: could not find object of path \"" + path + "\" in model of file \"" + args[0] + "\", cannot merge file \"" + filename + "\""); retval |= (int)SubTreeMerge.SubTreeMerge.Errors.PathError; continue; } SubTreeMerge.SubTreeMerge subTreeMerge = new SubTreeMerge.SubTreeMerge(); subTreeMerge.gmeConsole = new SubTreeMerge.FlexConsole(SubTreeMerge.FlexConsole.ConsoleType.CONSOLE); subTreeMerge.merge(currentObject, filename); retval = (int)subTreeMerge.exitStatus; } }, abort: false); mainMgaProject.Save(); return(retval); }
//[Fact] public void DesignSpaceWithRefs() { var fullTestPath = Path.Combine(_exportModelDirectory, "DesignSpaceWithRefs"); var xmeFilePath = Path.Combine(fullTestPath, "DesignSpaceWithRefs.xme"); var pathExportedDesign = Path.Combine(fullTestPath, "DesignSpace.adm"); // delete design file if it already exists if (File.Exists(pathExportedDesign)) { File.Delete(pathExportedDesign); } String connectionString; GME.MGA.MgaUtils.ImportXMEForTest(xmeFilePath, out connectionString); var proj = new MgaProject(); bool ro_mode; proj.Open(connectionString, out ro_mode); proj.EnableAutoAddOns(true); try { var designExporter = new CyPhyDesignExporter.CyPhyDesignExporterInterpreter(); designExporter.Initialize(proj); var mgaGateway = new MgaGateway(proj); MgaFCO currentFCO = null; mgaGateway.PerformInTransaction(delegate { currentFCO = proj.get_ObjectByPath("/@DesignSpaces|kind=DesignSpace|relpos=0/@DesignSpace|kind=DesignContainer|relpos=0") as MgaFCO; Assert.NotNull(currentFCO); }); var parameters = new CyPhyGUIs.InterpreterMainParameters() { CurrentFCO = currentFCO, Project = proj, OutputDirectory = fullTestPath }; var result = designExporter.Main(parameters); var design = avm.Design.LoadFromFile(pathExportedDesign); /// Add assert statements to check structure var root = design.RootContainer; Assert.NotNull(root); Assert.Equal(1, root.Container1.Count(c => c.Name == "AltContainer")); var AltContainer = root.Container1.First(c => c.Name == "AltContainer"); Assert.Equal(1, AltContainer.ComponentInstance.Count(ci => ci.Name == "CompA_5")); Assert.Equal(1, AltContainer.ComponentInstance.Count(ci => ci.Name == "CompA_6")); Assert.Equal(1, AltContainer.ComponentInstance.Count(ci => ci.Name == "CompA_7")); Assert.Equal(1, root.Container1.Count(c => c.Name == "DesignContainer")); var DesignContainer = root.Container1.First(c => c.Name == "DesignContainer"); Assert.Equal(1, DesignContainer.ComponentInstance.Count(ci => ci.Name == "CompA3")); Assert.Equal(1, DesignContainer.Container1.Count(c => c.Name == "Assembly1")); var Assembly1 = DesignContainer.Container1.First(c => c.Name == "Assembly1"); Assert.Equal(1, Assembly1.ComponentInstance.Count(ci => ci.Name == "CompA1")); Assert.Equal(1, Assembly1.ComponentInstance.Count(ci => ci.Name == "CompA2")); Assert.Equal(1, root.Container1.Count(c => c.Name == "ReusableContainer")); var ReusableContainer = root.Container1.First(c => c.Name == "ReusableContainer"); Assert.Equal(1, ReusableContainer.Container1.Count(c => c.Name == "Assembly4")); var Assembly4 = ReusableContainer.Container1.First(c => c.Name == "Assembly4"); Assert.Equal(1, Assembly4.ComponentInstance.Count(ci => ci.Name == "CompA1")); Assert.Equal(1, Assembly4.ComponentInstance.Count(ci => ci.Name == "CompA2")); Assert.Equal(1, ReusableContainer.ComponentInstance.Count(ci => ci.Name == "CompA4")); Assert.Equal(1, root.ComponentInstance.Count(ci => ci.Name == "CompA")); Assert.Equal(1, root.ComponentInstance.Count(ci => ci.Name == "CompA")); } finally { proj.Close(); } }
public void attributesAndRegistryCopy(IMgaFCO newCurrentObject, IMgaFCO otherCurrentObject) { // AT THIS POINT, INSTANCE FCO'S HAVEN'T YET MADE IT INTO THE _fcoMap. AS ALL FCO'S ARE TRAVERSED // TO SET ATTRIBUTE AND REGISTRY ENTRIES, A GOOD PLACE TO PUT THEM INTO THE _fcoMap IS HERE. if (!_fcoMap.ContainsKey(otherCurrentObject)) { _fcoMap[otherCurrentObject] = newCurrentObject; } foreach (IMgaReference otherReferencedBy in otherCurrentObject.ReferencedBy) { // THIS CHECK PREVENTS REFERENCES THAT ARE IN THE MERGED MODEL FROM BEING PROCESSED HERE (AND DESTROYED AS A RESULT) if (!_fcoMap.ContainsKey(otherReferencedBy)) { MgaObject otherReferencedByParent = null; GME.MGA.Meta.objtype_enum otherReferencedByParentObjTypeEnum; otherReferencedBy.GetParent(out otherReferencedByParent, out otherReferencedByParentObjTypeEnum); MgaObject newReferencedByParent = _currentMgaProject.get_ObjectByPath(otherReferencedByParent.AbsPath); if (newReferencedByParent == null) { gmeConsole.Error.WriteLine("Unable to make new reference in \"" + otherReferencedByParent.AbsPath + "\" to \"" + otherCurrentObject.AbsPath + "\": prospective parent path not found."); _exitStatus |= Errors.PathError; continue; } MgaObject newReferencedBy = newCurrentObject.Project.get_ObjectByPath(otherReferencedBy.AbsPath); if (newReferencedByParent == null) { gmeConsole.Error.WriteLine("Unable to redirect reference in \"" + otherReferencedByParent.AbsPath + "\" to \"" + otherCurrentObject.AbsPath + "\": reference not found"); _exitStatus |= Errors.PathError; continue; } GME.MGA.Meta.objtype_enum newReferencedByParentObjTypeEnum = newReferencedByParent.ObjType; /* * IMgaReference newReferencedBy = null; * if (newReferencedByParentObjTypeEnum == GME.MGA.Meta.objtype_enum.OBJTYPE_FOLDER) { * newReferencedBy = (newReferencedByParent as MgaFolder).CreateRootObject(otherReferencedBy.Meta) as IMgaReference; * } else if (newReferencedByParentObjTypeEnum == GME.MGA.Meta.objtype_enum.OBJTYPE_MODEL) { * newReferencedBy = (newReferencedByParent as MgaModel).CreateChildObject(otherReferencedBy.MetaRole) as IMgaReference; * } else { * gmeConsole.Error.WriteLine("Unable to make new reference in \"" + newReferencedByParent.AbsPath + "\" to \"" + newCurrentObject.AbsPath + "\": prospective parent neither a folder nor a model."); * _exitStatus |= Errors.PathError; * continue; * } * * newReferencedBy.Referred = newCurrentObject as MgaFCO; * // KMS this will be destroyed at the end * otherReferencedBy.DestroyObject(); */ ReferenceSwitcher.Switcher.MoveReferenceWithRefportConnections(newCurrentObject as MgaFCO, (MgaReference)newReferencedBy, (x, y, z) => { Console.WriteLine(x + " ; " + y + " ; " + z); }); //_fcoMap[otherReferencedBy] = newReferencedBy; } } // ATTRIBUTES MgaAttributes otherMgaAttributes = otherCurrentObject.Attributes; foreach (MgaAttribute otherMgaAttribute in otherMgaAttributes) { //IMgaAttribute newMgaAttribute = newCurrentObject.get_Attribute( otherMgaAttribute.Meta ); // THROWS 0x87650080 ERROR --- E_META_INVALIDATTR IMgaAttribute newMgaAttribute = newCurrentObject.Attributes.Cast <IMgaAttribute>().FirstOrDefault(x => x.Meta.Name == otherMgaAttribute.Meta.Name); if (newMgaAttribute.Value != otherMgaAttribute.Value || otherMgaAttribute.Status == (int)GME.MGA.attstatus_enum.ATTSTATUS_HERE) { newMgaAttribute.Value = otherMgaAttribute.Value; } } // REGISTRY copyRegistry(newCurrentObject, otherCurrentObject); // RECURSE if (otherCurrentObject.ObjType != GME.MGA.Meta.objtype_enum.OBJTYPE_MODEL) { return; } foreach (MgaObject otherChildMgaObject in otherCurrentObject.ChildObjects) { if (otherChildMgaObject.MetaBase.ObjType == GME.MGA.Meta.objtype_enum.OBJTYPE_CONNECTION) { continue; } IMgaFCO otherChildIMgaFCO = otherChildMgaObject as IMgaFCO; IMgaFCO newChildIMgaFCO = null; if (_fcoMap.ContainsKey(otherChildIMgaFCO)) { newChildIMgaFCO = _fcoMap[otherChildIMgaFCO]; } else { IEnumerable <MgaFCO> newChildMgaFCOs = (newCurrentObject as MgaModel).ChildObjects.Cast <MgaFCO>().Where(x => x.Name == otherChildIMgaFCO.Name); if (newChildMgaFCOs.Count() > 1) { gmeConsole.Error.WriteLine("Error: FCO of path \"" + newCurrentObject.AbsPath + "\" has more than one child named \"" + otherChildIMgaFCO.Name + "\" -- cannot discern objects, skipping"); _exitStatus |= Errors.DuplicateChildNameError; continue; } else if (newChildMgaFCOs.Count() == 0) { gmeConsole.Error.WriteLine("Error: FCO of path \"" + newCurrentObject.AbsPath + "\" has no child named \"" + otherChildIMgaFCO.Name + "\", but corresponding object in file does. This shouldn't happen, skipping"); _exitStatus |= Errors.MissingChildError; continue; } newChildIMgaFCO = newChildMgaFCOs.First(); _fcoMap[otherChildIMgaFCO] = newChildIMgaFCO; } attributesAndRegistryCopy(newChildIMgaFCO, otherChildIMgaFCO); } }
public void merge(MgaFCO currentObject, string filename) { _currentMgaProject = currentObject.Project; MgaProject mgaProject = GetProject(filename, currentObject.Project.MetaName); if (mgaProject == null) { return; } try { _projectFilename = filename; MgaGateway mgaGateway = new MgaGateway(mgaProject); mgaGateway.PerformInTransaction(delegate { // "DO" LOOP IS ONLY TO ALLOW "break" TO TERMINATE THIS INTERPRETER do { int origPrefs = _currentMgaProject.Preferences; // Magic word allows us to remove ConnPoints _currentMgaProject.Preferences = origPrefs | (int)GME.MGA.preference_flags.MGAPREF_IGNORECONNCHECKS | (int)GME.MGA.preference_flags.MGAPREF_FREEINSTANCEREFS; try { // GET FCO TO BE MERGED FROM OTHER MGA FILE IMgaFCO otherCurrentObject = mgaProject.get_ObjectByPath(currentObject.AbsPath) as IMgaFCO; if (otherCurrentObject == null) { gmeConsole.Error.WriteLine("Could not perform merge: could not find object of path \"" + currentObject.AbsPath + "\" in file \"" + filename + "\""); _exitStatus |= Errors.PathError; break; } recordConnections(otherCurrentObject); // GET PARENT (IN CURRENT MODEL) OF THE FCO TO BE MERGED INTO THE CURRENT MODEL MgaObject currentParentMGAObject = null; GME.MGA.Meta.objtype_enum currentParentObjTypeEnum; currentObject.GetParent(out currentParentMGAObject, out currentParentObjTypeEnum); // THE ROOT OF THE MERGED FCO IMgaFCO newCurrentObject = null; IMgaFCO otherArchetype = otherCurrentObject.ArcheType; if (otherArchetype != null) { MgaFCO newArchetype = mgaProject.get_ObjectByPath(otherArchetype.AbsPath) as MgaFCO; if (newArchetype == null) { gmeConsole.Error.WriteLine("Could not find object of path \"" + otherArchetype.AbsPath + "\" (archetype of \"" + otherCurrentObject.AbsPath + "\" in file \"" + filename + "\") in current model."); _exitStatus |= Errors.PathError; break; } if (currentParentObjTypeEnum == GME.MGA.Meta.objtype_enum.OBJTYPE_FOLDER) { newCurrentObject = (currentParentMGAObject as MgaFolder).DeriveRootObject(newArchetype, otherCurrentObject.IsInstance); } else if (currentParentObjTypeEnum == GME.MGA.Meta.objtype_enum.OBJTYPE_MODEL) { newCurrentObject = (currentParentMGAObject as MgaModel).DeriveChildObject(newArchetype, otherCurrentObject.MetaRole, otherCurrentObject.IsInstance); } else { gmeConsole.Error.WriteLine("Unable to merge \"" + otherCurrentObject.AbsPath + "\" of file \"" + filename + "\": prospective parent neither a folder nor a model."); _exitStatus |= Errors.GMEError; break; } attributesAndRegistryCopy(newCurrentObject, otherCurrentObject); connectionCopy(); } else { if (currentParentObjTypeEnum == GME.MGA.Meta.objtype_enum.OBJTYPE_FOLDER) { newCurrentObject = (currentParentMGAObject as MgaFolder).CreateRootObject(currentObject.Meta); } else if (currentParentObjTypeEnum == GME.MGA.Meta.objtype_enum.OBJTYPE_MODEL) { newCurrentObject = (currentParentMGAObject as MgaModel).CreateChildObject(currentObject.MetaRole); } else { gmeConsole.Error.WriteLine("Unable to merge \"" + otherCurrentObject.AbsPath + "\" of file \"" + filename + "\": prospective parent neither a folder nor a model."); _exitStatus |= Errors.GMEError; break; } subTreeCopy(newCurrentObject, otherCurrentObject); instanceCopy(); attributesAndRegistryCopy(newCurrentObject, otherCurrentObject); referenceCopy(); connectionCopy(); //referenceConnectionCopy(); } } finally { _currentMgaProject.Preferences = origPrefs; } } while (false); }, transactiontype_enum.TRANSACTION_NON_NESTED, abort: false); currentObject.DestroyObject(); } finally { mgaProject.Close(true); } }