See the top rated post in this thread. Click here

Results 1 to 2 of 2

Thread: Update Block Attributes

  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.

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,397
    Login to Give a bone
    1

    Default Re: Update Block Attributes

    Your UpdateAttributesInBlock function tries to update attributes in the block definition. You need to update the attributes that are in the BlockReference. The block definition does not really contain attributes, but attribute definitions that are a template for the inserted block. The typical workflow is to get a blockref, check it's HasAttributes property and if true, edit the atts. However, since you are inserting the block, you already have an instance of BlkRef. Use that to update the atts.
    C:> ED WORKING....

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
  •