How can I make a program to automatically fill attributes in AutoCAD from data in an Excel sheet?

How can I make a program to automatically fill attributes in AutoCAD from data in an Excel sheet?

irahmanJS6UB
Participant Participant
289 Views
2 Replies
Message 1 of 3

How can I make a program to automatically fill attributes in AutoCAD from data in an Excel sheet?

irahmanJS6UB
Participant
Participant

Hi, I have a project I am working on with blocks and attributes. I am trying to drop a block and have a code that can fill in the attributes of the block from an Excel Data sheet. Is there a way I can code this? I know I can use ATTOUT and ATTIN, but I need to create a code using C# and .NET. Thank you.

0 Likes
290 Views
2 Replies
Replies (2)
Message 2 of 3

irahmanJS6UB
Participant
Participant

Here is an image of my blocks.

2022_07_12_15_32_49_Greenshot.png

0 Likes
Message 3 of 3

BrentBurgess1980
Collaborator
Collaborator

Hi @irahmanJS6UB,

I save my data as a csv file, then use that. I find it easier than having references to Excel.

I have just grabbed some code from a project of mine, to give you an idea of what you need to do.

using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                    PromptSelectionResult blocks = null;

                    using (FileStream fs = new FileStream(csvFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {

                        List<string[]> lines = new List<string[]>();
                        using (StreamReader sr = new StreamReader(fs))
                            {
                            do
                                {
                                string line = sr.ReadLine();

                                if (line == null)
                                    {
                                    ed.WriteMessage("The drawing register csv file '{0}' does not have any data.\nTitle blocks have not been updated. Please check register and try again.", csvFile);
                                    return;
                                    }

                                string[] entries = line.Split(',');
                                //lines.Add(entries);
                                lines.Add(ValidateLine(entries));
                                count++;
                                }
                            while (!sr.EndOfStream);
                            }



                        string[] attTags = lines[0];

                                        

                        AcApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nUpdating title block\n");

                        foreach (string s in getLayoutNames(db, tr))
                            {
                            blocks = getBlocksInLayout(tr);// s, tr);
                            if (blocks.Status == PromptStatus.OK)
                                {
                                foreach (ObjectId id in blocks.Value.GetObjectIds())
                                    {
                                    BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);

                                    LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                                    LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(lt[br.Layer], OpenMode.ForWrite);
                                    bool locked = ltr.IsLocked;
                                    ltr.IsLocked = false;

                                    if (br.AttributeCollection.Count > 0)
                                        {
                                        br.UpgradeOpen();
                                        foreach (ObjectId attId in br.AttributeCollection)
                                            {
                                            AttributeReference attref = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
                                            if (attref != null)
                                                {
                                                bool drawingFound = false;

                                                for (int i = 1; i < lines.Count; i++)
                                                    {
                                                    string[] entry = lines[i];
                                                    if (docName.Equals(entry[0], StringComparison.OrdinalIgnoreCase))
                                                        {

                                                        found = true;
                                                        for (int j = 0; j < attTags.Length; j++)
                                                            {
                                                            if (attref.Tag.Equals(attTags[j], StringComparison.OrdinalIgnoreCase))
                                                                {
                                                                if (attref.TextString != entry[j])
                                                                    {
                                                                    attref.TextString = entry[j];
                                                                    ed.WriteMessage("\n{0} updated to {1}", attref.Tag, attref.TextString);
                                                                    }
                                                                }
                                                            }
                                                        drawingFound = true;
                                                        break;
                                                        }
                                                    if (drawingFound) break;
                                                    }
                                                }
                                            }
                                        }
                                    ltr.IsLocked = locked;
                                    }
                                }
                            if (!found)
                                {

                                AcApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nCould not find {0} in register\n", docName);
                                }
                            }
                        }
                    ed.SetImpliedSelection(new ObjectId[] { });

                    tr.Commit();
                    ed.Regen();
                    }

Hope this helps

0 Likes