This is my first post so Hello everybody J
If I give you a brief description of my programming history I hope you will then be gentle with me.The main goal is to calculate the total weight (Attribute Value) of the blocks in the Drawing, export these block files into Excel, showing block totals and weight totals, a sort of bill of materials but using weights.
See possible output below
Block Name Total Blocks Block Weight Total Block Weight
Nut 50 10g 500g
Bolt 75 25g 1875g
Washer 100 5g 500g
Totals 225 2875g
(There could be over 50 different blocks and weight values in a drawing)
The block name and the number of times it occurs in the drawing and total weights are important. These values will be used to calculate the size of lorry required for delivery.
A friend in the Formwork industry has showed me that AutoCAD has ability of to extract attributes using the ATTEXT to a text file, which in turn can be opened in Excel and Access. This is great until the drawing contains say 10 or more block files each inserted say 50 times.
So I have attempted to write some VBA code and I’m failing miserably. I don’t have the syntax knowledge to get it to work.
From reading other peoples code am I right in thinking you must firstly find and count the blocks in the drawing and then ask the blocks for their attributes or do I include the code in the middle of the count loop???????
This all sound like I know what I’m talking about.
I assure you I don’t I have a little VB and AutoCAD knowledge
Se below some code that I have attempted………
Public excelApp As Object
Public wkbObj As Object
Public shtObj As Object
Dim gtotal As Integer
Sub CommandButton1_Click()
Dim a, b, blocktotal, count As Integer
Dim blockname As String
Dim ent As Object
blocktotal = ThisDrawing.Blocks.count
For a = 0 To blocktotal - 1
blockname = ThisDrawing.Blocks.Item(a).Name
If Not Mid$(blockname, 1, 1) = "*" Then ListBox1.AddItem blockname
Next a
For a = 0 To ListBox1.ListCount - 1
blockname = ListBox1.List(a): blocktotal = 0
For b = 0 To ThisDrawing.ModelSpace.count - 1
Set ent = ThisDrawing.ModelSpace.Item(b)
If ent.EntityType = acBlockReference And ent.Name = blockname Then blocktotal = blocktotal + 1
Next b
ListBox2.AddItem blocktotal
gtotal = gtotal + blocktotal
Next a
ListBox3.AddItem gtotal
End Sub
Sub CommandButton2_Click()
On Error Resume Next
Set excelApp = GetObject(, "Excel.Application")
If Err 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err 0 Then
MsgBox "Could not start Excel!", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = excelApp.Workbooks.Add
Set shtObj = wbkObj.Worksheets(1)
shtObj.Name = "Block Name & Count"
Dim a, b, blocktotal As Integer
Dim blockname As String
b = 2
For a = 0 To ListBox1.ListCount - 1
blockname = ListBox1.List(a)
blocktotal = ListBox2.List(a)
shtObj.Cells(b, 1).Value = blockname
shtObj.Cells(b, 2).Value = blocktotal
b = b + 1
Next a
b = b + 1
shtObj.Cells(1, 1).columnwidth = 12
shtObj.Cells(1, 1).Value = "Blocks"
shtObj.Cells(1, 1).Font.Bold = True
shtObj.Cells(1, 2).columnwidth = 8
shtObj.Cells(1, 2).Value = "Number"
shtObj.Cells(1, 2).Font.Bold = True
shtObj.Cells(b, 1).Value = "Grand Total="
shtObj.Cells(b, 1).Font.Bold = True
shtObj.Cells(b, 2).Value = gtotal
End Sub
Sub CommandButton3_Click()
End
End Sub