예제 #1
0
        // Create HTML table based on IEntityCollection
        public static string HtmlExportEntityCollection(IEntityCollection collection)
        {
            List <string> columnNames = new List <string>();
            bool          bFirstItem  = true;
            string        sBody       = "";

            // get column properties
            IEnumerable <IEntityProperty> columnProperties = collection.OfType <IEntityObject>().First().Details.Properties.All();

            // iterate the collection and extract values by column name
            foreach (IEntityObject entityObj in collection)
            {
                if (bFirstItem)
                {
                    // opening an html tag and creating a table
                    //sBody = "<html>"
                    //sBody = sBody + "</br></br>"
                    //sBody = sBody + "<body style=""font-family: Arial, Helvetica, sans-serif;"" >"
                    sBody = "<table border=\"1\">";

                    // add columns names to the list
                    // row begins
                    sBody = sBody + "<tr>";
                    foreach (IEntityProperty entityProperty in columnProperties)
                    {
                        columnNames.Add(entityProperty.Name);
                        sBody = sBody + "<td>";
                        sBody = sBody + " " + entityProperty.DisplayName;
                        sBody = sBody + "</td>";
                    }
                    // row ends
                    sBody      = sBody + "</tr>";
                    bFirstItem = false;
                }

                sBody = sBody + "<tr>";
                for (int i = 0; i <= columnNames.Count - 1; i++)
                {
                    sBody = sBody + "<td>";
                    sBody = sBody + LightSwitchHelper.GetValue(entityObj, columnNames[i]);
                    sBody = sBody + "</td>";
                }
                sBody = sBody + "</tr>";
            }

            // Add closing tags if there was at least one item
            // bFirstItem = True by default
            // It is set to False when the first item is encountered
            if (!bFirstItem)
            {
                // closing the tags
                sBody = sBody + "</table>";
                //sBody = sBody + "</body>"
                //sBody = sBody + "</html>"
            }
            return(sBody);
        }
예제 #2
0
        // exports collection to a new workbook starting at cell A1 on the first worksheet
        public static dynamic ExportEntityCollection(IEntityCollection collection)
        {
            dynamic result = null;

            try
            {
                ExcelHelper excel = new ExcelHelper();
                excel.CreateWorkbook();

                List <string> columnNames = new List <string>();

                // get column properties
                IEnumerable <IEntityProperty> columnProperties = collection.OfType <IEntityObject>().First().Details.Properties.All();

                int columnCounter = 1;
                int rowCounter    = 1;

                // add columns names to the list
                foreach (IEntityProperty entityProperty in columnProperties)
                {
                    columnNames.Add(entityProperty.Name);

                    // add column headers to Excel workbook
                    excel.Cells(rowCounter, columnCounter, entityProperty.DisplayName);
                    columnCounter += 1;
                }

                // add values on the row following the headers
                rowCounter = 2;

                // iterate the collection and extract values by column name
                foreach (IEntityObject entityObj in collection)
                {
                    for (int i = 0; i <= columnNames.Count - 1; i++)
                    {
                        excel.Cells(rowCounter, i + 1, LightSwitchHelper.GetValue(entityObj, columnNames[i]));
                    }
                    rowCounter += 1;
                }

                excel.ShowWorkbook();
                result = excel.Workbook;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(result);
        }
예제 #3
0
        // Exports an IEntityCollection to a table in given Document. BookmarkName is the name of the bookmark associated
        // with the table.
        public static dynamic ExportEntityCollection(dynamic Document, string BookmarkName, int StartRow, bool BuildColumnHeadings, IEntityCollection collection)
        {
            List<string> columnNames = new List<string>();

            // get column properties
            IEnumerable<IEntityProperty> columnProperties = collection.OfType<IEntityObject>().First().Details.Properties.All();
            int columnCounter = 1;
            int rowCounter = StartRow;

            // validate that the Document argument is expected type of Word.Document
            if (!IsWordDocumentObject(Document))
            {
                throw new System.ArgumentException("'Document' is not the expected type of dynamic. Expected dynamic should be a Word.Document dynamic.", "Document");
            }

            // validate the BookmarkName argument
            if (!IsValidBookmark(Document, BookmarkName))
            {
                throw new System.ArgumentException("'BookmarkName' was not found in 'Document'", "BookmarkName");
            }

            // validate that the bookmark is part of a table
            if (Document.Bookmarks(BookmarkName).Range.Tables.Count == 0)
            {
                throw new System.ArgumentException("No table was found at the bookmark", "BookmarkName");
            }

            // add table
            dynamic oTable = null;
            oTable = Document.Bookmarks(BookmarkName).Range.Tables(1);

            // validate the StartRow argument
            if (StartRow > oTable.Rows.Count)
            {
                throw new System.ArgumentException("'StartRow' is greater then the number of rows in the table", "StartRow");
            }

            // add columns names to the list
            foreach (IEntityProperty entityProperty in columnProperties)
            {
                columnNames.Add(entityProperty.Name);
                if (columnCounter > oTable.Columns.Count)
                {
                    oTable.Columns.Add();
                }
                if (BuildColumnHeadings)
                {
                    oTable.Cell(rowCounter, columnCounter).Range.Text = entityProperty.DisplayName;
                }
                columnCounter += 1;
            }

            // add values on the row following the headers
            if (BuildColumnHeadings)
            {
                rowCounter += 1;
            }

            // iterate the collection and extract values by column name
            foreach (IEntityObject entityObj in collection)
            {
                for (int i = 0; i <= columnNames.Count - 1; i++)
                {
                    if (rowCounter > oTable.Rows.Count)
                    {
                        oTable.Rows.Add();
                    }
                    oTable.Cell(rowCounter, i + 1).Range.Text = LightSwitchHelper.GetValue(entityObj, columnNames[i]);
                }
                rowCounter += 1;
            }

            return Document;
        }
예제 #4
0
        // Exports an IEntityCollection to a table in either the active (UseActiveDocument = True) or a new document (UseActiveDocument = False)
        public static dynamic ExportEntityCollection(IEntityCollection collection, bool UseActiveDocument)
        {
            dynamic doc = null;
            WordHelper wordProxy = new WordHelper();
            bool bUseActiveDocument = false;
            dynamic rg = null;

            // if Word is active then use it
            if (wordProxy.GetWord())
            {
                // obtain a reference to the selection range
                if (UseActiveDocument)
                {
                    rg = wordProxy.Word.Selection.Range;
                    bUseActiveDocument = true;
                }
                else
                {
                    wordProxy.CreateDocument();
                }
            }
            else
            {
                wordProxy.CreateWord();
                wordProxy.CreateDocument();
            }

            List<string> columnNames = new List<string>();

            // get column properties
            IEnumerable<IEntityProperty> columnProperties = collection.OfType<IEntityObject>().First().Details.Properties.All();
            int columnCounter = 1;
            int rowCounter = 1;

            // add table
            dynamic oTable = null;
            if (bUseActiveDocument)
            {
                oTable = wordProxy.AddTable(1, columnProperties.Count(), rg);
            }
            else
            {
                oTable = wordProxy.AddTable(1, columnProperties.Count());
            }

            // add columns names to the list
            foreach (IEntityProperty entityProperty in columnProperties)
            {
                columnNames.Add(entityProperty.Name);

                // add column headers to table
                wordProxy.SetTableCell(oTable, 1, columnCounter, entityProperty.DisplayName);
                columnCounter += 1;
            }

            // add values on the row following the headers
            rowCounter = 2;

            // iterate the collection and extract values by column name
            foreach (IEntityObject entityObj in collection)
            {
                oTable.Rows.Add();
                for (int i = 0; i <= columnNames.Count - 1; i++)
                {
                    wordProxy.SetTableCell(oTable, rowCounter, i + 1, LightSwitchHelper.GetValue(entityObj, columnNames[i]));
                }
                rowCounter += 1;
            }

            doc = wordProxy.Document;
            wordProxy.ShowDocument();

            return doc;
        }
예제 #5
0
        // Create HTML table based on IEntityCollection
        public static string HtmlExportEntityCollection(IEntityCollection collection)
        {
            List<string> columnNames = new List<string>();
            bool bFirstItem = true;
            string sBody = "";

            // get column properties
            IEnumerable<IEntityProperty> columnProperties = collection.OfType<IEntityObject>().First().Details.Properties.All();

            // iterate the collection and extract values by column name
            foreach (IEntityObject entityObj in collection)
            {
                if (bFirstItem)
                {
                    // opening an html tag and creating a table
                    //sBody = "<html>"
                    //sBody = sBody + "</br></br>"
                    //sBody = sBody + "<body style=""font-family: Arial, Helvetica, sans-serif;"" >"
                    sBody = "<table border=\"1\">";

                    // add columns names to the list
                    // row begins
                    sBody = sBody + "<tr>";
                    foreach (IEntityProperty entityProperty in columnProperties)
                    {
                        columnNames.Add(entityProperty.Name);
                        sBody = sBody + "<td>";
                        sBody = sBody + " " + entityProperty.DisplayName;
                        sBody = sBody + "</td>";
                    }
                    // row ends
                    sBody = sBody + "</tr>";
                    bFirstItem = false;
                }

                sBody = sBody + "<tr>";
                for (int i = 0; i <= columnNames.Count - 1; i++)
                {
                    sBody = sBody + "<td>";
                    sBody = sBody + LightSwitchHelper.GetValue(entityObj, columnNames[i]);
                    sBody = sBody + "</td>";
                }
                sBody = sBody + "</tr>";
            }

            // Add closing tags if there was at least one item
            // bFirstItem = True by default
            // It is set to False when the first item is encountered
            if (!bFirstItem)
            {
                // closing the tags
                sBody = sBody + "</table>";
                //sBody = sBody + "</body>"
                //sBody = sBody + "</html>"
            }
            return sBody;
        }
예제 #6
0
        // exports an IEntityCollection to a workbook starting at the specified location
        public static dynamic ExportEntityCollection(IEntityCollection collection, string Workbook, string Worksheet, string Range)
        {
            ExcelHelper xlProxy = new ExcelHelper();
            dynamic     wb      = null;
            dynamic     ws      = null;
            dynamic     rg      = null;
            dynamic     result  = null;

            try
            {
                wb = xlProxy.Excel.Workbooks.Open(Workbook);
                if ((wb != null))
                {
                    ws = wb.Worksheets(Worksheet);

                    if ((ws != null))
                    {
                        rg = ws.Range(Range);

                        List <string> columnNames = new List <string>();

                        // get column properties
                        IEnumerable <IEntityProperty> columnProperties = collection.OfType <IEntityObject>().First().Details.Properties.All();

                        int columnCounter = 0;
                        int rowCounter    = 0;

                        // add columns names to the list
                        foreach (IEntityProperty entityProperty in columnProperties)
                        {
                            columnNames.Add(entityProperty.Name);

                            rg.Offset(rowCounter, columnCounter).Value = entityProperty.DisplayName;
                            columnCounter += 1;
                        }

                        // add values on the row following the headers
                        rowCounter = 1;

                        // iterate the collection and extract values by column name
                        foreach (IEntityObject entityObj in collection)
                        {
                            for (int i = 0; i <= columnNames.Count - 1; i++)
                            {
                                rg.Offset(rowCounter, i).Value = LightSwitchHelper.GetValue(entityObj, columnNames[i]);
                            }
                            rowCounter += 1;
                        }

                        result = wb;
                        xlProxy.ShowWorkbook();
                    }
                }
            }
            catch (System.Runtime.InteropServices.COMException comException)
            {
                result = null;
                xlProxy.Quit();

                switch (comException.ErrorCode)
                {
                case 2147352565:
                    // Bad worksheet name
                    throw new System.ArgumentException("Unknown worksheet", "Worksheet");

                case -2146827284:
                    // Bad path parameter or invalid range reference
                    if (comException.InnerException == null)
                    {
                        throw new System.ArgumentException("Invalid range reference", "Range");
                    }
                    else
                    {
                        throw new System.ArgumentException("Can't open Excel workbook", comException.InnerException);
                    }

                default:
                    throw comException;
                }
            }

            return(result);
        }
예제 #7
0
        // Exports an IEntityCollection to a table in given Document. BookmarkName is the name of the bookmark associated
        // with the table.
        public static dynamic ExportEntityCollection(dynamic Document, string BookmarkName, int StartRow, bool BuildColumnHeadings, IEntityCollection collection)
        {
            List <string> columnNames = new List <string>();

            // get column properties
            IEnumerable <IEntityProperty> columnProperties = collection.OfType <IEntityObject>().First().Details.Properties.All();
            int columnCounter = 1;
            int rowCounter    = StartRow;

            // validate that the Document argument is expected type of Word.Document
            if (!IsWordDocumentObject(Document))
            {
                throw new System.ArgumentException("'Document' is not the expected type of dynamic. Expected dynamic should be a Word.Document dynamic.", "Document");
            }

            // validate the BookmarkName argument
            if (!IsValidBookmark(Document, BookmarkName))
            {
                throw new System.ArgumentException("'BookmarkName' was not found in 'Document'", "BookmarkName");
            }

            // validate that the bookmark is part of a table
            if (Document.Bookmarks(BookmarkName).Range.Tables.Count == 0)
            {
                throw new System.ArgumentException("No table was found at the bookmark", "BookmarkName");
            }

            // add table
            dynamic oTable = null;

            oTable = Document.Bookmarks(BookmarkName).Range.Tables(1);

            // validate the StartRow argument
            if (StartRow > oTable.Rows.Count)
            {
                throw new System.ArgumentException("'StartRow' is greater then the number of rows in the table", "StartRow");
            }

            // add columns names to the list
            foreach (IEntityProperty entityProperty in columnProperties)
            {
                columnNames.Add(entityProperty.Name);
                if (columnCounter > oTable.Columns.Count)
                {
                    oTable.Columns.Add();
                }
                if (BuildColumnHeadings)
                {
                    oTable.Cell(rowCounter, columnCounter).Range.Text = entityProperty.DisplayName;
                }
                columnCounter += 1;
            }

            // add values on the row following the headers
            if (BuildColumnHeadings)
            {
                rowCounter += 1;
            }

            // iterate the collection and extract values by column name
            foreach (IEntityObject entityObj in collection)
            {
                for (int i = 0; i <= columnNames.Count - 1; i++)
                {
                    if (rowCounter > oTable.Rows.Count)
                    {
                        oTable.Rows.Add();
                    }
                    oTable.Cell(rowCounter, i + 1).Range.Text = LightSwitchHelper.GetValue(entityObj, columnNames[i]);
                }
                rowCounter += 1;
            }

            return(Document);
        }
예제 #8
0
        // Exports an IEntityCollection to a table in either the active (UseActiveDocument = True) or a new document (UseActiveDocument = False)
        public static dynamic ExportEntityCollection(IEntityCollection collection, bool UseActiveDocument)
        {
            dynamic    doc                = null;
            WordHelper wordProxy          = new WordHelper();
            bool       bUseActiveDocument = false;
            dynamic    rg = null;

            // if Word is active then use it
            if (wordProxy.GetWord())
            {
                // obtain a reference to the selection range
                if (UseActiveDocument)
                {
                    rg = wordProxy.Word.Selection.Range;
                    bUseActiveDocument = true;
                }
                else
                {
                    wordProxy.CreateDocument();
                }
            }
            else
            {
                wordProxy.CreateWord();
                wordProxy.CreateDocument();
            }

            List <string> columnNames = new List <string>();

            // get column properties
            IEnumerable <IEntityProperty> columnProperties = collection.OfType <IEntityObject>().First().Details.Properties.All();
            int columnCounter = 1;
            int rowCounter    = 1;

            // add table
            dynamic oTable = null;

            if (bUseActiveDocument)
            {
                oTable = wordProxy.AddTable(1, columnProperties.Count(), rg);
            }
            else
            {
                oTable = wordProxy.AddTable(1, columnProperties.Count());
            }


            // add columns names to the list
            foreach (IEntityProperty entityProperty in columnProperties)
            {
                columnNames.Add(entityProperty.Name);

                // add column headers to table
                wordProxy.SetTableCell(oTable, 1, columnCounter, entityProperty.DisplayName);
                columnCounter += 1;
            }

            // add values on the row following the headers
            rowCounter = 2;

            // iterate the collection and extract values by column name
            foreach (IEntityObject entityObj in collection)
            {
                oTable.Rows.Add();
                for (int i = 0; i <= columnNames.Count - 1; i++)
                {
                    wordProxy.SetTableCell(oTable, rowCounter, i + 1, LightSwitchHelper.GetValue(entityObj, columnNames[i]));
                }
                rowCounter += 1;
            }


            doc = wordProxy.Document;
            wordProxy.ShowDocument();

            return(doc);
        }
예제 #9
0
        /// <summary>
        /// Returns the list of Object in a IfcRelAssociatesMaterial with the specified material select.
        /// </summary>
        /// <param name="instanceCollection"></param>
        /// <param name="matSel">The material select to search.</param>
        /// <param name="deepSearch">
        /// True if the function needs to execute a deeper semantical analysis of the relations (it can expand the query result).
        /// False if a direct analysis of explicit associations with the specific MaterialSet.
        /// </param>
        public static IEnumerable <IIfcDefinitionSelect> GetInstancesOfMaterial(this IEntityCollection instanceCollection, IIfcMaterialSelect matSel, bool deepSearch)
        {
            // Debug.WriteLine(string.Format("GetInstance {0}, {1}", matSel.EntityLabel.ToString(), DeepSearch));
            if (matSel is IIfcMaterial)
            {
                // straight return of objects of all associations
                var assocs = instanceCollection.OfType <IIfcRelAssociatesMaterial>().Where(
                    x => x.RelatingMaterial.EntityLabel == matSel.EntityLabel
                    );
                foreach (var assoc in assocs)
                {
                    // ... and returns one object at a time in the enumerable
                    foreach (var item in assoc.RelatedObjects)
                    {
                        yield return(item);
                    }
                }
            }
            else if (matSel is IIfcMaterialLayer)
            {
                if (!deepSearch)
                {
                    // straight return of objects of all associations
                    var assocs = instanceCollection.OfType <IIfcRelAssociatesMaterial>().Where(
                        x => x.RelatingMaterial.EntityLabel == matSel.EntityLabel
                        );
                    foreach (var assoc in assocs)
                    {
                        // ... and returns one object at a time in the enumerable
                        foreach (var item in assoc.RelatedObjects)
                        {
                            yield return(item);
                        }
                    }
                }
                else // this is deep search
                {
                    foreach (var straightMatch in GetInstancesOfMaterial(instanceCollection, ((IIfcMaterialLayer)matSel).ToMaterialLayerSet, false))
                    {
                        yield return(straightMatch);
                    }
                }
            }
            else if (matSel is IIfcMaterialList)
            {
                if (!deepSearch)
                {
                    // straight return of objects of all associations
                    var assocs = instanceCollection.OfType <IIfcRelAssociatesMaterial>().Where(
                        x => x.RelatingMaterial.EntityLabel == matSel.EntityLabel
                        );
                    foreach (var assoc in assocs)
                    {
                        // ... and returns one object at a time in the enumerable
                        foreach (var item in assoc.RelatedObjects)
                        {
                            yield return(item);
                        }
                    }
                }
                else // this is deep search
                {
                    // a problem with this is that some exporters produce multiple IfcMaterialList that
                    // they share the same underlying set of Materials, so we are looking for a signature of the underlying materials.
                    //
                    var baseMatArray = ((IIfcMaterialList)matSel).Materials.Select(x => x.EntityLabel).ToArray(); // this is the signature.
                    var cmp          = EqualityComparer <int> .Default;
                    foreach (var testingMaterialList in instanceCollection.OfType <IIfcMaterialList>())
                    {
                        bool bDoesMatch;
                        if (testingMaterialList.EntityLabel == matSel.EntityLabel)
                        { // no need to compare
                            bDoesMatch = true;
                        }
                        else
                        {
                            var compMatArray = testingMaterialList.Materials.Select(x => x.EntityLabel).ToArray(); // this is the other signature.
                            bDoesMatch = ArraysEqual(baseMatArray, compMatArray, cmp);
                        }
                        if (bDoesMatch)
                        {
                            foreach (var straightMatch in GetInstancesOfMaterial(instanceCollection, testingMaterialList, false))
                            {
                                yield return(straightMatch);
                            }
                        }
                    }
                }
            }
            else if (matSel is IIfcMaterialLayerSet)
            {
                // no difference in deep mode available for this type

                // given a material layerset ...
                // ... search for all its usages modes ...
                var lsUsages = instanceCollection.OfType <IIfcMaterialLayerSetUsage>().Where(
                    x => x.ForLayerSet.EntityLabel == ((IIfcMaterialLayerSet)matSel).EntityLabel
                    );
                foreach (var lsUsage in lsUsages)
                {
                    // ... then for each usage mode, searches the relations with objects ...
                    foreach (var item in GetInstancesOfMaterial(instanceCollection, lsUsage, false))
                    {
                        yield return(item);
                    }
                }
            }
            else if (matSel is IIfcMaterialLayerSetUsage)
            {
                if (deepSearch)
                {
                    // identify the underlying material layer set and return all its usages.
                    foreach (var item in instanceCollection.GetInstancesOfMaterial(((IIfcMaterialLayerSetUsage)matSel).ForLayerSet, false))
                    {
                        yield return(item);
                    }
                }
                else
                {
                    // straight return of objects of all associations
                    var assocs = instanceCollection.OfType <IIfcRelAssociatesMaterial>().Where(
                        x => x.RelatingMaterial.EntityLabel == matSel.EntityLabel
                        );
                    foreach (var assoc in assocs)
                    {
                        // ... and returns one object at a time in the enumerable
                        foreach (var item in assoc.RelatedObjects)
                        {
                            yield return(item);
                        }
                    }
                }
            }
            else
            {
                Debugger.Break();
                Debug.WriteLine("Unexpected case");
            }
        }