Hi,
This is weird, I'd tried different approaches to this, using just a transaction, 1 top transaction plus a local insert transaction, and in both cases AutoCAD go south after a random number of inserts (over 150), in some cases with "funny" results like a message about a proxy in the block definition, no erasable inserts, etc, obviously because a memory or db corruption . After some test I have a code that at least does not crash AutoCAD (tested up to 1501 inserts), but have a serious performance problem, that I routed to the change of dynprops part, the time it takes to change the values is consistently increasing (up to seconds) after each insert, but I don't see a memory issue in process explorer, just a delay changing the dynprops. The code is crude, assuming a lot of things with a minimum error checking :
Public Sub InsertBlockTest3(ByVal blkName As String, ByVal px As Point3d, ByVal Theta As Double, ByVal sx As Double, ByVal sy As Double, ByVal sz As Double, ByVal Height As Double, ByVal Width As Double, ByVal Layer As String)
Dim Db As Database
Db = HostApplicationServices.WorkingDatabase()
Using acTrans As Transaction = Db.TransactionManager.StartTransaction()
Dim bt As BlockTable = acTrans.GetObject(Db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord
Dim id As ObjectId
btr = acTrans.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
If bt.Has(blkName) Then
Dim btrSrc As BlockTableRecord
btrSrc = acTrans.GetObject(bt.Item(blkName), OpenMode.ForRead)
id = btrSrc.Id
Else
MsgBox("Block definition not found for Block:" + blkName)
End If
Dim blkRef As New BlockReference(px, id)
Dim sc As New Scale3d(sx, sy, sz)
blkRef.Layer = Layer
blkRef.Rotation = Theta
blkRef.ScaleFactors = sc
btr.AppendEntity(blkRef)
acTrans.AddNewlyCreatedDBObject(blkRef, True)
Dim dPropsCollection As DynamicBlockReferencePropertyCollection
dPropsCollection = blkRef.DynamicBlockReferencePropertyCollection
Dim t1 As Date = Now
For Each dprop As DynamicBlockReferenceProperty In dPropsCollection
Select Case dprop.PropertyName
Case "Heigth"
dprop.Value = Height
Case "Width"
dprop.Value = Width
End Select
Next
Dim t2 As Date = Now
Debug.Print("Time in props:" + (t2 - t1).TotalMilliseconds.ToString + "[ms]")
Try
btr.UpdateAnonymousBlocks()
blkRef.RecordGraphicsModified(True)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox("Error Updating RefBTR:blkName :=" + blkName + ": " + ex.Message)
End Try
acTrans.Commit()
End Using
End Sub
<CommandMethod("BTest3")> _
Public Sub btest3()
Dim blkName As String = "A"
Dim px As New Point3d(0, 0, 0)
Dim offset As New Vector3d(4.5, 0, 0)
Debug.Print(Now.TimeOfDay.ToString)
For i = 0 To 199
InsertBlockTest3(blkName, px, 0, 1, 1, 1, 2, 4, "0")
px += offset
Next
Debug.Print(Now.TimeOfDay.ToString)
End Sub
My concerns are not only with the poor documentation, but with the absence of a clear workflow and design patterns to achieve a very recurrent task as insert blocks. With AutoCAD 2013 we are about to complete 9 version of this API without a decent documentation, clear design patterns and official guidance.
Regards,
Gaston Nunez