Jump to content

Very Basic VBA


gazzalp

Recommended Posts

Guys, i want to start learning VBA for autocad, but i have absolutely NO coding experience. I would search around on the internet, but most of you guys would have been in the situation before and therefore would know the best possible ways to learn. At the moment i am looking for the best websites/books that are must haves for learning. As i said i have no experience, so i need it explained to me in really simple terms. Im hoping that there are online tutuorials that show the basics - ie: how to write a code to draw a line. I then want to be able to do things like draw a line and it will automatically go on a certain layer and certain colour, but keep my current layer as current ( ie: im on layer 0, it draws a line on layer 1, changes colour to red, but i stay onlayer 0 when im finished the command). the reason for this is so i can make up toolbars which automatically draw dimensions/viewports/texh/hatch asnd they go on certain layers. So i really need a tutuorial which is a step by step showing what the code is, and why its that.

 

So, which are the best websites/books/forms of help for this? Also does anyone have VBA code to do what i asked (dimensions on certain layer etc.) because im hoping to be able to look at it and maybe understand it from that, i assume this would be a fairly simple code. Thanks all for your help.

Link to comment
Share on other sites

  • Replies 23
  • Created
  • Last Reply

Top Posters In This Topic

  • gazzalp

    12

  • rkmcswain

    5

  • SEANT

    4

  • skipsophrenic

    3

Top Posters In This Topic

Posted Images

Think about the choice of VBA. It is dead. The only reason it's still being shipped with AutoCAD is backwards compatibility. In 64-bit AutoCAD, it's barely hanging on, using a crutch, which makes the code run about 10X slower...

 

With no coding experience, I would start with autolisp for the types of things you mentioned, and when you need a more advanced API, look towards .NET

 

Are you aware that tool palettes can draw an entity on a certain layer (keeping the current layer current)?

Link to comment
Share on other sites

Thanks for the reply. Yes im aware that tool palletes can do what im after, but i find they take up too much space - id rather keep my toolbars as they are. I actually thought that it was the other way around - lisp is pretty much useless and VBA was the better way to go. I know my boss has made up one thing in VBA which we couldnt do in lisp - the stuff i posted about was just very beginner kind of stuff, that i want to learn, rather than being thrown in the deep end. VBA also seems like its alot simpler to understand, having looked at it the coding seems to make sense. Alot of things we do already use VBA, so it may be best for me to keep using it. VBA also seems alot better i think for usuing other programs in conjunction with autocad, ie: linking dwgs to excel. Also this may sound like a stupid question, but is autolisp different to lisp? Does anyone else have opinions on which is better?

Link to comment
Share on other sites

Within the scope of this conversation, autolisp = lisp.

 

Even though this is primarily a lisp site, there are some good VBA tutorials here: http://www.afralisp.net/

 

If you just want to learn to write some fairly easy routines, VBA will do, but just be aware that there is no guarantee that VBA will be around in future versions of AutoCAD. You can do what you mentioned above using lisp.

 

VBA and lisp can each do things the other can't.

.NET is more powerful than both of them combined, but at the expense of being a bit more difficult to learn.

Link to comment
Share on other sites

thanks. Ill have a think about which one would suit us best. For the mean time, ive had a look at afralisp and some other sites, i havent yet found exactly one thats really easy to follow. Im looking for one that has tutuorials to do the most basic things, and has step by step instructions on what to write, but most of all WHY. Theres no point reading a tutorial that says "type the following: command.cdr.line" (for an example) but not explaining what each of those words actually do. Does anyone know a site that shows this?

Link to comment
Share on other sites

The above mentioned Afralisp site does that (I've just started learning

lisp myself) so you shouldn't have too much of a problem there.

Link to comment
Share on other sites

Ive had a look at a few of them , and theres really only one or two examples that are really basic, i guess i was hoping for a bit more...

Link to comment
Share on other sites

...i guess i was hoping for a bit more...

 

Don't forget about the acad_dev.chm file that comes with AutoCAD. Plenty of reading in there including a tutorial in the developers guide. Start at the beginning and work your way through...

Link to comment
Share on other sites

Thanks guys, and that acad)dev help file seems great, thanks for the info. Going back to what was said in the first reply about VBA not being around in the future, according to the visual lisp developers bible i was correct, the bible says they could make lisp better but autocad is more interested in other technologies such as VBA. So i still havent found out which one will be used more in the future/is actually better. Im thinking VBA would be the way to go, it definately seems like its alot better for things like linking to excel, and if a lisp bible says that autocad seems to be going the way of VBA id expect that to be correct...

Link to comment
Share on other sites

Yeah i know what you mean. Ive had a look at alot of tutorials, does anyone know of a site that will show the basic command for all autocad commands. IE: how to add linear dimensions, how to add a line, how to change colours, how to change linetypes etc. Surely there would be a way to find out how to do all these, i mean its only a basic code which would be something like "add.text" etc. but obviously the code might not be add - it may be insert, but i dont know this.

Link to comment
Share on other sites

does anyone know of a site that will show the basic command for all autocad commands. IE: how to add linear dimensions, how to add a line

 

It's all in the Object Model - have you found this yet...?

Properties, Methods, and Events...

 

There are examples for almost every object, property, method, etc. in there.

Link to comment
Share on other sites

according to the visual lisp developers bible i was correct, the bible says they could make lisp better but autocad is more interested in other technologies such as VBA.

 

That was obviously written years ago. :wink:

Link to comment
Share on other sites

It does appear at one time Autodesk shifted its focus away from Lisp towards VBA; I’d guess the era around AutoCAD 2000 - Inventor debut. I base that assumption on Inventor's incorporation of VBA, but not Lisp.

 

The VBA bent was short-lived, however. Presumably Autodesk became aware of Microsoft’s own plans to abandon the VBA/VB6 architecture in favor of .NET. As a result the technical development for VBA has practically stopped.

 

Though the actual timeframe for the VBA removal may still be in question, clearly it is destined to happen. A less clear situation arises if one agrees that getting into AutoCAD customization via .NET is a daunting first step. Both Lisp and VBA are much more accessible – but which one would make the .NET transition easier?

Examples.jpg

Link to comment
Share on other sites

Thanks alot for that, i will go through them now. One simple thing - looking at the add line example, it adds a line to a certain position, what do i need to change in the code so that in doesnt put the line in a certain position, but instead asks me where to put it? Also would be good to know how to change the layer, and colour, then i should be able to work my way through them all :) thanks for your help

Link to comment
Share on other sites

Here is a generic example.

 

Sub Example_AddLine()
  [color="SeaGreen"] ' This example adds a line in model space[/color]
   
   Dim lineObj As AcadLine
   Dim startPoint() As Double
   Dim endPoint() As Double
   Dim TransPoint() As Double
   Dim TargetLayer As AcadLayer
   
  startPoint = ThisDrawing.Utility.GetPoint(, "Select Start Point: ")
 [color="SeaGreen"] 'point returned as WCS point. Needs temporary translation to UCS point[/color]
  TransPoint = ThisDrawing.Utility.TranslateCoordinates(startPoint, acWorld, acUCS, 0)
  [color="seagreen"]'That translated point can be used as reference for next point selection[/color]
  endPoint = ThisDrawing.Utility.GetPoint(TransPoint, "Select End Point: ")
   
   [color="seagreen"]' Create the line in model space with the WCS points[/color]
   Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)

   
   Set TargetLayer = ThisDrawing.Layers.Add("LineLayer")
   [color="seagreen"]'Get reference to layer[/color]
   TargetLayer.color = acRed
   [color="seagreen"]'set line to that layer[/color]
   lineObj.Layer = "LineLayer"
   
End Sub

Link to comment
Share on other sites

Thanks alot, just what i was looking for. Im guessing for things like dimensions, hatch and text i can use the same code and subsitute words like dimension for line etc?

Link to comment
Share on other sites

Generally that is true. As we’re all aware, however, AutoCAD’s various entities require different properties to be completly defined. You will likely encounter areas where AutoCAD will acknowledge a UCS within a VBA routine, but most of the time it will reference all geometry to the WCS.

 

Entities such as Block References and Polylines (and a few others) are defined via a “per entity” coordinate system (OCS). Translation to and from this OCS can also be accomplished with the ThisDrawing.Utility.TranslateCoordinates method.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...