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 - Page IV

Programming for AutoCAD using Visual Basic brings quite a few advantages as well as a few disadvantages. The main advantage is having full access to all the Visual Basic functions, tools, controls, etc. The biggest disadvantage is having to make sure that the target computer has all the support and system files necessary to run your program.

One area that Visual Basic stands out is in Batch Processing. In this example, we're going to design a simple little standalone Visual Basic program that will "plot" a directory of drawings to DWF files. and then create an HTML web page with a list of links to these DWF files.
By the way, this program can very easily be modified to suit any type of Batch Processing that your little heart desires.

Open Visual Basic and create a new Standard.exe Project.

Firstly, you will need to insert a new Form with the following controls :

  • DriveListBox - Drive1
  • DirListBox - Dir1
  • FileListBox - File1
  • 5 Labels - Label1 to Label5
  • CommandButton - btnExport
  • CommandButton - btnCancel

 Now enter the following coding under 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 bottom of this page.)

Option Explicit
'----------------------

Private Sub btnCancel_Click()

End
'End the Application

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

Private Sub btnExport_Click()

'declare variables
Dim acadApp As Object
Dim acadDoc As Object
Dim filename As String
Dim dirname As String
Dim pViewport As Object
Dim mylen As Integer
Dim filename1 As String
Dim nFile As Integer
Dim MMode As Integer

'test for root directory
If Right(File1.Path, 1) <> "\" Then

'if not root add \
dirname = File1.Path & "\"

Else

'if root directory do nothing
dirname = File1.Path


End If

'check if A:Drive has been choosen
If UCase(dirname) = "A:\" Then

'Hide the dialogue box
Me.Hide

'inform the user
MsgBox ("Insufficient Space to Process Drawings on Drive A:"), 16, _
"DWF Batcher"

'end the application
End

End If

'inform the user that we are about to connect to AutoCAD
Label5.Caption = "Please Wait......" & vbCr & "Connecting to AutoCAD"

'if error carry on with next line
On Error Resume Next

'set reference to AutoCAD Application
Set acadApp = GetObject(, "AutoCAD.Application")

'if there is an error (AutoCAD not open)
If Err Then

'clear the error
Err.Clear

'open AutoCAD
Set acadApp = CreateObject("Autocad.Application")

'set reference to AutoCAD Application
Set acadApp = GetObject(, "AutoCAD.Application")

'if there is another error
If Err Then

'inform user
MsgBox Err.Description

'exit application
Exit Sub

End If

End If

'Make Autocad Visible
acadApp.Visible = True

'set reference to active document
Set acadDoc = acadApp.ActiveDocument

'Retrieve the SDI mode
MMode = acadDoc.GetVariable("SDI")

'Check SDI
If MMode = 0 Then

'Switch it on
acadDoc.SetVariable "SDI", 1

End If

'Hide the dialogue box
Me.Hide

'get the next free file number
nFile = FreeFile

'open the html file to write to
Open dirname & "DWFBatcher.htm" For Output As #nFile

'write the header
Print #nFile, "<html><head><title>WMF Batcher</title></head><body> _
<h3>DWF _ Batcher</h3>"

'retrieve the first entry
filename = Dir(dirname, vbNormal)

'start the loop
Do While filename <> ""

'ignore files that do not end with .DWG
If UCase(Right$(filename, 4)) = ".DWG" Then

'use bitwise comparison to make sure filename is not a directory
If (GetAttr(dirname & filename) And vbNormal) = vbNormal Then

'if drawing not saved
If Not acadDoc.Saved Then

'save the drawing
acadDoc.Save

End If

End If

'*******************************************************************
'This is the section of the program where you process each drawing.
'You can do anything your little heart desires........

'open drawing to process

acadDoc.Open dirname & filename

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

'subtract the .DWG Extension
mylen = mylen - 4

'retrieve the drawing name
filename1 = Left(filename, mylen)

'write the DWF file name link
Print #nFile, "<a href=" & dirname & filename1 & ".DWF" & ">" & _
dirname & filename1 & ".DWF" & "</a>" & "<p>"

'set reference to the viewports
Set pViewport = acadDoc.ActiveViewport

'zoom to extents
pViewport.ZoomExtents

'create the DWF file
'commands shown are for the default DWFOUT settings
'you may need to replace to suit your requirements

acadDoc.SendCommand "DWFOUT" & vbCr & vbCr & vbCr & vbCr & _
vbCr & vbCr

'save the drawing
acadDoc.Save

End If

'get the next entry
filename = Dir

'carry on looping
Loop

'write the footer
Print #nFile, "</body></html>"

'close the file
Close #nFile

'reset SDI
acadDoc.SetVariable "SDI", MMode

'Close Autocad
acadApp.Quit

're-display the dialog
Me.Show

'inform the user
Label5.Caption = "Process Complete......" & vbCr & _
"Select another Directory or Cancel"

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

Private Sub Dir1_Change()

'retrieves the drive and directory name
File1.Path = Dir1.Path

'displays it in the dialog
Label4.Caption = Dir1.Path

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


Private Sub Drive1_Change()

'retrieves the drive name
Dir1.Path = Drive1.Drive

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


Private Sub Form_Load()

'Populate the labels
Label1.Caption = "Directory to Process :"
Label2.Caption = "Choose Drive and Directory"
Label3.Caption = "Brought to you by CAD Encoding"

End Sub

Save your Project as "DwfBatcher." When you run this program, a dialog will appear as shown above. Choose the drive and directory containing the drawings you would like to process, then select "Go for it." If AutoCAD is not already open, it will be opened and each drawing will be plotted as a DWF file. On completion, an HTML file will be created in the same directory as the drawings.


To download the source coding for all four of these routines, put the index finger of your left hand in your right nostril, stick your tongue out, and then click here.

 
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