Message 1 of 5
DeepClone Dynamic BlockTableRecord

Not applicable
07-03-2008
10:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Public Overridable Function InsertBlockRef(ByVal dblInsert As Point3d, ByVal btrSpace As BlockTableRecord, ByVal strSourceBlockName As String, ByVal strSourceBlockPath As String, Optional ByVal strBlockNewName As String = "") As BlockReference
Dim dlock As DocumentLock = Nothing
Dim bt As BlockTable
Dim btr As BlockTableRecord = Nothing
Dim br As BlockReference = Nothing
'Dim id As ObjectId
Dim db As Autodesk.AutoCAD.DatabaseServices.Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Application.DocumentManager.MdiActiveDocument.Editor
'insert block and rename it
Try
Try
dlock = ThisDrawing.LockDocument
Catch ex As Exception
Dim aex As New System.Exception("Error locking document for InsertBlock: " & strSourceBlockName & ": ", ex)
Throw aex
End Try
bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
Dim btrSource As BlockTableRecord = GetBlock(strSourceBlockName, strSourceBlockPath, strBlockNewName)
'if getblock returns nothing then the rest of this code is worthless, skip it and dispose the transaction
If btrSource IsNot Nothing Then
btr = trans.GetObject(btrSource.ObjectId, OpenMode.ForRead)
btrSpace = trans.GetObject(btrSpace.ObjectId, OpenMode.ForWrite)
'Get the Attributes
Dim attColl As AttributeCollection
Dim ent As Entity
Dim btrenum As BlockTableRecordEnumerator
br = New BlockReference(dblInsert, btr.ObjectId)
btrSpace.AppendEntity(br)
trans.AddNewlyCreatedDBObject(br, True)
attColl = br.AttributeCollection
btrenum = btr.GetEnumerator
While btrenum.MoveNext
ent = btrenum.Current.GetObject(OpenMode.ForWrite)
If TypeOf ent Is AttributeDefinition Then
Dim attdef As AttributeDefinition = ent
Dim attref As New AttributeReference
attref.SetAttributeFromBlock(attdef, br.BlockTransform)
attref.TextString = attdef.TextString
attColl.AppendAttribute(attref)
trans.AddNewlyCreatedDBObject(attref, True)
End If
End While
trans.Commit()
End If
Catch ex As System.Exception
Dim aex2 As New System.Exception("Error in inserting new block: " & strSourceBlockName & ": ", ex)
Throw aex2
Finally
If Not trans Is Nothing Then trans.Dispose()
If Not dlock Is Nothing Then dlock.Dispose()
End Try
End Using
Return br
End Function
Public Function GetBlock(ByVal strsourceblockname As String, ByVal strsourceblockpath As String, Optional ByVal strblocknewname As String = "") As BlockTableRecord
Dim dlock As DocumentLock = Nothing
Dim bt As BlockTable
Dim btr As BlockTableRecord = Nothing
Dim id As ObjectId
Dim db As Autodesk.AutoCAD.DatabaseServices.Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Application.DocumentManager.MdiActiveDocument.Editor
'insert block and rename it
Try
Try
dlock = ThisDrawing.LockDocument
Catch ex As Exception
Dim aex As New System.Exception("Error locking document for InsertBlock: " & strsourceblockname & ": ", ex)
Throw aex
End Try
bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
If bt.Has(strsourceblockname) Then
'block found, get instance for copying
If strblocknewname <> "" Then
'renaming source block, so create a new definition
Dim idMap As New IdMapping
Dim btrS As BlockTableRecord = trans.GetObject(bt.Item(strsourceblockname), OpenMode.ForWrite)
btr = btrS.DeepClone(bt, idMap, True)
btr.Name = strblocknewname
bt.Add(btr)
trans.AddNewlyCreatedDBObject(btr, True)
Else
btr = trans.GetObject(bt.Item(strsourceblockname), OpenMode.ForRead)
End If
Else
'block not found, insert into drawing
Using sourcedb As Database = New Database(False, False)
Try
sourcedb.ReadDwgFile(strsourceblockpath, IO.FileShare.Read, True, "")
id = db.Insert(strsourceblockpath, sourcedb, True)
btr = trans.GetObject(id, OpenMode.ForWrite)
If strblocknewname <> "" Then
btr.Name = strblocknewname
Else
btr.Name = strsourceblockname
End If
Catch ex As System.Exception
Dim aex As New System.Exception("Block file not found " & strsourceblockpath & ": ", ex)
Throw aex
Exit Function
End Try
sourcedb.Dispose()
End Using
End If
trans.Commit()
Catch ex As System.Exception
Dim aex2 As New System.Exception("Error in inserting new block: " & strsourceblockname & ": ", ex)
Throw aex2
Finally
If Not trans Is Nothing Then trans.Dispose()
If Not dlock Is Nothing Then dlock.Dispose()
End Try
End Using
Return btr
End Function
Dim dlock As DocumentLock = Nothing
Dim bt As BlockTable
Dim btr As BlockTableRecord = Nothing
Dim br As BlockReference = Nothing
'Dim id As ObjectId
Dim db As Autodesk.AutoCAD.DatabaseServices.Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Application.DocumentManager.MdiActiveDocument.Editor
'insert block and rename it
Try
Try
dlock = ThisDrawing.LockDocument
Catch ex As Exception
Dim aex As New System.Exception("Error locking document for InsertBlock: " & strSourceBlockName & ": ", ex)
Throw aex
End Try
bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
Dim btrSource As BlockTableRecord = GetBlock(strSourceBlockName, strSourceBlockPath, strBlockNewName)
'if getblock returns nothing then the rest of this code is worthless, skip it and dispose the transaction
If btrSource IsNot Nothing Then
btr = trans.GetObject(btrSource.ObjectId, OpenMode.ForRead)
btrSpace = trans.GetObject(btrSpace.ObjectId, OpenMode.ForWrite)
'Get the Attributes
Dim attColl As AttributeCollection
Dim ent As Entity
Dim btrenum As BlockTableRecordEnumerator
br = New BlockReference(dblInsert, btr.ObjectId)
btrSpace.AppendEntity(br)
trans.AddNewlyCreatedDBObject(br, True)
attColl = br.AttributeCollection
btrenum = btr.GetEnumerator
While btrenum.MoveNext
ent = btrenum.Current.GetObject(OpenMode.ForWrite)
If TypeOf ent Is AttributeDefinition Then
Dim attdef As AttributeDefinition = ent
Dim attref As New AttributeReference
attref.SetAttributeFromBlock(attdef, br.BlockTransform)
attref.TextString = attdef.TextString
attColl.AppendAttribute(attref)
trans.AddNewlyCreatedDBObject(attref, True)
End If
End While
trans.Commit()
End If
Catch ex As System.Exception
Dim aex2 As New System.Exception("Error in inserting new block: " & strSourceBlockName & ": ", ex)
Throw aex2
Finally
If Not trans Is Nothing Then trans.Dispose()
If Not dlock Is Nothing Then dlock.Dispose()
End Try
End Using
Return br
End Function
Public Function GetBlock(ByVal strsourceblockname As String, ByVal strsourceblockpath As String, Optional ByVal strblocknewname As String = "") As BlockTableRecord
Dim dlock As DocumentLock = Nothing
Dim bt As BlockTable
Dim btr As BlockTableRecord = Nothing
Dim id As ObjectId
Dim db As Autodesk.AutoCAD.DatabaseServices.Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Application.DocumentManager.MdiActiveDocument.Editor
'insert block and rename it
Try
Try
dlock = ThisDrawing.LockDocument
Catch ex As Exception
Dim aex As New System.Exception("Error locking document for InsertBlock: " & strsourceblockname & ": ", ex)
Throw aex
End Try
bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
If bt.Has(strsourceblockname) Then
'block found, get instance for copying
If strblocknewname <> "" Then
'renaming source block, so create a new definition
Dim idMap As New IdMapping
Dim btrS As BlockTableRecord = trans.GetObject(bt.Item(strsourceblockname), OpenMode.ForWrite)
btr = btrS.DeepClone(bt, idMap, True)
btr.Name = strblocknewname
bt.Add(btr)
trans.AddNewlyCreatedDBObject(btr, True)
Else
btr = trans.GetObject(bt.Item(strsourceblockname), OpenMode.ForRead)
End If
Else
'block not found, insert into drawing
Using sourcedb As Database = New Database(False, False)
Try
sourcedb.ReadDwgFile(strsourceblockpath, IO.FileShare.Read, True, "")
id = db.Insert(strsourceblockpath, sourcedb, True)
btr = trans.GetObject(id, OpenMode.ForWrite)
If strblocknewname <> "" Then
btr.Name = strblocknewname
Else
btr.Name = strsourceblockname
End If
Catch ex As System.Exception
Dim aex As New System.Exception("Block file not found " & strsourceblockpath & ": ", ex)
Throw aex
Exit Function
End Try
sourcedb.Dispose()
End Using
End If
trans.Commit()
Catch ex As System.Exception
Dim aex2 As New System.Exception("Error in inserting new block: " & strsourceblockname & ": ", ex)
Throw aex2
Finally
If Not trans Is Nothing Then trans.Dispose()
If Not dlock Is Nothing Then dlock.Dispose()
End Try
End Using
Return btr
End Function