Hey all,
I've been coming up with issues writing changes to an external database (one that is lazy-loaded via Database.readDwgFile()).
While constructing this post I decided to do a couple more tests to make sure that my setup was not working.
What I found is that it was working!
Regardless, I would still like to add to the .NET forums by creating some documentation in case someone else needs it.
When working with an external database or side database that is NOT loaded into the AutoCAD editor, I needed to save changes that were made. Normally, I'd expect that after a transaction is commited, then the changes would reflect... this is NOT what happened.
So I've added this line of code in order to save the database after all changes were committed:
database.SaveAs(drawing.FullName, true, DwgVersion.Current, database.SecurityParameters);
This method works as described in ObjectARX - Managed Class Reference documentation:
Database.SaveAs MethodRuns the save process on the database and writes the drawing information out to fileName. The fileName argument is taken as is. If no file extension is present, .dwg is not appended.
If the database executing the SaveAs() function is not the current database in the AutoCAD editor, then the thumbnail preview image is not saved to fileName.
To specify security parameters, set security that conveys your preferences.If the security is not properly initialized, the method fails. See SecurityParameters for more information on initializing this struct.
If bBakAndRename is false, then no .bak file is created, a full save is always done, and if the Database being saved is the main database for a document in AutoCAD, then the document's filename will NOT change to the saved name.
Now, when I open the drawing manually, I notice the changes made by the .NET assembly.
I also want to put up here, since it is partially relevant, that I am NOT required to Commit the Transaction for changes to take effect. This appears to be the default behavior upon the closing of a transaction. However, if the transaction is aborted, then no changes are made.
Hi
Here is a C# sample that illustrates how to work with ReadDwgFile: it will prompt the user to select an entity in the current drawing, then load a side database and clone the object into it. I would recommend you follow that approach when working on a side database you want to modify.
[CommandMethod("wblockclone", CommandFlags.Session)] static public void wblockclone() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityOptions peo = new PromptEntityOptions("\nSelect entity to copy: "); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return; using(DocumentLock doclock = doc.LockDocument()) { ObjectIdCollection ObjectcIds = new ObjectIdCollection(); ObjectcIds.Add(per.ObjectId); Database destDb = new Database(false, true); destDb.ReadDwgFile("c:\\Temp\\DestDrawing.dwg", System.IO.FileShare.ReadWrite, true, ""); using (Transaction Tx = destDb.TransactionManager.StartTransaction()) { BlockTable bt = Tx.GetObject( destDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = Tx.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; IdMapping oIdMap = new IdMapping(); destDb.WblockCloneObjects( ObjectcIds, btr.ObjectId, oIdMap, DuplicateRecordCloning.Ignore, false); Tx.Commit(); } destDb.SaveAs(destDb.Filename, DwgVersion.Current); } }
Regards,
Philippe.
Hi Philippe,
Is it possible to execute a VBA routine inside a side database ?
~Ranjan
You can use AxDbDocument to access side DB from VBA, here is a sample:
Private Sub UpdateDWGProps() Dim oApp As AcadApplication Set oApp = GetObject(, "AutoCAD.Application") 'This is what we need to look into a drawing without opening it in the GUI Dim axDoc As Variant 'AxDbDocument Set axDoc = oApp.GetInterfaceObject("ObjectDBX.AxDbDocument.19") axDoc.Open ("C:\Temp\Drawing1.dwg") Dim oDwgProps As AcadSummaryInfo Set oDwgProps = ThisDrawing.SummaryInfo Debug.Print "-------------- Drawing Properties --------------" Debug.Print " Title = " & oDwgProps.Title Debug.Print " Subject = " & oDwgProps.Subject Debug.Print " Author = " & oDwgProps.Author Debug.Print " Keywords = " & oDwgProps.Keywords Debug.Print " Comments = " & oDwgProps.Comments Debug.Print " HyperlinkBase = " & oDwgProps.HyperlinkBase Debug.Print " LastSavedBy = " & oDwgProps.LastSavedBy Debug.Print " RevisionNumber = " & oDwgProps.RevisionNumber Dim index As Long Dim key As String Dim value As String For index = 0 To oDwgProps.NumCustomInfo - 1 oDwgProps.GetCustomByIndex index, key, value 'Debug.Print " Custom prop(" & str(index) & ") = " & value 'Set a new value oDwgProps.SetCustomByIndex index, key, "NewValue" Next oDwgProps.AddCustomInfo "Developer", "ADN" axDoc.SaveAs (axDoc.name) End Sub
Hope that helps,
Philippe.
Hi Philippe,
Thanks for your Reply.
Actually My question is not about accessing side database via VBA. Instead...
I want to run a VBA macro [VBARUN "TEST.DVB"] inside a database which is read using ReadDwgFile
Check below...
Public Sub batchProcess() ' get all DWG files from a specific folder Dim directory As New System.IO.DirectoryInfo("C:\TEMP") Dim files As System.IO.FileInfo() = _ directory.GetFiles("*.dwg") For Each file As System.IO.FileInfo In files ' generate a temp file location Dim tempFileName As String = _ System.IO.Path.GetTempFileName() Using db As New Database(False, True) db.ReadDwgFile(file.FullName, FileOpenMode.OpenForReadAndAllShare, False, Nothing) Using trans As Transaction = _ db.TransactionManager.StartTransaction() 'Run a VBA MACRO / SendCommad ' commit changes trans.Commit() End Using End Using ' dispose the database Next End Sub
I doubt it's possible, a command needs the ditor to be executed, which is only available on the currently open drawing.
By trying to mix .Net and VBA code, you are going toward some convoluted workflow, a better approach would be to migrate your VBA routine to .Net and perform all operation from there. There is no feature that VBA has which cannot be achieved from the .Net API.
Hope that helps,
Philippe.
Okay! Understood.
I know i must convert the vba to .net.
I am thinking of a shortcut now , i.e. convert the vba code to .net but use the COM interop.
For example : How can I use the below as is with .net code pasted above ?
Private Function GetLeaderCoord() As Variant Dim strString As String Dim obj As AcadBlock Dim obj2 As AcadEntity For Each obj In ThisDrawing.Blocks For Each obj2 In obj If obj2.ObjectName = "AcDbLeader" Then Dim x As AcadLeader Set x = obj2 Dim y GetLeaderCoord = x.Coordinates Exit Function End If Next obj2 Next obj End Function
You may take a look at our VBA to .Net migration guide: