public void Main(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, ComponentStartMode startMode) { GMEConsole.Clear(); System.Windows.Forms.Application.DoEvents(); IMgaFCO selectedObj = null; bool isContextValid = CheckForValidContext(currentobj); if (isContextValid == false) { return; } // get all the ComponentRefs in the DesignContainer var crefs = (currentobj as IMgaModel). ChildFCOs. Cast<IMgaFCO>(). Where(x => x.MetaBase.Name == "ComponentRef"); // If there is exactly '1', select it if (crefs.Count() == 1) { selectedObj = crefs.FirstOrDefault(); } if (selectedObj == null) { if (selectedobjs.Count != 1) { GMEConsole.Error.WriteLine(SelectedObjErrorMessage); return; } selectedObj = selectedobjs.Cast<IMgaFCO>().FirstOrDefault(); } if (selectedObj == null) { GMEConsole.Error.WriteLine(SelectedObjErrorMessage); return; } if (selectedObj.MetaBase.Name != "ComponentRef") { GMEConsole.Error.WriteLine(SelectedObjErrorMessage); return; } if ((selectedObj as IMgaReference).Referred == null) { GMEConsole.Error.WriteLine("Selected ComponentRef is a null reference."); return; } GMEConsole.Info.WriteLine("Running CLM_light..."); // everything is checked we can operate on the objects IMgaFCO designContainer = currentobj; IMgaFCO componentRef = selectedObj; IMgaModel component = (selectedObj as IMgaReference).Referred as IMgaModel; GMEConsole.Info.WriteLine("Discovering components."); //GMEConsole.Info.WriteLine("{0} components were found.", components.Count); // TODO: filter detected components if needed using (ComponentSelectionForm csf = new ComponentSelectionForm(component, currentobj as IMgaModel, GMEConsole.Error, GMEConsole.Info)) { var dialogresult = csf.ShowDialog(); if (dialogresult == System.Windows.Forms.DialogResult.OK) { GMEConsole.Info.WriteLine("Inserting new components."); List<IMgaFCO> components = new List<IMgaFCO>(); foreach (var item in csf.dgvSelector.SelectedRows) { var dgvr = item as System.Windows.Forms.DataGridViewRow; var cli = dgvr.DataBoundItem as ComponentListItem; components.Add(cli.Fco); } GMEConsole.Info.WriteLine("{0} components were selected.", components.Count); List<KeyValuePair<IMgaFCO, string>> messages = new List<KeyValuePair<IMgaFCO, string>>(); var insertedComponents = InsertComponents(designContainer, componentRef, components, messages); } } GMEConsole.Info.WriteLine("Done."); }
/** * First we disconnect all connections to refports, then move the reference, then reconnect */ public static void MoveReferenceWithRefportConnections(IMgaFCO fco2, IMgaReference origref, WriteLineF WriteLine) { Queue <IMgaReference> references = new Queue <IMgaReference>(); references.Enqueue(origref); IMgaFCO targetModel = fco2; while (targetModel is IMgaReference) { targetModel = ((IMgaReference)targetModel).Referred; } MgaFCOs fco2ChildFCOs = ((IMgaModel)targetModel).ChildFCOs; Dictionary <string, IMgaFCO> newRefeChildren = GetNameMap(fco2ChildFCOs, x => { }); // TODO: warn, but only for refport-connected children //GMEConsole.Warning.WriteLine("Warning: " + fco2.Name + " has multiple children named " + x)); int origPrefs = fco2.Project.Preferences; // Magic word allows us to remove ConnPoints fco2.Project.Preferences = origPrefs | (int)GME.MGA.preference_flags.MGAPREF_IGNORECONNCHECKS | (int)GME.MGA.preference_flags.MGAPREF_FREEINSTANCEREFS; try { MgaConnection conn = null; var ReconnectList = MakeList(new { ConnRole = "src", Ref = origref, Port = fco2, Conn = conn }); while (references.Count != 0) { IMgaReference refe = references.Dequeue(); foreach (IMgaConnPoint connPoint in refe.UsedByConns) { if (connPoint.References[1] != refe) { continue; } IMgaFCO fco2Port; if (newRefeChildren.TryGetValue(connPoint.Target.Name, out fco2Port)) { if (fco2Port == null) { // fco2Port == null => multiple children with the same name // Try matching based on Kind too fco2Port = fco2ChildFCOs.Cast <IMgaFCO>().Where(x => x.Name == connPoint.Target.Name && x.Meta.MetaRef == connPoint.Target.Meta.MetaRef).FirstOrDefault(); } if (fco2Port != null) { ReconnectList.Add(new { ConnRole = connPoint.ConnRole, Ref = refe, Port = fco2Port, Conn = connPoint.Owner }); connPoint.Remove(); } } else { WriteLine((x, y) => "Can't find corresponding port for " + x + " in " + y, connPoint.Target, fco2); connPoint.Owner.DestroyObject(); } } foreach (IMgaReference x in refe.ReferencedBy.Cast <IMgaReference>()) { if (x.ID == origref.ID) { throw new Exception("Circular reference chain starting with " + origref.AbsPath); } references.Enqueue(x); } } origref.Referred = (MgaFCO)fco2; foreach (var reconnect in ReconnectList) { if (reconnect.ConnRole == "src") { (reconnect.Conn as IMgaSimpleConnection).SetSrc((MgaFCOs)GetRefChain(reconnect.Ref), (MgaFCO)reconnect.Port); } else { (reconnect.Conn as IMgaSimpleConnection).SetDst((MgaFCOs)GetRefChain(reconnect.Ref), (MgaFCO)reconnect.Port); } } } finally { fco2.Project.Preferences = origPrefs; } }
public void Main(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, ComponentStartMode startMode) { GMEConsole.Clear(); System.Windows.Forms.Application.DoEvents(); IMgaFCO selectedObj = null; bool isContextValid = CheckForValidContext(currentobj); if (isContextValid == false) { return; } // get all the ComponentRefs in the DesignContainer var crefs = (currentobj as IMgaModel). ChildFCOs. Cast <IMgaFCO>(). Where(x => x.MetaBase.Name == "ComponentRef"); // If there is exactly '1', select it if (crefs.Count() == 1) { selectedObj = crefs.FirstOrDefault(); } if (selectedObj == null) { if (selectedobjs.Count != 1) { GMEConsole.Error.WriteLine(SelectedObjErrorMessage); return; } selectedObj = selectedobjs.Cast <IMgaFCO>().FirstOrDefault(); } if (selectedObj == null) { GMEConsole.Error.WriteLine(SelectedObjErrorMessage); return; } if (selectedObj.MetaBase.Name != "ComponentRef") { GMEConsole.Error.WriteLine(SelectedObjErrorMessage); return; } if ((selectedObj as IMgaReference).Referred == null) { GMEConsole.Error.WriteLine("Selected ComponentRef is a null reference."); return; } GMEConsole.Info.WriteLine("Running CLM_light..."); // everything is checked we can operate on the objects IMgaFCO designContainer = currentobj; IMgaFCO componentRef = selectedObj; IMgaModel component = (selectedObj as IMgaReference).Referred as IMgaModel; GMEConsole.Info.WriteLine("Discovering components."); //GMEConsole.Info.WriteLine("{0} components were found.", components.Count); // TODO: filter detected components if needed using (ComponentSelectionForm csf = new ComponentSelectionForm(component, currentobj as IMgaModel, GMEConsole.Error, GMEConsole.Info)) { var dialogresult = csf.ShowDialog(); if (dialogresult == System.Windows.Forms.DialogResult.OK) { GMEConsole.Info.WriteLine("Inserting new components."); List <IMgaFCO> components = new List <IMgaFCO>(); foreach (var item in csf.dgvSelector.SelectedRows) { var dgvr = item as System.Windows.Forms.DataGridViewRow; var cli = dgvr.DataBoundItem as ComponentListItem; components.Add(cli.Fco); } GMEConsole.Info.WriteLine("{0} components were selected.", components.Count); List <KeyValuePair <IMgaFCO, string> > messages = new List <KeyValuePair <IMgaFCO, string> >(); var insertedComponents = InsertComponents(designContainer, componentRef, components, messages); } } GMEConsole.Info.WriteLine("Done."); }