Friend Shared Function AddBlockRecordToActiveDatabase(ByVal BlockName As String, ByVal path As String) As Boolean
Dim key As Boolean = False
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim PathString As String = GetPath(path)
ed.WriteMessage(vbCrLf & "Путь файла=" & PathString & vbCrLf)
ed.WriteMessage(vbCrLf & "Имя блока=" & BlockName & vbCrLf)
If File.
Exists(PathString
) Then ' Using doclock As DocumentLock = doc.LockDocument()
Using SourceDb As New Database(False, True)
Try
SourceDb.ReadDwgFile(PathString, System.IO.FileShare.Read, True, "")
ed.WriteMessage(vbCrLf & "Шаг 1" & vbCrLf)
Dim RequestedBlockIds As New ObjectIdCollection()
ed.WriteMessage(vbCrLf & "Шаг 2" & vbCrLf)
Using acTrans As Transaction = SourceDb.TransactionManager.StartTransaction()
ed.WriteMessage(vbCrLf & "Шаг 3" & vbCrLf)
Dim bt As BlockTable = DirectCast(acTrans.GetObject(SourceDb.BlockTableId, OpenMode.ForRead), BlockTable)
If bt.Has(BlockName) Then
ed.WriteMessage(vbCrLf & "Шаг 4" & vbCrLf)
Dim btr As BlockTableRecord = DirectCast(acTrans.GetObject(bt(BlockName), OpenMode.ForRead), BlockTableRecord)
ed.WriteMessage(vbCrLf & "Шаг 5" & vbCrLf)
RequestedBlockIds.Add(btr.ObjectId)
ed.WriteMessage(vbCrLf & "Шаг 6" & vbCrLf)
'btr.Dispose()
key = True
End If
ed.WriteMessage(vbCrLf & "Шаг 7" & vbCrLf)
' bt.Dispose()
acTrans.Commit()
End Using
If RequestedBlockIds.Count > 0 Then
ed.WriteMessage(vbCrLf & "Шаг 8" & vbCrLf)
Dim ActiveDb As Database = doc.Database
ed.WriteMessage(vbCrLf & "Шаг 9" & vbCrLf)
Dim mapping As New IdMapping()
ed.WriteMessage(vbCrLf & "Шаг 10" & vbCrLf)
SourceDb.WblockCloneObjects(RequestedBlockIds, ActiveDb.BlockTableId, mapping, DuplicateRecordCloning.Ignore, False)
ed.WriteMessage(vbCrLf & "Шаг 11" & vbCrLf)
End If
ed.WriteMessage(vbCrLf & "Шаг 12" & vbCrLf)
' RequestedBlockIds = Nothing
Catch ex As System.Exception
ed.WriteMessage("Ошибка: Поврежден файл " & PathString & vbCrLf)
ed.WriteMessage(vbCrLf & ex.Message & path & vbCrLf)
ed.WriteMessage(vbCrLf & "Шаг 13" & vbCrLf)
key = False
End Try
End Using
ed.WriteMessage(vbCrLf & "Шаг 14" & vbCrLf)
'SourceDb.Dispose()
ed.WriteMessage(vbCrLf & "Шаг 15" & vbCrLf)
' End Using
ed.WriteMessage(vbCrLf & "Шаг 16" & vbCrLf)
Return key
Else
ed.WriteMessage("Ошибка: Отсутствует файл " & PathString & vbCrLf)
Return key
End If
End Function