AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

AutoCAD and HTML

This article first appeared in the September 2002 issue of "CAD Encoding."

In this article, we are going to have a look at extracting information from AutoCAD and displaying the results in an HTML file.
Just to be clever, and to keep everybody happy, we'll be writing the coding using VBA, AutoLisp, Visual Lisp and last but not least VB. Each of the routines will be slightly different to give you an idea of the capabilities of each language.

Let's have a look at VBA first.
This wee routine will extract all the layer names contained within a drawing, create an HTML file on the fly, and list all the layer names :

Insert a Userform and add a listbox and two buttons, retaining their default names :

  • UserForm1

  • ListBox1

  • CommandButton1

  • Commandbutton2

Add the following coding to the General Declarations section :

(I recommend you download the source coding for this project as word-wrapping can cause major problems. The download link is at the end of Page IV.)

Option Explicit
'-----------------------------------------------------
Public AllLayers As AcadLayers
'-----------------------------------------------------

Private Sub CommandButton1_Click()

Dim nFile As Integer
Dim Layer As AcadLayer
Dim dName As String
Dim mylen As Integer
Dim dPrefix As String

'if there is an error
On Error GoTo Err_Control

'hide the dialog
Me.hide

'get the drawing name
dName = ThisDrawing.GetVariable("DWGNAME")

'get the drawing name
dPrefix = ThisDrawing.GetVariable("DWGPREFIX")

'get the length of the filename
mylen = Len(dName)

'subtract the .DWG Extension
mylen = mylen - 4

'retrieve the drawing name
dName = Left(dName, mylen)

'get the next free file number
nFile = FreeFile

'open the html file to write to
Open dPrefix & dName & " - AllLayers.htm" _
For Output As #nFile

'write the header
Print #nFile, "<html><head><title> _
Cad Encoding - Layers to HTML</title></head>
<body><h3>Layers - Drawing No : " & dPrefix & _
dName & "</h3><hr>"

'process each layer
For Each Layer In AllLayers

'write to the HTML file
Print #nFile, "Layer Name = " & Layer.Name & "<p>"

'process next layer
Next

'write the footer
Print #nFile, "<hr><h3>Created by CAD Encoding</h3> _
</body></html>"

'close the file
Close #nFile

'display message box
MsgBox ("Layers written to HTML File :" & vbCr & _
dPrefix & dName &" - AllLayers.htm"), , "Layers to HTML"

'error control
Exit_Here:
  Exit Sub
Err_Control:
  MsgBox Err.Description
  Resume Exit_Here
  
End Sub
'----------------------------------------------------

Private Sub CommandButton2_Click()

End

End Sub
'---------------------------------------------------

Private Sub UserForm_Initialize()

Dim Layer As AcadLayer

'if there is an error
On Error GoTo Err_Control

'get the layers collection
Set AllLayers = ThisDrawing.Layers

'process each layer
For Each Layer In AllLayers

    'display each layer name in the list box
    ListBox1.AddItem Layer.Name
    
'process next layer
Next
    
'error control
Exit_Here:
  Exit Sub
  
Err_Control:
  MsgBox Err.Description
  Resume Exit_Here
    
End Sub

Now insert a new Module and add this :

Sub AllLayers()

UserForm1.Show

End Sub
 

Save this as "AllLayers.dvb."
Now open any drawing and run the macro.
The List Box should be populated with the drawings Layer names.
Select the "HTML" button. A dialog should appear informing you that an HTML file has been created. Navigate to the directory containing the HTML file and open it.
A list of all the Layers in your drawing should have been created.


On the next page we'll have a look at something similar, but this time using AutoLisp.

 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website