<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Insert Dynamics block with parameters and attributs in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8571453#M23632</link>
    <description>&lt;P&gt;The code was not in my previous post:&lt;/P&gt;
&lt;PRE&gt;[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();
                }
            }
        }&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Feb 2019 14:56:21 GMT</pubDate>
    <dc:creator>KZambaux</dc:creator>
    <dc:date>2019-02-04T14:56:21Z</dc:date>
    <item>
      <title>Insert Dynamics block with parameters and attributs</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8571442#M23631</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a block with parameters and attributs.&lt;/P&gt;
&lt;P&gt;I can insert it. I see parameters and I can use it.&lt;/P&gt;
&lt;P&gt;However, I don't have the attributs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Attached, there are the block I want to insert and the code I am using.&lt;/P&gt;
&lt;P&gt;What am I missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your help&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 14:53:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8571442#M23631</guid>
      <dc:creator>KZambaux</dc:creator>
      <dc:date>2019-02-04T14:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Dynamics block with parameters and attributs</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8571453#M23632</link>
      <description>&lt;P&gt;The code was not in my previous post:&lt;/P&gt;
&lt;PRE&gt;[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();
                }
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Feb 2019 14:56:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8571453#M23632</guid>
      <dc:creator>KZambaux</dc:creator>
      <dc:date>2019-02-04T14:56:21Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Dynamics block with parameters and attributs</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8571918#M23633</link>
      <description>&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;                            if (&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;btr&lt;/FONT&gt;&lt;/STRONG&gt;.HasAttributeDefinitions)
                            {
                                // Add attributes from the block table record
                                foreach (ObjectId objID in btr)&lt;/PRE&gt;
&lt;P&gt;You are supposed to use "acBlkTblRec" instead of "btr".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 17:37:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8571918#M23633</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2019-02-04T17:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Dynamics block with parameters and attributs</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8573326#M23634</link>
      <description>&lt;P&gt;Thanks for your help. However, your solution doesn't solved my problem.&lt;/P&gt;
&lt;P&gt;I still don't have access to attributs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Via the command line in Autocad, I used the command ATTSYNC. Then I select my block. Here, attributs are showing up.&lt;/P&gt;
&lt;P&gt;How to perform ATTSYNC in code?&lt;/P&gt;
&lt;P&gt;Maybe my issue is coming from the attributs themselves? Are they correctly defined?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Tue, 05 Feb 2019 08:11:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8573326#M23634</guid>
      <dc:creator>KZambaux</dc:creator>
      <dc:date>2019-02-05T08:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Dynamics block with parameters and attributs</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8574036#M23635</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Your idea was right!! Thanks for your help.&lt;/P&gt;
&lt;P&gt;I need also to change a little my code.&lt;/P&gt;
&lt;P&gt;Here is the code I used:&lt;/P&gt;
&lt;PRE&gt;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();
                }
            }
        }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With this code, attributs are showing perfectly.&lt;/P&gt;
&lt;P&gt;I used this code as well&lt;/P&gt;
&lt;P&gt;&lt;A title="Create Attribute Definitions and Attribute References (.NET)" href="https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-2107599E-9405-4D8B-A6DD-83D603B41568" target="_blank" rel="noopener"&gt;https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-2107599E-9405-4D8B-A6DD-83D603B41568&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am still facing an issue.&lt;/P&gt;
&lt;P&gt;As said, attributs are showing up. However, when I move the block, then regen, attributs values are not updated.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Tue, 05 Feb 2019 14:00:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-dynamics-block-with-parameters-and-attributs/m-p/8574036#M23635</guid>
      <dc:creator>KZambaux</dc:creator>
      <dc:date>2019-02-05T14:00:19Z</dc:date>
    </item>
  </channel>
</rss>

