See the top rated post in this thread. Click here

Results 1 to 2 of 2

Thread: Update Block Attributes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member
    Join Date
    2020-04
    Posts
    4
    Login to Give a bone
    0

    Default Update Block Attributes

    Hello

    I want to update a block attribute after inserting the block. The block is getting inserted. When I try to obtain Attribute collection I see nothing. I have Pasted the code below. Please advice the solution
    Code:
    [CommandMethod("BlockAtt")]
            public void BlockAtt()
            {
                //AcadApplication acAPP = new AcadApplication();
    
                ObjectId msId;
    
                string BlockPATH = @"C:\AutoGen\Library\LWP\Rail Reaction\React_CarRail C - CwtR.dwg";
    
                 //= new AcadApplication();
                Document acD = Application.DocumentManager.MdiActiveDocument;
                Database acCurDb = acD.Database;
    
                //string BlockPATH = @"C:\AutoGen\Library\LWP\Jamb\BS\CO - E-102 - BS - Plan.dwg";
                Point3d insPT = new Point3d(0, 0, 0);
                int sFac = 2;
                
    
                using (DocumentLock acLocDoc = acD.LockDocument())
                {
                    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                    {
    
                        ObjectId ObjID;
    
                        // Open the Block table for read
                        BlockTable acBlkTbl = acCurDb.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
    
                        // Open Modelspace to add block
                        BlockTableRecord ms = acBlkTbl[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
    
                        using (Database dbInsert = new Database(false, true))
                        {
                            dbInsert.ReadDwgFile(BlockPATH, FileShare.Read, true, "");
                            ObjID = acCurDb.Insert(Path.GetFileNameWithoutExtension(BlockPATH), dbInsert, true);
                        }
    
                        BlockReference BlkRef = new BlockReference(insPT, ObjID);
                        ms.AppendEntity(BlkRef);
                        acTrans.AddNewlyCreatedDBObject(BlkRef, true);
    
                        Scale3d BlkScale = new Scale3d(sFac, sFac, sFac);
                        BlkRef.ScaleFactors = BlkScale;
    
                        BlkRef.Rotation = (0 * Math.PI / 180);
    
                        AcadApplication AcadAPP = (AcadApplication)Application.AcadApplication;
                        AcadAPP.ZoomExtents();
    
                        
                        msId = acBlkTbl[BlockTableRecord.ModelSpace];
                        //UpdateAttributesInBlock(msId, blockName, attName, AttValue, BlkRef);
    
                        acTrans.Commit();
                    }
                    UpdateAttributesInBlock(msId, blockName, attName, AttValue);
                }
    
    
            }
    
            public static void UpdateAttributesInBlock(ObjectId btrId, string BlkNm, string attNm, string AttVal)
            {
    
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
    
                Transaction tr = db.TransactionManager.StartTransaction();
                using (tr)
                {
                    //BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                    // Not needed, but quicker than aborting
    
                    foreach (ObjectId entID in btr)
                    {
                        Entity ent = tr.GetObject(entID, OpenMode.ForRead) as Entity;
                        if (ent != null)
                        {
    
                            BlockReference br = ent as BlockReference;
                            if (br != null)
                            {
                                BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
                                if (bd.Name == BlkNm)
                                {
                                    AttributeCollection attCol = br.AttributeCollection;
                                    
                                    foreach (ObjectId arId in attCol)
                                    {
                                        DBObject obj = tr.GetObject(arId, OpenMode.ForRead);
                                        AttributeReference ar = obj as AttributeReference;
                                        if (ar != null)
                                        {
                                            if (ar.Tag == attNm)
                                            {
                                                ar.UpgradeOpen();
                                                ar.TextString = AttVal;
                                                ar.DowngradeOpen();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
    
                    tr.Commit();
                }
            }
    Last edited by Opie; 2021-03-02 at 02:16 PM.

Similar Threads

  1. attributes in block attributes
    By t_goofbeer in forum AutoLISP
    Replies: 8
    Last Post: 2009-09-17, 08:46 PM
  2. Why I can not update the block's attributes
    By nfyu in forum VBA/COM Interop
    Replies: 4
    Last Post: 2009-02-25, 01:18 PM
  3. Replies: 21
    Last Post: 2007-03-20, 02:03 PM
  4. Replies: 5
    Last Post: 2006-09-11, 05:56 PM
  5. Replies: 9
    Last Post: 2005-01-25, 02:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •