I tried to increment a number after the blockname and in the xref manager it said i had three different xrefs, but still only wound up with one drawing. What I want to do is only see 1 xref for all three of the drawing with the same block name. Here is the entire code I am working with if you or anyone could offer any suggestions. Thanks.
<CommandMethod("CreateMasterDWG")> _
Public Sub newBlock()
Dim document As Document = Application.DocumentManager.MdiActiveDocument
Dim editor As Editor = document.Editor
Dim database As Database = document.Database
Dim fName2 As String = database.Filename
If fName2 = "" Then
Exit Sub
End If
Dim curdir2 As String = Path.GetDirectoryName(fName2)
Dim db As Database = New Database(True, False)
Dim i As Integer = 1
Dim frm1 As New Form1
Dim strFileName As String
frm1.SaveFileDialog1.InitialDirectory = curdir2
frm1.SaveFileDialog1.Title = "Select a location to save the master file."
frm1.SaveFileDialog1.Filter = "Drawing Files|*.dwg"
frm1.SaveFileDialog1.AddExtension = True
If frm1.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFileName = frm1.SaveFileDialog1.FileName
Dim xrefPath As String = strFileName
Else
Exit Sub
End If
Using trans As Transaction = db.TransactionManager.StartTransaction()
db.SaveAs(strFileName, DwgVersion.Newest)
End Using
Dim xrefdb As Database = New Database(False, False)
xrefdb.ReadDwgFile(strFileName, FileShare.ReadWrite, False, "")
Dim oidc As New ObjectIdCollection()
Try
Using trans As Transaction = database.TransactionManager.StartTransaction()
Dim btr As BlockTable = DirectCast(trans.GetObject(database.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btrMs As BlockTableRecord = TryCast(btr(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead), BlockTableRecord)
'Dim i As Integer = 0
Dim partnum As String = ""
'go through each object in the block table record
For Each entID As ObjectId In btrMs
'create the objectIDCollection used to create the wblock
Dim oidc2 As New ObjectIdCollection()
'create variable used to determine is it is a block reference
Dim ent As Entity = trans.GetObject(entID, OpenMode.ForRead, False)
'if it is a block then perform code below
If TypeOf ent Is BlockReference Then
'since it is a block, add to the IDcollection
oidc2.Add(entID)
Dim blkRef As BlockReference = TryCast(trans.GetObject(entID, OpenMode.ForRead, False), BlockReference)
Dim attRefIds As AttributeCollection = blkRef.AttributeCollection
'cycle through the attributes associated with the current block
For Each attrefid As ObjectId In attRefIds
Dim attref As AttributeReference = trans.GetObject(attrefid, OpenMode.ForRead, False)
Select Case attref.Tag
'if block has attribute project ID then create the wblock
Case "Part Number"
'create string with partnum
Dim strPartNum As String = attref.TextString
Dim DestData As New Database(True, False)
Using document.LockDocument()
Using transNewDb As Transaction = DestData.TransactionManager.StartTransaction()
Dim bt As BlockTable = TryCast(transNewDb.GetObject(DestData.BlockTableId, OpenMode.ForRead), BlockTable)
Dim IDMap As New IdMapping()
DestData.WblockCloneObjects(oidc2, bt(BlockTableRecord.ModelSpace), IDMap, DuplicateRecordCloning.Replace, False)
transNewDb.Commit()
Dim fName As String = database.Filename
Dim curdir As String = Path.GetDirectoryName(fName)
If Not Directory.Exists(curdir & "\parts") Then
Directory.CreateDirectory(curdir & "\parts")
End If
Directory.CreateDirectory(curdir & "\parts\xrefs")
DestData.SaveAs(curdir & "\parts\" & strPartNum & ".dwg", DwgVersion.Newest)
DestData.SaveAs(curdir & "\parts\xrefs\" & strPartNum & ".dwg", DwgVersion.Newest)
Dim strPartFile As String = curdir & "\parts\" & strPartNum & ".dwg"
'CreateXrefMaster(strFileName, strPartFile)
Using trx As Transaction = xrefdb.TransactionManager.StartTransaction()
Dim xrefBt As BlockTable = trx.GetObject(xrefdb.BlockTableId, OpenMode.ForWrite)
Dim btrMs2 As BlockTableRecord = trx.GetObject(xrefBt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim file As String = strPartFile
'list the names of all files in the specified directory
Dim xrefObjId As New ObjectId
xrefObjId = xrefdb.AttachXref(file, Path.GetFileNameWithoutExtension(file))
Dim bref As New BlockReference(Point3d.Origin, xrefObjId)
btrMs2.AppendEntity(bref)
trx.AddNewlyCreatedDBObject(bref, True)
trx.Commit()
End Using
End Using
End Using
End Select
Next
End If
Next
End Using
xrefdb.SaveAs(strFileName, DwgVersion.Newest)
Catch ex As System.Exception
Windows.MessageBox.Show("Error occurred: " & ex.Message, "Error", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
End Try
End Sub