<?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 block in multiple documents in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915884#M13812</link>
    <description>&lt;P&gt;&lt;BR /&gt;eHadMultipleReaders is probably due to the using of an OpenCloseTransaction instead of a standard transaction.&lt;/P&gt;
&lt;P&gt;This seems to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            if (blockFileDialog.ShowDialog() != DialogResult.OK)
                return;
            if (targetFilesDialog.ShowDialog() != DialogResult.OK)
                return;
            var pointResult = ed.GetPoint("\nEnter the start point of the line: ");
            if (pointResult.Status != PromptStatus.OK)
                return;
            string blockName = Path.GetFileNameWithoutExtension(blockFileDialog.FileName);
            var insPoint = pointResult.Value.TransformBy(ed.CurrentUserCoordinateSystem);
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(blockFileDialog.FileName, FileOpenMode.OpenForReadAndReadShare, true, null);
                foreach (string targetFile in targetFilesDialog.FileNames)
                {
                    using (var targetDb = new Database(false, true))
                    {
                        targetDb.ReadDwgFile(targetFile, FileOpenMode.OpenForReadAndAllShare, true, null);
                        using (var tr = targetDb.TransactionManager.StartTransaction())
                        {
                            var blockTable = (BlockTable)tr.GetObject(targetDb.BlockTableId, OpenMode.ForRead);
                            if (!blockTable.Has(blockName))
                            {
                                var space = (BlockTableRecord)tr.GetObject(targetDb.CurrentSpaceId, OpenMode.ForWrite);
                                var id = targetDb.Insert(blockName, sourceDb, true);
                                var br = new BlockReference(insPoint, id);
                                space.AppendEntity(br);
                                tr.AddNewlyCreatedDBObject(br, true);
                                targetDb.SaveAs(targetFile, DwgVersion.Current);
                            }
                            tr.Commit();
                        }
                    }
                }
            }
        }

        private static OpenFileDialog targetFilesDialog = new OpenFileDialog
        {
            InitialDirectory = "c:\\",
            Filter = @"DWG Files (*.dwg)|*.dwg",
            FilterIndex = 2,
            RestoreDirectory = true,
            Multiselect = true,
            Title = @"Select CAD files to update"
        };

        private static OpenFileDialog blockFileDialog = new OpenFileDialog
        {
            InitialDirectory = "c:\\",
            Filter = @"DWG Files (*.dwg)|*.dwg",
            FilterIndex = 2,
            RestoreDirectory = true,
            Multiselect = false,
            Title = @"Select block file to insert"
        };&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 29 Jan 2022 22:28:12 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2022-01-29T22:28:12Z</dc:date>
    <item>
      <title>Insert block in multiple documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915579#M13808</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to create a routine for inserting a block (ie. Record Stamp) into multiple dwg files. I thought I have all the information but I can not get the routine to run. I have tried getting the documents and adding a lock and it still does not seem to make a difference. Is there something that i am missing in my code to do this, or is this even possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;private static Document _document;
        private static Editor _commandLine;
        [CommandMethod("gcs-MultiDocInsertBlock")]
        public void MultiDocInsertBlock()
        {
            _document = CADApplication.DocumentManager.MdiActiveDocument;
            _commandLine = _document.Editor;
            BlockFileDialog().ShowDialog();
            var blockFileName = BlockFileDialog().FileName;
            var blockName = Path.GetFileName(blockFileName);
            if (FileDialog().ShowDialog() != DialogResult.OK) 
                return;
            var insPt = SetPoint();
            foreach (var file in FileDialog().FileNames)
            {
                _commandLine.WriteMessage("file is "+ file);
                try
                {
                    using (var database = new Database(false, true))
                    {
                        Document document =  CADApplication.DocumentManager.GetDocument(database);

                        using(document.LockDocument())
                        {
                            database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, true, null);
                            using (var transaction = database.TransactionManager.StartOpenCloseTransaction())
                            {
                                var blockTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
                                if (blockTable.Has(blockName))
                                {
                                    _commandLine.WriteMessage("block does exist!");
                                    return;
                                }

                                _commandLine.WriteMessage("block does not exist!");
                                using (var sourceDb = new Database(false, true))
                                {
                                    sourceDb.ReadDwgFile(blockFileName, FileOpenMode.OpenForReadAndAllShare, true, "");
                                    database.Insert(blockName, sourceDb, true);
                                }

                                using (var blockReference = new BlockReference(insPt, blockTable[blockName]))
                                {
                                    var space = (BlockTableRecord)transaction.GetObject(database.CurrentSpaceId,
                                        OpenMode.ForWrite);
                                    space.AppendEntity(blockReference);
                                    transaction.AddNewlyCreatedDBObject(blockReference, true);
                                }

                                transaction.Commit();
                            }
                            database.SaveAs(file, DwgVersion.Current);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _commandLine.WriteMessage($"\n{file}: {ex}");
                }
            }
        }
private static Point3d SetPoint()
        {
            var promptPointOptions = new PromptPointOptions("")
            {
                Message = "\nEnter the start point of the line: "
            };
            var promptPointResult = _commandLine.GetPoint(promptPointOptions);
            var point3d = promptPointResult.Value;
            return point3d;
        }

        private static OpenFileDialog FileDialog()
        {
            var openFileDialog = new OpenFileDialog
            {
                InitialDirectory = "c:\\",
                Filter = @"DWG Files (*.dwg)|*.dwg",
                FilterIndex = 2,
                RestoreDirectory = true,
                Multiselect = true,
                Title = @"Select CAD files to update"
            };
            return openFileDialog;
        }
        private static OpenFileDialog BlockFileDialog()
        {
            var openFileDialog = new OpenFileDialog
            {
                InitialDirectory = "c:\\",
                Filter = @"DWG Files (*.dwg)|*.dwg",
                FilterIndex = 2,
                RestoreDirectory = true,
                Multiselect = false,
                Title = @"Select block file to insert"
            };
            return openFileDialog;
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 16:57:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915579#M13808</guid>
      <dc:creator>will.wydock</dc:creator>
      <dc:date>2022-01-29T16:57:20Z</dc:date>
    </item>
    <item>
      <title>Re: Insert block in multiple documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915642#M13809</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the first problem is here:&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-general"&gt;&lt;CODE&gt;Document document =  CADApplication.DocumentManager.GetDocument(database);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While using a 'side database' (i.e. new Database &amp;amp; ReadDwgFile) you cannot access to the document of this database (and most of the time you not need to).&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 17:57:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915642#M13809</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-01-29T17:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Insert block in multiple documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915644#M13810</link>
      <description>The document and document lock was a new add as I could not get it working on just the database. It looks like I went the wrong direction in solving this.&lt;BR /&gt;</description>
      <pubDate>Sat, 29 Jan 2022 17:50:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915644#M13810</guid>
      <dc:creator>will.wydock</dc:creator>
      <dc:date>2022-01-29T17:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Insert block in multiple documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915867#M13811</link>
      <description>Thank you, I was able to solve my multiple document issue.&lt;BR /&gt;&lt;BR /&gt;When I am running my routine, I am always getting the following errors.&lt;BR /&gt;&lt;BR /&gt;eHadMultipleReaders&lt;BR /&gt;at Autodesk.AutoCAD.DatabaseServices.Database.Insert(String blockName, Database dataBase, Boolean preserveSourceDatabase)&lt;BR /&gt;at software.Common.BlockInsert.MultiDocInsertBlock() in \\Software\Common\BlockInsert.cs:line 56&lt;BR /&gt;&lt;BR /&gt;from what I have been reading that means that the database has been open and not closed. It does not matter whether I have used an old file or a brand new file that I have created. I am still getting the same error. How do you close a database to make sure that this error doesn't occur?&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;</description>
      <pubDate>Sat, 29 Jan 2022 22:07:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915867#M13811</guid>
      <dc:creator>will.wydock</dc:creator>
      <dc:date>2022-01-29T22:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Insert block in multiple documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915884#M13812</link>
      <description>&lt;P&gt;&lt;BR /&gt;eHadMultipleReaders is probably due to the using of an OpenCloseTransaction instead of a standard transaction.&lt;/P&gt;
&lt;P&gt;This seems to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            if (blockFileDialog.ShowDialog() != DialogResult.OK)
                return;
            if (targetFilesDialog.ShowDialog() != DialogResult.OK)
                return;
            var pointResult = ed.GetPoint("\nEnter the start point of the line: ");
            if (pointResult.Status != PromptStatus.OK)
                return;
            string blockName = Path.GetFileNameWithoutExtension(blockFileDialog.FileName);
            var insPoint = pointResult.Value.TransformBy(ed.CurrentUserCoordinateSystem);
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(blockFileDialog.FileName, FileOpenMode.OpenForReadAndReadShare, true, null);
                foreach (string targetFile in targetFilesDialog.FileNames)
                {
                    using (var targetDb = new Database(false, true))
                    {
                        targetDb.ReadDwgFile(targetFile, FileOpenMode.OpenForReadAndAllShare, true, null);
                        using (var tr = targetDb.TransactionManager.StartTransaction())
                        {
                            var blockTable = (BlockTable)tr.GetObject(targetDb.BlockTableId, OpenMode.ForRead);
                            if (!blockTable.Has(blockName))
                            {
                                var space = (BlockTableRecord)tr.GetObject(targetDb.CurrentSpaceId, OpenMode.ForWrite);
                                var id = targetDb.Insert(blockName, sourceDb, true);
                                var br = new BlockReference(insPoint, id);
                                space.AppendEntity(br);
                                tr.AddNewlyCreatedDBObject(br, true);
                                targetDb.SaveAs(targetFile, DwgVersion.Current);
                            }
                            tr.Commit();
                        }
                    }
                }
            }
        }

        private static OpenFileDialog targetFilesDialog = new OpenFileDialog
        {
            InitialDirectory = "c:\\",
            Filter = @"DWG Files (*.dwg)|*.dwg",
            FilterIndex = 2,
            RestoreDirectory = true,
            Multiselect = true,
            Title = @"Select CAD files to update"
        };

        private static OpenFileDialog blockFileDialog = new OpenFileDialog
        {
            InitialDirectory = "c:\\",
            Filter = @"DWG Files (*.dwg)|*.dwg",
            FilterIndex = 2,
            RestoreDirectory = true,
            Multiselect = false,
            Title = @"Select block file to insert"
        };&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 22:28:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-block-in-multiple-documents/m-p/10915884#M13812</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-01-29T22:28:12Z</dc:date>
    </item>
  </channel>
</rss>

