Jump to content

A beginning in VBA for AutoCAD


Costinbos77

Recommended Posts

Hello!

 

Because people at Autodesk did not listen to me and did not facilitated the use of a grid cell in a DCL, I am forced to learn VBA for AutoCAD.

I want to make a program to choose an ASCII.TXT a file , reading line by line, and then display the retrieved data in a grid cell.

 

1. Can you help me with some teaching material (pdf, sites ...). 'Help' from AutoCAD is not enough, especially for beginners.

 

2. What is the equivalent LISP expression below ?

 


(setq path ([color=red]getFiled[/color] " Text File" "" "txt;*" 4))

3. What should be imported or activated to display a grid of cells form a shape:

 

Forma.jpg

 

And to support the following:

- Edit;

- Sorting by column;

- Insert rows;

- Removal of ROWS;

 

4. In VB I saw controls: DataGrid and DataSource, DataList or Hierarchical FlexGrid and Hierarchical Recordset, but not available in VBA for Autcad call.

What control should I use?

 

5. What is the equivalent LISP expression below ?

 

([color=red]grRead[/color] T 15 0)

I apologize if you get bored with such trivialities beginner.

 

Costin

Edited by Costinbos77
Link to comment
Share on other sites

FWIW -

 

If you already have a background in VBA, then continuing with that API just makes sense.

 

If however, you're just now starting to learn a higher level language, I'd urge you to consider learning .NET in lieu of VBA. While VBA is still an available API, and will be supported for many moons, you're going to have many more resources (i.e., code samples, and documentation, etc.) available with .NET API.

 

Further, .NET has access to both ActiveX/COM (i.e., VBA, Visual LISP, etc.), in addition to myriad other API Features that are exposed to the .NET API directly.

 

Cheers

Link to comment
Share on other sites

Take Note of what BlackBox said. Learning VBA or .NET is not just a case of translating LISP commands it is a very steep learning curve. You need to decide which you want to use. VBA is easier than .NET but not as versatile. In Your Case I would suggest VBA. Joe Sutphin has a very good Book which is also available as PDF, and you have CADTutor to help you. But don't expect to be able to write Advanced programs from day one.

 

What you are wanting to do is perfectly possible in VBA and .NET, so make your choice and the best of luck.

Link to comment
Share on other sites

I found this ''AutoCAD 2006 VBA A Programmer’s Reference''.pdf by Joe Sutphin and I find it very good for beginning.

You know something similar for a newer version of AutoCAD ?

 

From what I understand, the language .NET is a combination of C and VB. And how I'm not a programmer, I will lose a lot of time to learn .NET. I do not want to do aircraft or super programs. Only some applications in Autocad and eventually Excel.

 

 

Thank you guys!

 

Costin

Edited by Costinbos77
Link to comment
Share on other sites

That book (AutoCAD 2006 VBA A Programmer’s Reference) is the latest one. As far as I know he has no intentions of updating it, but it covers everything that you will come across in VBA6. I have not found a better VBA reference and still use it.

Link to comment
Share on other sites

When you have the VBA IDE open click on "Insert" then "UserForm". You then have a blank form to which you add your controls from the tool palette. See chapter three of the book, it's explained in detail there.

Link to comment
Share on other sites

Hello!

For a week I still try to write an application in VBA for Autocad and it looks like I started on the wrong foot. I want to choose a file ASCII.TXT to read it line by line, and then to extract the portion of the line and assign them to variables. But there is nothing to handy in VBA. VB.NET language has? in LISP is a piece of cake .

 

(setq path (getfiled "" "" "" "txt;*" 4)  file (open path "r") )
(while (setq lin (read-line file))
 (setq nm (substr lin 1 11) 
         x (substr lin 12 10)
         y (substr lin 25 10)
         z (substr lin 41 
 )
 (close file)

 

Can not do anything with AutoLISP but it is very easy to learn and use.

 

Substr function has a correspondent in VBA ?

Edited by Costinbos77
Link to comment
Share on other sites

As a general rule of thumb you need a lot more lines of code in VBA than you do in LISP and you need a lot more lines of code in .NET than do in VBA.

 

You need to plan out what you want to do and then piece by piece write the code to do it.

 

To open an ASCII file follow the example given in Chapter 18 page 395 of Joe Sutphin's book to choose and open the file.

Link to comment
Share on other sites

Well said, Tyke. :thumbsup:

 

As a general rule of thumb you need a lot more lines of code in VBA than you do in LISP and you need a lot more lines of code in .NET than do in VBA.

 

The only (common?) exception to the number of lines of code needed, where .NET is advantageous, is when implementing custom (inherently static) Extension Methods for a given Type via assembly reference... I did skip VBA altogether (going Visual LISP-->.NET), but I do not believe VBA has this capability, does it? :unsure:

 

Recent example in this thread, for context.

 

Cheers

Link to comment
Share on other sites

It is more difficult to program in VBA than in AutoLISP, but I was lucky this time and in this way shall send thousands of thanks and good health Sitphin Joe's family, which has provided us his work.

 

Solving the first problem: it is not the greatest, but it works.


Private Declare Function [color=red]GetOpenFileName [/color]Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long ' V : 23.01.2014 .

Private Type [color=red]OPENFILENAME [/color]' V : 23.01.2014 .
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type ' OPENFILENAME

'' ---------------------------------------------------------------------------

Public Function [color=red]FGetFiled[/color](DialogTitle As String, InitialDir As String, Extensie As String) As String ' V : 23.01.2014 .
' Functie Preluare Cale Fisier !
ThisDrawing.Utility.Prompt (vbNewLine + "   FGetFiled  :  V : 23.01.2014 ;  START !")

Dim tipF As String
Dim OFName As OPENFILENAME
Dim Filter As String

Select Case Extensie
 Case "txt", "coo", "rad":  tipF = "TEXT"
 Case "csv":  tipF = "CSV"
 Case Else:  tipF = "UNKNOWN"
End Select

' Filter = "Drawing Files (*.dwg)" + Chr$(0) + "*.dwg" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
Filter = tipF + " Files (*." + Extensie + ")" + Chr$(0) + "*." + Extensie + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
' InitialDir = "C:\Program Files\AutoCAD 2006\Sample"
' DialogTitle = "Open a FILE !"

OFName.lStructSize = Len(OFName) 'Set the Structure size
OFName.hwndOwner = 0 'Set the Owner window
OFName.lpstrFilter = Filter 'Set the Filter
OFName.nMaxFile = 255 'Set the Maximum number of Chars
OFName.lpstrFile = Space(254) 'Create a Buffer
OFName.lpstrFileTitle = Space$(254) 'Create a Buffer
OFName.nMaxFileTitle = 255 'Set the Maximum number of Chars
OFName.lpstrInitialDir = InitialDir 'Set the initial Directory
OFName.lpstrTitle = DialogTitle 'Set the dialog Title
OFName.flags = 0 'No extra Flags

If GetOpenFileName(OFName) Then 'Show the 'Open File' dialog
  FGetFiled = Trim(OFName.lpstrFile)
 Else
  FGetFiled = "?"
End If

'MsgBox OutputStr
ThisDrawing.Utility.Prompt (vbNewLine + "   FGetFiled  :  END !")

End Function ' FGetFiled

 

Solving the second problem:

 

([color=blue]substr[/color] [color=magenta]"string"[/color] fromPosition noChr) ; LISP

and

[color=blue]dim[/color] [color=magenta][color=black]([/color]"string"[/color] , fromPosition , noChr) ' VBA

 

Thank you guys!

Costin

Edited by Costinbos77
Link to comment
Share on other sites

It is more difficult to program in VBA than in AutoLISP, but I was lucky this time and in this way shall send thousands of thanks and good health Sitphin Joe's family, which has provided us his work.

 

Solving the first problem: it is not the greatest, but it works.


Private Declare Function [color=red]GetOpenFileName [/color]Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long ' V : 23.01.2014 .

Private Type [color=red]OPENFILENAME [/color]' V : 23.01.2014 .
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type ' OPENFILENAME

'' ---------------------------------------------------------------------------

Public Function [color=red]FGetFiled[/color](DialogTitle As String, InitialDir As String, Extensie As String) As String ' V : 23.01.2014 .
' Functie Preluare Cale Fisier !
ThisDrawing.Utility.Prompt (vbNewLine + "   FGetFiled  :  V : 23.01.2014 ;  START !")

Dim tipF As String
Dim OFName As OPENFILENAME
Dim Filter As String

Select Case Extensie
 Case "txt", "coo", "rad":  tipF = "TEXT"
 Case "csv":  tipF = "CSV"
 Case Else:  tipF = "UNKNOWN"
End Select

' Filter = "Drawing Files (*.dwg)" + Chr$(0) + "*.dwg" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
Filter = tipF + " Files (*." + Extensie + ")" + Chr$(0) + "*." + Extensie + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
' InitialDir = "C:\Program Files\AutoCAD 2006\Sample"
' DialogTitle = "Open a FILE !"

OFName.lStructSize = Len(OFName) 'Set the Structure size
OFName.hwndOwner = 0 'Set the Owner window
OFName.lpstrFilter = Filter 'Set the Filter
OFName.nMaxFile = 255 'Set the Maximum number of Chars
OFName.lpstrFile = Space(254) 'Create a Buffer
OFName.lpstrFileTitle = Space$(254) 'Create a Buffer
OFName.nMaxFileTitle = 255 'Set the Maximum number of Chars
OFName.lpstrInitialDir = InitialDir 'Set the initial Directory
OFName.lpstrTitle = DialogTitle 'Set the dialog Title
OFName.flags = 0 'No extra Flags

If GetOpenFileName(OFName) Then 'Show the 'Open File' dialog
  FGetFiled = Trim(OFName.lpstrFile)
 Else
  FGetFiled = "?"
End If

'MsgBox OutputStr
ThisDrawing.Utility.Prompt (vbNewLine + "   FGetFiled  :  END !")

End Function ' FGetFiled

Solving the second problem:

 

Public Function [color=red]FSubstr[/color](text As String, pozIn As Integer, nrCar As Integer, nrType As Boolean) As String ' V : 23.01.2014 .
' Functie Sustragere Caractere ! pozIn = 0 !
FSubstr = Trim(Left(Replace(text, Left(text, pozIn), ""), nrCar))
If nrType Then
FSubstr = Val(FSubstr)
End If
End Function ' FSubstr

 

Thank you guys!

Costin

 

Good you have made a start and got it to work. When you have finished reading from the file dont forget to close it.

Link to comment
Share on other sites

Should I worry?

 

http://usa.autodesk.com/adsk/servlet/ps/dl/item?siteID=123112&id=14889082&linkID=9240617

 

From what is written here, it seems that VBA will disappear (and I'm now starting to learn it).

AutoLISP live!

 

That link refers to AutoCAD 2011 and now Autodesk have stated that with the new VB7 they will continue to support VBA, so you should not worry too much. But there is always the chance that Microsoft will decide to drop VBA, then Autodesk would also drop VBA. In that respect going down the .NET route would be much safer, or LISP. Most of what you learn in VBA can be used in .NET so learning VBA would not be a waste of time.

Link to comment
Share on other sites

It is good idea to define a 'Frime1' on a Form1 and empty 'Frime2' on a 'Form2' and replace its contents 'Frime2' (empty) with 'Frime1' to 'Form2'?

 

http://www.vbforums.com/showthread.php?372272-Merge-2-Forms-Into-1!

 

How might make the award?

 

I can set the Format property so that borrow it Windows settings on the shape and appearance of buttons ...?

Edited by Costinbos77
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...