Converting Drawings to a Different Format

The VBA forum deals with IntelliCAD 2000 VBA (Visual Basic for Applications) issues.

Moderator: CMS Inc

Post Reply
John Finlay

Converting Drawings to a Different Format

Post by John Finlay » Sat Sep 15, 2007 3:27 pm

BJ,

This thread is the first in a series to automate drawing conversion in a different format using VBA.

Outline of program:

The drawings are saved in different folders for each job so we need the ability to select a folder and then select all files with a .dwg extension.

Once we have the names of the files we then loop through each file and convert these to an earlier version while still maintaining the same name.

To keep track of which drawings we have converted we need a text file saved in the folder with the drawing name and a date when we last carried out the conversion.
BJ
Valued Contributor
Posts: 46
Joined: Fri Mar 09, 2007 12:01 am

that sounds correct

Post by BJ » Sat Sep 15, 2007 4:49 pm

That sounds about what I need.
BJ
John Finlay

Stage 1 of Project

Post by John Finlay » Sun Sep 16, 2007 5:00 pm

Lets get started by creating a form

1/ Start a new drawing and save it as ConvertDrawingsR2000

2/ Open VBA IDE by typing VBA at the command prompt.

2/ In the project window select the "ConvertDrawingsR2000" and from the Pulldown menu select Insert-->Userform.

3/ Select the form and change the its name in the properties window to frmConvertDrawings and change the Caption property to Convert Drawings R2000.

4/ Add the following by dragging these from the toolbox (the names are highlighted as you pass your cursor over the tools)

Label name = Folders
ComboBox name = cboFolders

Label name = Drawings
ListBiox name = lstDrawings

Label name = Path
TextBox name txtPath

Command Button name = cmdConvert
Caption = Convert

Command Button name = cmdCancel
Caption = Cancel

Arrange these so they line up and are large enough to display all the information needed by selecting the individual object and dragging the corner.

An example:
Image

5/ In the project window select the "ConvertDrawingsR2000" and from the Pulldown menu select Insert-->Module and name this modStartForm in the properties window.

6/ Add the following code in the module:

Code: Select all

Sub RunConvertDrawings()
frmConvertDrawings.Show
End Sub

7/ Now setup a toolbar (see help on creating toolbar buttons) to run the form and place the following code in the command section of the toolbar:

Code: Select all

^C^C^C(command "-vbarun" "modStartForm.RunConvertDrawings")
Next we will add the code behind the form to populate the Folders ComboBox, ListBox and TextBox.
BJ
Valued Contributor
Posts: 46
Joined: Fri Mar 09, 2007 12:01 am

done

Post by BJ » Mon Sep 17, 2007 9:18 am

I've did everything. I put a toolbar in with the code shown in 7/
But when I run it I get this in the command prompt:

: Missing: 1) >


BJ
BJ
Valued Contributor
Posts: 46
Joined: Fri Mar 09, 2007 12:01 am

wanted to add

Post by BJ » Mon Sep 17, 2007 10:40 am

Also, I don't know if it makes any difference but I have my options
set like this:

Tools/Options has "Disable VBA CommonProject Macros on Start Up"
Unchecked


Tools/Security has "Very High" checked




BJ
John Finlay

Post by John Finlay » Mon Sep 17, 2007 3:28 pm

BJ,

With Tools/Security has "Very High" checked the VBA Macros will not run.

Save your drawing.

Set Tools/Security to Low.

Close IntelliCAD.

Open IntelliCAD.

You will be the only one to operate this program so it’s Ok to have this setting on low.

The other Missing: 1) > means you have one parenthesis ) missing in the command. Check you have one opening ( and one closing ).
John Finlay

Post by John Finlay » Mon Sep 17, 2007 5:41 pm

Now we can write some code behind the Form.

Forms are special classes and the tools on the form have as series of possible events when certain actions are taken.

For instance when the Cancel button is selected the program must end.

1/ Open the saved drawing and type VBA to go to the VBAIDE and select the form from the project window.

Double click on the Cancel Button and a code window will appear with:

Code: Select all

Private Sub cmdCancel_Click()

End Sub
VBA has created a Private Sub on our cmdCancel button when it is Clicked.

A private sub can only be accessed within our form and no one can access this from IntelliCAD.

All we need to do is type End between the two lines and our Cancel button will now end the program and unload the form.

Your Code should look like:

Code: Select all

Private Sub cmdCancel_Click()
End
End Sub
In the project window double click on the frmConvertDrawings to display the form and press F5

The form will display and click on the Cancel button to close the form -very easy.

2/ Next we are going to create an Initialize event on the form.

This just means that when the form is opened (Initialized) certain actions or events will take place automatically.

To do this double click on the form and the code window will open and you will see this code:

Code: Select all

Private Sub UserForm_Click()

End Sub
This is a click event and we want a Initialize event so look at the upper right area of the code window with a drop down with Click in it. Select the arrow to expose all the events we can have with the form and select Initialize. The adjacent left drop down should have UserForm in it and selecting this you can see all tools that are on the form.

VBA will write this code:

Code: Select all

Private Sub UserForm_Initialize()

End Sub
Just delete the click event on the UserForm i.e.

Code: Select all

Private Sub UserForm_Click()

End Sub
3/ We want to populate our Folders ComboBox when the form is Initialized.

Our Folder structure is:
Main Jobs Folder.
Folder Job 1
Drawing 1
Drawing 2
Drawing...n
Folder Job 2
Drawing 1
Drawing 2
Drawing...n
Folder Job ...............n
Drawing 1
Drawing 2
Drawing...n

We want to display Folder Job 1 to n in our Folders ComboBox and we do this by adding a procedure in our form code window.

The code to do this is:

Code: Select all

Sub ReadFolders(RFPath As String)
' Loop through the Folders in RFPath and add these to a ComboBox named cboFolders
Dim FolderName As String

' Retrieve the first Folder.
FolderName = Dir(RFPath, vbDirectory)

' Start the loop.
Do While FolderName <> ""
    ' Ignore the current Folder and the encompassing Folder.
    If FolderName <> "." And FolderName <> ".." Then
        ' Use bitwise comparison to make sure FolderName is a Folder.
        If (GetAttr(RFPath & FolderName) And vbDirectory) = vbDirectory Then
            ' Add entry to cboFolders only if it is a Folder
            cboFolders.AddItem FolderName
        End If
    End If
    
    ' Get next entry.
    FolderName = Dir
Loop

End Sub
All this does is loop through the folders within a certain path and ignore "." and ".." which are the root Folders above the Folders we want and if they are a Folder we use the AddItem method of our ComboBox to populate our cboFolders ComboBox.

To automatically run this procedure we insert the following code between our Initialize code:

Code: Select all

Private Sub UserForm_Initialize()

' Populate the ComboBox cboFolders with the Folder Names
ReadFolders "C:\??????\Main Jobs Folder\"

End Sub
Put in whatever Path you have to point to the Main Jobs Folder and don't forget to add a Backslash.

Try this out by double clicking the Form name in the project window and pressing F5 as previously described.

Tomorrow we will add code to populate the Drawings 1 to n in our list box and set-up our routine to loop through these drawings and save these in R2000 format.

I should add that this could also be used to do batch activities to a range of drawings such as updating title blocks, changing layers or printing drawings. So the project is very useful.
BJ
Valued Contributor
Posts: 46
Joined: Fri Mar 09, 2007 12:01 am

So far so good

Post by BJ » Tue Sep 18, 2007 8:49 am

Got everything up to this point working ok. I can see a list of the individual job folders in my main jobs folder in the ComboBox.

BJ
John Finlay

Post by John Finlay » Tue Sep 18, 2007 6:30 pm

We need to add some more code behind our Form so as before go to the code window and add the code Subroutine ReadFiles that looks like a cut down version of the ReadFolders Subroutine. We only want the drawings and not any pictures, backups or other files so we restrict the path to .dwg extensions only.

Code: Select all

Sub ReadFiles(strPath As String)
'Loop through the Files in the selected Folder and add these to ListBox
Dim strFile As String

'Only select the drawing files with the extension .dwg so we add \*.dwg to the Path
strPath = strPath & "\*.dwg"

'Retrieve the first File.
strFile = Dir(strPath)

' Start the loop.
Do While strFile <> ""
    'Populate the ListBox with the drawing names
    lstDrawings.AddItem strFile
    
    'Get next entry
    strFile = Dir
Loop

End Sub
Now we have the code to populate the ListBox we need some event that will cause the Subroutine to run - I have chosen when a user selects a Folder as we need a folder in the path name to get our drawings.

Go to the Form and double click on the cboFolder

The change event procedure is partly written for us:

Code: Select all

Private Sub cboFolders_Change()

End Sub
We need to write some code in between the change event to clear the ListBox (if there were ant previous drawing in it) and run our ReadFiles Subroutine. I also will add to the TextBox the Path that we are using to confirm that the user is in the correct location prior to converting the drawings. You will have to add the correct path and again don't forget the add the back slash.

The change event code in full is:

Code: Select all

Private Sub cboFolders_Change()

' Update Path in a TextBox txtPath
txtPath.Value = "C:\???????\??????\" & cboFolders.Value

' Clear the ListBox lstDrawings and populate it with the drawings in the Folder
lstDrawings.Clear
ReadFiles txtPath

End Sub
Now we have our ListBox full of drawings all we need now is to loop through the drawings; Open them; Use SaveAs R2000 then Close them. This is very easy to do.

Return to the Form and double click on the Convert Button and the Click event procedure will be created:

Code: Select all

Private Sub cmdConvert_Click()

End Sub
I have chosen to place our code to convert the drawings inside the Click event see full code below:

Code: Select all

Private Sub cmdConvert_Click()
Dim SavePath As String
Dim OpenDoc As IntelliCAD.Document
Dim iCount As Integer

'Loop through Drawings and save these to ACAD 2000 Format
For iCount = 0 To lstDrawings.ListCount - 1
    'Open the Drawing
    Set OpenDoc = IntelliCAD.Documents.Open(txtPath.Value & "\" & lstDrawings.List(iCount, 0), False)
    
    ' Save the Path Name for SaveAs Method
    SavePath = IntelliCAD.ActiveDocument.Path & "\" & IntelliCAD.ActiveDocument.Name
    
    ' Save Drawing as aersion R2000
    IntelliCAD.ActiveDocument.SaveAs SavePath, vicVersionR2000
    
    'Close the Drawing
    OpenDoc.Close (True)
Next

'Close the form
Unload frmConvertDrawings

End Sub
Notice I unload the form and if you don't want the code to do this simply place a comment tag in front i.e. 'Unload frmConvertDrawings.

Next we will add a text file to the Folder that has the date and the drawing names we converted and we will add some more tools to our Form to display this information.

BJ, this should help you speed up converting drawings.
The next portion is not needed however; I want to have better control of the conversion process and I believe this is a good example to teach file handling.
BJ
Valued Contributor
Posts: 46
Joined: Fri Mar 09, 2007 12:01 am

got it to convert

Post by BJ » Wed Sep 19, 2007 9:02 pm

I got it to convert a drawing, but the drawing converted was a .bak file
The .dwg file was unchanged, though I could rename the .bak file and use it.
Also, is there a VBA function that will sort the listings in some sort of order?
The job folder listing as well as the file listing is all jumbled up.

BJ
John Finlay

Post by John Finlay » Wed Sep 19, 2007 10:18 pm

BJ,

Are you using Windows 2000?

If so I will create a sort on the Folders and Files.

From XP Help:

The sort order that is used by Windows Vista, Windows XP, and Windows Server 2003 for files and folders whose names contain numerals differs from the sort order that is used by Windows 2000. The following example shows the difference.
Windows Vista, Windows XP, and Windows Server 2003
Ie4_01
Ie4_128
Ie5
Ie6
Ie401sp2
Ie501sp2
Windows 2000
Ie4_01
Ie4_128
Ie401sp2
Ie5
Ie501sp2
Ie6
By default, the newer sort order considers strings in file and folder names as numeric content, not text. Numerals in folder and file names are sorted according to their numeric value.

In this example, 401 is a numerically higher value than 6. Therefore, the Ie401sp2 folder is listed after the Ie6 folder when you sort the folders by name in ascending order. In the following example, note how the following files, whose names contain numerals, are sorted.
Windows Vista, Windows XP, and Windows Server 2003
5.txt
11.txt
88.txt
Windows 2000
11.txt
5.txt
88.txt

Conversion:

The backups are automatically generated by IntelliCAD when any drawing is saved.

To test the procedure, I converted drawings from version 6.4 and opened these in version 3.3 that only has the capacity to read R2000 drawings.
This gave me the confidence that the conversion worked.

I believe you are using 6.2 Premium Edition , 6.2.36.1 from our eariler discussions and I will test the procedure with this version and let you know what I find.
BJ
Valued Contributor
Posts: 46
Joined: Fri Mar 09, 2007 12:01 am

Windows XP

Post by BJ » Sun Sep 23, 2007 9:40 am

I am using Windows XP
John Finlay

Post by John Finlay » Mon Sep 24, 2007 6:55 pm

BJ,

My version of XP is honouring a sort order in alpha/numeric form and I have obtained a simple sort to resolve your issues.

Regarding the version 6.2 also not correctly overwriting the drawing file yet allowing the backup to be saved in lower versions, I have tested this and agree with you.

We need to think about how we can overcome this shortfall.

1/ An option is for you to upgrade IntelliCAD to the latest version or;

2/ Transfer these backup files to another folder and rename them to .dwg files or;

3/ Delete the drawing files and then rename the backup files to .dwg (could be dangerous if there is an error).

What are your thoughts?
BJ
Valued Contributor
Posts: 46
Joined: Fri Mar 09, 2007 12:01 am

Post by BJ » Tue Sep 25, 2007 10:32 am

I would transfer the .bak files to another folder and then rename them.

I have been messing around with a lisp routine for doing this and have used (command ".saveas" "A2K" SAVEDWG ) , where SAVEDWG is
a path that (includes the name I want the drawing saved as), to a folder where I want the drawings placed. All set up for my particular work situation. The routine cycles through a segment of drawing numbers that
are set in variables by the user.

IC needs a repository of utlilities on the internet and the VBA program that does the conversion of drawings from IC to Acad versions would be great
for the IC community. I think most of the people doing work with IC share their drawings with Acad users. I hope in later versions of IC that they keep this conversion option as it is a good selling point.



_________________
Post Reply