private void CreateImportItem(List <ImportItem> importItems)
        {
            BMECInstanceManager instanceManager = BMECApi.Instance.InstanceManager;

            foreach (ImportItem importItem in importItems)
            {
                if (string.IsNullOrEmpty(importItem.Key) || !importItem.Valid)
                {
                    //log-- this means there are blank records
                    continue;
                }
                System.Windows.Forms.Application.DoEvents();
                ArrayList associations   = new ArrayList();
                ArrayList propertyValues = new ArrayList();

                //use the instance to set the properties
                foreach (IECPropertyValue propertyValue in importItem.Instance)
                {
                    if (propertyValue.IsNull)
                    {
                        continue;
                    }
                    if (propertyValue.StringValue == null)
                    {
                        continue;
                    }
                    string pval = string.Format("{0}={1}", propertyValue.AccessString, propertyValue.StringValue);
                    propertyValues.Add(pval);
                }

                foreach (string rItem in importItem.RelationClassValueCache.Keys)
                {
                    string relatedVal = string.Format("{0}={1}", rItem, importItem.RelationClassValueCache[rItem]);
                    associations.Add(relatedVal);
                }
                string clsName = importItem.Instance.ClassDefinition.Name;
                if (associations.Count > 0)
                {
                    WorkspaceUtilities.SaveSettings("Associations." + clsName, associations);
                }

                IECInstance ecInstance = instanceManager.CreateECInstance(clsName);
                BMECApi.Instance.SpecProcessor.FillCurrentPreferences(ecInstance, true);
                ECInstanceDialog ecDlg = new ECInstanceDialog(importItem.Instance);
                ecDlg.InitializeWithCustomProperties(associations, propertyValues, true);
                ecDlg.SaveData(false);
            }
        }
        private IECInstance EditECInstance(IECInstance ecInst)
        {
            ECInstanceDialog ecDlg = new ECInstanceDialog(ecInst);

            ecDlg.DialogTitle = string.Format(LocalizableStrings.EditingInstanceTitle,
                                              ecInst.ClassDefinition.DisplayLabel, SchemaUtilities.GetDisplayNameForECInstance(ecInst, false));
            ecDlg.ShowDialog();
            //User didn't save the new instance
            if (!ecDlg.SavePressed)
            {
                return(null);
            }

            //We were not able to save the new instance
            if (!ecDlg.SaveData())
            {
                return(null);
            }

            return(ecDlg.GetECInstance());
        }
            /*------------------------------------------------------------------------------------**/
            /// <summary>
            /// Import
            /// </summary>
            /// <param name="unparsed"></param>
            /*--------------+---------------+---------------+---------------+---------------+------*/
            public static void Import
            (
                string unparsed
            )
            {
                if (string.IsNullOrEmpty(unparsed))
                {
                    WorkspaceUtilities.DisplayErrorMessage(LocalizableStrings.BadImportCommand, LocalizableStrings.ImportCommandFormat);
                    return;
                }

                BMECApi.Instance.ClearActivePlacementTool();  // exit current placement tool
                IECClass ecClass = SchemaUtilities.PlantSchema.GetClass(unparsed.Trim());

                if (ecClass == null)
                {
                    WorkspaceUtilities.DisplayErrorMessage(string.Format(LocalizableStrings.BadClassName, unparsed.Trim()), "");
                    return;
                }

                BMECInstanceManager instanceManager = BMECApi.Instance.InstanceManager;

                OpenFileDialog openFileDlg = new OpenFileDialog();

                openFileDlg.Multiselect = false;
                openFileDlg.Filter      = "Import data file|*.txt";
                openFileDlg.ShowDialog();
                if (string.IsNullOrEmpty(openFileDlg.FileName))
                {
                    return;
                }

                if (!IsImportFileValidFormat(openFileDlg.FileName, ecClass))
                {
                    return;
                }

                System.IO.StreamReader sr = System.IO.File.OpenText(openFileDlg.FileName);

                ArrayList associations   = new ArrayList();
                ArrayList propertyValues = new ArrayList();


                WaitDialog wtDlg = new WaitDialog();

                wtDlg.SetTitleString(LocalizableStrings.ImportingInstances);
                wtDlg.SetInformationSting(LocalizableStrings.ImportingInstancesInfo);
                wtDlg.Show();

                string lineRead = sr.ReadLine().Trim();

                while (!sr.EndOfStream)
                {
                    Application.DoEvents();
                    if (string.IsNullOrEmpty(lineRead))
                    {
                        lineRead = sr.ReadLine().Trim();
                        continue;
                    }
                    if (lineRead == "[Associations]")
                    {
                        associations.Clear();
                        while (!sr.EndOfStream)
                        {
                            lineRead = sr.ReadLine().Trim();
                            if (string.IsNullOrEmpty(lineRead))
                            {
                                continue;
                            }

                            if (lineRead == "[InstanceData]")
                            {
                                break;
                            }

                            associations.Add(lineRead);
                        }
                    }

                    if (lineRead == "[InstanceData]")
                    {
                        propertyValues.Clear();
                        while (!sr.EndOfStream)
                        {
                            lineRead = sr.ReadLine().Trim();
                            if (lineRead == "[InstanceData]")
                            {
                                break;
                            }

                            if (lineRead == "[Associations]")
                            {
                                break;
                            }

                            if (string.IsNullOrEmpty(lineRead))
                            {
                                continue;
                            }

                            propertyValues.Add(lineRead);
                        }

                        IECInstance ecInstance = instanceManager.CreateECInstance(ecClass.Name);
                        BMECApi.Instance.SpecProcessor.FillCurrentPreferences(ecInstance, true);
                        ecInstance.InstanceId = "mechanical";
                        ECInstanceDialog ecDlg = new ECInstanceDialog(ecInstance);
                        ecDlg.InitializeWithCustomProperties(associations, propertyValues, true);
                        ecDlg.SaveData();
                    }
                }
                wtDlg.Close();
                sr.Close();
            }
        private void OnCellMouseClick
        (
            object sender,
            DataGridViewCellMouseEventArgs e
        )
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            if (e.ColumnIndex == 1)
            {
                this.BeginEdit(true);
                ComboBox comboBox = (ComboBox)this.EditingControl;
                if (null != comboBox)
                {
                    comboBox.DroppedDown = true;
                }
            }

            if (e.ColumnIndex < AddButtonColumn)
            {
                return;
            }

            DgnUtilities dgnUtil = DgnUtilities.GetInstance();
            IECClass     ecClass = getECClassInformationForCurrentSelection(e.RowIndex);
            //this[ClassColumn, e.RowIndex].Tag as IECClass;
            IECRelationshipClass ecRelClass = getECRelationshipClassInformationForCurrentSelection(e.RowIndex);
            //this[ComboBoxColumn, e.RowIndex].Tag as IECRelationshipClass;
            DataGridViewComboBoxCell itemInstanceCell = this[s_WBSInstanceKey, e.RowIndex] as DataGridViewComboBoxCell;

            string strVal = this[s_WBSInstanceKey, e.RowIndex].Value as string;

            if (strVal == LocalizableStrings.None && (e.ColumnIndex != AddButtonColumn && e.ColumnIndex != BrowseButtonColumn))
            {
                this[e.ColumnIndex, e.RowIndex].Selected = false;
                return;
            }

            //ADD///////////////////////////////////////////////////////////////////////////////////////////////////////////
            if (e.ColumnIndex == AddButtonColumn)
            {
                // If we are connected to iModelHub we should acquire a a new tag for WBS

                ECInstanceDialog ecDlg = new ECInstanceDialog(ecClass.Name);
                ecDlg.ShowDialog();
                //User didn't save the new instance
                if (!ecDlg.SavePressed)
                {
                    return;
                }
                //We were not able to save the new instance
                if (!ecDlg.SaveData())
                {
                    return;
                }

                RefillItems(e.RowIndex, ecClass.Name);

                if (itemInstanceCell.Items.Count > 0)
                {
                    itemInstanceCell.Value = SchemaUtilities.GetDisplayNameForECInstance(ecDlg.GetECInstance(), false);
                }
            }
            //EDIT//////////////////////////////////////////////////////////////////////////////////////////////////////////
            else if (e.ColumnIndex == EditButtonColumn)
            {
                IECInstance ecInst = this[e.ColumnIndex, e.RowIndex].Tag as IECInstance;
                if (ecInst == null)
                {
                    MessageBox.Show(LocalizableStrings.NoEditDesc, LocalizableStrings.NoEditTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else
                {
                    IECInstance ecInstance = EditECInstance(ecInst);
                    if (ecInstance == null)
                    {
                        return;
                    }

                    RefillItems(e.RowIndex, ecClass.Name);

                    if (itemInstanceCell.Items.Count > 0)
                    {
                        itemInstanceCell.Value = SchemaUtilities.GetDisplayNameForECInstance(ecInstance, false);
                    }

                    _bInstanceModified = true;
                }
            }
            //DELETE////////////////////////////////////////////////////////////////////////////////////////////////////////
            else if (e.ColumnIndex == DeleteButtonColumn)
            {
                IECInstance ecInst = this[e.ColumnIndex, e.RowIndex].Tag as IECInstance;
                if (ecInst == null)
                {
                    MessageBox.Show(LocalizableStrings.NoDeleteDescRefOut, LocalizableStrings.NoDeleteTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else
                {
                    //.GetRelationshipInstances (ecClass.Name, _instance.ClassDefinition.Name, ecRelClass.Name, dgnUtil.GetDGNConnection());
                    ECInstanceList ecList = dgnUtil.GetRelatedInstances(ecInst, ecRelClass, _ClassDef, dgnUtil.GetDGNConnection());
                    if (ecList.Count > 0)
                    {
                        MessageBox.Show(string.Format(LocalizableStrings.NoDeleteDescAssocCompFound, itemInstanceCell.Value), LocalizableStrings.NoDeleteTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }

                    if (MessageBox.Show(String.Format(LocalizableStrings.AreYouSureDelete, SchemaUtilities.GetDisplayNameForECInstance(ecInst, true)),
                                        String.Format(LocalizableStrings.DeleteInstanceTitle, ecInst.ClassDefinition.DisplayLabel), MessageBoxButtons.YesNo, MessageBoxIcon.Stop) == DialogResult.No)
                    {
                        return;
                    }

                    dgnUtil.DeleteECInstanceFromDgn(ecInst, dgnUtil.GetDGNConnection());

                    RefillItems(e.RowIndex, ecClass.Name);

                    if (itemInstanceCell.Items.Count > 0)
                    {
                        itemInstanceCell.Value = itemInstanceCell.Items[0];
                    }
                    else
                    {
                        itemInstanceCell.Value = null;
                    }
                }
                _bInstanceModified = true;
            }
            //BROWSE////////////////////////////////////////////////////////////////////////////////////////////////////////
            else if (e.ColumnIndex == BrowseButtonColumn)
            {
                IECInstance newEcinstance = ecClass.CreateInstance() as IECInstance;
                if (newEcinstance == null)
                {
                    return;
                }

                object instance = dgnUtil.TagBrowser(newEcinstance, dgnUtil.AssoicatedItemsTagBrowserOverrides);
                if (instance == null || !(instance is IECInstance))
                {
                    return;
                }
                IECInstance selectedECInstance = instance as IECInstance;

                //CommonTools.BIMAdapter.BIMAdapter.ReleaseTag (newEcinstance);
                dgnUtil.CopyProperties(selectedECInstance, newEcinstance, false);
                //BusinessKeyUtility.Release (newEcinstance);
                BusinessKeyUtility.SetIsReserved(newEcinstance, true);
                dgnUtil.UpdateFunctionalGuidProperty(selectedECInstance, newEcinstance);
                dgnUtil.WriteECInstanceToDgn(newEcinstance, dgnUtil.GetDGNConnection());

                //CommonTools.BIMAdapter
                //IECInstance inst = EditECInstance (ecInstance);
                //if(inst == null)
                //    return;
                //    inst = ecInstance;

                RefillItems(e.RowIndex, ecClass.Name);

                if (itemInstanceCell.Items.Count > 0)
                {
                    itemInstanceCell.Value = SchemaUtilities.GetDisplayNameForECInstance(newEcinstance, false);
                }

                _bInstanceModified = true;
            }
            this[e.ColumnIndex, e.RowIndex].Selected = false;
        }