Private Sub GetBlockNameDetailsFromFile(ByVal FileName As String)
Dim Doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim Ed As Editor = Doc.Editor
' create a new database
Dim dwgs As Database = New Database(False, True)
Dim ThisDrawingBlockCount As Integer = 0
Try
dwgs.ReadDwgFile(FileName, FileOpenMode.OpenForReadAndReadShare, True, "")
Using Doc.LockDocument
Using trans As Transaction = dwgs.TransactionManager.StartTransaction
Dim bt As BlockTable = CType(trans.GetObject(dwgs.BlockTableId, OpenMode.ForRead, False), BlockTable)
For Each OID As ObjectId In bt
Dim btr As BlockTableRecord = CType(trans.GetObject(OID, OpenMode.ForRead, False), BlockTableRecord)
If btr.IsErased = False AndAlso btr.IsAnonymous = False Then
If Not btr.Name.ToString.StartsWith("*") Then
Dim Col As ObjectIdCollection = btr.GetBlockReferenceIds(True, False)
Dim eXts As Extents3d
If Col.Count > 0 Then
Dim Bref As BlockReference = TryCast(trans.GetObject(Col.Item(0), OpenMode.ForRead), BlockReference)
eXts = Bref.GeometricExtents 'Save Block Extents just incase
Dim Ans As Boolean = True
Dim NR As DataRow = BlockDataSet.Tables("BlockDetails").NewRow
NR.Item("FileNameIncPath") = FileName
NR.Item("FileName") = Path.GetFileNameWithoutExtension(FileName)
NR.Item("BlockName") = btr.Name.ToString
NR.Item("HasAttribute") = btr.HasAttributeDefinitions
'Problem Area - Start
Dim Xts As Extents3d = New Extents3d
Dim ReturnFoundData As Boolean = False
For Each id As ObjectId In btr
Dim Entt As Entity = DirectCast(trans.GetObject(id, OpenMode.ForRead), Entity)
'Get Extents of object provided its not Text, mtext or Attribute
If (Entt.GetRXClass.UnmanagedObject <> Autodesk.AutoCAD.Runtime.RXObject.GetClass(GetType(AttributeDefinition)).UnmanagedObject Or _
Entt.GetRXClass.UnmanagedObject <> Autodesk.AutoCAD.Runtime.RXObject.GetClass(GetType(DBText)).UnmanagedObject Or _
Entt.GetRXClass.UnmanagedObject <> Autodesk.AutoCAD.Runtime.RXObject.GetClass(GetType(MText)).UnmanagedObject) AndAlso _
Entt.Bounds IsNot Nothing Then
Xts.AddExtents(CType(Entt.Bounds, Autodesk.AutoCAD.DatabaseServices.Extents3d))
ReturnFoundData = True
End If
Next
If ReturnFoundData = False Then
Xts = Bref.GeometricExtents
End If
'Problem Area - End
NR.Item("BlockWidth") = Math.Abs(Xts.MaxPoint.X - Xts.MinPoint.X)
NR.Item("BlockHeight") = Math.Abs(Xts.MaxPoint.Y - Xts.MinPoint.Y)
NR.Item("PointIsInside") = Ans
BlockDataSet.Tables("BlockDetails").Rows.Add(NR)
ThisDrawingBlockCount += 1
End If
End If
End If
Next
trans.Commit()
End Using
End Using
Catch ex As Exception
Ed.WriteMessage("Error Reading " & FileName & vbCrLf & ex.Message.ToString & vbCrLf)
Finally
dwgs.Dispose()
If ThisDrawingBlockCount = 0 Then
Dim NR As DataRow = BlockDataSet.Tables("BlockDetails").NewRow
NR.Item("FileNameIncPath") = FileName
NR.Item("FileName") = Path.GetFileNameWithoutExtension(FileName)
NR.Item("BlockName") = ""
NR.Item("HasAttribute") = False 'TODO
NR.Item("BlockWidth") = 0 'TODO
NR.Item("BlockHeight") = 0 'TODO
NR.Item("PointIsInside") = False 'TODO
BlockDataSet.Tables("BlockDetails").Rows.Add(NR)
End If
End Try
dwgs = Nothing
End Sub