- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm using the following code (most of it borrowed from various sources) to insert blocks, and it works fine. The resulting DWG file is fine, with the exception that the names are missing from the inserted blocks. This is not a big problem, until using the DXFOUT. The resulting DXF file cannot be opened. I'm told it's due to the blocks having no names. As you can see, the name is being passed to the function. How can I get the names to "stick" to the resulting block reference? I've tried to name the reference after the fact, but the Name property is Read Only.
Friend Function InsertBlock(ByRef AcadDbase As Database, BlkName As String, InsPt As Point3d, scale As Double, rotation As Double, ByRef ErrorMsg As String) As Boolean
Using trans As Transaction = AcadDbase.TransactionManager.StartTransaction
'Open the block table for read.
Dim BlkTbl As BlockTable = trans.GetObject(AcadDbase.BlockTableId, OpenMode.ForRead, True)
'Create a variable for the Object Id.
Dim BlkRecId As ObjectId = ObjectId.Null
'If block already exists...
If BlkTbl.Has(BlkName) Then
'Assign an ID.
BlkRecId = BlkTbl(BlkName)
'Append it into the current space.
Using BlkRef As New BlockReference(InsPt, BlkRecId)
Dim AcCurSpaceBlkTblRec As BlockTableRecord = trans.GetObject(AcadDbase.CurrentSpaceId, OpenMode.ForWrite)
AcCurSpaceBlkTblRec.AppendEntity(BlkRef)
trans.AddNewlyCreatedDBObject(BlkRef, True)
End Using
Else
'Create a new temporary database.
Using newDb As New Database(False, True)
'Into this temp database, read in a drawing that is used as a block.
newDb.ReadDwgFile(BlkName, FileOpenMode.OpenForReadAndReadShare, True, "")
'Insert this dwg into the acad database, and assign a block id.
BlkRecId = AcadDbase.Insert(BlkName, newDb, True)
'Create a block table record.
Dim BlkTblRec As BlockTableRecord = trans.GetObject(BlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
'Create a block reference.
Dim BlkRef As BlockReference = New BlockReference(InsPt, BlkRecId) With {
.ScaleFactors = New Scale3d(scale),
.Rotation = rotation}
'Append the record with the block ref.
BlkTblRec.AppendEntity(BlkRef)
'Add the new record to the database.
trans.AddNewlyCreatedDBObject(BlkRef, True)
'Close the temp database.
End Using
End If
'Commit the transaction.
trans.Commit()
'Close the transaction.
End Using
Return True
End Function
Software Programmer
Draper, Inc.
Solved! Go to Solution.