<?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: ATTSYNC in VB.NET in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/11480047#M46863</link>
    <description>&lt;P&gt;This crosses many classes so I will try and show the relevant code.&amp;nbsp; This is where the attribute sync is called&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; //Legacy titleblock was found
                if (oldTbList != null)
                {
                    using (Transaction acTrans = acDb.TransactionManager.StartTransaction())
                    {
                        BlockTable acBlkTbl = (BlockTable)acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead);
                        BlockTableRecord acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[oldTbList[0].Name], OpenMode.ForRead, false);

                        acBlkTblRec.SynchronizeAttributes();
                        acTrans.Commit();
                    }
                    _TbId = ReplaceTB(oldTbList, TbId, replaceLayout);
                }&lt;/LI-CODE&gt;&lt;P&gt;I committed the transaction here.&amp;nbsp; If I don't commit then I get the old Attribute Collection later.&amp;nbsp; On line 100 in the below code is where I get eWasErased.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private ObjectId ReplaceTB(List&amp;lt;TitleBlkLegacyProps&amp;gt; oldTbList, ObjectId tbId, bool delLayout = false)
        {

            string[] blkName = { newTBName };

            //Import the new Title Block
            SysLog.WriteLineToLog(DateTime.Now.ToString("yyyy/MM/dd") + " " + DateTime.Now.ToString("hh:mm:ss tt - ") + "Importing replacement title block");
            AcDb.ImportMultiBlocks(blkName, TBFullPath);

            ObjectId blkRecId = ObjectId.Null;
            ObjectId acBlkRefInsertId = ObjectId.Null;

            using (Transaction acTrans = AcDb.TransactionManager.StartTransaction())
            {
                BlockTable acTblItm = (BlockTable)acTrans.GetObject(AcDb.BlockTableId, OpenMode.ForRead);

                //Get the Title block definition (BlockRecord)
                acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acTblItm[newTBName], OpenMode.ForRead);
                blkRecId = acBlkTblRec.Id;

                //If the title block definition was found
                if (blkRecId != ObjectId.Null)
                {

                    using (BlockReference acBlkRefInsert = new BlockReference(new Point3d(0, 0, 0), blkRecId))
                    {
                        acBlkRefInsert.Layer = tbLayer;
                        BlockTableRecord acSpace;
                        if (delLayout == true)
                        {
                            //TODO get from DB
                            //Create New Layout, get the ID and block table record
                            AcLayout acLayout = new AcLayout("Add", "MRD-Print");
                            ObjectId acLayOID = acLayout.LayoutIdNew;
                            AcLayoutBtr = acLayout.LayoutBtrId;
                            //Get the Existing Paperspace Viewport
                            acLayout.GetVport(AcLayoutNameOld);
                            AcVprt = acLayout.ViewportOld;
                            //Create New viewport in New layout
                            new AcVprt(AcDb);
                            
                        }

                        acSpace = (BlockTableRecord)acTrans.GetObject(AcLayoutBtr.ObjectId, OpenMode.ForWrite);

                        acSpace.AppendEntity(acBlkRefInsert);
                        acTrans.AddNewlyCreatedDBObject(acBlkRefInsert, true);

                        acBlkRefInsertId = acBlkRefInsert.Id;

                        foreach (ObjectId id in acBlkTblRec)
                        {
                            DBObject obj = id.GetObject(OpenMode.ForRead);
                            AttributeDefinition attDef = obj as AttributeDefinition;
                            if ((attDef != null) &amp;amp;&amp;amp; (!attDef.Constant))
                            {
                                using (AttributeReference attRef = new AttributeReference())
                                {
                                    attRef.SetAttributeFromBlock(attDef, acBlkRefInsert.BlockTransform);
                                    attRef.TextString = "";
                                    acBlkRefInsert.AttributeCollection.AppendAttribute(attRef);
                                    acTrans.AddNewlyCreatedDBObject(attRef, true);
                                }
                            }
                        }
                    }

                    BlockReference acBlkRefOld = (BlockReference)acTrans.GetObject(tbId, OpenMode.ForWrite);
                    BlockReference acBlkRefNew = (BlockReference)acTrans.GetObject(acBlkRefInsertId, OpenMode.ForWrite);
                    Dictionary&amp;lt;string, string&amp;gt; acBlkRefAttDict = acBlkRefOld.GetAttToDict(acBlkTblRec);
                    AttributeCollection acAttColOld = acBlkRefOld.AttributeCollection;
                    AttributeCollection acAttColNew = acBlkRefNew.AttributeCollection;

                    // TODO Get From DB create external clean up format RunScript???
                    string[] exclude = { "", " ", "  ", ".", "..", "...", "-", "_" };
                    string replaceChar = ".";
                    string tbSaveAsTag = "DWGNO";

                    for (int i = 0; i &amp;lt;= oldTbList.Count - 1; i++)
                    {
                        string[] attValue = new string[1];
                        string[] attTag = new string[1];
                        string[] attCombine = new string[1];
                        if (oldTbList[i].AttOldIndex.Contains(";"))
                        {
                            attValue = oldTbList[i].AttOldIndex.Split(';');
                            attTag = oldTbList[i].AttOldTag.Split(';');
                            attCombine = oldTbList[i].CombineChar.Split(';');
                        }
                        else
                        {
                            attValue[0] = oldTbList[i].AttOldIndex;
                            attTag[0] = oldTbList[i].AttOldTag;
                            attCombine[0] = oldTbList[i].CombineChar;
                        }

                        for (int j = 0; j &amp;lt;= attValue.Length - 1; j++)
                        {
                            int attOld = Convert.ToInt32(attValue[j]);
                            AttributeReference attRefOld = (AttributeReference)acTrans.GetObject(acAttColOld[attOld], OpenMode.ForWrite);
                            if (attRefOld.Tag.Equals(attTag[j], StringComparison.OrdinalIgnoreCase))
                            {
                                int attNew = Convert.ToInt32(oldTbList[i].AttNewIndex);
                                AttributeReference attRefNew = (AttributeReference)acTrans.GetObject(acAttColNew[attNew], OpenMode.ForWrite);

                                foreach (string excludeStr in exclude)
                                {
                                    if (attRefOld.TextString.Equals(excludeStr, StringComparison.OrdinalIgnoreCase))
                                    {
                                        attRefOld.TextString = "";
                                    }
                                }
                                attRefNew.TextString = attRefNew.TextString + attCombine[j] + attRefOld.TextString;
                            }
                        }
                    }
                    acBlkRefOld.Erase();

                    new AcLayout("DeleteContains", acLayNameOld: "Layer");
                    if (delLayout == true)
                    {
                        new AcLayout("Delete", acLayNameOld: AcLayoutNameOld);
                    }
                    

                    foreach (ObjectId attId in acAttColNew)
                    {

                        AttributeReference attRef = (AttributeReference)acTrans.GetObject(attId, OpenMode.ForWrite);

                        foreach (string excludeStr in exclude)
                        {
                            if (attRef.TextString.Equals(excludeStr, StringComparison.OrdinalIgnoreCase))
                            {
                                attRef.TextString = replaceChar;
                            }
                        }
                        if (attRef.Tag.Equals(tbSaveAsTag, StringComparison.OrdinalIgnoreCase))
                        {
                            FnClnUp fnClnUp = new FnClnUp(attRef.TextString);
                            attRef.TextString = fnClnUp.Filename;

                        }
                    }
                }
                else
                {
                    // TODO if the block wasnt found

                }

                acTrans.Commit();
            }

            tbId = acBlkRefInsertId;
            return acBlkRefInsertId;
        }&lt;/LI-CODE&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>Thu, 13 Oct 2022 12:57:54 GMT</pubDate>
    <dc:creator>CBennett34F8SS</dc:creator>
    <dc:date>2022-10-13T12:57:54Z</dc:date>
    <item>
      <title>ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4645057#M46841</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know what the dot net equivalent is for attsync?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use invisible attributes in the block references created by my dot net code, but I just noticed a strange AutoCAD behaviour:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right-clicking on the block with attributes, and selecting the 'Edit Block in-place' option correctly displays the hidden attributes, making it possible for them to be deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, after deleting all the attributes and saving the ref edit, I right-click on the same block again, this time to check if it still has attibutes; and it does, even though I deleted all the attributes! There seems to be some sort of 'held-in-memory' issue here, which, after I did some research, I discovered that the ATTSYNC command is perfect for purging those deleted attributes from the block reference, AFTER they have been physically deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is, how do I implement this ATTSYNC command in my dot net code, without using sendcommand?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found a thread on this forum regarding this issue, which led me to the swamp; but then the code I found there is in F#, and so I was wondering if anyone knows of a simple VB.NET way of mimicking the AutoCAD ATTSYNC command?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cat&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2013 19:28:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4645057#M46841</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-11-22T19:28:05Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4647113#M46844</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a simple and 'brute force' method which erases the existing AttributeReferences from the inserted BlockReferences and adds them new ones from the BlockTableRecord AttributeDefinitions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SynchronizeAttributes() method is defined as an extension method for the BlockTableRecord type so that it can be called as an instance method of this type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example (assuming btr is a BlocTableRecord instance) : btr.SynchronizeAttributes()&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;C# code&lt;/P&gt;
&lt;PRE&gt;using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using AcRx = Autodesk.AutoCAD.Runtime;

namespace AttSyncSample
{
    public static class ExtensionMethods
    {
        public static void SynchronizeAttributes(this BlockTableRecord target)
        {
            if (target == null)
                throw new ArgumentNullException("btr");

            Transaction tr = target.Database.TransactionManager.TopTransaction;
            if (tr == null)
                throw new AcRx.Exception(ErrorStatus.NoActiveTransactions);


            RXClass attDefClass = RXClass.GetClass(typeof(AttributeDefinition));
            List&amp;lt;AttributeDefinition&amp;gt; attDefs = new List&amp;lt;AttributeDefinition&amp;gt;();
            foreach (ObjectId id in target)
            {
                if (id.ObjectClass == attDefClass)
                {
                    AttributeDefinition attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
                    attDefs.Add(attDef);
                }
            }
            foreach (ObjectId id in target.GetBlockReferenceIds(true, false))
            {
                BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForWrite);
                br.ResetAttributes(attDefs);
            }
            if (target.IsDynamicBlock)
            {
                foreach (ObjectId id in target.GetAnonymousBlockIds())
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                    foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
                    {
                        BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForWrite);
                        br.ResetAttributes(attDefs);
                    }
                }
            }
        }

        private static void ResetAttributes(this BlockReference br, List&amp;lt;AttributeDefinition&amp;gt; attDefs)
        {
            TransactionManager tm = br.Database.TransactionManager;
            Dictionary&amp;lt;string, string&amp;gt; attValues = new Dictionary&amp;lt;string, string&amp;gt;();
            foreach (ObjectId id in br.AttributeCollection)
            {
                if (!id.IsErased)
                {
                    AttributeReference attRef = (AttributeReference)tm.GetObject(id, OpenMode.ForWrite);
                    attValues.Add(attRef.Tag, attRef.TextString);
                    attRef.Erase();
                }
            }
            foreach (AttributeDefinition attDef in attDefs)
            {
                AttributeReference attRef = new AttributeReference();
                attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                if (attValues.ContainsKey(attDef.Tag))
                {
                    attRef.TextString = attValues[attDef.Tag.ToUpper()];
                }
                br.AttributeCollection.AppendAttribute(attRef);
                tm.AddNewlyCreatedDBObject(attRef, true);
            }
        }
    }
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;VB code&lt;/P&gt;
&lt;PRE&gt;Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime

Module ExtensionMethods

    &amp;lt;System.Runtime.CompilerServices.Extension&amp;gt; _
    Public Sub SynchronizeAttributes(target As BlockTableRecord)
        If target Is Nothing Then
            Throw New ArgumentNullException("btr")
        End If

        Dim tr As Transaction = target.Database.TransactionManager.TopTransaction
        If tr Is Nothing Then
            Throw New Exception(ErrorStatus.NoActiveTransactions)
        End If

        Dim attDefClass As RXClass = RXClass.GetClass(GetType(AttributeDefinition))
        Dim attDefs As New List(Of AttributeDefinition)()
        For Each id As ObjectId In target
            If id.ObjectClass = attDefClass Then
                Dim attDef As AttributeDefinition = DirectCast(tr.GetObject(id, OpenMode.ForRead), AttributeDefinition)
                attDefs.Add(attDef)
            End If
        Next
        For Each id As ObjectId In target.GetBlockReferenceIds(True, False)
            Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForWrite), BlockReference)
            br.ResetAttributes(attDefs)
        Next
        If target.IsDynamicBlock Then
            For Each id As ObjectId In target.GetAnonymousBlockIds()
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(id, OpenMode.ForRead), BlockTableRecord)
                For Each brId As ObjectId In btr.GetBlockReferenceIds(True, False)
                    Dim br As BlockReference = DirectCast(tr.GetObject(brId, OpenMode.ForWrite), BlockReference)
                    br.ResetAttributes(attDefs)
                Next
            Next
        End If
    End Sub

    &amp;lt;System.Runtime.CompilerServices.Extension&amp;gt; _
    Private Sub ResetAttributes(br As BlockReference, attDefs As List(Of AttributeDefinition))
        Dim tm As TransactionManager = br.Database.TransactionManager
        Dim attValues As New Dictionary(Of String, String)()
        For Each id As ObjectId In br.AttributeCollection
            If Not id.IsErased Then
                Dim attRef As AttributeReference = DirectCast(tm.GetObject(id, OpenMode.ForWrite), AttributeReference)
                attValues.Add(attRef.Tag, attRef.TextString)
                attRef.Erase()
            End If
        Next
        For Each attDef As AttributeDefinition In attDefs
            Dim attRef As New AttributeReference()
            attRef.SetAttributeFromBlock(attDef, br.BlockTransform)
            If attValues IsNot Nothing AndAlso attValues.ContainsKey(attDef.Tag) Then
                attRef.TextString = attValues(attDef.Tag.ToUpper())
            End If
            br.AttributeCollection.AppendAttribute(attRef)
            tm.AddNewlyCreatedDBObject(attRef, True)
        Next
    End Sub

End Module&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Nov 2013 18:01:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4647113#M46844</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2013-11-23T18:01:35Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4650199#M46845</link>
      <description>&lt;P&gt;Many thanks, Gilles... your code worked the magic!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2013 18:10:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4650199#M46845</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-11-25T18:10:52Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4650283#M46846</link>
      <description>&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if you want it to work with mtext attributes, in the ResetAttributes method, replace:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;attValues.Add(attRef.Tag, attRef.TextString)&lt;/PRE&gt;
&lt;P&gt;with,&lt;/P&gt;
&lt;P&gt;C#&lt;/P&gt;
&lt;PRE&gt;attValues.Add(attRef.Tag,
    attRef.IsMTextAttribute ? attRef.MTextAttribute.Contents : attRef.TextString);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;VB&lt;/P&gt;
&lt;PRE&gt;attValues.Add( _
    attRef.Tag, _
    If(attRef.IsMTextAttribute, attRef.MTextAttribute.Contents, attRef.TextString))&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2013 18:41:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4650283#M46846</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2013-11-25T18:41:42Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4652859#M46847</link>
      <description>&lt;P&gt;You can find a better implementation (works with multiline attributes, constant attributes, dynamic blocks) at TheSwamp:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.theswamp.org/index.php?topic=31859.msg508802#msg508802" target="_blank"&gt;http://www.theswamp.org/index.php?topic=31859.msg508802#msg508802&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2013 14:07:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4652859#M46847</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2013-11-26T14:07:45Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4653133#M46848</link>
      <description>Quite right it is, worked like a charm! many thanks again!</description>
      <pubDate>Tue, 26 Nov 2013 15:31:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/4653133#M46848</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-11-26T15:31:39Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/5654319#M46849</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="line-height: 16.7999992370605px;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 May 2015 21:27:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/5654319#M46849</guid>
      <dc:creator>arcticad</dc:creator>
      <dc:date>2015-05-27T21:27:20Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/6796899#M46850</link>
      <description>Trying the following codes to open a drawing in memory but above codes seems doesn't works using (Database db = new Database(false, true)) { try { db.ReadDwgFile(theProjDwg4CNBlkReplace, System.IO.FileShare.ReadWrite, false, null); using (Transaction trans = db.TransactionManager.StartTransaction()) { } } catch {} }</description>
      <pubDate>Tue, 10 Jan 2017 22:19:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/6796899#M46850</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-01-10T22:19:03Z</dc:date>
    </item>
    <item>
      <title>Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/6811463#M46851</link>
      <description>&lt;P&gt;I found the solution from the following link.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://through-the-interface.typepad.com/through_the_interface/2007/07/updating-a-sp-1.html?cid=6a00d83452464869e2010536eec215970b#comment-6a00d83452464869e2010536eec215970b" target="_blank"&gt;http://through-the-interface.typepad.com/through_the_interface/2007/07/updating-a-sp-1.html?cid=6a00d83452464869e2010536eec215970b#comment-6a00d83452464869e2010536eec215970b&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Update 2:&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The above code does not realign attributes after editing their values: if your attributes are anything other than left-justified, you will need to make a call to AdjustAlignment on the attribute reference after editing it.&lt;/P&gt;&lt;P&gt;There's a trick to this: you need to make sure the working database is set to the drawing you're working on, as well as passing it as an argument to the function.&lt;/P&gt;&lt;P&gt;You could set the working database early in the code, or insert this code to do it locally (the choice is yours):&lt;/P&gt;&lt;DIV&gt;&lt;P&gt;&amp;nbsp; ar.TextString = attbValue;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; // Begin alignment code&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; Database&lt;/SPAN&gt; wdb = &lt;SPAN&gt;HostApplicationServices&lt;/SPAN&gt;.WorkingDatabase;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; HostApplicationServices&lt;/SPAN&gt;.WorkingDatabase = db;&lt;/P&gt;&lt;P&gt;&amp;nbsp; ar.AdjustAlignment(db);&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; HostApplicationServices&lt;/SPAN&gt;.WorkingDatabase = wdb;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; // End alignment code&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; ar.DowngradeOpen();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 17 Jan 2017 19:05:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/6811463#M46851</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-01-17T19:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/8782754#M46852</link>
      <description>&lt;P&gt;This code work perfect in AutoCAD.&lt;/P&gt;
&lt;P&gt;But it does nothing in AutoCAD Mechanical... any idea why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i have also tried&amp;nbsp;&lt;STRONG&gt;ed.Command("_.attsync", "_name", bname);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;this also don't work in mechanical. but if i write the command manually in the command line, it works.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 08:52:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/8782754#M46852</guid>
      <dc:creator>stefan.hofer</dc:creator>
      <dc:date>2019-05-10T08:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/8786496#M46853</link>
      <description>&lt;P&gt;If someone have the same problem, this code works for me:&lt;/P&gt;
&lt;PRE&gt;AcadDocument thisDrawing = (AcadDocument)acApp.DocumentManager.MdiActiveDocument.GetAcadDocument();
thisDrawing.SendCommand("_.attsync\n" + "_name\n" + bname + "\n");&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 May 2019 06:40:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/8786496#M46853</guid>
      <dc:creator>stefan.hofer</dc:creator>
      <dc:date>2019-05-13T06:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9510342#M46854</link>
      <description>&lt;P&gt;I'm using VB.NET and when I add this code I get errors in other parts of my program "Autodesk.AutoCAD.ApplicationServices" is not a member of MyProgram.Autodesk.AutoCAD.&amp;nbsp; How can I fix this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to do this because I want to insert blocks with attributes in a drawing that are a different scale than the drawing I'm inserting them into.&amp;nbsp; The attributes don't scale with the block.&amp;nbsp; If I run attsync on the drawing when it's done they show up where they need to be.&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 15:59:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9510342#M46854</guid>
      <dc:creator>lvthunder</dc:creator>
      <dc:date>2020-05-12T15:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9510399#M46855</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not know what is: "MyProgram.Autodesk.AutoCAD". You should provide more details on how you're trying to use this code, or simply show the code you use to insert blocks. If it's a simple question of scale it may be solved while you're inserting the attribute definitions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another thing, I do not use VB but I often saw people using VB who have problem when using &lt;A href="https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/extension-methods" target="_blank" rel="noopener"&gt;extension methods&lt;/A&gt;&amp;nbsp; (may be one more crappy VB behavior).&amp;nbsp; You can also use these methods as classical static (Shared) methods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 16:18:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9510399#M46855</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-05-12T16:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9510434#M46856</link>
      <description>&lt;P&gt;Yeah, this is an older program that I wrote years ago, but I need to update it for our new project.&amp;nbsp; I wouldn't mind updating it to c#, but I don't know how to go about it without rewriting it from scratch.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code to insert the block&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    Public Sub InsertDrawingAsBlockExplode(ByVal doc As Document, ByVal filepath As String, ByVal blockname As String, ByVal ipt As Point3d, ByVal rotate As String, ByVal scale As String)
        If My.Computer.FileSystem.FileExists(filepath) Then
            Dim curdb As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim loc As DocumentLock = doc.LockDocument()
            Using loc
                Dim tr As Transaction = doc.TransactionManager.StartTransaction
                Using tr
                    Try
                        Dim blkid As ObjectId
                        Dim bt As BlockTable = CType(tr.GetObject(curdb.BlockTableId, OpenMode.ForRead, False), BlockTable)
                        If bt.Has(blockname) Then
                            Do Until bt.Has(blockname) = False
                                blockname = blockname &amp;amp; 1
                            Loop
                            'Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(String.Format("Block ""{0}"" does already exist" &amp;amp; vbLf &amp;amp; "Try another name", blockname))
                            'Return Nothing
                        End If
                        Dim btr As BlockTableRecord = CType(tr.GetObject(curdb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                        Dim db As Database = New Database(False, True)
                        Using db
                            db.ReadDwgFile(filepath, System.IO.FileShare.Read, False, "")
                            blkid = curdb.Insert(blockname, db, True)
                        End Using
                        If (Not bt.Has(blockname)) Then
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(String.Format("Block ""{0}"" does not added to block table" &amp;amp; vbLf &amp;amp; "Exit on Error", blockname))
                            Return
                        End If
                        Dim btrec As BlockTableRecord = CType(tr.GetObject(blkid, OpenMode.ForRead, False), BlockTableRecord)
                        If String.IsNullOrEmpty(btrec.Name) Then
                            btrec.UpgradeOpen()
                            btrec.Name = blockname
                            btrec.DowngradeOpen()
                        End If
                        Dim bref As BlockReference = New BlockReference(ipt, blkid)
                        Dim mat As Matrix3d = Matrix3d.Identity
                        bref.TransformBy(mat)
                        bref.ScaleFactors = New Scale3d(scale)
                        bref.Rotation = rotate
                        btr.AppendEntity(bref)
                        tr.AddNewlyCreatedDBObject(bref, True)
                        bref.ExplodeToOwnerSpace()
                        bref.Erase()
                        If btrec.HasAttributeDefinitions Then
                            Dim atcoll As Autodesk.AutoCAD.DatabaseServices.AttributeCollection = bref.AttributeCollection
                            For Each subid As ObjectId In btrec
                                Dim ent As Entity = DirectCast(subid.GetObject(OpenMode.ForRead), Entity)
                                Dim attDef As AttributeDefinition = TryCast(ent, AttributeDefinition)
                                If attDef IsNot Nothing Then
                                    ed.WriteMessage(vbLf &amp;amp; "Value: " + attDef.TextString)
                                    Dim attRef As New AttributeReference()
                                    attRef.SetPropertiesFrom(attDef)
                                    attRef.Visible = attDef.Visible
                                    attRef.SetAttributeFromBlock(attDef, bref.BlockTransform)
                                    attRef.HorizontalMode = attDef.HorizontalMode
                                    attRef.VerticalMode = attDef.VerticalMode
                                    attRef.Rotation = attDef.Rotation
                                    attRef.TextStyleId = attDef.TextStyleId
                                    attRef.Position = attDef.Position + ipt.GetAsVector()
                                    attRef.Tag = attDef.Tag
                                    attRef.FieldLength = attDef.FieldLength
                                    attRef.TextString = attDef.TextString
                                    attRef.AdjustAlignment(curdb)
                                    atcoll.AppendAttribute(attRef)
                                    tr.AddNewlyCreatedDBObject(attRef, True)
                                End If
                            Next
                        End If
                        'ed.UpdateScreen()
                        tr.Commit()
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.ToString &amp;amp; vbCr &amp;amp; ex.Message)
                    End Try
                End Using
            End Using
        Else
            MsgBox(filepath &amp;amp; " does not exist!")
        End If
    End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then here is how it updates the attributes.&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    Sub UpdateAttributeValues(ByVal BlockID As ObjectId, ByVal AttributeTagsValues As Dictionary(Of String, String))
        Using myTrans As Transaction = BlockID.Database.TransactionManager.StartTransaction
            Dim myBref As BlockReference = BlockID.GetObject(OpenMode.ForRead)
            Dim myAttCollection As AttributeCollection = myBref.AttributeCollection
            For Each myattrefid As ObjectId In myAttCollection
                Dim myAttRef As AttributeReference = myattrefid.GetObject(OpenMode.ForWrite)
                If AttributeTagsValues.ContainsKey(myAttRef.Tag) Then
                    myAttRef.TextString = AttributeTagsValues(myAttRef.Tag)
                    myAttRef.AdjustAlignment(BlockID.Database)
                End If
            Next
            myTrans.Commit()
        End Using
    End Sub&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 12 May 2020 16:32:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9510434#M46856</guid>
      <dc:creator>lvthunder</dc:creator>
      <dc:date>2020-05-12T16:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9511235#M46857</link>
      <description>&lt;P&gt;Here is a screenshot showing the error it's giving me.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Autodesk error.jpg" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/769395i86D5C8DACC2375D6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Autodesk error.jpg" alt="Autodesk error.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 22:07:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9511235#M46857</guid>
      <dc:creator>lvthunder</dc:creator>
      <dc:date>2020-05-12T22:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9511843#M46858</link>
      <description>&lt;P&gt;I do not know what is 'ShopDrawings', but it seems to be a namespace issue.&lt;/P&gt;
&lt;P&gt;I know VB makes the namespaces stuff unclear by implicitly creating a root namespace with the project name (read &lt;A href="https://www.fmsinc.com/free/NewTips/NET/NETtip40.asp" target="_blank" rel="noopener"&gt;this topic&lt;/A&gt;).&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 05:29:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9511843#M46858</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-05-13T05:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9513129#M46859</link>
      <description>&lt;P&gt;Yes, ShopDrawings is the name of my program, and from what I read the default namespace.&amp;nbsp; I think I'll try making them regular methods and see if that works.&amp;nbsp; Did you look through the code I'm using to insert the block and see if there was something I could do there to fix it on insert instead of having to do this?&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 14:32:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/9513129#M46859</guid>
      <dc:creator>lvthunder</dc:creator>
      <dc:date>2020-05-13T14:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Re : ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/11391769#M46860</link>
      <description>&lt;P&gt;This worked for me!&amp;nbsp; &amp;nbsp;Merci,&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Aug 2022 00:22:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/11391769#M46860</guid>
      <dc:creator>brian.k.smith</dc:creator>
      <dc:date>2022-08-31T00:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/11478943#M46861</link>
      <description>&lt;P&gt;This works great, but when I try to access the attribute collection after the SynchronizeAttributes has been committed. I get ewaserased.&amp;nbsp; If I try to access it before the transaction is commited I get the old data.&amp;nbsp; Does anyone know how to get the new instance of the attribute collection?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2022 00:38:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/11478943#M46861</guid>
      <dc:creator>CBennett34F8SS</dc:creator>
      <dc:date>2022-10-13T00:38:46Z</dc:date>
    </item>
    <item>
      <title>Re: ATTSYNC in VB.NET</title>
      <link>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/11479348#M46862</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;If you want someone to be able to help you, you should show your code.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2022 07:12:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/attsync-in-vb-net/m-p/11479348#M46862</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-10-13T07:12:33Z</dc:date>
    </item>
  </channel>
</rss>

