Insert block with attributes side loaded DWG

Insert block with attributes side loaded DWG

Michiel_
Participant Participant
375 Views
1 Reply
Message 1 of 2

Insert block with attributes side loaded DWG

Michiel_
Participant
Participant

Hi all,

 

When I try to insert a block with attributes in a side loaded dwg with the following code I get the error wrong database at the line brBlock.AttributeCollection.AppendAttribute(ar); . Can someone point me in the right direction of what I'm doing wrong?

 

Thanks in advance.

 

 using (Database targetDb = new Database(false, true))
                {
                    using (Transaction tr = targetDb.TransactionManager.StartTransaction())
                    {
                        targetDb.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, true, "");
                        targetDb.CloseInput(true);

                        bool bFixErrors = true;
                        bool becho = true;

                        ImportAlstomFiles.TransformDwg.InsertTbmBlock(targetDb);

                        targetDb.Audit(bFixErrors, becho);
                        targetDb.SaveAs(file, DwgVersion.Current);

                        tr.Commit();
                    }
                }

 

    public class TransformDwg
    {
        public static void InsertTbm(Database db)
        {

            string[] files = Directory.GetFiles(@"C:\ProgramData\Autodesk\ApplicationPlugins\SpNwDo.bundle\Contents\Block\Alstom\", "*.dwg");


            foreach (string file in files)
            {
                string blockname = Path.GetFileNameWithoutExtension(file);

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Database tmpDb = new Database(false, true);
                    tmpDb.ReadDwgFile(file, FileShare.Read, true, "");

                    db.Insert(blockname, tmpDb, true);

                    tr.Commit();
                }
            }

            // Variables to hold attribute values
            string blocknum = string.Empty;
            string station = string.Empty;
            string zone = string.Empty;
            string lijn1 = string.Empty;
            string lijn2 = string.Empty;
            string lijn3 = string.Empty;

            Point3d insPoint = new Point3d();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Open BlockTable and BlockTableRecord
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                // Retrieve the inserted block definition
                BlockTableRecord btrBlock = tr.GetObject(bt["Alstom_TBM_DATA01_S_N"], OpenMode.ForRead) as BlockTableRecord;

                // Loop through the inserted block references and retrieve attribute values
                foreach (ObjectId btrId in btr)
                {
                    if (btrId.ObjectClass.DxfName == "INSERT")
                    {
                        BlockReference br = tr.GetObject(btrId, OpenMode.ForWrite) as BlockReference;
                        SymbolTableRecord str = tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead) as SymbolTableRecord;

                        if (str.Name.StartsWith("*U"))
                        {
                            foreach (ObjectId brId in br.AttributeCollection)
                            {
                                AttributeReference ar = tr.GetObject(brId, OpenMode.ForRead) as AttributeReference;



                                if (ar.Tag == "AT_DATA_ZONE_N") zone = ar.TextString;
                                if (ar.Tag == "AT_DATA_STATION_N") station = ar.TextString;
                                if (ar.Tag == "AT_DATA_BLOCKNUM_N") blocknum = ar.TextString;


                                if (ar.Tag == "AT_DATA_DESC1_S_N" && lijn1 == string.Empty)
                                {
                                    if (ar.TextString.Contains("XXX"))
                                    {
                                        lijn1 = string.Empty;
                                    }

                                    else
                                    {
                                        lijn1 = ar.TextString;
                                    }
                                }

                                if (ar.Tag == "AT_DATA_DESC2_S_N" && lijn2 == string.Empty)
                                {
                                    if (ar.TextString.Contains("XXX"))
                                    {
                                        lijn2 = string.Empty;
                                    }

                                    else
                                    {
                                        lijn2 = ar.TextString;
                                    }
                                }

                                if (ar.Tag == "AT_DATA_DESC3_S_N" && lijn1 == string.Empty)
                                {
                                    if (ar.TextString.Contains("XXX"))
                                    {
                                        lijn1 = string.Empty;
                                    }
                                    else
                                    {
                                        lijn1 = ar.TextString;
                                    }

                                }
                                if (ar.Tag == "AT_DATA_DESC4_S_N" && lijn2 == string.Empty)
                                {
                                    if (ar.TextString.Contains("XXX"))
                                    {
                                        lijn2 = string.Empty;
                                    }
                                    else
                                    {
                                        lijn2 = ar.TextString;
                                    }
                                }
                                if (ar.Tag == "AT_DATA_DESC5_S_N" && lijn3 == string.Empty)
                                {
                                    if (ar.TextString.Contains("XXX"))
                                    {
                                        lijn3 = string.Empty;
                                    }
                                    else
                                    {
                                        lijn3 = ar.TextString;
                                    }
                                }




                                insPoint = br.Position;
                            }

                            using (BlockReference brBlock = new BlockReference(insPoint, btrBlock.ObjectId))
                            {

                                btr.UpgradeOpen();

                                // Append the new BlockReference to PaperSpace
                                btr.AppendEntity(brBlock);
                                tr.AddNewlyCreatedDBObject(brBlock, true);

                                // Loop through BlockTableRecord to create AttributeReferences
                                foreach (ObjectId btrBlockId in btrBlock)
                                {
                                    AttributeDefinition ad = tr.GetObject(btrBlockId, OpenMode.ForWrite) as AttributeDefinition;

                                    if (ad != null)
                                    {
                                        // Create a new AttributeReference in the correct database (db)
                                        using (AttributeReference ar = new AttributeReference())
                                        {
                                            // Set the attribute values and transformation
                                            ar.SetAttributeFromBlock(ad, brBlock.BlockTransform);

                                            // Update the text for each attribute based on collected values
                                            if (ar.Tag == "AT_DATA_ZONE_N")
                                                ar.TextString = zone;
                                            if (ar.Tag == "AT_DATA_STATION_N")
                                                ar.TextString = station;
                                            if (ar.Tag == "AT_DATA_BLOCKNUM_N")
                                                ar.TextString = blocknum;
                                            if (ar.Tag == "AT_DATA_DESC3_S_N")
                                                ar.TextString = lijn1;
                                            if (ar.Tag == "AT_DATA_DESC4_S_N")
                                                ar.TextString = lijn2;
                                            if (ar.Tag == "AT_DATA_DESC5_S_N")
                                                ar.TextString = lijn3;

                                            // Add the AttributeReference to the BlockReference's AttributeCollection
                                            brBlock.AttributeCollection.AppendAttribute(ar);

                                            // Ensure the attribute is added to the transaction
                                            tr.AddNewlyCreatedDBObject(ar, true);
                                        }
                                    }
                                }

                                lijn1 = string.Empty;
                                lijn2 = string.Empty;
                                lijn3 = string.Empty;
                            }
                        }
                    }
                }
            }
        }
0 Likes
Accepted solutions (1)
376 Views
1 Reply
Reply (1)
Message 2 of 2

Michiel_
Participant
Participant
Accepted solution

added the line ar.SetDatabaseDefaults(db);

 

problem is resolved

0 Likes