/*------------------------------------------------------------------------------------**/
            /// <summary>
            /// ReplaceComponents
            /// </summary>
            /// <param name="unparsed"></param>
            /*--------------+---------------+---------------+---------------+---------------+------*/
            public static void ReplaceComponents
            (
                string unparsed
            )
            {
                if (PipelineUtilities.IsReplaceDialogRunning)
                {
                    return;
                }

                BMECApi MechnanicalAPI = BMECApi.Instance;

                // Don't proceed for readonly files
                if (MechnanicalAPI.IsActiveModelReadOnly())
                {
                    MechnanicalAPI.SendToMessageCenterD(Bentley.DgnPlatformNET.OutputMessagePriority.Information, LocalizableStrings.DgnReadonly);
                    return;
                }

                ECInstanceList ecList = MechnanicalAPI.GetSelectedInstances();

                int count = 0;

                for (int i = 0; i < ecList.Count; i++)
                {
                    IECInstance ecInstanceTemp = ecList[i] as IECInstance;
                    if (ecInstanceTemp == null)
                    {
                        continue;
                    }

                    // Skip anything other than piping components or valve operators
                    if (!(SchemaUtilities.IsPipingComponent(ecInstanceTemp) || SchemaUtilities.IsComponentOfType(ecInstanceTemp, ClassNames.VALVE_OPERATING_DEVICE)))
                    {
                        continue;
                    }
                    //You can't replace fasteners
                    if (SchemaUtilities.IsComponentOfType(ecInstanceTemp, ClassNames.FASTENER))
                    {
                        continue;
                    }
                    //You can't replace seals
                    if (SchemaUtilities.IsComponentOfType(ecInstanceTemp, ClassNames.SEAL))
                    {
                        continue;
                    }

                    count++;
                }

                ECInstanceList nozzleList         = new ECInstanceList();
                bool           bHasFreeformNozzle = false;

                if (count == 0)
                {
                    for (int i = 0; i < ecList.Count; i++)
                    {
                        IECInstance ecInstanceTemp = ecList[i] as IECInstance;
                        if (ecInstanceTemp.ClassDefinition.Name == "NOZZLE")
                        {
                            IECPropertyValue propVal = ecInstanceTemp["TYPE_FOR_DATUM"];
                            if (propVal != null && propVal.StringValue == "Freeform")
                            {
                                bHasFreeformNozzle = true;
                                nozzleList.Add(ecInstanceTemp);
                            }
                        }
                    }
                }

                if (count == 0 && !bHasFreeformNozzle)
                {
                    MechnanicalAPI.SendToMessageCenterD(DgnPlatformNET.OutputMessagePriority.Information, LocalizableStrings.NoComponentsSelectedinDgn);
                    return;
                }

                for (int i = 0; i < ecList.Count; i++)
                {
                    DgnUtilities dgnUtil = DgnUtilities.GetInstance();
                    IECInstance  ecInst  = ecList[i];
                    if (dgnUtil.IsECInstanceFromReferenceFile(ecInst) || dgnUtil.IsECInstanceReferencedOut(ecInst))
                    {
                        MechnanicalAPI.SendToMessageCenterD(DgnPlatformNET.OutputMessagePriority.Information, LocalizableStrings.ReadonlyComps);
                        return;
                    }
                }

                if (s_ReplaceCompDialog == null)
                {
                    if (nozzleList.Count <= 0)
                    {
                        s_ReplaceCompDialog             = new DlgReplaceComponent(ecList, ReplaceDialogMode.SimpleReplace, true);
                        s_ReplaceCompDialog.FormClosed += new FormClosedEventHandler(ReplaceCompDialog_FormClosed);
                    }
                    else
                    {
                        s_ReplaceCompDialog             = new DlgReplaceComponent(nozzleList, ReplaceDialogMode.ChangeSpecSize, false);
                        s_ReplaceCompDialog.FormClosed += new FormClosedEventHandler(ReplaceCompDialog_FormClosed);
                    }
                }
                s_ReplaceCompDialog.Show();
            }
        private void OnCellValueChanged
        (
            object sender,
            DataGridViewCellEventArgs e
        )
        {
            if (e.ColumnIndex != 1)
            {
                return;
            }

            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            IECClass ecClass = getECClassInformationForCurrentSelection(e.RowIndex);
            //this[ClassColumn, e.RowIndex].Tag as IECClass;
            IECRelationshipClass ecRelClass = getECRelationshipClassInformationForCurrentSelection(e.RowIndex);
            //this[ClassColumn, e.RowIndex].Tag as IECRelationshipClass;
            string strVal = LocalizableStrings.None;

            if (null != this[ComboBoxColumn, e.RowIndex].Value)
            {
                strVal = this[ComboBoxColumn, e.RowIndex].Value.ToString();
            }

            if (strVal == _cstr_Varies)
            {
                return;
            }

            DgnUtilities dgnUtil = DgnUtilities.GetInstance();

            IECInstance newECIsnt = getInstanceForCurrentSelection(e.RowIndex, strVal);

            // get from Tag
            //dgnUtil.GetSourceInstance (ecClass, strVal);

            foreach (IECInstance ecInstance in _instList)
            {
                IECRelationshipInstanceCollection     ecRelCol      = ecInstance.GetRelationshipInstances();
                IEnumerator <IECRelationshipInstance> ecRelInstEnum = ecRelCol.GetEnumerator();

                bool newSourceSet = false;
                while (ecRelInstEnum.MoveNext())
                {
                    IECRelationshipInstance ecRelInst = ecRelInstEnum.Current;
                    if (!ecRelInst.ClassDefinition.Is(ecRelClass))
                    {
                        continue;
                    }

                    if (strVal == LocalizableStrings.None || string.IsNullOrEmpty(strVal))
                    {
                        ecRelCol.Remove(ecRelInst);
                    }
                    else
                    {
                        ecRelInst.Source = newECIsnt;
                    }
                    newSourceSet = true;
                    break;
                }

                if (!newSourceSet && newECIsnt != null)
                {
                    IECRelationshipInstance temporaryRelInst = ecRelClass.CreateInstance() as IECRelationshipInstance;
                    temporaryRelInst.Source = newECIsnt;
                    temporaryRelInst.Target = ecInstance;
                    ecInstance.GetRelationshipInstances().Add(temporaryRelInst);
                }

                IECInstance ecInst = newECIsnt;
                if (ecInst != null)
                {
                    if ((dgnUtil.IsECInstanceFromReferenceFile(ecInst) || dgnUtil.IsECInstanceReferencedOut(ecInst) || ecInst.IsReadOnly))
                    {
                        ecInst = null;
                    }
                }

                DataGridViewImageCell tempImageCell = this["optionEdit", e.RowIndex] as DataGridViewImageCell;
                tempImageCell.Tag = ecInst;

                tempImageCell     = this["optionDelete", e.RowIndex] as DataGridViewImageCell;
                tempImageCell.Tag = ecInst;

                //Mark the property (having business key CA/or NAME property if no Business key is defined) as changed and set the event which will reset the property pane.
                //This is required to update UNIT and SERVICE property listed in property pane on the fly whenever user changes the UNIT or SERVICE in ASSOCIATIONS pane.
                IECPropertyValue propVal = BusinessKeyUtility.GetPropertyValue(ecInstance);
                if (propVal != null)
                {
                    ecInstance.OnECPropertyValueChanged(propVal);
                }
            }

            SaveSettings();
        }