Esempio n. 1
0
        /// <summary>
        /// Creates an IFCImportFile from a file on the disk.
        /// </summary>
        /// <param name="ifcFilePath">The path of the file.</param>
        /// <param name="options">The IFC import options.</param>
        /// <param name="doc">The optional document argument.  If importing into Revit, not supplying a document may reduce functionality later.</param>
        /// <returns>The IFCImportFile.</returns>
        public static IFCImportFile Create(string ifcFilePath, IFCImportOptions options, Document doc)
        {
            m_sIFCImportFile = new IFCImportFile();
            bool success = TheFile.Process(ifcFilePath, options, doc);

            if (success)
            {
                // Store the original levels in the template file for Open IFC.  On export, we will delete these levels if we created any.
                // Note that we always have to preserve one level, regardless of what the ActiveView is.
                // TODO: Only for open, not import.
                if (doc != null)
                {
                    IFCBuildingStorey.ExistingLevelIdToReuse = ElementId.InvalidElementId;

                    View activeView = doc.ActiveView;
                    if (activeView != null)
                    {
                        Level genLevel = activeView.GenLevel;
                        if (genLevel != null)
                        {
                            IFCBuildingStorey.ExistingLevelIdToReuse = genLevel.Id;
                        }
                    }

                    FilteredElementCollector levelCollector   = new FilteredElementCollector(doc);
                    ICollection <Element>    levels           = levelCollector.OfClass(typeof(Level)).ToElements();
                    ICollection <ElementId>  levelIdsToDelete = new HashSet <ElementId>();
                    foreach (Element level in levels)
                    {
                        if (IFCBuildingStorey.ExistingLevelIdToReuse == ElementId.InvalidElementId)
                        {
                            IFCBuildingStorey.ExistingLevelIdToReuse = level.Id;
                        }
                        else if (level.Id != IFCBuildingStorey.ExistingLevelIdToReuse)
                        {
                            levelIdsToDelete.Add(level.Id);
                        }
                    }
                    doc.Delete(levelIdsToDelete);

                    // Collect material names, to avoid reusing.
                    FilteredElementCollector materialCollector = new FilteredElementCollector(doc);
                    ICollection <Element>    materials         = materialCollector.OfClass(typeof(Material)).ToElements();
                    foreach (Element materialAsElem in materials)
                    {
                        Material        material = materialAsElem as Material;
                        IFCMaterialInfo info     = IFCMaterialInfo.Create(material.Color, material.Transparency, material.Shininess,
                                                                          material.Smoothness, material.Id);
                        Importer.TheCache.CreatedMaterials.Add(material.Name, info);
                    }
                }
            }
            else
            {
                // Close up the log file, set m_sIFCImportFile to null.
                TheFile.Close();
            }

            return(m_sIFCImportFile);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an IFCImportFile from a file on the disk.
        /// </summary>
        /// <param name="ifcFilePath">The path of the file.</param>
        /// <param name="options">The IFC import options.</param>
        /// <param name="doc">The optional document argument.  If importing into Revit, not supplying a document may reduce functionality later.</param>
        /// <returns>The IFCImportFile.</returns>
        public static IFCImportFile Create(string ifcFilePath, IFCImportOptions options, Document doc)
        {
            m_sIFCImportFile = new IFCImportFile();
            bool success = TheFile.Process(ifcFilePath, options, doc);

            if (success)
            {
                // Store the original levels in the template file for Open IFC.  On export, we will delete these levels if we created any.
                // Note that we always have to preserve one level, regardless of what the ActiveView is.
                if (doc != null)
                {
                    IFCBuildingStorey.ExistingLevelIdToReuse = ElementId.InvalidElementId;

                    View activeView = doc.ActiveView;
                    if (activeView != null)
                    {
                        Level genLevel = activeView.GenLevel;
                        if (genLevel != null)
                        {
                            IFCBuildingStorey.ExistingLevelIdToReuse = genLevel.Id;
                        }
                    }

                    // For Link IFC, we will delete any unused levels at the end.  Instead, we want to try to reuse them.
                    // The for loop does a little unnecessary work if deleteLevelsNow, but the performance implications are very small.
                    bool deleteLevelsNow = (Importer.TheOptions.Action != IFCImportAction.Link);

                    FilteredElementCollector levelCollector   = new FilteredElementCollector(doc);
                    ICollection <Element>    levels           = levelCollector.OfClass(typeof(Level)).ToElements();
                    ICollection <ElementId>  levelIdsToDelete = new HashSet <ElementId>();
                    foreach (Element level in levels)
                    {
                        if (level == null)
                        {
                            continue;
                        }

                        if (IFCBuildingStorey.ExistingLevelIdToReuse == ElementId.InvalidElementId)
                        {
                            IFCBuildingStorey.ExistingLevelIdToReuse = level.Id;
                        }
                        else if (level.Id != IFCBuildingStorey.ExistingLevelIdToReuse)
                        {
                            levelIdsToDelete.Add(level.Id);
                        }
                    }

                    if (deleteLevelsNow)
                    {
                        doc.Delete(levelIdsToDelete);
                    }

                    // Collect material names, to avoid reusing.
                    FilteredElementCollector materialCollector = new FilteredElementCollector(doc);
                    ICollection <Element>    materials         = materialCollector.OfClass(typeof(Material)).ToElements();
                    foreach (Element materialAsElem in materials)
                    {
                        Material        material = materialAsElem as Material;
                        IFCMaterialInfo info     = IFCMaterialInfo.Create(material.Color, material.Transparency, material.Shininess,
                                                                          material.Smoothness, material.Id);
                        Importer.TheCache.CreatedMaterials.Add(material.Name, info);
                    }
                }
            }
            else
            {
                // Close up the log file, set m_sIFCImportFile to null.
                TheFile.Close();
            }

            return(m_sIFCImportFile);
        }