Attribute to excel with count

Attribute to excel with count

CAD-Geek
Advocate Advocate
1,057 Views
7 Replies
Message 1 of 8

Attribute to excel with count

CAD-Geek
Advocate
Advocate

After searching in forum I found code that extracts data from attribute block 

but I need to modify the excel output result as DATAEXTRACTION command

            List<object[]> rooms = new List<object[]>();
            using (var tr = db.TransactionManager.StartTransaction())
            {
                foreach (SelectedObject so in psr.Value)
                {
                    BlockReference bref = (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead);
                    //populate array with attribute data
                    Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcoll = bref.AttributeCollection;
                    object[] info = new object[4];
                    foreach (ObjectId id in attcoll)
                    {
                        AttributeReference atref = (AttributeReference)tr.GetObject(id, OpenMode.ForRead);

                        if (atref.Tag=="QUANTITY")
                        {
                            info[0] = atref.TextString;
                        }
                        if (atref.Tag=="MATERIAL")
                        {
                            info[1] = atref.TextString;
                        }
                        if (atref.Tag=="STANDARD")
                        {
                            info[2] = atref.TextString;
                        }

                        if (!rooms.Contains(info))
                        {
                            
                            rooms.Add(info);
                        }
                    }
                }
                tr.Commit();
            }

the excel output

1.JPG

 

I need the result to be something like this 

22.JPG

0 Likes
Accepted solutions (1)
1,058 Views
7 Replies
Replies (7)
Message 2 of 8

SENL1362
Advisor
Advisor

search for GROUP BY

0 Likes
Message 3 of 8

_gile
Consultant
Consultant

Hi,

 

You can have a look at this:

http://www.theswamp.org/index.php?topic=31859.msg452527#msg452527



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 8

CAD-Geek
Advocate
Advocate

thnx @_gile for reply I tried your code but I got this exception 

Gile.JPG

0 Likes
Message 5 of 8

CAD-Geek
Advocate
Advocate

thnx @SENL1362 for the reply I tried your suggest like this 

 
if (!rooms.Contains(info))
                        {
                            info[3] = rooms.GroupBy(x=>x).Count() + "c";
                            rooms.Add(info);
                        }

SENL.JPG

0 Likes
Message 6 of 8

SENL1362
Advisor
Advisor

For this:

 

groupby.png

 

 

 

 

 

Do this:

                //var info = new object[4];
                var rooms = new List<object[]>();
                rooms.Add(new object[] { 3, "", "MS02" });
                rooms.Add(new object[] { 1, "", "MS19E" });
                rooms.Add(new object[] { 1, "", "MS19E" });
                rooms.Add(new object[] { 3, "", "MS17B" });
                rooms.Add(new object[] { 1, "", "MS19E" });
                rooms.Add(new object[] { "1m", "439C", "" });
                rooms.Add(new object[] { "1m", "439C", "" });
                rooms.Add(new object[] { "1m", "439C", "" });
                rooms.Add(new object[] { 1, "", "MS47G" });
                rooms.Add(new object[] { 1, "", "MS19E" });
                rooms.Add(new object[] { 1, "", "MS19E" });
                rooms.Add(new object[] { 1, "", "MS19E" });
                rooms.Add(new object[] { "5m", "439C", "" });
                rooms.Add(new object[] { "5m", "439C", "" });
                rooms.Add(new object[] { "5m", "430D", "" });
                rooms.Add(new object[] { "5m", "439C", "" });


                var groupedRooms = rooms.GroupBy(x => new { A = x[1], B = x[2], C = x[0] }).Select(g => new { Count = g.Count(), Quantity = g.Key.C, Material = g.Key.A, Standard = g.Key.B });
                ed.WriteMessage("\nCount, 2-Quantity, 3-Material, 4-Standard");
                //foreach (var r in groupedRooms.OrderBy(n=>n.Count).ThenBy(m=>m.Material))
                foreach (var gr in groupedRooms)
                {
                    ed.WriteMessage($"\n{gr.Count,5}, {gr.Quantity,-10}, {gr.Material,-10}, {gr.Standard,-10}");
                }
0 Likes
Message 7 of 8

CAD-Geek
Advocate
Advocate

I tried 

 

                        if (atref.Tag=="QUANTITY")
                        {
                            rooms.Add(new object[] { atref.TextString });
                            //info[0] = atref.TextString;
                        }
                        if (atref.Tag=="MATERIAL")
                        {
                            rooms.Add(new object[] { atref.TextString });
                            //info[1] = atref.TextString;
                        }
                        if (atref.Tag=="STANDARD")
                        {
                            rooms.Add(new object[] { atref.TextString });
                            //info[2] = atref.TextString;
                        }
var groupedRooms = rooms.GroupBy(x => new { A = x[1], B = x[2], C = x[0] }).Select(g => new { Count = g.Count(), Quantity = g.Key.C, Material = g.Key.A, Standard = g.Key.B });
foreach (var r in groupedRooms.OrderBy(n=>n.Count).ThenBy(m=>m.Material))

and I use this code to export to excel @Hallex

ExcelTool.FillExistingBook(@"C:\Test\Programming\BCOUNT.xlsx", new object[] { "Count","QUANTITY", "MATERIAL", "Standard" },  excdata);

but I got the error 

Index was outside the bounds of the array

 

0 Likes
Message 8 of 8

_gile
Consultant
Consultant
Accepted solution

@CAD-Geek wrote:

thnx @_gile for reply I tried your code but I got this exception 

Gile.JPG


As you have a tag named "QUANTITY"  you should change the "Quantity" column name of the DataTable.

 

Try replacing:

dTable.Columns.Add("Quantity", typeof(int));

with:

dTable.Columns.Add("Count", typeof(int));

or whatever else different from any attribute Tag.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub