Shared Sub addDetailstoBlock(_detailBlocksArray As List(Of String))
' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
' Lock the document
Using acLckDoc As DocumentLock = acDoc.LockDocument()
' Start a transaction in the database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
' Get the Block table for the current database
Dim acBlockTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
' Get the ModelSpace
Dim modelSpace As BlockTableRecord = DirectCast(acTrans.GetObject(acBlockTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
' Get a fresh Name for our new DetailBlock (getDetailBlockSeqNum retrieves a Sequence number)
Dim blkName As String = "DetailBlock_" & getDetailBlockSeqNum()
' Create the BlockTableRecord
Dim acBlkTblRecId As ObjectId = Acad.findOrCreateBlockTableRecord(blkName).ObjectId
' Get the newly created BlockTableRecord
Dim acBlkTblRec As BlockTableRecord = DirectCast(acTrans.GetObject(acBlkTblRecId, OpenMode.ForWrite), BlockTableRecord)
'Declare a insertionPoint for inserting the 'sub-blocks'
Dim subBlkInsPnt As New Point3d(0, 0, 0)
'Traverse the _detailBlocksArray wich holds all desired 'sub-blocks'-names
For Each subBlockName As String In _detailBlocksArray
'searchOrImportBlock checks if the desired 'sub-block' exists in Block table current database, if not it imports it from a librairy-DWG
'either way, this gives us a ObjectId of the desired 'sub-block'
Dim subBlkId As ObjectId = Acad.searchOrImportBlock(Acad.MontageDetails, subBlockName)
'Create a BlockReference using the subBlkInsPnt & subBlkId
Dim subBlkRef As BlockReference = New BlockReference(subBlkInsPnt, subBlkId)
'Add the subBlkRef to our 'main' BlockTableRecord
acBlkTblRec.AppendEntity(subBlkRef)
acTrans.AddNewlyCreatedDBObject(subBlkRef, True)
'Increment the X-value of our subBlkInsPnt
Dim blkEx As Extents3d = subBlkRef.GeometricExtents()
subBlkInsPnt
= New Point3d
(subBlkInsPnt
.X + Math
.Abs(blkEx
.MinPoint.X - blkEx
.MaxPoint.X), subBlkInsPnt
.Y,
0) Next
'Determine a insertionPoint for our 'main' BlockReference
insPnt = New Point3d(-15000, 15000, 0)
'Create and add our 'main' BlockReference to the ModelSpace
Dim acBlkRef As New BlockReference(insPnt, acBlkTblRecId)
modelSpace.AppendEntity(acBlkRef)
acTrans.AddNewlyCreatedDBObject(acBlkRef, True)
' Commit the transaction
acTrans.Commit()
End Using ' Dispose of the transaction
End Using ' Unlock the document
End Sub
Shared Function findOrCreateBlockTableRecord(_blockName As String) As BlockTableRecord
' Get the current document and database, and start a transaction
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
' Lock the document
Using acLckDoc As DocumentLock = acDoc.LockDocument()
' Start a transaction in the database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
' Get the Block table for the current database
Dim acBlockTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
' If found in local BlockTableRecord
If acBlockTbl.Has(_blockName) Then
Dim rtnBlock As BlockTableRecord
rtnBlock = acTrans.GetObject(acBlockTbl(_blockName), OpenMode.ForWrite)
Return rtnBlock
Else
' Create our new block table record...
Dim rtnBlock As New BlockTableRecord
' ... and set its properties
rtnBlock.Name = _blockName
' Add the new block to the block table
acBlockTbl.UpgradeOpen()
Dim btrId As ObjectId = acBlockTbl.Add(rtnBlock)
acTrans.AddNewlyCreatedDBObject(rtnBlock, True)
' Commit the transaction
acTrans.Commit()
Return rtnBlock
End If
End Using ' Dispose of the transaction
End Using ' Unlock the document
End Function
Shared Function searchOrImportBlock(_sourceFileName As String, _blockName As String) As ObjectId
' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
' Lock the document
Using acLckDoc As DocumentLock = acDoc.LockDocument()
' Start a transaction in the database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
' Get the Block table for the current database
Dim acBlockTbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
' If found in local BlockTableRecord
If Not acBlockTbl.Has(_blockName) Then
' If not found in local BlockTableRecord then Import from _sourceFileName
importBlockFromSourceFile(_sourceFileName, _blockName)
End If
Return acBlockTbl(_blockName)
End Using ' Dispose of the transaction
End Using ' Unlock the document
End Function
Shared Function importBlockFromSourceFile(_sourceFileName As String, _blockName As String)
Dim sourceDb As New Database(False, True)
Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
' Read the DWG into a side database
sourceDb.ReadDwgFile(_sourceFileName, System.IO.FileShare.Read, True, "")
Using acTrans As Transaction = sourceDb.TransactionManager.StartTransaction()
' Open the block table
Dim bt As BlockTable = DirectCast(acTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, False), BlockTable)
If bt.Has(_blockName) Then
acObjIdColl.Add(bt(_blockName))
End If
End Using
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
' Lock the document
Using acLckDoc As DocumentLock = acDoc.LockDocument()
' Start a transaction in the database
Using acTrans = acDoc.TransactionManager.StartTransaction()
' Open the Block table for read
' Useless Dim acBlkTblNewDoc As BlockTable
' Useless acBlkTblNewDoc = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
' Open the Block table record Model space for read
' Useless Dim acBlkTblRecNewDoc As BlockTableRecord
' Useless acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace), OpenMode.ForRead)
' Clone the objects to the new database
Dim acIdMap As IdMapping = New IdMapping()
acCurDb.WblockCloneObjects(acObjIdColl, acCurDb.BlockTableId, acIdMap, DuplicateRecordCloning.Ignore, False)
' Commit the transaction
acTrans.Commit()
End Using ' Dispose of the transaction
End Using ' Unlock the document
'Return ObjectId
' This was the ObjectId of the btr in the original database... Return acObjIdColl(0)
End Function