Dave Espinosa-Aguilar's tutorials - Tallying length of lines

Discussion in 'AutoCAD' started by Pienpeas, Apr 22, 2004.

  1. Pienpeas

    Pienpeas Guest

    Hi,

    I've just been going through Dave Espinosa-Aguilar's excellent Integrating Integrating Autocad and Excel tutorials. I'm trying to tally up the length of lines on layers selected through

    ThisDrawing.Utility.GetEntity EntObj, PntA, "Select LINE: "

    which should then create a report in Excel, however the total length of the lines is always 0. Any ideas anyone???

    Using the following code:-

    Public excelApp As Object
    Public wkbObj As Object
    Public shtObj As Object

    Private Sub CommandButton1_Click()

    'Code for Select LINEs for length tally CommandButton
    Dim EntObj As AcadObject
    Dim PntA As Variant
    Dim etype As String
    On Error Resume Next
    ListBox1.Clear
    Err.Clear
    UserForm1.Hide
    MsgBox ("Right-click or select nothing to exit mode.")
    ThisDrawing.Utility.GetEntity EntObj, PntA, "Select LINE: "
    Do While Err = 0
    etype = EntObj.ObjectName
    If etype = "AcDbLine" Then
    ListBox1.AddItem EntObj.Layer
    Else
    MsgBox ("This entity is not a LINE.")
    End If
    ThisDrawing.Utility.GetEntity EntObj, PntA, "Select LINE: "
    Loop
    ' Delete multiple entries
    ListBox2.Clear
    text1$ = ListBox1.List(0)
    ListBox2.AddItem text1$
    For i = 1 To ListBox1.ListCount - 1
    text1$ = ListBox1.List(i)
    found = 0
    For j = 0 To ListBox2.ListCount - 1
    text2$ = ListBox2.List(j)
    If text1$ = text2$ Then found = 1
    Next j
    If found = 0 Then ListBox2.AddItem text1$
    Next i
    UserForm1.Show
    End Sub

    Private Sub CommandButton2_Click()

    'Code for Create Spreadsheet
    Dim EntObj As AcadObject
    Dim lname As String
    Dim ename As String
    Dim test As String
    Dim tot As Integer
    On Error Resume Next
    UserForm1.Hide
    Err.Clear
    Set excelApp = CreateObject("Excel.Application")
    If Err <> 0 Then
    MsgBox "Could not start Excel", vbExclamation
    End
    Else
    excelApp.Visible = True
    Set wbkObj = excelApp.Workbooks.Add
    Set shtObj = excelApp.Worksheets(1)
    shtObj.Cells(i + 1, 1) = "Layer Name"
    shtObj.Cells(i + 1, 2) = "Total Length"
    For i = 0 To ListBox2.ListCount - 1
    tot = 0
    test = ListBox2.List(i)
    For j = 0 To ThisDrawing.ModelSpace.Count '-1
    Set EntObj = ThisDrawing.ModelSpace.Item(j)
    lname = EntObj.Layer
    ename = EntObj.ObjectName
    If lname = test And ename = "AcDdLine" Then
    tot = tot + EntObj.Length
    End If
    Next j
    shtObj.Cells(i + 2, 1) = test
    shtObj.Cells(i + 2, 2) = tot
    Next i
    End If
    UserForm1.Show
    End Sub

    Private Sub CommandButton3_Click()

    'Code for Quit CommandButton
    End
    End Sub
     
    Pienpeas, Apr 22, 2004
    #1
  2. Pienpeas

    Kevin Terry Guest

    typo:
    should be
    note there is a better way to determine the line, by using the TypeOf
    method:
    If TypeOf EntObj Is AcadLine Then
    this will eliminate the possibility of this type of typo for one thing...

    HTH,
    Kevin

    Integrating Autocad and Excel tutorials. I'm trying to tally up the length
    of lines on layers selected through
    the lines is always 0. Any ideas anyone???
     
    Kevin Terry, Apr 22, 2004
    #2
  3. Pienpeas

    pienpeas Guest

    Thanks very much Kevin,

    Much Appreciated.
     
    pienpeas, Apr 22, 2004
    #3
  4. Pienpeas

    pienpeas Guest

    I have another question!

    I would like to modify this further if possible. Say i have two types of block that i am using in the drawing, named Block1 and Block2. Could i get the program to also count up the number of these block insertions for each layer? The same two blocks would be used on all of the different layers. I would also want another 2 columns in the excel worksheet 'Block1 Total' and 'Block2 Total' where the tally of each block would be sent to.

    Would this be easy to do?
     
    pienpeas, Apr 22, 2004
    #4
  5. A couple of comments...

    1) This is a good example of one of the advantages of using "TypeOf" to
    check entity types, since it will stop the program with an error if the
    object name is not recognized.... i.e. "If TypeOf EntObj is AkadLine Then"
    would cause an error.

    2) The debug window and a smallish model would help find this error with the
    following lines. It would show that objects which should be going through
    the IF statement are not.

    ename = EntObj.ObjectName
    Debug.Print EntObj.handle & " : " & ename & " : " & lname
    If lname = test And ename = "AcDdLine" Then
    Debug.Print " " & EntObj.Length
    tot = tot + EntObj.Length
    End If

    HTH,
    James
     
    James Belshan, Apr 22, 2004
    #5
  6. Pienpeas

    pienpeas Guest

    Thanks James,

    That is very helpful, i read through that code so many times in the early hours this morning trying to find the error!

    Can you guys and gals recommend any good books, tutorials or sites to me? I'm AutoCAD literate and have been learning VB for a year now and have just started to try and write some VBA programs for AutoCAD. So i'm looking for stuff that will help a newbie learn about AutoCAD VBA.

    Thanks again.
     
    pienpeas, Apr 22, 2004
    #6
  7. Pienpeas

    Kevin Terry Guest

    Joe Sutphin has a good one, and any VB book in general will be good for
    picking up the basics.

    Kevin

    hours this morning trying to find the error!
    I'm AutoCAD literate and have been learning VB for a year now and have just
    started to try and write some VBA programs for AutoCAD. So i'm looking for
    stuff that will help a newbie learn about AutoCAD VBA.
     
    Kevin Terry, Apr 22, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.