OK, so I finally had a chance to put my VB.NET purchase to work. A simple little application to gather & count all blocks inserted on all layers and display the results in a Treeview on a form.
After figuring how to fill the nodes of the Treeview, all went rather smoothly. That is, until I ran it. Since I'm still using Acad2002 I have to reference the COM object, and I think this is where the problems start. Anyway, my test drawing consisted of 4 layers with 3 blocks inserted a random number of times on each layer, for a grand total of 14 blocks in the drawing. Then I execute my block-count.exe from Windows Explorer and a snappy 15, or so, seconds later my form is displayed. "Hmmm...", I think to myself, "Pretty darn slow there. I wonder what it will do on a larger drawing?" So I load up a drawing with about 500 block inserts and a ton of layers (the routine searches Modelspace, so the layer count shouldn't matter), fire-up block-count.exe, and about 3 minutes later it displays the result. "AACCKK!!", this is not very good.....
So, is my slowdown caused by the COM object, or the filling of the Treeview, or just my basic lack of understanding how to do things in VB? I have never tried to do any programming outside of Lisp & VBA, so I suspect it's probably all 3......here's the pertinent code if anyone cares to see the crud I can produce:
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
Dim objAcad As AutoCAD.AcadApplication
Dim ThisDwg As AutoCAD.AcadDocument
Dim oEnt As AutoCAD.AcadEntity
Dim oBlk As AutoCAD.AcadBlockReference
Dim sLayName As String
Dim sBlkName As String
Dim oNodes As TreeNodeCollection
Dim oNodeLay As TreeNode
Dim oNodeBlk As TreeNode
Dim oNodeCnt As TreeNode
objAcad = GetObject(, "autocad.application")
ThisDwg = objAcad.ActiveDocument
oNodes = TreeView1.Nodes
For Each oEnt In ThisDwg.ModelSpace
If TypeOf oEnt Is AutoCAD.AcadBlockReference Then
oblk = oEnt
sBlkName = oblk.Name
sLayName = oblk.Layer
If NodeAvailable(sLayName, oNodes) Then
oNodeLay = New TreeNode(sLayName)
TreeView1.Nodes.Add(oNodeLay)
Else
oNodeLay = AvailableNode(sLayName, oNodes)
End If
If NodeAvailable(sBlkName, oNodeLay.Nodes) Then
oNodeBlk = New TreeNode(sBlkName)
oNodeLay.Nodes.Add(oNodeBlk)
oNodeCnt = New TreeNode("1")
oNodeBlk.Nodes.Add(oNodeCnt)
Else
oNodeBlk = AvailableNode(sBlkName, oNodeLay.Nodes)
oNodeCnt = oNodeBlk.Nodes.Item(0)
oNodeCnt.Text = oNodeCnt.Text + 1
End If
End If
Next
End Sub
'The following 2 functions derived from http://www.dotnetspider.com/technology/kb/Article1271.aspx by Mahesh
Private Function NodeAvailable(ByVal NodeValue As String, _
ByVal ndNodes As TreeNodeCollection) As Boolean
Dim ndNode As TreeNode
For Each ndNode In ndNodes
If ndNode.Text = NodeValue Then
Return False
End If
Next
Return True
End Function
Private Function AvailableNode(ByVal NodeValue As String, _
ByVal ndNodes As TreeNodeCollection) As TreeNode
Dim ndNode As TreeNode
For Each ndNode In ndNodes
If ndNode.Text = NodeValue Then
Return ndNode
End If
Next
Return Nothing
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "Expand &All" Then
TreeView1.ExpandAll()
Button2.Text = "Collapse &All"
Else
TreeView1.CollapseAll()
Button2.Text = "Expand &All"
End If
End Sub