.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Import Triangle from excel using modal form of c#.net.

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
bvyas89
1384 Views, 6 Replies

Import Triangle from excel using modal form of c#.net.


Hi 

I have an excel file with a column containing lower width,upper width and height like follows:

Row 1  point
Row 2 10
Row 3 7


I am looking for a .Net code that can read each row cell and draw the triangle automatically in AutoCAD window. I am having a bout 100 rows  and hence I prefer that the code will display all triangle and another polygons at once in AutoCAD window and not draw them one by one. 

I want to open excel file from user read data from excel show it in textbox on form and when user press draw button triangle should be drawn. I wish to have a .Net code to allow batch Trianle drawing instead.
I am new in programming. Please help me as earliest.
Thanks a lot for any assistance
6 REPLIES 6
Message 2 of 7
mzakiralam
in reply to: bvyas89

HI, I can not open your drawing file as I am using ACAD2011. Can you save the file in 2011 Format? or Can you explain lower width and upper width? I did not get what you mean by lower width and upper width. I think with lower width you mean the base of a triangle. I did not get the meaning of upper width. Where do you want to draw the triangle? In your description you have stated that row 1 is Point but in the Excel file row 1 is upper width which is also confusing to me. Kindly make clear so that I can help you.
Message 3 of 7
bvyas89
in reply to: mzakiralam

hi,
Please find Attach file. I am working in civil company.  I am intending to Draw tower from user input in excel. this  exercise wiil be beneficial to me to  complete my task. Lower width is base of triangle and height is height of triangle. Upper Width is not applicable here.

 

Actually i think that if i consider one starting point i can draw base line of traingle from  lower width as i got secon poin. and from base line i can calculate mid point that x and from height y for upper point then it is posssible to me to draw triangle.. 

hope you can help me for this.

thank you so much for your prompt reply. 

Message 4 of 7
bvyas89
in reply to: bvyas89

hope this drawing will clear what i exactly  mean.

Message 5 of 7
mzakiralam
in reply to: bvyas89

Hi ,

With below code you can start your project.

 

trian.JPG

 

 <CommandMethod("TRA")> Public Sub TriangleDraw()
        'declare the form to open
        Dim frmTriangle As TestTraingle = Nothing
        If frmTriangle Is Nothing Then
            frmTriangle = New TestTraingle()
        ElseIf frmTriangle.IsDisposed Then
            frmTriangle = New TestTraingle()
        End If
        'show the form
        Application.ShowModelessDialog(Application.MainWindow.Handle, frmTriangle, False)

    End Sub

 

 

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 TestTraingle
    Dim tra_upperwidth As String = Nothing
    Dim tra_lowerwidth As String = Nothing
    Dim tra_height As String = Nothing
    Private Sub TestTraingle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            'place the excelfile path 
            Dim excelFilePath = "Place your excel file path"
            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 Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.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(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(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

 

Message 6 of 7
mzakiralam
in reply to: bvyas89
Message 7 of 7
bvyas89
in reply to: mzakiralam

Thank you so much Mzakiralam... Its work fine ....

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost