Insert Dynamics block with parameters and attributs

Insert Dynamics block with parameters and attributs

KZambaux
Enthusiast Enthusiast
1,080 Views
4 Replies
Message 1 of 5

Insert Dynamics block with parameters and attributs

KZambaux
Enthusiast
Enthusiast

Hi,

I have a block with parameters and attributs.

I can insert it. I see parameters and I can use it.

However, I don't have the attributs.

 

Attached, there are the block I want to insert and the code I am using.

What am I missing?

 

Thanks for your help

Ahmed

 

0 Likes
Accepted solutions (2)
1,081 Views
4 Replies
Replies (4)
Message 2 of 5

KZambaux
Enthusiast
Enthusiast

The code was not in my previous post:

[CommandMethod("MyCommand3")]
        public void MyCommand3() // This method can have any name
        {
            InsertBlock3(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database, sourceFileName, new Point3d(0, 0, 0));
        }

        public static void InsertBlock3(Database db, string FileName, Point3d InsertionPoint, double Rotation = 0)
        {
            ObjectId blkid = ObjectId.Null;
            using (Database bdb = new Database(false, true))
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    bdb.ReadDwgFile(FileName, System.IO.FileShare.Read, true, "");
                    blkid = db.Insert(System.IO.Path.GetFileNameWithoutExtension(FileName), bdb, true);

                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    BlockTableRecord btr = default(BlockTableRecord);
                    btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);


                    using (btr)
                    {
                        BlockReference bref = new BlockReference(InsertionPoint, blkid);
                        using (bref)
                        {
                            BlockTableRecord acBlkTblRec;
                            acBlkTblRec = tr.GetObject(blkid, OpenMode.ForRead) as BlockTableRecord;


                            if (bref.IsDynamicBlock)
                            {
                                DynamicBlockReferencePropertyCollection dynBrefColl = bref.DynamicBlockReferencePropertyCollection;
                                foreach (DynamicBlockReferenceProperty dynBrefProps in dynBrefColl)
                                {
                                    if (dynBrefProps.PropertyName == "Visibility1")
                                    {
                                        dynBrefProps.Value = "M36";
                                    }
                                }
                            }

                            if (btr.HasAttributeDefinitions)
                            {
                                // Add attributes from the block table record
                                foreach (ObjectId objID in btr)
                                {
                                    DBObject dbObj = tr.GetObject(objID, OpenMode.ForRead) as DBObject;

                                    if (dbObj is AttributeDefinition)
                                    {
                                        AttributeDefinition acAtt = dbObj as AttributeDefinition;

                                        if (!acAtt.Constant)
                                        {
                                            using (AttributeReference acAttRef = new AttributeReference())
                                            {
                                                acAttRef.SetAttributeFromBlock(acAtt, bref.BlockTransform);
                                                acAttRef.Position = acAtt.Position.TransformBy(bref.BlockTransform);

                                                acAttRef.TextString = acAtt.TextString;

                                                bref.AttributeCollection.AppendAttribute(acAttRef);

                                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                            }
                                        }
                                    }
                                }
                            }


                            btr.AppendEntity(bref);
                            tr.AddNewlyCreatedDBObject(bref, true);
                        }
                    }
                    tr.Commit();
                }
            }
        }
0 Likes
Message 3 of 5

norman.yuan
Mentor
Mentor
Accepted solution

The steps of your code doing are correct, but you have code error where you used wrong BlockTableRecord (ModelSpace) to find AttributeDefinition in this line:

 

                            if (btr.HasAttributeDefinitions)
                            {
                                // Add attributes from the block table record
                                foreach (ObjectId objID in btr)

You are supposed to use "acBlkTblRec" instead of "btr".

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 5

KZambaux
Enthusiast
Enthusiast

Thanks for your help. However, your solution doesn't solved my problem.

I still don't have access to attributs.

 

Via the command line in Autocad, I used the command ATTSYNC. Then I select my block. Here, attributs are showing up.

How to perform ATTSYNC in code?

Maybe my issue is coming from the attributs themselves? Are they correctly defined?

 

Ahmed

0 Likes
Message 5 of 5

KZambaux
Enthusiast
Enthusiast
Accepted solution

Hi,

Your idea was right!! Thanks for your help.

I need also to change a little my code.

Here is the code I used:

public static void InsertBlock3(Database db, string FileName, Point3d InsertionPoint, double Rotation = 0)
        {
            ObjectId blkid = ObjectId.Null;
            using (Database bdb = new Database(false, true))
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    bdb.ReadDwgFile(FileName, System.IO.FileShare.Read, true, "");
                    blkid = db.Insert(System.IO.Path.GetFileNameWithoutExtension(FileName), bdb, true);

                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    BlockTableRecord btr = default(BlockTableRecord);
                    btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);


                    using (btr)
                    {
                        BlockReference bref = new BlockReference(InsertionPoint, blkid);
                        using (bref)
                        {
                            BlockTableRecord acBlkTblRec;
                            acBlkTblRec = tr.GetObject(blkid, OpenMode.ForRead) as BlockTableRecord;

                            BlockTableRecord accurspaceblktblrecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                            accurspaceblktblrecord.AppendEntity(bref);
                            tr.AddNewlyCreatedDBObject(bref, true);

                            if (bref.IsDynamicBlock)
                            {
                                DynamicBlockReferencePropertyCollection dynBrefColl = bref.DynamicBlockReferencePropertyCollection;
                                foreach (DynamicBlockReferenceProperty dynBrefProps in dynBrefColl)
                                {
                                    if (dynBrefProps.PropertyName == "Visibility1")
                                    {
                                        dynBrefProps.Value = "M36";
                                    }
                                }
                            }

                            if (acBlkTblRec.HasAttributeDefinitions)
                            {
                                // Add attributes from the block table record
                                foreach (ObjectId objID in acBlkTblRec)
                                {
                                    DBObject dbObj = tr.GetObject(objID, OpenMode.ForRead) as DBObject;

                                    if (dbObj is AttributeDefinition)
                                    {
                                        AttributeDefinition acAtt = dbObj as AttributeDefinition;

                                        if (!acAtt.Constant)
                                        {
                                            using (AttributeReference acAttRef = new AttributeReference())
                                            {
                                                acAttRef.SetAttributeFromBlock(acAtt, bref.BlockTransform);
                                                acAttRef.Position = acAtt.Position.TransformBy(bref.BlockTransform);

                                                acAttRef.TextString = acAtt.TextString;

                                                bref.AttributeCollection.AppendAttribute(acAttRef);

                                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    tr.Commit();
                }
            }
        }

 

With this code, attributs are showing perfectly.

I used this code as well

https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-2107599E-9405-4D8B-A6DD-83D603B41568

 

 

I am still facing an issue.

As said, attributs are showing up. However, when I move the block, then regen, attributs values are not updated.

When I insert my block via the tool itself, I can move the block, and attributs are updated with a regen. Here nothing is updated.

 

Ahmed

0 Likes