Пример #1
0
        public static void InternalSaveMiniProject(IStorage pStgSave, AltaxoDocument projectToSave, string graphDocumentName)
        {
            ComDebug.ReportInfo("GraphDocumentDataObject.InternalSaveMiniProject BEGIN");

            try
            {
                Exception saveEx = null;
                Ole32Func.WriteClassStg(pStgSave, typeof(GraphDocumentEmbeddedComObject).GUID);

                // Store the version of this assembly
                {
                    var     assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    Version version  = assembly.GetName().Version;
                    using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoVersion", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                    {
                        string text      = version.ToString();
                        byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(text);
                        stream.Write(nameBytes, 0, nameBytes.Length);
                    }
                }

                // Store the name of the item
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoGraphName", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(graphDocumentName);
                    stream.Write(nameBytes, 0, nameBytes.Length);
                }

                // Store the project
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoProjectZip", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    using (var zippedStream = new System.IO.Compression.ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Create))
                    {
                        var info = new Altaxo.Serialization.Xml.XmlStreamSerializationInfo();
                        projectToSave.SaveToZippedFile(zippedStream, info);
                    }
                    stream.Close();
                }

                if (null != saveEx)
                {
                    throw saveEx;
                }
            }
            catch (Exception ex)
            {
                ComDebug.ReportError("InternalSaveMiniProject, Exception ", ex);
            }
            finally
            {
                Marshal.ReleaseComObject(pStgSave);
            }

            ComDebug.ReportInfo("GraphDocumentDataObject.InternalSaveMiniProject END");
        }
Пример #2
0
        public void Save(IStorage pStgSave, bool fSameAsLoad)
        {
            ComDebug.ReportInfo("{0}.IPersistStorage.Save fSameAsLoad={1}", GetType().Name, fSameAsLoad);

            try
            {
                Exception saveEx = null;
                Ole32Func.WriteClassStg(pStgSave, GetType().GUID);

                // Store the version of this assembly
                {
                    var     assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    Version version  = assembly.GetName().Version;
                    using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoVersion", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                    {
                        string text      = version.ToString();
                        byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(text);
                        stream.Write(nameBytes, 0, nameBytes.Length);
                    }
                }

                // Store the name of the item
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoGraphName", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(_document.Name);
                    stream.Write(nameBytes, 0, nameBytes.Length);
                }

                // Store the project
                using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoProjectZip", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
                {
                    _comManager.InvokeGuiThread(() =>
                    {
                        saveEx = Current.IProjectService.SaveProject(stream);
                    }
                                                );
                }

                _isDocumentDirty = false;

                if (null != saveEx)
                {
                    throw saveEx;
                }
            }
            catch (Exception ex)
            {
                ComDebug.ReportError("{0}.IPersistStorage:Save threw an exception: {1}", GetType().Name, ex);
            }
            finally
            {
                Marshal.ReleaseComObject(pStgSave);
            }
        }
Пример #3
0
        public static IntPtr RenderToNewStream(TYMED tymed, Action <System.IO.Stream> RenderToStreamProcedure)
        {
            if (!(tymed == TYMED.TYMED_ISTREAM))
            {
                throw new ArgumentException(nameof(tymed) + " is not TYMED_ISTREAM");
            }

            int hr = Ole32Func.CreateStreamOnHGlobal(IntPtr.Zero, true, out var strm);

            if (!(hr == ComReturnValue.S_OK))
            {
                throw new InvalidOperationException("The COM operation was not successful");
            }
            using (var strmWrapper = new ComStreamWrapper(strm, true))
            {
                RenderToStreamProcedure(strmWrapper);
            }
            return(Marshal.GetIUnknownForObject(strm)); // Increments the reference count
        }
Пример #4
0
        public int Load(IStorage pstg)
        {
            if (!(null == _document))
            {
                throw new InvalidOperationException(nameof(_document) + " should be null");
            }

            string  documentName = null;
            Version altaxoVersion;

            ComDebug.ReportInfo("{0}.IPersistStorage.Load", GetType().Name);

            try
            {
                using (var stream = new ComStreamWrapper(pstg.OpenStream("AltaxoVersion", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
                {
                    var bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                    var versionString = System.Text.Encoding.UTF8.GetString(bytes);
                    altaxoVersion = Version.Parse(versionString);
                }
                ComDebug.ReportInfo("{0}.IPersistStorage.Load -> Version: {1}", GetType().Name, altaxoVersion);
            }
            catch (Exception ex)
            {
                ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoVersion, exception: {1}", GetType().Name, ex);
            }

            try
            {
                using (var stream = new ComStreamWrapper(pstg.OpenStream("AltaxoGraphName", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
                {
                    var bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                    documentName = System.Text.Encoding.UTF8.GetString(bytes);
                }
                ComDebug.ReportInfo("{0}.IPersistStorage.Load -> Name of GraphDocument: {1}", GetType().Name, documentName);
            }
            catch (Exception ex)
            {
                ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoGraphName, exception: {1}", GetType().Name, ex);
            }

            try
            {
                Exception exInner    = null;
                string    loadErrors = null;

                using (var streamWrapper = new ComStreamWrapper(pstg.OpenStream("AltaxoProjectZip", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
                {
                    _comManager.InvokeGuiThread(() =>
                    {
                        try
                        {
                            Current.IProjectService.CloseProject(true);
                            loadErrors = Current.IProjectService.LoadProjectFromStream(streamWrapper);
                        }
                        catch (Exception ex)
                        {
                            exInner = ex;
                        }
                    });
                }
                if (null != exInner)
                {
                    throw exInner;
                }

                if (!string.IsNullOrEmpty(loadErrors))
                {
                    ComDebug.ReportInfo("{0}.IPersistStorage.Load Project loaded with errors: {1}", GetType().Name, loadErrors);
                }
                else
                {
                    ComDebug.ReportInfo("{0}.IPersistStorage.Load Project loaded successfully", GetType().Name);
                }
            }
            catch (Exception ex)
            {
                ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoProjectZip, exception: {1}", GetType().Name, ex);
            }

            Marshal.ReleaseComObject(pstg);

            Altaxo.Graph.GraphDocumentBase newDocument = null;

            if (null != documentName)
            {
                if (Current.Project.GraphDocumentCollection.TryGetValue(documentName, out var newDocGdi))
                {
                    newDocument = newDocGdi;
                }
                else if (Current.Project.Graph3DDocumentCollection.TryGetValue(documentName, out var newDoc3D))
                {
                    newDocument = newDoc3D;
                }
            }

            if (null == newDocument)
            {
                if (null != Current.Project.GraphDocumentCollection.FirstOrDefault())
                {
                    newDocument = Current.Project.GraphDocumentCollection.First();
                }
                else if (null != Current.Project.Graph3DDocumentCollection.FirstOrDefault())
                {
                    newDocument = Current.Project.Graph3DDocumentCollection.First();
                }
            }

            if (null != newDocument)
            {
                Document = newDocument;
                _comManager.InvokeGuiThread(() => Current.IProjectService.ShowDocumentView(Document));
                ComDebug.ReportInfo("{0}.IPersistStorage.Load ShowDocumentView for {1}", GetType().Name, Document.Name);
            }

            if (null == Document)
            {
                ComDebug.ReportError("{0}.IPersistStorage.Load Document is null, have to throw an exception now!!", GetType().Name);
                throw new InvalidOperationException();
            }

            _isDocumentDirty = false;
            return(ComReturnValue.S_OK);
        }
Пример #5
0
		public static void InternalSaveMiniProject(IStorage pStgSave, AltaxoDocument projectToSave, string graphDocumentName)
		{
			ComDebug.ReportInfo("GraphDocumentDataObject.InternalSaveMiniProject BEGIN");

			try
			{
				Exception saveEx = null;
				Ole32Func.WriteClassStg(pStgSave, typeof(GraphDocumentEmbeddedComObject).GUID);

				// Store the version of this assembly
				{
					var assembly = System.Reflection.Assembly.GetExecutingAssembly();
					Version version = assembly.GetName().Version;
					using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoVersion", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
					{
						string text = version.ToString();
						byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(text);
						stream.Write(nameBytes, 0, nameBytes.Length);
					}
				}

				// Store the name of the item
				using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoGraphName", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
				{
					byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(graphDocumentName);
					stream.Write(nameBytes, 0, nameBytes.Length);
				}

				// Store the project
				using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoProjectZip", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
				{
					using (var zippedStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(stream))
					{
						var zippedStreamWrapper = new Altaxo.Main.ZipOutputStreamWrapper(zippedStream);
						var info = new Altaxo.Serialization.Xml.XmlStreamSerializationInfo();
						projectToSave.SaveToZippedFile(zippedStreamWrapper, info);
						zippedStream.Close();
					}
					stream.Close();
				}

				if (null != saveEx)
					throw saveEx;
			}
			catch (Exception ex)
			{
				ComDebug.ReportError("InternalSaveMiniProject, Exception ", ex);
			}
			finally
			{
				Marshal.ReleaseComObject(pStgSave);
			}

			ComDebug.ReportInfo("GraphDocumentDataObject.InternalSaveMiniProject END");
		}
		public void Save(IStorage pStgSave, bool fSameAsLoad)
		{
			ComDebug.ReportInfo("{0}.IPersistStorage.Save fSameAsLoad={1}", this.GetType().Name, fSameAsLoad);

			try
			{
				Exception saveEx = null;
				Ole32Func.WriteClassStg(pStgSave, this.GetType().GUID);

				// Store the version of this assembly
				{
					var assembly = System.Reflection.Assembly.GetExecutingAssembly();
					Version version = assembly.GetName().Version;
					using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoVersion", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
					{
						string text = version.ToString();
						byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(text);
						stream.Write(nameBytes, 0, nameBytes.Length);
					}
				}

				// Store the name of the item
				using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoGraphName", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
				{
					byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(_document.Name);
					stream.Write(nameBytes, 0, nameBytes.Length);
				}

				// Store the project
				using (var stream = new ComStreamWrapper(pStgSave.CreateStream("AltaxoProjectZip", (int)(STGM.DIRECT | STGM.READWRITE | STGM.CREATE | STGM.SHARE_EXCLUSIVE), 0, 0), true))
				{
					_comManager.InvokeGuiThread(() =>
					{
						saveEx = Current.ProjectService.SaveProject(stream);
					}
					);
				}

				_isDocumentDirty = false;

				if (null != saveEx)
					throw saveEx;
			}
			catch (Exception ex)
			{
				ComDebug.ReportError("{0}.IPersistStorage:Save threw an exception: {1}", this.GetType().Name, ex);
			}
			finally
			{
				Marshal.ReleaseComObject(pStgSave);
			}
		}
		public int Load(IStorage pstg)
		{
			if (!(null == _document))
				throw new InvalidOperationException(nameof(_document) + " should be null");

			string documentName = null;
			Version altaxoVersion;
			ComDebug.ReportInfo("{0}.IPersistStorage.Load", this.GetType().Name);

			try
			{
				using (var stream = new ComStreamWrapper(pstg.OpenStream("AltaxoVersion", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
				{
					var bytes = new byte[stream.Length];
					stream.Read(bytes, 0, bytes.Length);
					var versionString = System.Text.Encoding.UTF8.GetString(bytes);
					altaxoVersion = Version.Parse(versionString);
				}
				ComDebug.ReportInfo("{0}.IPersistStorage.Load -> Version: {1}", this.GetType().Name, altaxoVersion);
			}
			catch (Exception ex)
			{
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoVersion, exception: {1}", this.GetType().Name, ex);
			}

			try
			{
				using (var stream = new ComStreamWrapper(pstg.OpenStream("AltaxoGraphName", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
				{
					var bytes = new byte[stream.Length];
					stream.Read(bytes, 0, bytes.Length);
					documentName = System.Text.Encoding.UTF8.GetString(bytes);
				}
				ComDebug.ReportInfo("{0}.IPersistStorage.Load -> Name of GraphDocument: {1}", this.GetType().Name, documentName);
			}
			catch (Exception ex)
			{
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoGraphName, exception: {1}", this.GetType().Name, ex);
			}

			try
			{
				using (var streamWrapper = new ComStreamWrapper(pstg.OpenStream("AltaxoProjectZip", IntPtr.Zero, (int)(STGM.READ | STGM.SHARE_EXCLUSIVE), 0), true))
				{
					_comManager.InvokeGuiThread(() =>
					{
						Current.ProjectService.CloseProject(true);
						Current.ProjectService.LoadProject(streamWrapper);
					});
				}
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Project loaded", this.GetType().Name);
			}
			catch (Exception ex)
			{
				ComDebug.ReportInfo("{0}.IPersistStorage.Load Failed to load stream AltaxoProjectZip, exception: {1}", this.GetType().Name, ex);
			}

			Marshal.ReleaseComObject(pstg);

			Altaxo.Graph.GraphDocumentBase newDocument = null;

			if (null != documentName)
			{
				Altaxo.Graph.Gdi.GraphDocument newDocGdi;
				Altaxo.Graph.Graph3D.GraphDocument newDoc3D;

				if (Current.Project.GraphDocumentCollection.TryGetValue(documentName, out newDocGdi))
					newDocument = newDocGdi;
				else if (Current.Project.Graph3DDocumentCollection.TryGetValue(documentName, out newDoc3D))
					newDocument = newDoc3D;
			}

			if (null == newDocument)
			{
				if (null != Current.Project.GraphDocumentCollection.FirstOrDefault())
					newDocument = Current.Project.GraphDocumentCollection.First();
				else if (null != Current.Project.Graph3DDocumentCollection.FirstOrDefault())
					newDocument = Current.Project.Graph3DDocumentCollection.First();
			}

			if (null != newDocument)
			{
				Document = newDocument;
				_comManager.InvokeGuiThread(() => Current.ProjectService.ShowDocumentView(_document));
			}

			if (null == Document)
			{
				ComDebug.ReportError("{0}.IPersistStorage.Load Document is null, have to throw an exception now!!", this.GetType().Name);
				throw new InvalidOperationException();
			}

			_isDocumentDirty = false;
			return ComReturnValue.S_OK;
		}