OK, no matter what I do, this is failing. It fails if the block exists, and it fails for a different reason if the block is loaded from disk. I don't know what I am doing wrong.
Public Sub InsertDrawingAsBlock(ByVal doc As Document, ByVal path As String, ByVal blockname As String, _
ByVal iPt As Point3d, Optional ByVal Space As String = "Model", _
Optional ByVal LayerName As String = "Misc", Optional ByVal Xplode As Boolean = False, _
Optional ByVal bRotate As Double = 0.0, Optional ByVal bXScale As Single = 1.0, _
Optional ByVal bYScale As Single = 1.0, Optional ByVal bZScale As Single = 1.0)
Dim curdb As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim loc As DocumentLock = doc.LockDocument()
Using loc
Dim blkid As ObjectId = ObjectId.Null
Dim db As Database = doc.Database
Using db
db.ReadDwgFile(path, System.IO.FileShare.Read, True, "")
blkid = curdb.Insert(path, db, True)
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
Dim ucsMat As Matrix3d = ed.CurrentUserCoordinateSystem
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
If bt.Has(blockname) Then
Dim br As New BlockReference(Point3d.Origin, bt(blockname))
br.TransformBy(ucsMat * Matrix3d.Displacement(iPt - Point3d.Origin))
Dim btrId As ObjectId = bt(blockname).GetObject(OpenMode.ForRead).ObjectId
btr.UpgradeOpen()
btr.AppendEntity(br)
tr.AddNewlyCreatedDBObject(br, True)
Else
bt.UpgradeOpen()
'Dim br As New BlockReference(Point3d.Origin, bt(blockname))
'br.TransformBy(ucsMat * Matrix3d.Displacement(iPt - Point3d.Origin))
Dim btrec As BlockTableRecord = DirectCast(blkid.GetObject(OpenMode.ForRead), BlockTableRecord)
btrec.UpgradeOpen()
btrec.Name = blockname
btrec.DowngradeOpen()
Using btr
Using bref As New BlockReference(iPt, blkid)
'Rotate 45 degrees
'45 * (Math.PI/180)
bref.Rotation = bRotate * (Math.PI / 180)
'Scale factor
bref.ScaleFactors = New Scale3d(bXScale, bYScale, bZScale)
bref.TransformBy(ucsMat * Matrix3d.Displacement(iPt - Point3d.Origin))
btr.AppendEntity(bref)
tr.AddNewlyCreatedDBObject(bref, True)
Using btAttRec As BlockTableRecord = DirectCast(bref.BlockTableRecord.GetObject(OpenMode.ForRead), BlockTableRecord)
Dim atcoll As Autodesk.AutoCAD.DatabaseServices.AttributeCollection = bref.AttributeCollection
For Each subid As ObjectId In btAttRec
Dim ent As Entity = DirectCast(subid.GetObject(OpenMode.ForRead), Entity)
Dim attDef As AttributeDefinition = TryCast(ent, AttributeDefinition)
If attDef IsNot Nothing Then
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 Using
bref.DowngradeOpen()
'Does it need to be exploded?
If Xplode = True Then
bref.ExplodeToOwnerSpace()
bref.Erase()
End If
End Using
End Using
btrec.DowngradeOpen()
bt.DowngradeOpen()
ed.Regen()
End If
tr.Commit()
End Using
End Using
End Using
End Sub