.NET

Reply
Active Contributor
bvyas89
Posts: 30
Registered: ‎01-16-2014
Message 1 of 15 (410 Views)
Accepted Solution

Draw Tower WIth Dialog Box

410 Views, 14 Replies
02-13-2014 10:40 PM

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

 

 

 

HI , please find below code. I have tested this and this is working. One thing I have changed in your excel file is a syntax. In your excel file it was 'Lower width'. I have changed it as 'Lower Width'. for your information, I have created a class 'Tower'. This tower class will collect all the necessary data from excel file. I have test this code and it is working fine. Hope you will find it useful. I did not put comment on the code as it is self explanatory. but incase if you do not get anything just ask me.

 

 <CommandMethod("TR")> Public Sub DrawTower()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim filePath As String = "C:\Users\alamzaki\Desktop\TowerInput.xls"
Dim traTower As New Tower(filePath)
'MsgBox(traTower.triInfoList.Count)
'MsgBox(traTower.triInfoList.Item(0).panelHeight)
Using docLock As DocumentLock = doc.LockDocument()
Using tx As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim basePoint As Point2d = New Point2d(traTower.triInfoList.Item(0).lowerWidth / 2, 0)
For i As Integer = 0 To traTower.triInfoList.Count - 1
Dim base As Double = traTower.triInfoList.Item(i).lowerWidth
Dim ht As Double = traTower.triInfoList.Item(i).panelHeight
Dim point1 As Point2d = New Point2d(basePoint.X - base / 2, basePoint.Y)
Dim point2 As Point2d = New Point2d(basePoint.X, basePoint.Y + ht)
Dim point3 As Point2d = New Point2d(basePoint.X + base / 2, basePoint.Y)
Call TraingleBuild(point1, point2, point3, tx, ms)
basePoint = point2
Next
tx.Commit()
End Using
End Using
End Sub
Public Sub TraingleBuild(pt1 As Point2d, pt2 As Point2d, pt3 As Point2d, trans As Transaction, blkTblRec As BlockTableRecord)
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)
'for leg do not close the polyline
tra.Closed = True
blkTblRec.AppendEntity(tra)
trans.AddNewlyCreatedDBObject(tra, True)
End Sub
Public Class Tower
Structure TriangleInfo
Dim panelHeight As Double
Dim upperWidth As Double
Dim lowerWidth As Double
End Structure
Public triInfoList As New List(Of TriangleInfo)
Sub New(excelFilePath As String)
'read excel file
Try
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(excelFilePath)
Dim xlSheet As Excel.Worksheet = xlWorkBook.Worksheets("Sheet1")
Dim range As Excel.Range
Dim rCnt As Integer
Dim cCnt As Integer
Dim Obj As Object
Dim stTriangleInfo As New TriangleInfo()
range = xlSheet.UsedRange
For rCnt = 2 To range.Rows.Count
For cCnt = 1 To range.Columns.Count
Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
'MsgBox(Obj.value)
If xlSheet.Cells(1, cCnt).Value = "Panel Height" Then
stTriangleInfo.panelHeight = CDbl(Obj.Value)
ElseIf xlSheet.Cells(1, cCnt).Value = "Upper Width" Then
stTriangleInfo.upperWidth = CDbl(Obj.Value)
ElseIf xlSheet.Cells(1, cCnt).Value = "Lower Width" Then
stTriangleInfo.lowerWidth = CDbl(Obj.value)
End If
Next
triInfoList.Add(stTriangleInfo)
Next
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
Private Sub ReleaseExcelObject(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
End Class

 

Active Contributor
bvyas89
Posts: 30
Registered: ‎01-16-2014
Message 2 of 15 (398 Views)

Re: Draw Tower WIth Dialog Box

02-13-2014 11:00 PM in reply to: bvyas89

Now I want to draw Tower on draw button burt not getting idea how to draw it. Please help me.

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 3 of 15 (334 Views)

Re: Draw Tower WIth Dialog Box

02-16-2014 02:22 PM in reply to: bvyas89
Use a loop to get all data of a tower from datagrid view and draw it as you did with above code
Active Contributor
bvyas89
Posts: 30
Registered: ‎01-16-2014
Message 4 of 15 (322 Views)

Re: Draw Tower WIth Dialog Box

02-17-2014 10:42 PM in reply to: bvyas89

Yupp i tried that but not working for me... As i need the last point as first point for next panel. Canyou please help me. I know There is very small thing which i am missing in my code.Waiting for reply.

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 5 of 15 (311 Views)

Re: Draw Tower WIth Dialog Box

02-18-2014 02:33 AM in reply to: bvyas89
HI,
first Thing is I can not open your drawing as I have only ACAD 2010 and ACAD 2011. Kindly save your drawing file in 2010 or 2011 Format so that I can see your drawing. Another issue is from where K-Panel and L-Panel data should be read? because for base triangle data are read from Excel file. but in your new_draw_tower.png Picture it seems like that data Need to read from datagrid view or something else? please clarify from stat to end once more.. if all data are in Excel sheet then please provide me that sheet as well. for better clarification you can draw a Picture like before.
Active Contributor
bvyas89
Posts: 30
Registered: ‎01-16-2014
Message 6 of 15 (305 Views)

Re: Draw Tower WIth Dialog Box

02-18-2014 04:12 AM in reply to: mzakiralam

please find attach drawing.  Sir If i take starting point zero i can do that easily . bt now i want to draw continously by reading data row by row. and start from end point.

 

my current code is as follow:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Panel_name, Panel_type As String
Dim Panel_height, Panel_lwidth, Panel_uwidth As Double
Dim Panel_no As Integer
Panel_name = String.Empty
Panel_type = String.Empty

'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)

For Each row As System.Windows.Forms.DataGridViewRow In DataGridView1.Rows
Panel_no = row.Cells(0).Value
Panel_name = row.Cells(1).Value
Panel_height = row.Cells(2).Value
Panel_uwidth = row.Cells(3).Value
Panel_lwidth = row.Cells(4).Value
Panel_type = row.Cells(5).Value
Dim Xx As Double = ((Panel_lwidth - Panel_uwidth) / 2)
Dim pt1 As Point2d
Dim pt_start As Point2d
Dim pt2 As Point2d = New Point2d(Panel_lwidth / 2, Panel_height)
Dim pt3 As Point2d = New Point2d(Panel_lwidth, 0)
Dim pt4 As Point2d = New Point2d(Panel_lwidth - Xx, Panel_height)
Dim pt5 As Point2d = New Point2d(0 + Xx, Panel_height)
pt1 = New Point2d(0, 0)
Dim tra As Polyline = New Polyline()

If Panel_type = "LEG" Then
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

End If
If Panel_type = "K-PANEL" Then
'draw traiangle
tra.AddVertexAt(0, pt_start, 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
ElseIf Panel_type = "X-PANEL" Then
tra.AddVertexAt(0, pt_start, 0, 0, 0)
tra.AddVertexAt(1, pt4, 0, 0, 0)
tra.AddVertexAt(2, pt3, 0, 0, 0)
tra.AddVertexAt(3, pt5, 0, 0, 0)
tra.Closed = True
'add triangle in model space
End If
'add triangle in model space
ms.AppendEntity(tra)
tx.AddNewlyCreatedDBObject(tra, True)
tx.Commit()
pt_start = pt5
Next
End Using
End Using
End Sub

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 7 of 15 (294 Views)

Re: Draw Tower WIth Dialog Box

02-18-2014 09:21 AM in reply to: bvyas89

Hi , below you will find some code to draw tower. I did not test but should be ok in logical point of view.

 

 Dim htLegPanel As String = Nothing
 Dim uWidthLegPanel As String = Nothing
 Dim lWidthLegPanel As String = Nothing
 'K-Panel
 Dim htKPanel As String = Nothing
 Dim uWidthKPanel As String = Nothing
 Dim lWidthKPanel As String = Nothing
 'X-panel
 Dim htXPanel As String = Nothing
 Dim uWidthXPanel As String = Nothing
 Dim lWidthXPanel As String = Nothing

 

  <CommandMethod("DT")> Public Sub DrawTraingle()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        'add your excel file path
        Dim fPath As String = "C:\Users\alamzaki\Desktop\TowerInput.xls"
        Call READEXCELFILE(fPath)
        Dim base As Double = Nothing
        Dim ht As Double = Nothing
        Dim basePoint As Point2d = New Point2d(0, 0)
        'Leg Triangle
        base = CDbl(lWidthLegPanel)
        ht = CDbl(htLegPanel)
        'start point
        Dim L_pt1 As Point2d = basePoint
        'mid point
        Dim L_pt2 As Point2d = New Point2d(((base / 2) + ht), ht)
        'end point
        Dim L_pt3 As Point2d = New Point2d(base, 0)
        'K triangle
        base = CDbl(lWidthKPanel)
        ht = CDbl(htKPanel)
        basePoint = L_pt2
        'start point
        Dim K_pt1 As Point2d = New Point2d(basePoint.X - (base / 2), basePoint.Y)
        'mid point
        Dim K_pt2 As Point2d = New Point2d(basePoint.X, basePoint.Y + ht)
        'end point
        Dim K_pt3 As Point2d = New Point2d(basePoint.X + (base / 2), basePoint.Y)
        'X triangle
        base = CDbl(lWidthXPanel)
        ht = CDbl(htXPanel)
        basePoint = K_pt2
        Dim X_pt1 As Point2d = New Point2d(basePoint.X - (base / 2), basePoint.Y)
        Dim X_pt2 As Point2d = New Point2d(basePoint.X, basePoint.Y + ht)
        Dim X_pt3 As Point2d = New Point2d(basePoint.X + (base / 2), basePoint.Y)

        Using tx As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            'draw leg
            Call TraingleBuild(L_pt1, L_pt2, L_pt3, False, tx, ms)
            'draw k
            Call TraingleBuild(K_pt1, K_pt2, K_pt3, True, tx, ms)
            'draw x
            Call TraingleBuild(X_pt1, X_pt2, X_pt3, True, tx, ms)
            tx.Commit()
        End Using
    End Sub

  Public Sub READEXCELFILE(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
            htLegPanel = xlSheet.Cells(2, 3).Value
            uWidthLegPanel = xlSheet.Cells(2, 4).Value
            lWidthLegPanel = xlSheet.Cells(2, 5).Value
            'kpanel
            htKPanel = xlSheet.Cells(3, 3).Value
            uWidthKPanel = xlSheet.Cells(3, 4).Value
            lWidthKPanel = xlSheet.Cells(3, 5).Value
            ''xpanel
            htXPanel = xlSheet.Cells(4, 3).Value
            uWidthXPanel = xlSheet.Cells(4, 4).Value
            lWidthXPanel = xlSheet.Cells(4, 5).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(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
    Public Sub TraingleBuild(pt1 As Point2d, pt2 As Point2d, pt3 As Point2d, triangleClosed As Boolean, trans As Transaction, blkTblRec As BlockTableRecord)
        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)
        'for leg do not close the polyline
        If triangleClosed = True Then
            tra.Closed = True
        End If
        blkTblRec.AppendEntity(tra)
        trans.AddNewlyCreatedDBObject(tra, True)
    End Sub

 

Active Contributor
bvyas89
Posts: 30
Registered: ‎01-16-2014
Message 8 of 15 (278 Views)

Re: Draw Tower WIth Dialog Box

02-18-2014 08:30 PM in reply to: mzakiralam
hello... Thanx for your reply. It Doesnot woking for me... input ,No of panel and type can be very so i want it dynamic that read it and perform Action
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 9 of 15 (263 Views)

Re: Draw Tower WIth Dialog Box

02-19-2014 01:07 AM in reply to: bvyas89
ok so if it is varied , I have to change the code. I just gave you some snippet so that you get some idea. I will rephrase the code and post later
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 10 of 15 (256 Views)

Re: Draw Tower WIth Dialog Box

02-19-2014 02:59 AM in reply to: bvyas89

HI , please find below code. I have tested this and this is working. One thing I have changed in your excel file is a syntax. In your excel file it was 'Lower width'. I have changed it as 'Lower Width'. for your information, I have created a class 'Tower'. This tower class will collect all the necessary data from excel file. I have test this code and it is working fine. Hope you will find it useful. I did not put comment on the code as it is self explanatory. but incase if you do not get anything just ask me.

 

   <CommandMethod("TR")> Public Sub DrawTower()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim filePath As String = "C:\Users\alamzaki\Desktop\TowerInput.xls"
        Dim traTower As New Tower(filePath)
        'MsgBox(traTower.triInfoList.Count)
        'MsgBox(traTower.triInfoList.Item(0).panelHeight)
        Using docLock As DocumentLock = doc.LockDocument()
            Using tx As Transaction = db.TransactionManager.StartTransaction()
                Dim bt As BlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
                Dim ms As BlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                Dim basePoint As Point2d = New Point2d(traTower.triInfoList.Item(0).lowerWidth / 2, 0)
                For i As Integer = 0 To traTower.triInfoList.Count - 1
                    Dim base As Double = traTower.triInfoList.Item(i).lowerWidth
                    Dim ht As Double = traTower.triInfoList.Item(i).panelHeight
                    Dim point1 As Point2d = New Point2d(basePoint.X - base / 2, basePoint.Y)
                    Dim point2 As Point2d = New Point2d(basePoint.X, basePoint.Y + ht)
                    Dim point3 As Point2d = New Point2d(basePoint.X + base / 2, basePoint.Y)
                    Call TraingleBuild(point1, point2, point3, tx, ms)
                    basePoint = point2
                Next
                tx.Commit()
            End Using
        End Using
    End Sub

 Public Sub TraingleBuild(pt1 As Point2d, pt2 As Point2d, pt3 As Point2d, trans As Transaction, blkTblRec As BlockTableRecord)
        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)
        'for leg do not close the polyline
        tra.Closed = True
        blkTblRec.AppendEntity(tra)
        trans.AddNewlyCreatedDBObject(tra, True)
    End Sub

Public Class Tower
    Structure TriangleInfo
        Dim panelHeight As Double
        Dim upperWidth As Double
        Dim lowerWidth As Double
    End Structure
    Public triInfoList As New List(Of TriangleInfo)

    Sub New(excelFilePath As String)
        'read excel file
        Try
            Dim xlApp As New Excel.Application
            Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(excelFilePath)
            Dim xlSheet As Excel.Worksheet = xlWorkBook.Worksheets("Sheet1")
            Dim range As Excel.Range
            Dim rCnt As Integer
            Dim cCnt As Integer
            Dim Obj As Object
            Dim stTriangleInfo As New TriangleInfo()
            range = xlSheet.UsedRange

            For rCnt = 2 To range.Rows.Count
                For cCnt = 1 To range.Columns.Count
                    Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
                    'MsgBox(Obj.value)
                    If xlSheet.Cells(1, cCnt).Value = "Panel Height" Then
                        stTriangleInfo.panelHeight = CDbl(Obj.Value)
                    ElseIf xlSheet.Cells(1, cCnt).Value = "Upper Width" Then
                        stTriangleInfo.upperWidth = CDbl(Obj.Value)
                    ElseIf xlSheet.Cells(1, cCnt).Value = "Lower Width" Then
                        stTriangleInfo.lowerWidth = CDbl(Obj.value)
                    End If
                Next
                triInfoList.Add(stTriangleInfo)
            Next
            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

    Private Sub ReleaseExcelObject(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

End Class

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.