TheSwamp
Code Red => .NET => Topic started by: Jeff_M 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:
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
-
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".
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
-
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:
(setq myproj (vlax-create-object "MyProject.StartupClass"))
Then when you are done with your project, you can release the project with
(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 ...
-
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! :)
-
... 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
-
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)
-
... to learn over the weekend!
ou go mis-quoting me....you neglected to include the 2 words preceding these, namely "to TRY" :D
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.
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.
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.
-
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.
-
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.
-
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.
-
Holly cow, Is that the REAL Bobby Jones, AKA the Auto*esk NG's? Welcome to the Swamp Bobby!
-
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:
-
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.
-
Appears to be.... is the same photo I have seen many many years ago...
Yep, from T5 days if I'm not mistaken.
-
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 ...
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 (http://www.theswamp.org/forum/index.php?topic=6292.msg76878#msg76878) 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 ...
-
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.
http://www.theswamp.org/lilly_pond/ make a youish folder
-
Thank you Mr. Wahr!
I've uploaded the file. http://www.theswamp.org/lilly_pond/BobbyCJones/Calling%20your%20code%20from%20AutoCAD.doc
**WARNING** This only applies to code written against the AutoCAD COM API. This will not work with the new managed API.
-
Ok .. here is a quick rundown ... <snipped all the good stuff>
If you need an example, let me know ...
Thanks Keith. It is finally starting to make sense to me. I'll give it a try and if I get stuck I'll be back asking for that example.
And Thanks to Bobby, too, for posting that document. I'll be reading that tonight. Oh, and thanks for resurrecting this thread. It might be the kick I needed to get back into the learning mode.
-
....
I've uploaded the file. http://www.theswamp.org/lilly_pond/BobbyCJones/Calling%20your%20code%20from%20AutoCAD.doc
**WARNING** This only applies to code written against the AutoCAD COM API. This will not work with the new managed API.
Thanks for sharing Bobby.
Could you do [us] a favour .. Could you state explicitly in the document which Programming IDE, API <done> and ACAD Version that code is meant to suit.
If [we] don't, it's likely that the thread will fill with posts like "I copied the code and want to know Why doesn't ....
I'm guilty of missing this too ...
Sorry if this seems a PITA.
Great to see you here ..
Regards
Kerry
-
Thanks for sharing Bobby.
Could you do [us] a favour .. Could you state explicitly in the document which Programming IDE, API <done> and ACAD Version that code is meant to suit.
If [we] don't, it's likely that the thread will fill with posts like "I copied the code and want to know Why doesn't ....
I'm guilty of missing this too ...
Sorry if this seems a PITA.
Great to see you here ..
Regards
Kerry
Sure, not a problem. I wrote this using VS.NET 2003 with AutoCAD 2004 as my target version. However, the ideas presented in it are valid for any version of Acad with a COM API. Likewise, all versions of .NET are capable of COM Interop and can be used.
Also, sorry for the odd formatting in the document. I pulled it out of a larger piece where it all looked much better :)
Look at that <PointingOverThere> You can d/l it from the AUGI web site. Although it looks like they have included a bunch of stuff from some other class too...what's up with that?
http://www.augi.com/education/auhandouts/2003/CP12-1.zip
-
:pissed: :realmad: :pissed: :realmad:
OK, what else can MS do to make this harder for me? Every help file I can find, including the one that came with my Visual Studio .NET 2003, says Visual Studio and then select File> New> Project. In the New Project dialog select Visual Basic Projects for the Project Type. Select “Class Library” template
So I try this and there is no Class Library Template to be found.....so now I go off to search my computer and the 'net......on the MS website I find thisThe Class Library template is not available in the Standard Edition of Visual Basic .NET.
What da hell? Does this mean I can't create DLL's with the Standard Edition? What good IS the Standard edition?
I'm starting to think knitting is looking better as a hobby than is this programming for Acad. :lmao:
-
If I recall correctly that is indeed the case Jeff; sorry.
:(
-
OK, I admit it....I'm a putz and don't know jack....
I just found the AutoCAD_NET_labs.zip file on the Autodesk website that has a number of examples and yup, I CAN build DLL's (I just built the HelloWorld command.... :woot: ). Now to just make my own little template or, at least, figure out what the DLL requires vs the EXE that my projects default to......
Carry on.....
-
Maybe what I'm thinking is you can't create type libraries without the Enterprise version or equivalent.
Oh coffee truck ...
-
Thanks, Michael, for keeping me thinking here. I don't know how much more coffee I can drink today. Just when I think I'm getting somewhere, *BLAM* !!!! Now where did THAT brick wall come from? Time to switch to some asprin & water.
All I can say is "It's a good thing it's a cold & rainy day and the wife is at work. I just might make some progress today."
-
<Jeff_M=Blue>Thanks, Michael, for keeping me thinking here.
I believe the words you're looking for are "Thanks for the red herring Michael".
I don't know how much more coffee I can drink today.
There's a limit? Adjusting scorecard.
Just when I think I'm getting somewhere, *BLAM* !!!! Now where did THAT brick wall come from?
But that's what makes it fun, right? <seg>.
Time to switch to some aspirin & water.
There are many mornings I start with a hearty bowl of milk and aspirin.
All I can say is "It's a good thing it's a cold & rainy day and the wife is at work. I just might make some progress today."
Know 'xactly what you mean, Linda works Saturdays, I write proposals same lately.
Good luck Jeff, hope you get what you want done today++.
:)
-
Jeff, here is a link (http://www.devcity.net/Articles/111/1/vbnet_se_dll.aspx) that may help you out with the class library template.
-
Thanks, Troy! I'll be testing it out with my app later on today.
-
For those that don't know, install the ObjectARX Wizard from the ObjectARX SDK.
Apart from adding in all the aRX stuff, it ALSO adds in an "Autodesk Managed CS Project" (and VB) under "Visual C# Projects" and VB respectively.
Cheers,
Glenn.
-
Hey Jeff,
More and more familiar people. I'm not providing any solutions, cause I can't......I myself have just recently jumped in to .NET (with both feet and a rock tied to them).
I was wondering if there was an MSDN for the MSDN <g> Having tons of fun learning this though.
-
Hi Ken,
Glad to see you floatin' in the Swamp!
I think it will seem more like fun when I get my first app to actually work.....