Beispiel #1
0
        private async Task SaveTree()
        {
            try
            {
                ElementsTree tree = null;

                try
                {
                    tree         = ReadDataFromDocument(App.IActiveDoc2);
                    tree.Version = tree.Version + 1;
                }
                catch (ThirdPartyStreamNotFoundException)
                {
                    //create new tree only if stream was never created, show an error otherwise
                    tree = new ElementsTree(1,
                                            new Element(1, "Root",
                                                        new Element(2, "Level1-A",
                                                                    new Element(4, "Level2")),
                                                        new Element(5, "Level1-B")));
                }

                await SaveDataToDocument(App.IActiveDoc2, tree);

                App.SendMsgToUser2("Data saved",
                                   (int)swMessageBoxIcon_e.swMbInformation,
                                   (int)swMessageBoxBtn_e.swMbOk);
            }
            catch (Exception ex)
            {
                App.SendMsgToUser2(ex.Message,
                                   (int)swMessageBoxIcon_e.swMbStop,
                                   (int)swMessageBoxBtn_e.swMbOk);
            }
        }
Beispiel #2
0
        private async Task SaveDataToDocument(IModelDoc2 model, ElementsTree data)
        {
            int err  = -1;
            int warn = -1;

            model.SetSaveFlag();

            const int S_OK = 0;

            bool?result = null;;

            var onSaveToStorageNotifyFunc = new Func <int>(() =>
            {
                try
                {
                    StoreData(model, data, STREAM_NAME);
                    result = true;
                }
                catch
                {
                    result = false;
                }
                return(S_OK);
            });

            var partSaveToStorageNotify = new DPartDocEvents_SaveToStorageNotifyEventHandler(onSaveToStorageNotifyFunc);
            var assmSaveToStorageNotify = new DAssemblyDocEvents_SaveToStorageNotifyEventHandler(onSaveToStorageNotifyFunc);
            var drwSaveToStorageNotify  = new DDrawingDocEvents_SaveToStorageNotifyEventHandler(onSaveToStorageNotifyFunc);

            #region Attach Event Handlers

            switch ((swDocumentTypes_e)model.GetType())
            {
            case swDocumentTypes_e.swDocPART:
                (model as PartDoc).SaveToStorageNotify += partSaveToStorageNotify;
                break;

            case swDocumentTypes_e.swDocASSEMBLY:
                (model as AssemblyDoc).SaveToStorageNotify += assmSaveToStorageNotify;
                break;

            case swDocumentTypes_e.swDocDRAWING:
                (model as DrawingDoc).SaveToStorageNotify += drwSaveToStorageNotify;
                break;
            }

            #endregion

            if (!model.Save3((int)swSaveAsOptions_e.swSaveAsOptions_Silent, ref err, ref warn))
            {
                throw new InvalidOperationException($"Failed to save the model: {(swFileSaveError_e)err}");
            }

            await Task.Run(() =>
            {
                while (!result.HasValue)
                {
                    System.Threading.Thread.Sleep(10);
                }
            });

            #region Detach Event Handlers

            switch ((swDocumentTypes_e)model.GetType())
            {
            case swDocumentTypes_e.swDocPART:
                (model as PartDoc).SaveToStorageNotify -= partSaveToStorageNotify;
                break;

            case swDocumentTypes_e.swDocASSEMBLY:
                (model as AssemblyDoc).SaveToStorageNotify -= assmSaveToStorageNotify;
                break;

            case swDocumentTypes_e.swDocDRAWING:
                (model as DrawingDoc).SaveToStorageNotify -= drwSaveToStorageNotify;
                break;
            }

            #endregion

            if (!result.Value)
            {
                throw new Exception("Failed to store the data");
            }
        }