You are on page 1of 7

Imports Autodesk.

AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Public Class Class1

<Autodesk.AutoCAD.Runtime.CommandMethod("RunMe")>
Public Sub RunMe()
MsgBox("I'm glad this works.")
End Sub

<Autodesk.AutoCAD.Runtime.CommandMethod("DrawLine")>
Public Sub DrawLine()
Dim myDoc As Document = DocumentManager.MdiActiveDocument
Dim myDB As Database = myDoc.Database
Dim myEditor As Editor = myDoc.Editor
Using myTrans As Transaction = myDoc.TransactionManager.StartTransaction
Dim myBTR As BlockTableRecord =
myDB.CurrentSpaceId.GetObject(OpenMode.ForWrite)
Dim startPoint As Point3d = myEditor.GetPoint("Select Start
Point:").Value
Dim myPPO As New PromptPointOptions("Select End Point:")
myPPO.BasePoint = startPoint
myPPO.UseBasePoint = True
Dim endPoint As Point3d = myEditor.GetPoint(myPPO).Value
Dim myLine As New Line(startPoint, endPoint)
myBTR.AppendEntity(myLine)
myTrans.AddNewlyCreatedDBObject(myLine, True)
myTrans.Commit()
End Using
End Sub

<Autodesk.AutoCAD.Runtime.CommandMethod("DrawGrid")>
Public Sub DrawGrid()
Dim myDoc As Document = DocumentManager.MdiActiveDocument
Dim myDB As Database = myDoc.Database
Dim myEditor As Editor = myDoc.Editor
Using myTrans As Transaction = myDoc.TransactionManager.StartTransaction
Dim myBTR As BlockTableRecord =
myDB.CurrentSpaceId.GetObject(OpenMode.ForWrite)
For X As Double = 0 To 10 Step 0.5
For Y As Double = 0 To 10 Step 0.5
Dim startPoint As New Point3d(X, 0, 0)
Dim endPoint As New Point3d(X, 10, 0)
Dim myLine As New Line(startPoint, endPoint)
myBTR.AppendEntity(myLine)
myTrans.AddNewlyCreatedDBObject(myLine, True)
startPoint = New Point3d(0, Y, 0)
endPoint = New Point3d(10, Y, 0)
myLine = New Line(startPoint, endPoint)
myBTR.AppendEntity(myLine)
myTrans.AddNewlyCreatedDBObject(myLine, True)
Next
Next
myTrans.Commit()
End Using
End Sub

Public Function DrawLineFunction(Xs As Double, Ys As Double, Zs As Double,


Xe As Double, Ye As Double, Ze As Double) As
ObjectId
Dim myDoc As Document = DocumentManager.MdiActiveDocument
Dim myDB As Database = myDoc.Database
Using myTrans As Transaction = myDoc.TransactionManager.StartTransaction
Dim myBTR As BlockTableRecord =
myDB.CurrentSpaceId.GetObject(OpenMode.ForWrite)
Dim startPoint As New Point3d(Xs, Ys, Zs)
Dim endPoint As New Point3d(Xe, Ye, Ze)
Dim myLine As New Line(startPoint, endPoint)
myBTR.AppendEntity(myLine)
myTrans.AddNewlyCreatedDBObject(myLine, True)
myTrans.Commit()
Return myLine.ObjectId
End Using
End Function

<Autodesk.AutoCAD.Runtime.CommandMethod("DrawLineTest")>
Public Sub DrawLineTest()
'draw square first
DrawLineFunction(0, 0, 0, 4, 0, 0)
DrawLineFunction(4, 0, 0, 4, 4, 0)
DrawLineFunction(4, 4, 0, 0, 4, 0)
DrawLineFunction(0, 4, 0, 0, 0, 0)
'now draw corner to corner
DrawLineFunction(0, 0, 0, 4, 4, 0)
DrawLineFunction(0, 4, 0, 4, 0, 0)
End Sub

<Autodesk.AutoCAD.Runtime.CommandMethod("GetBTRNames")>
Public Sub GetBTRNames()
For Each myBName As String In GetAllBlockTableRecords()
MsgBox(myBName)
Next
End Sub

Public Function GetAllBlockTableRecords() As List(Of String)


Dim myDB As Database = HostApplicationServices.WorkingDatabase
Dim retList As New List(Of String)
Using myTrans As Transaction = myDB.TransactionManager.StartTransaction
Dim myBT As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)
For Each myBTRid As ObjectId In myBT
Dim myBTR As BlockTableRecord = myBTRid.GetObject(OpenMode.ForRead)
retList.Add(myBTR.Name)
Next
End Using
Return retList
End Function

Public Function GetBlockReferencesInModelSpace(DBin As Database) _


As Dictionary(Of String, List(Of ObjectId))

Dim retVal As New Dictionary(Of String, List(Of ObjectId))


Using myTrans As Transaction = DBin.TransactionManager.StartTransaction
Dim myBT As BlockTable = DBin.BlockTableId.GetObject(OpenMode.ForRead)
Dim myBTR As BlockTableRecord =
myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead)
For Each myEntID As ObjectId In myBTR
If myEntID.ObjectClass.DxfName = "INSERT" Then
Dim myBRef As BlockReference =
myEntID.GetObject(OpenMode.ForRead)
If myBRef.Name.StartsWith("*") Then
Dim parentBTR As BlockTableRecord =

myBRef.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)
If retVal.ContainsKey(parentBTR.Name) = False Then
retVal.Add(parentBTR.Name, New List(Of ObjectId))
End If
retVal(parentBTR.Name).Add(myEntID)
Else
If retVal.ContainsKey(myBRef.Name) = False Then
retVal.Add(myBRef.Name, New List(Of ObjectId))
End If
retVal(myBRef.Name).Add(myEntID)
End If
End If
Next
myTrans.Commit()
End Using
Return retVal
End Function

<Autodesk.AutoCAD.Runtime.CommandMethod("WriteBlocksOut")>
Public Sub WriteBlocksOut()
Dim myDB As Database = HostApplicationServices.WorkingDatabase
Dim myBlocks As Dictionary(Of String, List(Of ObjectId)) =
GetBlockReferencesInModelSpace(myDB)

For Each myKVP As KeyValuePair(Of String, List(Of ObjectId)) In myBlocks


If myKVP.Key.ToUpper = "WINDOW" Then
Using myTrans As Transaction =
myDB.TransactionManager.StartTransaction
Dim myDesktop As String =
My.Computer.FileSystem.SpecialDirectories.Desktop
Dim myOutput As New IO.StreamWriter(IO.Path.Combine(myDesktop,
"Windows.txt"))
For Each myOID As ObjectId In myKVP.Value
Dim myBRef As BlockReference =
myOID.GetObject(OpenMode.ForRead)
myOutput.WriteLine(DateTime.Now.ToString & vbTab &
myKVP.Key & vbTab &
myBRef.Position.X & vbTab &
myBRef.Position.Y & vbTab &
myBRef.Position.Z & vbTab &
myBRef.ScaleFactors.X & vbTab &
myBRef.Rotation)
Next
myOutput.Close()
End Using
End If
Next
End Sub
<Autodesk.AutoCAD.Runtime.CommandMethod("WriteBlocksAndAttributesOut")>
Public Sub WriteBlocksAndAttributesOut()
Dim myDB As Database = HostApplicationServices.WorkingDatabase
Dim myBlocks As Dictionary(Of String, List(Of ObjectId)) =
GetBlockReferencesInModelSpace(myDB)

For Each myKVP As KeyValuePair(Of String, List(Of ObjectId)) In myBlocks


If myKVP.Key.ToUpper = "WINDOW" Then
Using myTrans As Transaction =
myDB.TransactionManager.StartTransaction
Dim myDesktop As String =
My.Computer.FileSystem.SpecialDirectories.Desktop
Dim myOutput As New IO.StreamWriter(IO.Path.Combine(myDesktop,
"WindowsAtts.txt"))
For Each myOID As ObjectId In myKVP.Value
Dim myBRef As BlockReference =
myOID.GetObject(OpenMode.ForRead)
myOutput.WriteLine(DateTime.Now.ToString & vbTab &
myKVP.Key & vbTab &
myBRef.Position.X & vbTab &
myBRef.Position.Y & vbTab &
myBRef.Position.Z & vbTab &
myBRef.ScaleFactors.X & vbTab &
myBRef.Rotation)
For Each myAttID As ObjectId In myBRef.AttributeCollection
Dim myAtt As AttributeReference =
myAttID.GetObject(OpenMode.ForRead)
myOutput.WriteLine(vbTab & myAtt.Tag & vbTab &
myAtt.TextString)
Next
Next
myOutput.Close()
End Using
End If
Next
End Sub

<Autodesk.AutoCAD.Runtime.CommandMethod("WriteBlocksAndAttributesOutToExcel")>
Public Sub WriteBlocksAndAttributesOutToExcel()
Dim myDB As Database = HostApplicationServices.WorkingDatabase
Dim myBlocks As Dictionary(Of String, List(Of ObjectId)) =
GetBlockReferencesInModelSpace(myDB)

For Each myKVP As KeyValuePair(Of String, List(Of ObjectId)) In myBlocks


If myKVP.Key.ToUpper = "WINDOW" Then
Using myTrans As Transaction =
myDB.TransactionManager.StartTransaction
Dim myDesktop As String =
My.Computer.FileSystem.SpecialDirectories.Desktop
Dim myExcel As Object = CreateObject("Excel.Application")
myExcel.Visible = True
Dim myWB As Object = myExcel.Workbooks.Add
Dim curRow As Integer = 1
For Each myOID As ObjectId In myKVP.Value
Dim myBRef As BlockReference =
myOID.GetObject(OpenMode.ForRead)
myExcel.ActiveSheet.Cells(curRow, "A").value =
DateTime.Now.ToString
myExcel.ActiveSheet.Cells(curRow, "B").value = myKVP.Key
myExcel.ActiveSheet.Cells(curRow, "C").value =
myBRef.Position.X
myExcel.ActiveSheet.Cells(curRow, "D").value =
myBRef.Position.Y
myExcel.ActiveSheet.Cells(curRow, "E").value =
myBRef.Position.Z
myExcel.ActiveSheet.Cells(curRow, "F").value =
myBRef.ScaleFactors.X
myExcel.ActiveSheet.Cells(curRow, "G").value =
myBRef.Rotation
curRow += 1
For Each myAttID As ObjectId In myBRef.AttributeCollection
Dim myAtt As AttributeReference =
myAttID.GetObject(OpenMode.ForRead)
myExcel.ActiveSheet.Cells(curRow, "B").value =
myAtt.Tag
myExcel.ActiveSheet.Cells(curRow, "C").value =
myAtt.TextString
curRow += 1
Next
Next
myWB.SaveAs(IO.Path.Combine(myDesktop, "WindowsAtts.xlsx"))
myWB = Nothing
myExcel = Nothing
End Using
End If
Next
End Sub
<Autodesk.AutoCAD.Runtime.CommandMethod("WriteBlocksAndAttributesOutToExcel2")>
Public Sub WriteBlocksAndAttributesOutToExcel2()
Dim myDB As Database = HostApplicationServices.WorkingDatabase
Dim myBlocks As Dictionary(Of String, List(Of ObjectId)) =
GetBlockReferencesInModelSpace(myDB)

For Each myKVP As KeyValuePair(Of String, List(Of ObjectId)) In myBlocks


Using myTrans As Transaction = myDB.TransactionManager.StartTransaction
Dim myDesktop As String =
My.Computer.FileSystem.SpecialDirectories.Desktop
Dim myExcel As Object = CreateObject("Excel.Application")
myExcel.Visible = True
Dim myWB As Object = myExcel.Workbooks.Add
Dim curRow As Integer = 1
For Each myOID As ObjectId In myKVP.Value
Dim myBRef As BlockReference =
myOID.GetObject(OpenMode.ForRead)
myExcel.ActiveSheet.Cells(curRow, "A").value =
DateTime.Now.ToString
myExcel.ActiveSheet.Cells(curRow, "B").value = myKVP.Key
myExcel.ActiveSheet.Cells(curRow, "C").value =
myBRef.Position.X
myExcel.ActiveSheet.Cells(curRow, "D").value =
myBRef.Position.Y
myExcel.ActiveSheet.Cells(curRow, "E").value =
myBRef.Position.Z
myExcel.ActiveSheet.Cells(curRow, "F").value =
myBRef.ScaleFactors.X
myExcel.ActiveSheet.Cells(curRow, "G").value = myBRef.Rotation
curRow += 1
For Each myAttID As ObjectId In myBRef.AttributeCollection
Dim myAtt As AttributeReference =
myAttID.GetObject(OpenMode.ForRead)
myExcel.ActiveSheet.Cells(curRow, "B").value = myAtt.Tag
myExcel.ActiveSheet.Cells(curRow, "C").value =
myAtt.TextString
curRow += 1
Next
Next
myWB.SaveAs(IO.Path.Combine(myDesktop, "WindowsAtts.xlsx"))
myWB = Nothing
myExcel = Nothing
End Using
Next
End Sub
<Autodesk.AutoCAD.Runtime.CommandMethod("WriteBlocksAndAttributesOutToExcel3")>
Public Sub WriteBlocksAndAttributesOutToExcel3()
Dim myOFD As New System.Windows.Forms.OpenFileDialog()
myOFD.Multiselect = True
myOFD.Filter = "AutoCAD Drawing (*.dwg)|*.dwg"
If myOFD.ShowDialog = System.Windows.Forms.DialogResult.OK Then
Dim myExcel As Object = CreateObject("Excel.Application")
myExcel.Visible = True
Dim myWB As Object = myExcel.Workbooks.Add
Dim myDesktop As String =
My.Computer.FileSystem.SpecialDirectories.Desktop
Dim curRow As Integer = 1
For Each myFileName As String In myOFD.FileNames
Dim myDB As New Database(False, True)
myDB.ReadDwgFile(myOFD.FileName,
FileOpenMode.OpenForReadAndAllShare, False, "")
Dim myBlocks As Dictionary(Of String, List(Of ObjectId)) =
GetBlockReferencesInModelSpace(myDB)

For Each myKVP As KeyValuePair(Of String, List(Of ObjectId)) In


myBlocks
Using myTrans As Transaction =
myDB.TransactionManager.StartTransaction
For Each myOID As ObjectId In myKVP.Value
Dim myBRef As BlockReference =
myOID.GetObject(OpenMode.ForRead)
myExcel.ActiveSheet.Cells(curRow, "A").value =
DateTime.Now.ToString
myExcel.ActiveSheet.Cells(curRow, "B").value =
myFileName
myExcel.ActiveSheet.Cells(curRow, "C").value =
myKVP.Key
myExcel.ActiveSheet.Cells(curRow, "D").value =
myBRef.Position.X
myExcel.ActiveSheet.Cells(curRow, "E").value =
myBRef.Position.Y
myExcel.ActiveSheet.Cells(curRow, "F").value =
myBRef.Position.Z
myExcel.ActiveSheet.Cells(curRow, "G").value =
myBRef.ScaleFactors.X
myExcel.ActiveSheet.Cells(curRow, "H").value =
myBRef.Rotation
curRow += 1
For Each myAttID As ObjectId In
myBRef.AttributeCollection
Dim myAtt As AttributeReference =
myAttID.GetObject(OpenMode.ForRead)
myExcel.ActiveSheet.Cells(curRow, "C").value =
myAtt.Tag
myExcel.ActiveSheet.Cells(curRow, "D").value =
myAtt.TextString
curRow += 1
Next
Next
End Using
Next
myDB.Dispose()
Next
myWB.SaveAs(IO.Path.Combine(myDesktop, "WindowsAtts.xlsx"))
myWB = Nothing
myExcel = Nothing
End If
End Sub

End Class

You might also like