Ejemplo n.º 1
0
        /// <summary>
        /// Read files and serialize xml files into class structure
        /// </summary>
        /// <param name="bcfPath">the name of bcfzip</param>
        /// <param name="tempVisInfoHolder"></param>
        /// <param name="tempFileContentHoder"></param>
        /// <param name="tempExtInfoHolder"></param>
        /// <returns></returns>
        private static BCFZIP ReadRawData(string bcfPath, out Dictionary <string, Dictionary <string, VisualizationInfo> > tempVisInfoHolder,
                                          out Dictionary <string, Dictionary <string, byte[]> > tempFileContentHoder, out Dictionary <string, Dictionary <string, ComponentExtensionInfo> > tempExtInfoHolder)
        {
            BCFZIP bcfZip = new BCFZIP(bcfPath);

            tempVisInfoHolder    = new Dictionary <string, Dictionary <string, VisualizationInfo> >();
            tempFileContentHoder = new Dictionary <string, Dictionary <string, byte[]> >();
            tempExtInfoHolder    = new Dictionary <string, Dictionary <string, ComponentExtensionInfo> >();
            try
            {
                using (ZipArchive archive = ZipFile.OpenRead(bcfPath))
                {
                    ProgressManager.InitializeProgress("Gathering information from the BCF file...", archive.Entries.Count);
                    double value = 0;

                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        value++;
                        ProgressManager.StepForward();
                        string topicId = entry.ExtractGuidFolderName();

                        if (entry.FullName.EndsWith(".bcfp", StringComparison.OrdinalIgnoreCase))
                        {
                            //serialize project file
                            XmlSerializer serializer = new XmlSerializer(typeof(ProjectExtension));
                            XmlReader     reader     = XmlReader.Create(entry.Open());
                            if (serializer.CanDeserialize(reader))
                            {
                                bcfZip.ProjectFile = (ProjectExtension)serializer.Deserialize(reader);
                                continue;
                            }
                        }
                        else if (entry.FullName.EndsWith(".version", StringComparison.OrdinalIgnoreCase))
                        {
                            //serialize version file
                            XmlSerializer serializer = new XmlSerializer(typeof(Version));
                            XmlReader     reader     = XmlReader.Create(entry.Open());
                            if (serializer.CanDeserialize(reader))
                            {
                                bcfZip.VersionFile = (Version)serializer.Deserialize(reader);
                                continue;
                            }
                        }
                        else if (entry.FullName.EndsWith(".color", StringComparison.OrdinalIgnoreCase))
                        {
                            //serialize color file
                            XmlSerializer serializer = new XmlSerializer(typeof(RevitExtensionInfo));
                            XmlReader     reader     = XmlReader.Create(entry.Open());
                            if (serializer.CanDeserialize(reader))
                            {
                                bcfZip.ExtensionColor = (RevitExtensionInfo)serializer.Deserialize(reader);
                                continue;
                            }
                        }
                        else if (!string.IsNullOrEmpty(topicId) && entry.FullName.EndsWith(".bcf", StringComparison.OrdinalIgnoreCase))
                        {
                            //serialize markup file
                            XmlSerializer serializer = new XmlSerializer(typeof(Markup));
                            XmlReader     reader     = XmlReader.Create(entry.Open());
                            if (serializer.CanDeserialize(reader))
                            {
                                Markup markup = (Markup)serializer.Deserialize(reader);
                                bcfZip.Markups.Add(markup);
                            }
                        }
                        else if (!string.IsNullOrEmpty(topicId) && entry.FullName.EndsWith(".bcfv", StringComparison.OrdinalIgnoreCase))
                        {
                            //serialize viewpoint file
                            XmlSerializer serializer = new XmlSerializer(typeof(VisualizationInfo));
                            XmlReader     reader     = XmlReader.Create(entry.Open());
                            if (serializer.CanDeserialize(reader))
                            {
                                VisualizationInfo visInfo = (VisualizationInfo)serializer.Deserialize(reader);
                                if (tempVisInfoHolder.ContainsKey(topicId))
                                {
                                    if (!tempVisInfoHolder[topicId].ContainsKey(entry.Name))
                                    {
                                        tempVisInfoHolder[topicId].Add(entry.Name, visInfo);
                                    }
                                }
                                else
                                {
                                    Dictionary <string, VisualizationInfo> visInfoDictionary = new Dictionary <string, VisualizationInfo>();
                                    visInfoDictionary.Add(entry.Name, visInfo);
                                    tempVisInfoHolder.Add(topicId, visInfoDictionary);
                                }
                            }
                        }
                        else if (!string.IsNullOrEmpty(topicId) && entry.FullName.EndsWith(".bcfvx", StringComparison.OrdinalIgnoreCase))
                        {
                            //serialize extension file
                            XmlSerializer serializer = new XmlSerializer(typeof(ComponentExtensionInfo));
                            XmlReader     reader     = XmlReader.Create(entry.Open());
                            if (serializer.CanDeserialize(reader))
                            {
                                ComponentExtensionInfo extInfo = (ComponentExtensionInfo)serializer.Deserialize(reader);
                                if (tempExtInfoHolder.ContainsKey(topicId))
                                {
                                    if (!tempExtInfoHolder[topicId].ContainsKey(entry.Name))
                                    {
                                        tempExtInfoHolder[topicId].Add(entry.Name, extInfo);
                                    }
                                }
                                else
                                {
                                    Dictionary <string, ComponentExtensionInfo> extInfoDictionary = new Dictionary <string, ComponentExtensionInfo>();
                                    extInfoDictionary.Add(entry.Name, extInfo);
                                    tempExtInfoHolder.Add(topicId, extInfoDictionary);
                                }
                            }
                        }
                        else
                        {
                            //obtain bytearray of miscellaneous files including images
                            using (MemoryStream ms = new MemoryStream())
                            {
                                entry.Open().CopyTo(ms);
                                byte[] byteArray = ms.ToArray();
                                if (tempFileContentHoder.ContainsKey(topicId))
                                {
                                    if (!tempFileContentHoder[topicId].ContainsKey(entry.Name))
                                    {
                                        tempFileContentHoder[topicId].Add(entry.Name, byteArray);
                                    }
                                }
                                else
                                {
                                    Dictionary <string, byte[]> fileContents = new Dictionary <string, byte[]>();
                                    fileContents.Add(entry.Name, byteArray);
                                    tempFileContentHoder.Add(topicId, fileContents);
                                }
                            }
                        }
                    }
                    ProgressManager.FinalizeProgress();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to read data from BCF.\n" + ex.Message, "Read Raw Data", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(bcfZip);
        }
Ejemplo n.º 2
0
        public static bool Write(string bcfPath, BCFZIP bcf)
        {
            bool written = false;

            try
            {
                ProgressManager.InitializeProgress("Writing BCF..", bcf.Markups.Count);

                string tempDirectory = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "SmartBCF", bcf.FileId);
                if (Directory.Exists(tempDirectory))
                {
                    Directory.Delete(tempDirectory, true);
                }

                //Create root directory
                Directory.CreateDirectory(tempDirectory);

                //Project File
                string projectFilePath = System.IO.Path.Combine(tempDirectory, "project.bcfp");
                using (FileStream stream = new FileStream(projectFilePath, FileMode.Create))
                {
                    XmlSerializer projectSerializer = new XmlSerializer(typeof(ProjectExtension));
                    projectSerializer.Serialize(stream, bcf.ProjectFile);
                    stream.Close();
                }

                //Version File
                string versionFilePath = System.IO.Path.Combine(tempDirectory, "bcf.version");
                using (FileStream stream = new FileStream(versionFilePath, FileMode.Create))
                {
                    XmlSerializer versionSerializer = new XmlSerializer(typeof(Version));
                    versionSerializer.Serialize(stream, bcf.VersionFile);
                    stream.Close();
                }

                //Color File
                string colorFilePath = System.IO.Path.Combine(tempDirectory, "extension.color");
                using (FileStream stream = new FileStream(colorFilePath, FileMode.Create))
                {
                    XmlSerializer colorSerializer = new XmlSerializer(typeof(RevitExtensionInfo));
                    colorSerializer.Serialize(stream, bcf.ExtensionColor);
                    stream.Close();
                }

                //Markup and Viewpoint
                XmlSerializer markupSerializer  = new XmlSerializer(typeof(Markup));
                XmlSerializer visInfoSerializer = new XmlSerializer(typeof(VisualizationInfo));
                XmlSerializer extInfoSerializer = new XmlSerializer(typeof(ComponentExtensionInfo));
                foreach (Markup markup in bcf.Markups)
                {
                    ProgressManager.StepForward();

                    string topicDirectory = System.IO.Path.Combine(tempDirectory, markup.Topic.Guid);
                    Directory.CreateDirectory(topicDirectory);

                    string markupFilePath = System.IO.Path.Combine(topicDirectory, "markup.bcf");
                    using (FileStream stream = new FileStream(markupFilePath, FileMode.Create))
                    {
                        markupSerializer.Serialize(stream, markup);
                        stream.Close();
                    }

                    //Viewpoint
                    foreach (ViewPoint vp in markup.Viewpoints)
                    {
                        //Snapshot
                        if (!string.IsNullOrEmpty(vp.Snapshot) && null != vp.SnapshotImage)
                        {
                            string snapshotPath = System.IO.Path.Combine(topicDirectory, vp.Snapshot);
                            using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(vp.SnapshotImage)))
                            {
                                image.Save(snapshotPath, System.Drawing.Imaging.ImageFormat.Png);
                            }
                        }

                        if (!string.IsNullOrEmpty(vp.Viewpoint))
                        {
                            //Visinfo
                            string visInfoPath = System.IO.Path.Combine(topicDirectory, vp.Viewpoint);
                            if (null != vp.VisInfo)
                            {
                                VisualizationInfo visInfo = vp.VisInfo;
                                using (FileStream stream = new FileStream(visInfoPath, FileMode.Create))
                                {
                                    visInfoSerializer.Serialize(stream, visInfo);
                                    stream.Close();
                                }

                                string extensionPath           = visInfoPath.Replace(".bcfv", ".bcfvx");
                                ComponentExtensionInfo extInfo = new ComponentExtensionInfo();
                                extInfo.ViewpointGuid = vp.Guid;
                                var revitComponents = from comp in visInfo.Components
                                                      where (null != comp.Action) && (null != comp.Responsibility)
                                                      select comp;
                                if (revitComponents.Count() > 0)
                                {
                                    var componentsToWrite = from comp in revitComponents
                                                            where (comp.Action.Guid != Guid.Empty.ToString()) || (comp.Responsibility.Guid != Guid.Empty.ToString()) || (!string.IsNullOrEmpty(comp.ElementName))
                                                            select comp;
                                    if (componentsToWrite.Count() > 0)
                                    {
                                        ObservableCollection <ComponentExtension> compExtensions = new ObservableCollection <ComponentExtension>();
                                        List <Component> componentList = revitComponents.ToList();
                                        foreach (Component comp in componentList)
                                        {
                                            compExtensions.Add(new ComponentExtension(comp));
                                        }
                                        extInfo.Extensions = compExtensions;
                                        using (FileStream stream = new FileStream(extensionPath, FileMode.Create))
                                        {
                                            extInfoSerializer.Serialize(stream, extInfo);
                                            stream.Close();
                                        }
                                    }
                                }

                                //Bitmap
                                if (vp.VisInfo.Bitmaps.Count > 0)
                                {
                                    foreach (VisualizationInfoBitmaps bitmap in visInfo.Bitmaps)
                                    {
                                        if (!string.IsNullOrEmpty(bitmap.Reference) && null != bitmap.BitmapImage)
                                        {
                                            string bitmapPath = System.IO.Path.Combine(topicDirectory, bitmap.Reference);
                                            using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bitmap.BitmapImage)))
                                            {
                                                if (bitmap.Bitmap == BitmapFormat.JPG)
                                                {
                                                    image.Save(bitmapPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                                                }
                                                else if (bitmap.Bitmap == BitmapFormat.PNG)
                                                {
                                                    image.Save(bitmapPath, System.Drawing.Imaging.ImageFormat.Png);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                ZipFile.CreateFromDirectory(tempDirectory, bcfPath, CompressionLevel.Fastest, false);
                if (File.Exists(bcfPath))
                {
                    written = true;
                }
                ProgressManager.FinalizeProgress();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to write the BCF file.\n" + ex.Message, "Write BCF", MessageBoxButton.OK, MessageBoxImage.Warning);
                ProgressManager.FinalizeProgress();
            }
            return(written);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Organize raw data into BCFZIP class structure
        /// </summary>
        /// <param name="bcfzip"></param>
        /// <param name="tempVisInfoHolder"></param>
        /// <param name="tempFileContentHolder"></param>
        /// <param name="tempExtInfoHolder"></param>
        /// <returns></returns>
        private static BCFZIP MapContents(BCFZIP bcfzip, Dictionary <string, Dictionary <string, VisualizationInfo> > tempVisInfoHolder,
                                          Dictionary <string, Dictionary <string, byte[]> > tempFileContentHolder, Dictionary <string, Dictionary <string, ComponentExtensionInfo> > tempExtInfoHolder)
        {
            BCFZIP mappedBCF = null;

            try
            {
                Dictionary <string /*guid*/, RevitExtension> extensions = new Dictionary <string, RevitExtension>();
                foreach (RevitExtension ext in bcfzip.ExtensionColor.Extensions)
                {
                    if (!extensions.ContainsKey(ext.Guid))
                    {
                        extensions.Add(ext.Guid, ext);
                    }
                }

                for (int i = 0; i < bcfzip.Markups.Count; i++)
                {
                    Markup markup  = bcfzip.Markups[i];
                    string topicId = markup.Topic.Guid;

                    //BimSnippet
                    BimSnippet bimSnippet = markup.Topic.BimSnippet;
                    if (!string.IsNullOrEmpty(bimSnippet.Reference) && !bimSnippet.isExternal)
                    {
                        if (tempFileContentHolder.ContainsKey(topicId))
                        {
                            if (tempFileContentHolder[topicId].ContainsKey(bimSnippet.Reference))
                            {
                                bimSnippet.FileContent = tempFileContentHolder[topicId][bimSnippet.Reference];
                            }
                        }
                    }
                    bimSnippet.TopicGuid    = topicId;
                    markup.Topic.BimSnippet = bimSnippet;

                    //DocumentReferences
                    List <TopicDocumentReferences> docList = new List <TopicDocumentReferences>();
                    foreach (TopicDocumentReferences doc in markup.Topic.DocumentReferences)
                    {
                        TopicDocumentReferences docRef = doc;
                        docRef.TopicGuid = topicId;
                        if (!string.IsNullOrEmpty(docRef.ReferencedDocument) && !docRef.isExternal)
                        {
                            if (tempFileContentHolder.ContainsKey(topicId))
                            {
                                if (tempFileContentHolder[topicId].ContainsKey(docRef.ReferencedDocument))
                                {
                                    docRef.FileContent = tempFileContentHolder[topicId][docRef.ReferencedDocument];
                                }
                            }
                        }
                        docList.Add(docRef);
                    }
                    markup.Topic.DocumentReferences = docList;

                    if (markup.Viewpoints.Count > 0)
                    {
                        ViewPoint firstViewpoint = markup.Viewpoints.First();

                        for (int j = 0; j < markup.Comment.Count; j++)
                        {
                            string viewPointGuid = markup.Comment[j].Viewpoint.Guid;
                            if (string.IsNullOrEmpty(viewPointGuid))
                            {
                                markup.Comment[j].Viewpoint.Guid = firstViewpoint.Guid;
                            }
                        }
                    }

                    //viewpoints
                    for (int j = 0; j < markup.Viewpoints.Count; j++)
                    {
                        ViewPoint viewpoint = markup.Viewpoints[j];
                        //bitmap
                        if (tempVisInfoHolder.ContainsKey(topicId))
                        {
                            if (tempVisInfoHolder[topicId].ContainsKey(viewpoint.Viewpoint))
                            {
                                VisualizationInfo visInfo = tempVisInfoHolder[topicId][viewpoint.Viewpoint];
                                visInfo.ViewPointGuid = viewpoint.Guid;
                                List <VisualizationInfoBitmaps> bitmapList = new List <VisualizationInfoBitmaps>();
                                foreach (VisualizationInfoBitmaps bitmap in visInfo.Bitmaps)
                                {
                                    VisualizationInfoBitmaps visBitmap = bitmap;
                                    if (!string.IsNullOrEmpty(bitmap.Reference))
                                    {
                                        if (tempFileContentHolder.ContainsKey(topicId))
                                        {
                                            if (tempFileContentHolder[topicId].ContainsKey(bitmap.Reference))
                                            {
                                                visBitmap.BitmapImage = tempFileContentHolder[topicId][bitmap.Reference];
                                            }
                                        }
                                    }
                                    visBitmap.ViewPointGuid = viewpoint.Guid;
                                    bitmapList.Add(visBitmap);
                                }
                                visInfo.Bitmaps = bitmapList;

                                string viewpointExt = viewpoint.Viewpoint.Replace(".bcfv", ".bcfvx");
                                if (tempExtInfoHolder.ContainsKey(topicId))
                                {
                                    if (tempExtInfoHolder[topicId].ContainsKey(viewpointExt))
                                    {
                                        ComponentExtensionInfo extInfo = tempExtInfoHolder[topicId][viewpointExt];
                                        if (visInfo.Components.Count > 0)
                                        {
                                            foreach (ComponentExtension compExt in extInfo.Extensions)
                                            {
                                                var compFound = from comp in visInfo.Components where comp.IfcGuid == compExt.IfcGuid select comp;
                                                if (compFound.Count() > 0)
                                                {
                                                    int compIndex = visInfo.Components.IndexOf(compFound.First());
                                                    visInfo.Components[compIndex].ElementName    = compExt.ElementName;
                                                    visInfo.Components[compIndex].Action         = (extensions.ContainsKey(compExt.ActionGuid)) ? extensions[compExt.ActionGuid] : new RevitExtension();
                                                    visInfo.Components[compIndex].Responsibility = (extensions.ContainsKey(compExt.ResponsibilityGuid)) ? extensions[compExt.ResponsibilityGuid] : new RevitExtension();
                                                }
                                            }
                                        }
                                    }
                                }

                                markup.Viewpoints[j].VisInfo = visInfo;
                            }
                        }
                        //snapshot
                        if (tempFileContentHolder.ContainsKey(topicId))
                        {
                            if (tempFileContentHolder[topicId].ContainsKey(viewpoint.Snapshot))
                            {
                                markup.Viewpoints[j].SnapshotImage = tempFileContentHolder[topicId][viewpoint.Snapshot];
                                if (null == markup.TopicImage && null != markup.Viewpoints[j].SnapshotImage)
                                {
                                    markup.TopicImage = markup.Viewpoints[j].SnapshotImage;
                                }
                            }
                        }
                    }

                    if (markup.Viewpoints.Count > 0)
                    {
                        markup.SelectedViewpoint = markup.Viewpoints.First();
                    }
                    bcfzip.Markups[i] = markup;
                }
                mappedBCF = bcfzip;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create maps from BCF contents.\n" + ex.Message, "Mapping BCF Contents", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(mappedBCF);
        }