private static ProjectExtension DeserializeProject(string path) { ProjectExtension output = null; try { using (var markupFile = new FileStream(path, FileMode.Open)) { var serializerM = new XmlSerializer(typeof(ProjectExtension)); output = serializerM.Deserialize(markupFile) as ProjectExtension; } } catch (System.Exception ex1) { MessageBox.Show("exception: " + ex1); } return(output); }
/// <summary> /// Serializes to a bcfzip and saves it to disk /// </summary> /// <param name="bcffile"></param> /// <returns></returns> public static bool SaveBcfFile(BcfFile bcffile) { try { if (bcffile.Issues.Count == 0) { MessageBox.Show("The current BCF Report is empty.", "No Issue", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } if (!Directory.Exists(bcffile.TempPath)) { Directory.CreateDirectory(bcffile.TempPath); } // Show save file dialog box string name = !string.IsNullOrEmpty(bcffile.Filename) ? bcffile.Filename : "New BCF Report"; string filename = SaveBcfDialog(name); // Process save file dialog box results if (string.IsNullOrWhiteSpace(filename)) { return(false); } var bcfProject = new ProjectExtension { Project = new Project { Name = string.IsNullOrEmpty(bcffile.ProjectName) ? bcffile.Filename : bcffile.ProjectName, ProjectId = bcffile.ProjectId.Equals(Guid.Empty) ? Guid.NewGuid().ToString() : bcffile.ProjectId.ToString() }, ExtensionSchema = "ExtensionSchema.xsd" }; var bcfVersion = new Version { VersionId = "2.0", DetailedVersion = "2.0 RC" }; var serializerP = new XmlSerializer(typeof(ProjectExtension)); Stream writerP = new FileStream(Path.Combine(bcffile.TempPath, "project.bcfp"), FileMode.Create); serializerP.Serialize(writerP, bcfProject); writerP.Close(); var serializerVers = new XmlSerializer(typeof(Version)); Stream writerVers = new FileStream(Path.Combine(bcffile.TempPath, "bcf.version"), FileMode.Create); serializerVers.Serialize(writerVers, bcfVersion); writerVers.Close(); var serializerV = new XmlSerializer(typeof(VisualizationInfo)); var serializerM = new XmlSerializer(typeof(Markup)); foreach (var issue in bcffile.Issues) { // Serialize the object, and close the TextWriter string issuePath = Path.Combine(bcffile.TempPath, issue.Topic.Guid); if (!Directory.Exists(issuePath)) { Directory.CreateDirectory(issuePath); } //BCF 1 compatibility //there needs to be a view whose viewpoint and snapshot are named as follows and not with a guid //uniqueness is still guarenteed by the guid field if (issue.Viewpoints != null) { if (issue.Viewpoints.Any() && (issue.Viewpoints.Count == 1 || issue.Viewpoints.All(o => o.Viewpoint != "viewpoint.bcfv"))) { if (File.Exists(Path.Combine(issuePath, issue.Viewpoints[0].Viewpoint))) { File.Move(Path.Combine(issuePath, issue.Viewpoints[0].Viewpoint), Path.Combine(issuePath, "viewpoint.bcfv")); } issue.Viewpoints[0].Viewpoint = "viewpoint.bcfv"; if (File.Exists(Path.Combine(issuePath, issue.Viewpoints[0].Snapshot))) { File.Move(Path.Combine(issuePath, issue.Viewpoints[0].Snapshot), Path.Combine(issuePath, "snapshot.png")); } issue.Viewpoints[0].Snapshot = "snapshot.png"; } } //serialize markup with updated content Stream writerM = new FileStream(Path.Combine(issuePath, "markup.bcf"), FileMode.Create); serializerM.Serialize(writerM, issue); writerM.Close(); //serialize views if (issue.Viewpoints != null) { foreach (var bcfViewpoint in issue.Viewpoints) { if (bcfViewpoint.Viewpoint != null) { string viewpointPath = Path.Combine(issuePath, bcfViewpoint.Viewpoint); if (bcfViewpoint.VisInfo != null) { Stream writerV = new FileStream(viewpointPath, FileMode.Create); serializerV.Serialize(writerV, bcfViewpoint.VisInfo); writerV.Close(); } else if (File.Exists(viewpointPath)) // this code block is for Solibri { XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; try { doc.Load(viewpointPath); doc.DocumentElement.SetAttribute("noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "visinfo.xsd" ); doc.Save(viewpointPath); } catch (Exception ex) { MessageBox.Show("Failed to write schema info. Cannot be imported to Solibri.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } } else // fallback logic for Solibri and BCFier { // add a blank viewpoint file Stream writerV = new FileStream(viewpointPath, FileMode.Create); serializerV.Serialize(writerV, new VisualizationInfo()); writerV.Close(); } } } } } //overwrite, without doubts if (File.Exists(filename)) { File.Delete(filename); } using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) { zip.AddDirectory(bcffile.TempPath); zip.Save(filename); } //this library cause conflicts with Solibri //ZipFile.CreateFromDirectory(bcffile.TempPath, filename, CompressionLevel.Fastest, false); //DeleteDirectory(bcffile.TempPath); //Open browser at location Uri uri2 = new Uri(filename); string reportname = Path.GetFileName(uri2.LocalPath); if (File.Exists(filename)) { string argument = @"/select, " + filename; System.Diagnostics.Process.Start("explorer.exe", argument); } bcffile.HasBeenSaved = true; bcffile.Filename = reportname; } catch (System.Exception ex1) { MessageBox.Show("exception: " + ex1); } return(true); }