Author Topic: First VB.NET application......ssssllllloooooowwwwww  (Read 23925 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
First VB.NET application......ssssllllloooooowwwwww
« on: August 12, 2005, 04:02:24 PM »
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:
Code: [Select]

    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

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
First VB.NET application......ssssllllloooooowwwwww
« Reply #1 on: August 12, 2005, 08:41:13 PM »
For anyone who gets past that first post, I figured out how to accomplish this in VBA. The trouble is, you aren't supposed to distribute VBA macros with the treeview control in it since VBA doesn't come with it.

And yep, it's a wee bit quicker. The same drawing that took about 3 minutes to do in VB.NET took just about the blink of an eye in VBA......

So how do I let someone else use my routine? "Just Do It"? Find out why the .NET approach is so slow? Give it up because I really don't have a clue?

Anywho, here's the code tha works in VBA. You will need to add the MSTreeview Control 6.0(SP4) to your controls. Create a form with a Treeview and 2 Command buttons. Use the Captions of Button 1 as "Done" and Button2 as "Expand All".
Code: [Select]

Option Explicit

Private Sub Button2_Click()
    Dim oNode As Node
        If Button2.Caption = "Expand All" Then
            For Each oNode In TreeView1.Nodes
                oNode.Expanded = True
            Next
            Button2.Caption = "Collapse All"
        Else
            For Each oNode In TreeView1.Nodes
                oNode.Expanded = False
            Next
            Button2.Caption = "Expand All"
        End If

End Sub

Private Sub Button1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
        Dim oEnt As AcadEntity
        Dim oBlk As AcadBlockReference
        Dim sLayName As String
        Dim sBlkName As String
        Dim oNodes As Nodes
        Dim oNodeLay As Node
        Dim oNodeBlk As Node
        Dim oNodeCnt As Node

        Set oNodes = TreeView1.Nodes
        For Each oEnt In ThisDrawing.ModelSpace
            If TypeOf oEnt Is AcadBlockReference Then
                Set oBlk = oEnt
                sBlkName = oBlk.Name
                sLayName = oBlk.Layer
                On Error Resume Next
                Set oNodeLay = TreeView1.Nodes.Item(sLayName)
                If Err Then
                    Err.Clear
                    Set oNodeLay = TreeView1.Nodes.Add(, , sLayName, sLayName)
                End If
                Set oNodeBlk = TreeView1.Nodes.Item(sLayName & sBlkName)
                If Err Then
                    Set oNodeBlk = TreeView1.Nodes.Add(sLayName, tvwChild, sLayName & sBlkName, sBlkName)
                    Set oNodeCnt = TreeView1.Nodes.Add(sLayName & sBlkName, tvwChild, sLayName & sBlkName & "Count", "1")
                    Err.Clear
                Else
                    Set oNodeCnt = TreeView1.Nodes.Item(sLayName & sBlkName & "Count")
                    oNodeCnt.Text = oNodeCnt.Text + 1
                End If
            End If
        Next
    End Sub

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
First VB.NET application......ssssllllloooooowwwwww
« Reply #2 on: August 12, 2005, 09:02:55 PM »
The answer is called "marshalling" ... anytime you have to access an object across threads or processes (which is what your application is doing with AutoCAD) the process slows down considerably, sometimes to the point of being painfully slow ...

Consider making it an ActiveX dll and load it with something like this in lisp:
Code: [Select]

(setq myproj (vlax-create-object "MyProject.StartupClass"))


Then when you are done with your project, you can release the project with
Code: [Select]

(vlax-release-object myproj)


Of course, this will require you add a class to your project and export that class so it is callable and creatable by AutoCAD .. also, your dll will need to be registered on the computer with regsvr32.exe

Not too difficult to do once you understand the basics of it ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
First VB.NET application......ssssllllloooooowwwwww
« Reply #3 on: August 12, 2005, 09:08:36 PM »
Thanks Keith! I thought that the dll might be a solution, but that just brought me back to the clueless part. But now that I know it is the direction to head:

Oh boy, something new to try to learn over the weekend! :)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
First VB.NET application......ssssllllloooooowwwwww
« Reply #4 on: August 12, 2005, 09:56:30 PM »
Quote
... to learn over the weekend!


You young guys with good retention and unimpaired cognition make me envious  :D

The general consensus is to save the NET stuff for AC2006+ Jeff.
Keiths comments about marshalling are spot on. Translation across interfaces is a bummer.

That being said, every step IS an advancement. There is tons of sample NET stuff around unrelated to AutoCAD.
I'd be interested in knowing your thoughts on using VB.NET. I'd have thought the differences from VB6 would outweigh any syntax similarities.
I suppose not being a prolific VB'er made the choice of C# more natural for me.

Have fun with it

Regards
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
First VB.NET application......ssssllllloooooowwwwww
« Reply #5 on: August 12, 2005, 10:04:57 PM »
So, I forgot to tell you that you can use netload to load a dll into AutoCAD 2005+ ... all you need to do is create the command reference that AutoCAD can access and you should be on your way .. (more fodder to think about)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
First VB.NET application......ssssllllloooooowwwwww
« Reply #6 on: August 12, 2005, 10:59:53 PM »
Quote from: Kerry Brown
Quote
... to learn over the weekend!
Quote from: Kerry Brown
ou go mis-quoting me....you neglected to include the 2 words preceding these, namely "to TRY" :D
Quote from: Kerry Brown

You young guys with good retention and unimpaired cognition make me envious  :D
Shoot, I can't remember how decided to start this goofy little application in the first place. And I'm not too far behind you, BTW.
Quote from: Kerry Brown

The general consensus is to save the NET stuff for AC2006+ Jeff.
Keiths comments about marshalling are spot on. Translation across interfaces is a bummer.
Yeah, but when it's the only thing you have, you gotta start somewhere.
Quote from: Kerry Brown

I'd be interested in knowing your thoughts on using VB.NET. I'd have thought the differences from VB6 would outweigh any syntax similarities.
I suppose not being a prolific VB'er made the choice of C# more natural for me.
From my understanding they compile into the same runtime code, so there should be no difference in the running of the applications. I chose Vb for 2 reasons, the first is just that I knew enough VBA to think that the syntaxes would be similar (heh, right....), and the second because I had no clue what # was. Since I had not used anything other than VBA, I just went with a suggestion from a former web admin.

As for actually using it, it seems pretty straight forward. Although I do think that the help is geared more for someone who already knows what to look for. And the examples just make me scratch my head and go "Huh?"

But, as I said, this is the first time I've actually tried to make something useful with it. Time, patience and practice should get me moving in the right direction. Oh, and if I can ever get out from under this Acad2002 cloud, that would help, too.

Glenn R

  • Guest
First VB.NET application......ssssllllloooooowwwwww
« Reply #7 on: August 13, 2005, 06:57:45 AM »
Jeff,

As pointed out, Marshalling is DEFINATELY going to be the biggest performance killer for you here, but I have some more observations for you to ponder...

1. All languages are SUPPOSED to be equal under .NET, but as the old saying goes, some are more equal than others.

2. WRT point 1, C# would be the best language to use in .NET as it doesn't have the legacy baggage that VB does (operator overloading, function overloading to name but a few) as well as other benefits.

3. When adding things to a list, in C# there is usally 2 methods to do this:
The standard Add each item, or, collect items in an array then add the array. Depending on what you are doing, the latter can be much faster.

4. In C# you can do this (seudo code):
If whatEver Is Something then
Something myVar = (SomeThing)whatEver

The above reuires 2 (two) casts whereas this:
Something myVar = whatEver as Something

...requires only one cast, and in certain situations (read I use the latter ALWAYS) will be less expensive and more efficient.

Just some thoughts.

Cheers,
Glenn.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: First VB.NET application......ssssllllloooooowwwwww
« Reply #8 on: November 02, 2005, 01:43:50 PM »
Jeff,

As pointed out, Marshalling is DEFINITELY going to be the biggest performance killer for you here, but I have some more observations for you to ponder...


I know this is late but....

Marshalling is the problem here but it was never mentioned what type of marshalling.  It appears that this code is running out of process and the slowness is due to marshalling across process boundaries, not marshalling between .NET and COM.  Simply reworking this code to load and run in process would most likely fix the performance issues.
Bobby C. Jones

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: First VB.NET application......ssssllllloooooowwwwww
« Reply #9 on: November 02, 2005, 03:01:29 PM »
Simply reworking this code to load and run in process would most likely fix the performance issues.
Hi Bobby! I'm glad to see you found your way into the Swamp.

Since I am a fledling wannabe would you mind expanding on this suggestion? I'll admit that due to work loads I've been unable to spend much time researching/learning this...IOW, I don't have a clue how to determine whether something is in/out process or how to make it otherwise.

Thanks, and I look forward to any future words of wisdom here in the Swamp.

Sdoman

  • Guest
Re: First VB.NET application......ssssllllloooooowwwwww
« Reply #10 on: November 02, 2005, 03:14:20 PM »
Holly cow, Is that the REAL Bobby Jones, AKA the Auto*esk NG's?  Welcome to the Swamp Bobby!

LE

  • Guest
Re: First VB.NET application......ssssllllloooooowwwwww
« Reply #11 on: November 02, 2005, 03:34:10 PM »
Holly cow, Is that the REAL Bobby Jones, AKA the Auto*esk NG's?  Welcome to the Swamp Bobby!

Appears to be.... is the same photo I have seen many many years ago.....    :roll:

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: First VB.NET application......ssssllllloooooowwwwww
« Reply #12 on: November 02, 2005, 03:34:41 PM »
Hey Jeff,
In a nutshell you create a .net dll, prepare it to act as a COM server, grab or create an instance of an AcadApplication object, and load your dll in process with AutoCAD via the AcadApplication.GetInterfaceObject method.

Alternately you can reference your dll from a VBA macro inside of AutoCAD and forgo the GetInterfaceObject call.  Dll's referenced this way are by default running in AutoCAD's process.

Is there a place to share files here in the swamp?  I have a write up that I did a while back that goes into detail on all of this.
Bobby C. Jones

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: First VB.NET application......ssssllllloooooowwwwww
« Reply #13 on: November 02, 2005, 03:35:19 PM »
Appears to be.... is the same photo I have seen many many years ago...

Yep, from T5 days if I'm not mistaken.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: First VB.NET application......ssssllllloooooowwwwww
« Reply #14 on: November 02, 2005, 03:40:06 PM »
Since I am a fledling wannabe would you mind expanding on this suggestion? I'll admit that due to work loads I've been unable to spend much time researching/learning this...IOW, I don't have a clue how to determine whether something is in/out process or how to make it otherwise.

Ok .. here is a quick rundown ...

Quote from: Jeff_M
I execute my block-count.exe from Windows Explorer
The mere fact that you are calling the program from within Windows, it will be by default OUT of process for any other currently running or subsequent running program. However, if you could manage to run AutoCAD from your program as a child process to your code, that particular instance of AutoCAD would be IN process. Unfortunately, I don't see this as being a valid option at this point, not without some serious coding on your part.

Now to make your program in-process, you have to do one of a couple of things ...
  • Run your program as part of the in-process language interpreter (VBA, LISP etc)
  • Load your program as an ActiveX dll from AutoCAD
  • Inject your code into the AutoCAD program from your code

Now while all of the above resolutions are perfectly acceptable to fix your problem, the easiest ones are listed first, and the hardest one is listed last.

Example:
.... you have already stated you can use your code in VBA .. simple
.... compile your VB project utilizing a calss object (as I explained in this post) a little more difficult as it requires a little understanding of VB classes.
.... write code to inject your program into the AutoCAD process .. this is very difficult and can result in an unstable AutoCAD program if not done correctly. In this option, you will still need to start your injected code from within AutoCAD. Presumably it would need to be tied to an event to get it running, then you could use hotkeys to begin execution. Like I said .. very difficult ...

Best solution - utilize a single VB class (BlockCount.Application) to create an ActiveX dll. Load that DLL into AutoCAD and execute by creating the BlockCount.Application object.

If you need an example, let me know ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie