PDA

View Full Version : VB.net for 2008; First Code



guitarchitect7
2007-11-19, 05:50 PM
I have probably spoken with most of you in this forum section already, so need to be redundant with what I say. But quickly, I have just recently started learning VB.net. I have gone through various tutorials, looked at some Augi material, and search the web briefly for research; the time has now come to write my first code.

The importance of this thread is not specifically to get code, but to make the connection of .NET to AutoCAD. This seems to be where I am still struggling. I have seen some import functions of certain applications and what have you, but none of that really makes sense as of yet. This is really where writing my first code comes into play.

In a top 10 list or something if you will, what are the main things I need to consider when I attempt to start writing programs for AutoCAD with .NET, focusing mostly on getting the program to read AutoCAD functionality and then incorporting the macros into AutoCAD itself (preferably with lisp wrappers).

Thanks in advance!

Opie
2007-11-19, 06:07 PM
See the sticky thread in this forum

guitarchitect7
2007-11-20, 06:54 PM
Thanks for moving the post for me. I was thinking VB the whole time although it really is a .NET program question.

I have visted some of those links in the thread that you recommended. I have also viewed other tutorials. I'm beginning to see the connection again; I think.

1.) It seems that you need to load the acmgd.dll and the acdbmgd.dll to allow to define commands and access them through AutoCAD. Why these particular files?

2.) I have been trying to follow the "Hello World" tutorial. I got it to work, but noticed you have to do all this importing.

ex.Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports acadApp = Autodesk.AutoCAD.ApplicationServices.Application

How do you know what to import?

I think the hump I'm running accross is now that I know which files to access to be able to program for AutoCAD, how do I know which object attributes to call and import to do what I want?

Please tell me Revit is easier!!!!

Opie
2007-11-20, 07:11 PM
Keep reading. ;)

There should be an object browser for the particular objects you are importing.

I doubt learning to program Revit in .Net is any easier than learning to program AutoCAD with .Net. You have to learn how to program before you can figure out what you want to do with it.

guitarchitect7
2007-11-20, 07:25 PM
Keep reading. ;)

There should be an object browser for the particular objects you are importing.

I doubt learning to program Revit in .Net is any easier than learning to program AutoCAD with .Net. You have to learn how to program before you can figure out what you want to do with it.

I know of the object browser and see the tree of how it finds the names. What I was looking for was a logic of know what to import. Maybe this is just a newbie type question, but right now I can't make the connection of why to import one thing over the other to what I may be trying to do. I believe that if I can make this connection, then I can advance on to being able to program.

I have done a little of lisp and understand the logic of most of it. I can connect having to do this to do that sort of thing, but havn't found good explainations of why in VB.NET.

Opie
2007-11-20, 07:40 PM
I know of the object browser and see the tree of how it finds the names. What I was looking for was a logic of know what to import. Maybe this is just a newbie type question, but right now I can't make the connection of why to import one thing over the other to what I may be trying to do. I believe that if I can make this connection, then I can advance on to being able to program.

I have done a little of lisp and understand the logic of most of it. I can connect having to do this to do that sort of thing, but havn't found good explainations of why in VB.NET.
I would speculate you would import a file when you need to have access to an object or property or method that is included in that particular file.

dgorsman
2007-11-21, 12:13 AM
There should be a dependancy map (thats what I call it, anyways), similar to the one in the AutoCAD VBA help, which shows what object is inherited from what, and so on. One of the more important pieces of documentation when doing object-oriented design.

sinc
2007-11-26, 10:15 PM
I hope you are using an IDE such as Visual Express VB edition?

If so, look in the Help. Somewhere in there should be the .NET SDK help. In it, you should see help for the Microsoft .NET framework, as well as the Autocad .NET framework.

All that help is split up into the same categories as the Import statements. For example, there should be a whole section on all the classes in the Autodesk.ApplicationServices namespace, then another whole section for Autodesk.DatabaseServices, and a whole section for Autodesk.Runtime, etc.

Whenever you find a class in the help that you want to use, you must import the namespace it belongs to.

My development machine is not in front of me, so I can't check my setup to give you more details right now. You may need to download and install the ObjectARX SDK before you see any of this stuff - I can't remember for sure what all I did when I was getting things setup.

Kerry Brown
2007-11-28, 06:26 AM
guitarchitect7

Have you done the 7 Labs published by AutoDesk.

I think that you need to do a bit of reading if you don't yet know the purpose of include/import files.

There are tutorials and videos at Microsoft's Developers sites which will lead you through the basics.

guitarchitect7
2007-11-28, 09:33 PM
guitarchitect7

Have you done the 7 Labs published by AutoDesk.

I think that you need to do a bit of reading if you don't yet know the purpose of include/import files.

There are tutorials and videos at Microsoft's Developers sites which will lead you through the basics.

Sorry for the delay on responses here. Thanks for everyone's input.

I have began the 7 labs and studying each one to hopefully get a better understanding of how to use .net with Cad. I understand what the imports do, it was just that I didn't make the connection of knowing which imports to import. As I have messed a little more with it since my last posts, it is becoming more apparent, but its tough thing to grasp on right at the beginning.

The part that seems to be tying me up is just knowing where to start. With lisp it was usually fairly simple as it seem to relate more with the command module wihin CAD so I would just write code matching close to what I would execute withing CAD. With .net, it seems you have to go about things different to achieve the same functions, and its learning that different way that is taking some time.

One program I would be interested in writing is setting search paths. I have used profiles, but that changes toolbar locations for the other drafters and that is not ideal. I have been curious, when you all learned, what program seemed to be a good starting one that was benificial to the CAD program.

ethomson
2007-11-29, 02:59 PM
I would reiterate what everyone else is saying.. While you may be getting along fine following the samples, it sounds like you need to get a firmer grasp on VB.NET and OO programming as that is going to hold you back if you don't really understand what you are doing there.

That said, the trickiest part of starting out doing .net programming for Autocad is the idea of transactions and working with the database. You need to really understand what you are doing there. Take a good look at the video http://through-the-interface.typepad.com/through_the_interface/2007/11/devtv-introduct.html
on Intro to Autocad .NET programming. It's very well produced and covers alot of the concepts you need to understand. Don't try to watch it all the way through though. Take it bit by bit.

As far as a first program to start with. Something simple like creating a new command thats simple. Or even creating a command that does the same thing as an existing command just to get the hang of it. Then move on to something more complicated like creating blocks programmitcally. Your first few programs shouldn't be something that is going to be immediately beneficial to you, other than building a foundation for future programs, where you can look back and see how you accomplished things.

I would also highly recommend, Jerry Winters VB.NET book for an absolute beginner or even as just reference material @ vbcad.com.

But first spend some time going through some good OOP tutorials and get VB.NET down. Take a look at devcity.net they've got some great articles, and their forums are great for new programmers.

guitarchitect7
2007-12-03, 05:23 PM
You touched on it well. I am anxious to look at the videos as I have enjoyed some of their blogs they have posted. I have looked at that book by Jerry Winters, but need to get around to ordering it. It sounds as though that will be a huge benifit for me in my learning process.

I really have appreciated everyone's support on this. Hopefully in time I can do the same for another newbie! If there is anything else that comes to mind that could really help a person as myself along, please feel free to share. I'll keep checking the boards and see if there is anything new that can help.

guitarchitect7
2007-12-11, 08:47 PM
After doing some reading, I think I'm ready to actually write my first code. Some things still dont make complete sense, but I have a better feel for how things relate and work together.

Now I want to take this lisp code:


(defun PenWidths ()
(command "-layer"
"m" "W-0.13mm" "c" "1" "" "l" "continuous" ""
"m" "W-0.25mm" "c" "2" "" "l" "continuous" ""
"m" "W-0.3mm" "c" "3" "" "l" "continuous" ""
"m" "W-0.45mm" "c" "4" "" "l" "continuous" ""
"m" "W-0.70mm" "c" "5" "" "l" "continuous" ""
"m" "W-1.0mm" "c" "6" "" "l" "continuous" ""
"m" "WH-252" "c" "252" "" "l" "continuous" ""
"m" "WH-253" "c" "253" "" "l" "continuous" ""
"m" "WH-254" "c" "254" "" "l" "continuous" ""
""
)
(princ)
)


...and covert it over to .NET. I do change some variables within the lisp:


(setq oldlayer (getvar "clayer"))
(setq oldexpert (getvar "expert"))
(setvar "expert" 3)

...that I would like to incoporate.

One thing I noticed is there are not many discussions about creating layers with .NET. I curious to why? Does anyone else do this or have another way?

Anyway I'm starting with accessing the layer table. I want to use and if,else to see if the layers are created and then skip over if so. Here is an example code that I'm trying to follow:

Try

'Get the layer table first, open for read as it may already be there
Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)

'Check if EmployeeLayer exists...
If lt.Has("EmployeeLayer") Then
layerId = lt.Item("EmployeeLayer")
Else
'If not, create the layer here.
Dim ltr As LayerTableRecord = New LayerTableRecord()
ltr.Name = "EmployeeLayer" ' Set the layer name
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2)
' it doesn't exist so add it, but first upgrade the open to write
lt.UpgradeOpen()
layerId = lt.Add(ltr)
trans.AddNewlyCreatedDBObject(ltr, True)
trans.Commit()
End If
Catch ex As System.Exception
MsgBox("Error in CreateLayer Command" + ex.Message)
Finally
trans.Dispose()
End Try
Return layerId
End Function


The thing I notice is the extensive code to just check and create one layer. Any thoughts? Is this typical, another way at all, not worth my time?

guitarchitect7
2007-12-11, 10:13 PM
here is what I have typed out:


'specify the command name
<CommandMethod("PenWdithLayers")> _
'create new procedure to create layers
Public Sub PenWidthLayers()
'set parameters
Dim layerId As ObjectId 'returns object ID vaulte under the layerID parameter
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim trans As Transaction = db.TransactionManager.StartTransaction
Try
Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
'check layer table for duplicate layers
If lt.Has("W-0.13mm,W-0.25mm,W-0.3mm,W-0.45mm,W-0.70mm,W-1.0mm,WH-252,WH-253,WH-254") Then
layerId = lt.Item("W-0.13mm,W-0.25mm,W-0.3mm,W-0.45mm,W-0.70mm,W-1.0mm,WH-252,WH-253,WH-254")
'if layer doesn't exist, then create it here
Else
Dim ltr As LayerTableRecord = New LayerTableRecord


End If
Catch ex As System.Exception
MsgBox("Error in creating layer!" + ex.Message)
trans.Abort()
Finally
trans.Dispose()
End Try
End Sub

My first question is finding the duplicate layers. By listing them all as I have above (w-0.13mm,w-0.25mm,etc), does that work? Or is there another way listing all layers that need to be found?

Then my next part comes on creating the layers them selve. From the example given before, i was curious of a shortern condensed way of doing this. Any thoughts?

Thanks in advance.

sinc
2007-12-12, 10:42 PM
My first question is finding the duplicate layers. By listing them all as I have above (w-0.13mm,w-0.25mm,etc), does that work? Or is there another way listing all layers that need to be found?

No, things like lt("Item1,Item2,Item3") don't work. You need to use a loop, and check one layer at a time.

Creating a layer is relatively simple. It is gone over in some detail in the labs you can download with the ObjectARX documentation - do you have that? I think it even had both a VB and a C# version, although I only really paid attention to the C#.

And there is a possible gotcha. The default indexers for SymbolTables (which include layer tables and block tables) have bugs in the .NET API, and may return erased objects. That means you may run into problems if you use things like layerTable("LayerName") to try and retrieve layers by name. See this topic for a workaround:

http://www.theswamp.org/index.php?topic=12123.msg151231#msg151231