Ejemplo n.º 1
0
        bool openMotionData(string fileName)
        {
            if (fileName == null)
            {
                if (dialogOpenMotionData.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }
                fileName = dialogOpenMotionData.FileName;
            }
            string ext = Path.GetExtension(fileName);

            using (Stream stream = new FileStream(fileName, FileMode.Open)) {
                switch (ext)
                {
                case ".mdsx":
                case ".mdsb":
                    using (MotionDataHandler.Motion.Old.MotionDataSet newDataSet = new MotionDataHandler.Motion.Old.MotionDataSet()) {
                        // 古いデータをロード
                        WaitForForm waitForm2 = new WaitForForm(ctrl => {
                            newDataSet.ProgressChanged += ctrl.OnProgressChanged;
                            try {
                                switch (ext)
                                {
                                case ".mdsx":
                                    newDataSet.RetrieveXml(stream);
                                    break;

                                case ".mdsb":
                                    newDataSet.RetrieveBinary(stream);
                                    break;

                                default:
                                    System.Diagnostics.Debug.Fail("format naming error");
                                    break;
                                }
                                ctrl.DialogResult = DialogResult.OK;
                            } catch (Exception ex) {
                                ErrorLogger.Tell(ex, fileName + ": ファイルを読み込めませんでした");
                            } finally {
                                newDataSet.ProgressChanged -= ctrl.OnProgressChanged;
                            }
                        });
                        waitForm2.SetOperationTitle(fileName);
                        if (waitForm2.ShowDialog() == DialogResult.OK)
                        {
                            // データを変換
                            WaitForForm waitForm = new WaitForForm(ctrl => {
                                ctrl.OperationTitle = "Converting into New Data Format...";
                                _dataSet.FromOldVersion(newDataSet);
                                ctrl.DialogResult = DialogResult.OK;
                            }, () => new ProgressChangedEventArgs(_dataSet.ProgressPercentage, _dataSet.ProgressMessage));
                            if (waitForm.ShowDialog() == DialogResult.OK)
                            {
                                TimeController.Singleton.SetVisibleTime(TimeController.Singleton.BeginTime, TimeController.Singleton.EndTime);
                                setSaveMotionDataFileName(fileName);
                                _dataSet.DoObjectInfoSetChanged();
                                _dataSet.DoFrameListChanged();
                                _isOverWritable = false;
                                return(true);
                            }
                        }
                        return(false);
                    }

                case ".mdsx2":
                case ".mdsb2": {
                    WaitForForm waitForm = new WaitForForm(ctrl => {
                            ctrl.OperationTitle = fileName;
                            try {
                                switch (ext)
                                {
                                case ".mdsx2":
                                    using (XmlReader reader = XmlReader.Create(stream)) {
                                        _dataSet.RetrieveXml(reader);
                                    }
                                    break;

                                case ".mdsb2":
                                    using (BinaryReader reader = new BinaryReader(stream)) {
                                        _dataSet.RetrieveBinary(reader);
                                    }
                                    break;

                                default:
                                    System.Diagnostics.Debug.Fail("format naming error");
                                    break;
                                }
                                ctrl.DialogResult = DialogResult.OK;
                            } catch (Exception ex) {
                                ErrorLogger.Tell(ex, fileName + ": ファイルを読み込めませんでした");
                            }
                        }, () => _dataSet.ProgressChangedEventArgs);
                    if (waitForm.ShowDialog() == DialogResult.OK)
                    {
                        TimeController.Singleton.SetVisibleTime(TimeController.Singleton.BeginTime, TimeController.Singleton.EndTime);
                        setSaveMotionDataFileName(fileName);
                        _dataSet.DoObjectInfoSetChanged();
                        _dataSet.DoFrameListChanged();
                        _isDataSetModified = false;
                        _isOverWritable    = true;
                        return(true);
                    }
                    return(false);
                }

                default:
                    try {
                        throw new ArgumentException("Unknown format: " + ext);
                    } catch (Exception ex) {
                        ErrorLogger.Tell(ex, fileName + ": ファイルを読み込めませんでした");
                    }
                    return(false);
                }
            }
        }