示例#1
0
        public void EncodedHeaderAndRows()
        {
            var bom = new ExtractedBOM
            {
                Columns = new [] { new Column {
                                       Label = "a,a"
                                   }, new Column {
                                       Label = "b,\"b"
                                   }, new Column {
                                       Label = "c\"c"
                                   } },
                Data = new []
                {
                    new object[] { "foo\",\"foo", "bar,bar", "\"baz\"" }
                }
            };

            string[] lines = BomToCsvLines(bom);
            Assert.Equal(new[]
            {
                "\"a,a\",\"b,\"\"b\",c\"c",
                "\"foo\"\",\"\"foo\",\"bar,bar\",\"baz\""
            },
                         lines);
        }
示例#2
0
        /// <summary>
        /// Convert BOM to CSV representation.
        /// </summary>
        /// <returns>CSV string.</returns>
        public static string ToCSV(this ExtractedBOM bom)
        {
            if (!bom.HasColumns())
            {
                throw new ApplicationException("Invalid BOM: header is expected.");
            }

            var columnsLength = bom.Columns.Length;

            var builder = new StringBuilder(32 * 1024);

            builder.AppendJoin(",", bom.Columns.Select(column => Encode(column.Label)));
            builder.AppendLine();

            for (var i = 0; i < bom.Data?.Length; i++)
            {
                object[] row = bom.Data[i];
                if (row.Length != columnsLength)
                {
                    throw new ApplicationException(
                              $"Invalid BOM: row {i} has different number of columns than header.");
                }

                builder.AppendJoin(",", row.Select(value => Encode(value.ToString())));
                builder.AppendLine();
            }

            return(builder.ToString());
        }
示例#3
0
        public void DataOnly()
        {
            var bom = new ExtractedBOM
            {
                Data = new []
                {
                    new object[] { "foo" }
                }
            };

            Assert.Throws <ApplicationException>(() => bom.ToCSV());
        }
        public void Run(Document doc)
        {
            LogTrace("Processing " + doc.FullFileName);

            try
            {
                ExtractedBOM extractedBOM = EmptyBOM;

                switch (doc.DocumentType)
                {
                case DocumentTypeEnum.kPartDocumentObject:
                    LogTrace("No BOM for Part documents.");
                    break;

                case DocumentTypeEnum.kAssemblyDocumentObject:

                    try
                    {
                        // TODO: remove this project activation when new inventorcoreconsole.exe
                        // will be available on PROD environment
                        var fullFileName = doc.FullFileName;
                        // close the orignal doc
                        doc.Close(true);
                        // activate default project
                        var dir = System.IO.Directory.GetCurrentDirectory();
                        ActivateProject(dir);
                        // open doc with project activated
                        doc = inventorApplication.Documents.Open(fullFileName);
                        // ^

                        extractedBOM = ProcessAssembly((AssemblyDocument)doc);
                    }
                    catch (Exception e)
                    {
                        LogError("Failed to extract BOM. " + e.ToString());
                    }
                    break;

                // complain about non-supported document types
                default:
                    throw new ArgumentOutOfRangeException(nameof(doc), "Unsupported document type");
                }

                // save as JSON
                string bomJson = JsonConvert.SerializeObject(extractedBOM, SerializerSettings);
                System.IO.File.WriteAllText(OutputJsonName, bomJson);
            }
            catch (Exception e)
            {
                LogError("Processing failed. " + e.ToString());
            }
        }
示例#5
0
        public void ColumnsOnly()
        {
            var bom = new ExtractedBOM
            {
                Columns = new [] { new Column {
                                       Label = "a"
                                   }, new Column {
                                       Label = "b", Numeric = true
                                   } }
            };

            string[] lines = BomToCsvLines(bom);
            Assert.Single(lines);
            Assert.Equal("a,b", lines[0]);
        }
示例#6
0
        private ExtractedBOM ProcessAssembly(AssemblyDocument doc)
        {
            using (new HeartBeat())
            {
                var extractedBOM = new ExtractedBOM
                {
                    Columns = new[]
                    {
                        new Shared.Column {
                            Label = "Row Number"
                        },
                        new Shared.Column {
                            Label = "Part Number"
                        },
                        new Shared.Column {
                            Label = "Quantity", Numeric = true
                        },
                        new Shared.Column {
                            Label = "Description"
                        },
                        new Shared.Column {
                            Label = "Material"
                        }
                    },
                };
                var rows = new List <object[]>();

                AssemblyComponentDefinition assemblyComponentDef = doc.ComponentDefinition;
                BOM      bom      = assemblyComponentDef.BOM;
                BOMViews bomViews = bom.BOMViews;

                // enable structured view in case of iAssembly
                if (assemblyComponentDef.IsiAssemblyFactory && bom.StructuredViewEnabled == false)
                {
                    bom.StructuredViewEnabled = true;
                }

                BOMView structureView = bomViews["Structured"];

                GetBomRowProperties(structureView.BOMRows, rows);

                extractedBOM.Data = rows.ToArray();

                return(extractedBOM);
            }
        }
示例#7
0
        public void SingleColumn()
        {
            var bom = new ExtractedBOM
            {
                Columns = new [] { new Column {
                                       Label = "a"
                                   } },
                Data = new []
                {
                    new object[] { "foo" },
                    new object[] { "bar" }
                }
            };

            string[] lines = BomToCsvLines(bom);
            Assert.Equal(new[] { "a", "foo", "bar" }, lines);
        }
示例#8
0
        public void DifferentNumberOfColumnAndData()
        {
            var bom = new ExtractedBOM
            {
                Columns = new [] { new Column {
                                       Label = "a"
                                   }, new Column {
                                       Label = "b"
                                   } },
                Data = new []
                {
                    new object[] { "foo" }
                }
            };

            Assert.Throws <ApplicationException>(() => bom.ToCSV());
        }
示例#9
0
        public void NumericData()
        {
            var bom = new ExtractedBOM
            {
                Columns = new [] { new Column {
                                       Label = "a", Numeric = true
                                   }, new Column {
                                       Label = "b", Numeric = true
                                   }, new Column {
                                       Label = "c", Numeric = true
                                   } },
                Data = new []
                {
                    new object[] { 1, 2, 3 },
                    new object[] { 4, 5, 6 }
                }
            };

            string[] lines = BomToCsvLines(bom);
            Assert.Equal(new[] { "a,b,c", "1,2,3", "4,5,6" }, lines);
        }
示例#10
0
        public override void Run(Document doc)
        {
            LogTrace("Processing " + doc.FullFileName);

            try
            {
                ExtractedBOM extractedBOM = EmptyBOM;

                switch (doc.DocumentType)
                {
                case DocumentTypeEnum.kPartDocumentObject:
                    LogTrace("No BOM for Part documents.");
                    break;

                case DocumentTypeEnum.kAssemblyDocumentObject:

                    try
                    {
                        extractedBOM = ProcessAssembly((AssemblyDocument)doc);
                    }
                    catch (Exception e)
                    {
                        LogError("Failed to extract BOM. " + e.ToString());
                    }
                    break;

                // complain about non-supported document types
                default:
                    throw new ArgumentOutOfRangeException(nameof(doc), "Unsupported document type");
                }

                // save as JSON
                string bomJson = JsonConvert.SerializeObject(extractedBOM, SerializerSettings);
                System.IO.File.WriteAllText(OutputJsonName, bomJson);
            }
            catch (Exception e)
            {
                LogError("Processing failed. " + e.ToString());
            }
        }
示例#11
0
        public void MultipleRowAndColumns()
        {
            var bom = new ExtractedBOM
            {
                Columns = new [] { new Column {
                                       Label = "a"
                                   }, new Column {
                                       Label = "b"
                                   }, new Column {
                                       Label = "c", Numeric = true
                                   } },
                Data = new []
                {
                    new object[] { "foo", "bar", 1 },
                    new object[] { "foo 2", "bar 2", 2 },
                    new object[] { "foo 3", "bar 3", 3 }
                }
            };

            string[] lines = BomToCsvLines(bom);
            Assert.Equal(new[] { "a,b,c", "foo,bar,1", "foo 2,bar 2,2", "foo 3,bar 3,3" }, lines);
        }
示例#12
0
 private static string[] BomToCsvLines(ExtractedBOM bom)
 {
     return(bom
            .ToCSV()
            .Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
 }
示例#13
0
        public void EmptyBom()
        {
            var bom = new ExtractedBOM();

            Assert.Throws <ApplicationException>(() => bom.ToCSV());
        }
示例#14
0
 public static bool HasData(this ExtractedBOM bom)
 {
     return(bom.Data?.Length > 0 && bom.Data?[0].Length > 0);
 }
示例#15
0
 public static bool HasColumns(this ExtractedBOM bom)
 {
     return(bom.Columns?.Length > 0);
 }