private void btOk_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text == "")
            {
                MessageBox.Show("The field \"Name\" must be filled.", "Field is not filled", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            if (cbCategory.SelectedIndex == -1)
            {
                MessageBox.Show("You must select one category.", "Category not selected", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            if (objIdColl.Count == 0)
            {
                MessageBox.Show("Select at least one object.", "Objects not selected", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            //Check ambiguous block name
            if (_objBlockModOld.Name != _objBlockModNew.Name)
            {
                using (Database dbRef = new Database(false, true))
                {
                    //Read dbRef
                    dbRef.ReadDwgFile(ArCaRefMgrController.DBPath, FileShare.ReadWrite, true, "");
                    using (Transaction trRef = dbRef.TransactionManager.StartTransaction())
                    {
                        BlockTable       btRef = trRef.GetObject(dbRef.BlockTableId, OpenMode.ForRead) as BlockTable;
                        BlockTableRecord btr;
                        foreach (ObjectId objId in btRef)
                        {
                            btr = trRef.GetObject(objId, OpenMode.ForRead) as BlockTableRecord;
                            if (btr.Name == _objBlockModNew.Name)
                            {
                                MessageBox.Show("The block called \"" + _objBlockModNew.Name + "\"  already exists in the library block. Choose another name.", "Invalid Name", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                                trRef.Abort();
                                return;
                            }
                        }
                        trRef.Abort();
                    }
                }
            }

            Database dbLocal = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;

            Point3d basePoint = new Point3d(Convert.ToDouble(tbX.Text), Convert.ToDouble(tbY.Text), Convert.ToDouble(tbZ.Text));

            using (dbLocal)
            {
                //-----------------Add Mode--------------
                if (_IsAddMode)
                {
                    //-----------Selection Block Mode------------
                    if (_SelectionIsABlock)
                    {
                        //Start Transaction to change BlockTableRecord
                        using (Transaction trLocal = dbLocal.TransactionManager.StartTransaction())
                        {
                            Document Doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                            using (DocumentLock dl = Doc.LockDocument())
                            {
                                BlockTable       bt      = trLocal.GetObject(dbLocal.BlockTableId, OpenMode.ForRead) as BlockTable;
                                BlockTableRecord BlocRec = trLocal.GetObject(bt[_objBlockModNew.Name], OpenMode.ForWrite) as BlockTableRecord;
                                //Change PreviewIcon
                                BlocRec.PreviewIcon = bmpPreview;
                            }
                            trLocal.Commit();
                        }
                        using (Database dbRef = new Database(false, true))
                        {
                            //Read dbRef
                            dbRef.ReadDwgFile(ArCaRefMgrController.DBPath, FileShare.ReadWrite, true, "");
                            // Copy blocks from source to destination database
                            IdMapping mapping = new IdMapping();
                            dbLocal.WblockCloneObjects(objIdColl, dbRef.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                            //Save dbRef
                            dbRef.SaveAs(ArCaRefMgrController.DBPath, DwgVersion.Current);
                        }
                    }
                    //------------------Selection Objects Mode------------------
                    else
                    {
                        using (Database dbAux = new Database())
                        {
                            //Create dbAux (block)
                            dbLocal.Wblock(dbAux, objIdColl, basePoint, DuplicateRecordCloning.Ignore);
                            using (Database dbRef = new Database(false, true))
                            {
                                //Read dbRef
                                dbRef.ReadDwgFile(ArCaRefMgrController.DBPath, FileShare.ReadWrite, true, "");
                                //Insert dbAuc (Block) in dbRef
                                ObjectId obj = dbRef.Insert(_objBlockModNew.Name, dbAux, true);
                                //Start Transaction to change BlockTableRecord
                                using (Transaction trRef = dbRef.TransactionManager.StartTransaction())
                                {
                                    BlockTableRecord BlocRec = trRef.GetObject(obj, OpenMode.ForWrite) as BlockTableRecord;
                                    //Change PreviewIcon
                                    BlocRec.PreviewIcon = bmpPreview;
                                }
                                //Save dbRef
                                dbRef.SaveAs(ArCaRefMgrController.DBPath, DwgVersion.Current);
                            }
                        }
                    }
                }
                //----------------Edit Mode----------------
                else
                {
                    //-------------Selection was not changed-------------
                    if (_bDbIsInExternalDWG)
                    {
                        using (Database dbRef = new Database(false, true))
                        {
                            //Read dbRef
                            dbRef.ReadDwgFile(ArCaRefMgrController.DBPath, FileShare.ReadWrite, true, "");
                            //Start Transaction to change BlockTableRecord
                            using (Transaction trRef = dbRef.TransactionManager.StartTransaction())
                            {
                                BlockTable       btRef   = trRef.GetObject(dbRef.BlockTableId, OpenMode.ForRead) as BlockTable;
                                BlockTableRecord BlocRec = trRef.GetObject(btRef[_objBlockModOld.Name], OpenMode.ForWrite) as BlockTableRecord;
                                //Change Origin
                                BlocRec.Origin = basePoint;
                                //Chage Name
                                BlocRec.Name = _objBlockModNew.Name;
                                trRef.Commit();
                            }
                            //Save dbRef
                            dbRef.SaveAs(ArCaRefMgrController.DBPath, DwgVersion.Current);
                        }
                    }
                    //----------------Selection was changed------------------
                    else
                    {
                        using (Database dbAux = new Database())
                        {
                            //Create dbAux (block)
                            dbLocal.Wblock(dbAux, objIdColl, basePoint, DuplicateRecordCloning.Ignore);
                            using (Database dbRef = new Database(false, true))
                            {
                                //Read dbRef
                                dbRef.ReadDwgFile(ArCaRefMgrController.DBPath, FileShare.ReadWrite, true, "");
                                //*************Delete Old Block************
                                //Start Transaction to delet BlockTableRecord
                                using (Transaction trRef = dbRef.TransactionManager.StartTransaction())
                                {
                                    BlockTable       btRef   = trRef.GetObject(dbRef.BlockTableId, OpenMode.ForRead) as BlockTable;
                                    BlockTableRecord BlocRec = trRef.GetObject(btRef[_objBlockModOld.Name], OpenMode.ForWrite) as BlockTableRecord;
                                    //Delet BlockTableRecord
                                    BlocRec.Erase();
                                    trRef.Commit();
                                }
                                //************Insert New Block**************
                                //-----------Selection Block Mode------------
                                if (_SelectionIsABlock)
                                {
                                    //Start Transaction to change BlockTableRecord
                                    using (Transaction trLocal = dbLocal.TransactionManager.StartTransaction())
                                    {
                                        Document Doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                                        using (DocumentLock dl = Doc.LockDocument())
                                        {
                                            BlockTable       btRef   = trLocal.GetObject(dbLocal.BlockTableId, OpenMode.ForRead) as BlockTable;
                                            BlockTableRecord BlocRec = trLocal.GetObject(btRef[_objBlockModNew.Name], OpenMode.ForWrite) as BlockTableRecord;
                                            //Change PreviewIcon
                                            BlocRec.PreviewIcon = bmpPreview;
                                        }
                                        trLocal.Commit();
                                    }

                                    // Copy blocks from source to destination database
                                    IdMapping mapping = new IdMapping();
                                    dbLocal.WblockCloneObjects(objIdColl, dbRef.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                                    //Save dbRef
                                    dbRef.SaveAs(ArCaRefMgrController.DBPath, DwgVersion.Current);
                                }
                                //--------------Selection Objects Mode--------------
                                else
                                {
                                    //Insert dbAuc (Block) in dbRef
                                    ObjectId obj = dbRef.Insert(_objBlockModNew.Name, dbAux, true);
                                    //Start Transaction to change BlockTableRecord
                                    using (Transaction trRef = dbRef.TransactionManager.StartTransaction())
                                    {
                                        BlockTableRecord BlocRec = trRef.GetObject(obj, OpenMode.ForWrite) as BlockTableRecord;
                                        //Change PreviewIcon
                                        BlocRec.PreviewIcon = bmpPreview;
                                        //Change Origin
                                        BlocRec.Origin = basePoint;
                                        //Chage Name
                                        BlocRec.Name = _objBlockModNew.Name;
                                        trRef.Commit();
                                    }
                                }
                                //***********************************************
                                //Save dbRef
                                dbRef.SaveAs(ArCaRefMgrController.DBPath, DwgVersion.Current);
                            }
                        }
                    }
                }
            }

            var             lstFound      = ArCaRefMgrController.ObjMain.LstGroup.Where(objTag => objTag.Name.Equals((cbCategory.SelectedValue as BlockGroupModel).Name));
            BlockGroupModel objBlockGroup = lstFound.First();

            //Remove in all Lists
            bool IsInserted = false;

            foreach (BlockGroupModel objIt in ArCaRefMgrController.ObjMain.LstGroup)
            {
                if (objIt.LstBlock.Contains(_objBlockModOld))
                {
                    int position = objIt.LstBlock.IndexOf(_objBlockModOld);
                    objIt.LstBlock.Remove(_objBlockModOld);
                    if (objBlockGroup == objIt)
                    {
                        //Insert if is in the same group
                        objBlockGroup.LstBlock.Insert(position, _objBlockModNew);
                        IsInserted = true;
                    }
                }
            }

            //If not inset, add in correct group
            if (!IsInserted)
            {
                objBlockGroup.LstBlock.Add(_objBlockModNew);
            }


            //Write XML
            ArCaRefMgrParser.WriteXML();

            this.Close();
        }