You are on page 1of 3

D:\VB.NET\New folder\PRACTICE 1\PRACTICE 1\PRACTICE1.

vb 1
1
2 Imports Autodesk.AutoCAD.ApplicationServices
3 Imports Autodesk.AutoCAD.DatabaseServices
4 Imports Autodesk.AutoCAD.Runtime
5 Imports Autodesk.AutoCAD.EditorInput
6 Imports excel = Microsoft.Office.Interop.Excel
7
8 Public Class PRACTICE1
9 <CommandMethod("ShowExcelutilform")>
10 Public Sub ShowExcelutilform()
11 Dim form As New Excelutilform()
12 form.Show()
13 End Sub
14
15 Private Function GetExcelWorksheet() As excel.Worksheet
16
17 'Create Excel Application Object and make it Visible
18 Dim Excel As excel.Application
19 Excel = CreateObject("Excel.Application")
20 Excel.Visible = True
21
22 'Create a New workbook
23 Dim wb As excel.Workbook
24 wb = Excel.Workbooks.Add
25
26 'Create anew Worksheet
27 Dim ws As excel.Worksheet
28 ws = wb.Worksheets(1)
29
30 Return ws
31
32 End Function
33
34 Public Function ExportLines() As String
35 Dim result As String = ""
36
37 'Gather all the Lines ina a Selection Set
38 Dim cu As CommonUtil = New CommonUtil()
39 Dim ss = cu.GetObejects("Line")
40 If ss Is Nothing Then
41 Return "No Line Found in the Drawing."
42 End If
43
44 Try
45 'Connect to Excel and get a Worksheet
46 Dim ws As New excel.Worksheet
47 ws = GetExcelWorksheet()
48 ws.Name = "Lines"
49
D:\VB.NET\New folder\PRACTICE 1\PRACTICE 1\PRACTICE1.vb 2
50 'Create column Headers
51 Dim range As excel.Range
52 range = ws.Range("B2")
53 range.Value = "StartPtx"
54 range = ws.Range("C2")
55 range.Value = "StartPty"
56 range = ws.Range("D2")
57 range.Value = "EndPtx"
58 range = ws.Range("E2")
59 range.Value = "EndPty"
60 range = ws.Range("F2")
61 range.Value = "Layer"
62 range = ws.Range("G2")
63 range.Value = "LineTyp"
64 range = ws.Range("H2")
65 range.Value = "Length"
66
67 'Loop through the lines and extract to excel one at time
68 Dim doc As Document =
Application.DocumentManager.MdiActiveDocument
69 Dim rowNum As Integer = 3
70 Using trans As Transaction =
doc.TransactionManager.StartTransaction()
71
72 For Each sObj As SelectedObject In ss
73 Dim line As Line = TryCast(trans.GetObject
(sObj.ObjectId, OpenMode.ForRead), Line)
74 ws.Range("B" & rowNum).Value = line.StartPoint.X
75 ws.Range("C" & rowNum).Value = line.StartPoint.Y
76 ws.Range("D" & rowNum).Value = line.EndPoint.X
77 ws.Range("E" & rowNum).Value = line.EndPoint.Y
78 ws.Range("F" & rowNum).Value = line.Layer
79 ws.Range("G" & rowNum).Value = line.Linetype
80 ws.Range("G" & rowNum).Value = line.Length
81
82 rowNum += 1
83
84 Next
85
86 End Using
87 result = "Successfully extracted lines to Excel"
88
89 Catch ex As Exception
90 result = "Error encountered: " & ex.Message
91
92 End Try
93 Return result
94 End Function
95
D:\VB.NET\New folder\PRACTICE 1\PRACTICE 1\PRACTICE1.vb 3
96 End Class
97
98
99

You might also like