Hello folks 🙂 I'm a fairly recent entry to the worlds of both VB.NET and AutoCAD customization and so far I have been mostly playing with the tutorials provided in help to get a bit the "hang" of it.
One of the most useful things for my job would be to open multiple files and add/delete specific things in them. For example adding a logo from a block to the table etc. However, where so far I'm doing well on opening multiple files in AutoCAD and also able to automatically draw entities in a new file created from a template, I cannot make my code to add new entities to multiple existing drawings. Below, I'm posting the code I've written (mostly a "puzzle" of tutorial code, hehe 😛 )
Can you please help me on what I'm doing wrong ?
Thanks in advance for your help 🙂
CODE BELOW
===========================================
Public Class RepformMain
Dim repformDialog As New repformSetup
<CommandMethod("repform")> _
Public Sub repform()
'' We use this to open multiple (if needed) files in AutoCAD for automatic REPFORMing
Dim openDialogBox As New Autodesk.AutoCAD.Windows.OpenFileDialog("Select files to repform", "", "dwg", "RepformFiles", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple)
Dim dm As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
If repformDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
If openDialogBox.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim fileNamesResult As Array = openDialogBox.GetFilenames() '' Multiple filenames are considered arrays so we assign them to one
For Each fileName As String In fileNamesResult
Try
Dim acDocumentOpening As Document = dm.Open(fileName, False) '' dm.Open function can open them one at a time
Dim acDocumentDatabase As Database = acDocumentOpening.Database
Using acLckDoc As DocumentLock = acDocumentOpening.LockDocument()
Using acTrans As Transaction = acDocumentDatabase.TransactionManager.StartTransaction() '' Start transaction with drawing
''Open the Block table for read
Dim acDocumentBlockTable As BlockTable
acDocumentBlockTable = acTrans.GetObject(acDocumentDatabase.BlockTableId, _
OpenMode.ForRead)
''Open the Block table record Model space for write
Dim acNewRecord As BlockTableRecord = acTrans.GetObject(acDocumentBlockTable(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' Create a single-line text object
Dim acCirc As Circle = New Circle()
With acCirc
.SetDatabaseDefaults(acDocumentDatabase)
.Center = New Point3d(5, 5, 0)
.Radius = 300
End With
acNewRecord.AppendEntity(acCirc)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
'' Save the changes and dispose of the transaction
acTrans.Commit()
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message) '' In case of error present an error box with the information
End Try
Next
End If
End If
End Sub
I haven't examined your code closely, or tried it, but I looked enough to see that you do have a DocumentLock in there, so it may be that the last step you need is to use the Session CommandFlag.