PDA

View Full Version : Program that hides AutoCAD entities runs slower in .NET than VBA



some-1-2
2007-06-27, 07:16 PM
Hi All,

I have a simple code in VBA that goeas through al the entities in AutoCAD and hides them. The program runs in 3 seconds. When I upgraded this code to .NET I found out that it runs four time slower than in VBA. It is a simple 10 lines code and it is making the application runs sooo slow. Is this something normal!!

I am worried because if I have a larger .NET program with 100s lines of code then it will be terribly slow to run with AutoCAD.

Any ideas - Thanks

sinc
2007-06-28, 03:58 PM
Can you post the code?

some-1-2
2007-06-28, 11:39 PM
Here is the code in .NET:

Public Sub HideAllObjects()
' Get the running instance of AutoCAD. If none
' can be found, just inform the user and quit

On Error Resume Next
moAcad = GetObject(, "AutoCAD.Application")

moAcad.Visible = True
acadDoc = moAcad.ActiveDocument
acadDoc.Application.Visible = True
ThisDrawing = moAcad.ActiveDocument

MsgBox(ThisDrawing.Name)

Dim Entry As AXDBLib.AcadEntity
Dim totEntit As Integer
totEntit = 0

For Each Entry In moAcad.ActiveDocument.ModelSpace 'ThisDrawing.ModelSpace
If Entry.Color = 250 Then
Entry.Visible = True

Else

Entry.Visible = False
End If
totEntit = totEntit + 1
Next
Pods.totEntBox.Text = totEntit

moAcad.ActiveDocument.Regen(True)

End Sub

Jeff_M
2007-06-30, 04:49 AM
Well, first off you are using the ActiveX API to access AutoCAD instead of the .NET managed wrappers. Second, you are accessing & manipulating AutoCAD externally, and as such are operating "out of process" which will always be slow.

I'm attaching a zip file with 2 VB.NET 2005 projects. The WindowsApplication1 is based on the code you posted for using ActiveX. It took 5 minutes to run on my test drawing with ~4200 entities. The ClassLibrary1 is based on the .NET managed code and runs as the command TESTME inside of Autocad. Load it with NETLOAD then type TESTME to run it. On the same test drawing it runs in less than a second.

I didn't include any error handling (it will choke if any layers are locked) but it should give you an idea of what can be done.

Good Luck!
Jeff