
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello
I am making Customise tool for autocad which can draw tower automatically. user need to fill excel sheet. Which has all input like panel upper width ,lower width ,height. I want to draw continous all panel one by one as per the panel type user has select.
With Form1 and following code i am able to draw each thing seperately but now i want to draw a tower in single button click.
Imports System.IO
Imports Microsoft.Office.Interop
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices
Public Class Form1
Dim tra_upperwidth As String = Nothing
Dim tra_lowerwidth As String = Nothing
Dim tra_height As String = Nothing
Dim FileName As String
Private Sub btnpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpeak.Click
'get current document
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
'lock document as using modeless form
Using docLock As DocumentLock = doc.LockDocument
Using tx As Transaction = db.TransactionManager.StartTransaction()
'open blocktable
Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
'open model space for write
Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
'converte text value
Dim uWidth As Double = CDbl(TextBox1.Text)
Dim base As Double = CDbl(TextBox2.Text)
Dim tHeight As Double = CDbl(TextBox3.Text)
Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt2 As Point2d = New Point2d(base, 0)
Dim pt3 As Point2d = New Point2d(base / 2, tHeight)
'draw traiangle
Dim tra As Polyline = New Polyline()
tra.AddVertexAt(0, pt1, 0, 0, 0)
tra.AddVertexAt(1, pt2, 0, 0, 0)
tra.AddVertexAt(2, pt3, 0, 0, 0)
tra.Closed = True
'add triangle in model space
ms.AppendEntity(tra)
tx.AddNewlyCreatedDBObject(tra, True)
tx.Commit()
End Using
End Using
End Sub
Public Sub READEXCELFILE(ByVal filePath As String)
'read excel file
Try
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(filePath)
Dim xlSheet As Excel.Worksheet = xlWorkBook.Worksheets("Sheet1")
'get all cells value
tra_upperwidth = xlSheet.Cells(1, 2).Value
tra_lowerwidth = xlSheet.Cells(2, 2).Value
tra_height = xlSheet.Cells(3, 2).Value
xlWorkBook.Close()
'quit excel application
xlApp.Quit()
'relaese excel object
ReleaseExcelObject(xlWorkBook)
ReleaseExcelObject(xlApp)
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
Public Shared Sub ReleaseExcelObject(ByVal obj As Object)
'release excel object after finishing
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Autodesk.AutoCAD.Runtime.Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim OpenFileDialog As New Windows.Forms.OpenFileDialog
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "Excel Worksheets (*.xls)|*.xls|Text File(*.txt)|*.txt|All Files (*.*)|*.*"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
FileName = OpenFileDialog.FileName
End If
Try
'place the excelfile path
Dim excelFilePath = FileName
If File.Exists(excelFilePath) Then
Call READEXCELFILE(excelFilePath)
End If
TextBox1.Text = tra_upperwidth
TextBox2.Text = tra_lowerwidth
TextBox3.Text = tra_height
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnLeg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeg.Click
'get current document
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
'lock document as using modeless form
Using docLock As DocumentLock = doc.LockDocument
Using tx As Transaction = db.TransactionManager.StartTransaction()
'open blocktable
Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
'open model space for write
Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
'converte text value
Dim uWidth As Double = CDbl(TextBox1.Text)
Dim base As Double = CDbl(TextBox2.Text)
Dim tHeight As Double = CDbl(TextBox3.Text)
Dim Xx As Double = ((base - uWidth) / 2)
' Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt2 As Point2d = New Point2d(base / 2, tHeight)
Dim pt3 As Point2d = New Point2d(base, 0)
Dim pt4 As Point2d = New Point2d(base - Xx, tHeight)
Dim pt5 As Point2d = New Point2d(0 + Xx, tHeight)
'draw traiangle
Dim tra As Polyline = New Polyline()
tra.AddVertexAt(0, pt1, 0, 0, 0)
tra.AddVertexAt(1, pt2, 0, 0, 0)
tra.AddVertexAt(2, pt3, 0, 0, 0)
tra.AddVertexAt(3, pt4, 0, 0, 0)
tra.AddVertexAt(4, pt5, 0, 0, 0)
tra.Closed = True
'add triangle in model space
ms.AppendEntity(tra)
tx.AddNewlyCreatedDBObject(tra, True)
tx.Commit()
End Using
End Using
End Sub
Private Sub btnKpanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKpanel.Click
'get current document
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
'lock document as using modeless form
Using docLock As DocumentLock = doc.LockDocument
Using tx As Transaction = db.TransactionManager.StartTransaction()
'open blocktable
Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
'open model space for write
Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
'converte text value
Dim uWidth As Double = CDbl(TextBox1.Text)
Dim base As Double = CDbl(TextBox2.Text)
Dim tHeight As Double = CDbl(TextBox3.Text)
Dim Xx As Double = ((base - uWidth) / 2)
' Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt2 As Point2d = New Point2d(base / 2, tHeight)
Dim pt3 As Point2d = New Point2d(base, 0)
Dim pt4 As Point2d = New Point2d(base - Xx, tHeight)
Dim pt5 As Point2d = New Point2d(0 + Xx, tHeight)
'draw traiangle
Dim tra As Polyline = New Polyline()
tra.AddVertexAt(0, pt1, 0, 0, 0)
tra.AddVertexAt(1, pt2, 0, 0, 0)
tra.AddVertexAt(2, pt3, 0, 0, 0)
tra.AddVertexAt(3, pt4, 0, 0, 0)
tra.AddVertexAt(4, pt5, 0, 0, 0)
tra.AddVertexAt(5, pt1, 0, 0, 0)
tra.AddVertexAt(6, pt3, 0, 0, 0)
tra.Closed = True
'add triangle in model space
ms.AppendEntity(tra)
tx.AddNewlyCreatedDBObject(tra, True)
tx.Commit()
End Using
End Using
End Sub
Private Sub btnXpanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXpanel.Click
'get current document
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
'lock document as using modeless form
Using docLock As DocumentLock = doc.LockDocument
Using tx As Transaction = db.TransactionManager.StartTransaction()
'open blocktable
Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
'open model space for write
Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
'converte text value
Dim uWidth As Double = CDbl(TextBox1.Text)
Dim base As Double = CDbl(TextBox2.Text)
Dim tHeight As Double = CDbl(TextBox3.Text)
Dim Xx As Double = ((base - uWidth) / 2)
' Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt1 As Point2d = New Point2d(0, 0)
Dim pt2 As Point2d = New Point2d(base, 0)
Dim pt3 As Point2d = New Point2d(base - Xx, tHeight)
Dim pt4 As Point2d = New Point2d(0, 0)
Dim pt5 As Point2d = New Point2d(0 + Xx, tHeight)
Dim pt6 As Point2d = New Point2d(base, 0)
'draw traiangle
Dim tra As Polyline = New Polyline()
tra.AddVertexAt(0, pt1, 0, 0, 0)
tra.AddVertexAt(1, pt2, 0, 0, 0)
tra.AddVertexAt(2, pt3, 0, 0, 0)
tra.AddVertexAt(3, pt4, 0, 0, 0)
tra.AddVertexAt(4, pt5, 0, 0, 0)
tra.AddVertexAt(5, pt6, 0, 0, 0)
tra.Closed = True
'add triangle in model space
ms.AppendEntity(tra)
tx.AddNewlyCreatedDBObject(tra, True)
Dim line1 As Line = New Line(New Point3d(pt5.X, pt5.Y, 0), New Point3d(pt3.X, pt3.Y, 0))
ms.AppendEntity(line1)
tx.AddNewlyCreatedDBObject(line1, True)
tx.Commit()
End Using
End Using
End Sub
End Class
Solved! Go to Solution.