IntelliCAD 2000 VBA Article



IntelliCAD 2000 Visual Basic for Applications (VBA) 6

By: Frank Zander

This article takes a sneak peek at the new Visual Basic for Applications (VBA) 6 inside of the soon to be released IntelliCAD 2000. This article is written using an early beta version of IntelliCAD 2000. Therefore the displayed information may or will change in the final release of IntelliCAD 2000. In this article, I will discuss several of the exciting new features of IntelliCAD 2000's implementation of VBA. I will then demonstrate how to create a VBA program to search for all the blocks in a drawing and insert the list of blocks into the drawing.

What's new?

• Visual Basic for Applications (VBA) 6

IntelliCAD 2000 implements VBA 6 the latest version of VBA. All Microsoft Office 2000 applications use VBA 6. Everything you learn how to do in VBA 6 in IntelliCAD 2000 is portable to all Office 2000 programs (and vice versa).

[Insert Picture: " vbaide.png"]

• VBA 6.0 Macro Warning and Security

With the ability to embed VBA projects into drawings, malicious code and virus have come to the CADD world. With the implementation of VBA 6 programmers can now digitally sign code. Digitally signed VBA code allows the end user to verify the author of the code

[Insert Picture: "Digitalsignature.png"]

IntelliCAD 2000 allows the end user to set the macro warning to High, Medium or Low. Using the High setting, only signed code is allowed to run after the author has been added to the Trusted Sources dialog box.

[Insert Picture: "security.png"]

If the end user of one encounters an unsigned VBA macro , the following will occur:

1. If the application's security settings are set on "High," the client application will not permit the unsigned code to run.

2. If the application's security settings are set on "Medium," the client application will display a warning to ask the user if they want to enable or disable this unsigned code.

By contrast, if a user encounters signed VBA code in a file, The user is informed:

1. Of the true identity of the publisher (in this case Microsoft Corporation).

2. That there is no problem with the certificate (the lack of additional warnings)

3. The Details button will show the digitaly signed certificate.

Users can choose to trust all subsequent VBA code from the same publisher source.

Simply by clicking the "More Info" button, users can inspect the certificate and verify its validity.

[Insert Picture: "VertisignCertificate.png]

In addition, if the code is changed, the user is notified of that the code has been modified.

• The IntelliCAD 2000 Object model

The IntelliCAD 2000 object model is complete! The Preview version of IntelliCAD98 VBA did not allow programmers to get at several of the drawing objects. For instance, I was never able to get a selection set to work (maybe it was just me I do not know). Because the Preview VBA in IntelliCAD98 did not include any help for the object model, finding any clue as to the object properties and methods was an effort in frustration. The object model and programming methods of IntelliCAD 2000 so nicely parrot those found in AutoCAD 2000 and r14 that AutoCAD VBA programmers will find programming in IntelliCAD 2000 a breeze. Aha this is what I was hopping for. Even with the non-existent help-file (as of this writing —probably because this is such beta software) I was able to figure out the object model and methods using my background in VBA programming in AutoCAD. Surly, a help file outlining the VBA object model and methods will ship with the shipping version of IntelliCAD 2000. As an example of how closely IntelliCAD 2000 mimics AutoCAD's VBA, below are two code snippets from each program to create a selection set and an Mtext object.

IntelliCAD 2000 code for Mtext: NewMText = ThisDocument.ModelSpace.AddMText(PT1, MtextWidth, strMTextOutPut)

AutoCAD 2000 code for Mtext: NewMText = ThisDrawing.ModelSpace.AddMText(PT1, MtextWidth, strMTextOutPut)

IntelliCAD 2000 code for Selection set:

NewSS.Select vicSelectionSetAll, , , FilterType, FilterData

AutoCAD 2000 code for Selection set:

NewSS.Select acSelectionSetAll, , , FilterType, FilterData

For the programmer, the ability to write code for one CADD program and use it in another, it is simply wonderful. IntelliCAD98 did this with LISP and now in IntelliCAD 2000 with VBA.

Creating the Block Report Program

• Forms, Controls and Modules

To create the Block Report program you will need to create a form and a module. The form contains: a listbox with the name: lstResults; four buttons named cmdFindBlocks, cmdRemove, cmdMtextPlace, and cmdExit. The module contains one Public Sub program called LoadBlockReport. See the section below called The Module code:

[Insert Picture: "Block Report with data.png"]

Running an IntelliCAD2000 VBA macro.

• To run an IntelliCAD 2000 VBA macro from the command prompt with a dialog box type: VBARUN.

Then select the Start.LoadBlockReport macro from the dialog box

[Insert Picture: "Run IntelliCAD VBA Macro.png"]

• To run the Block Report program from the command prompt with out a dialog box type: -VBARUN

Then enter the macro: Start.LoadBlockReport

• To launch the Block Report program from a custom icon on a tool bar use:

^C^C^C-vbarun;Start.LoadBlockReport;

[Insert picture "Customize Toolbar.png"]

In Conclusion

I look forward to using the shipping version of IntelliCAD 2000. Once the object model and properties are documented and example code is included in the help system, this will be a very attractive CADD VBA development platform. The ability to digitally sign code in VBA 6 will give programmers better control over their intellectual property. End users will be able to use digitally signed VBA programs with confidence. Code for this project is available for downloading for both IntelliCAD98 and IntelliCAD 2000 at Contract CADD Group's website:

Frank Zander is a CADD consultant at Contract CADD Group in Surrey, British Columbia, Canada. He can be reached via e-mail at: frankzander@ or by telephone at: (604)582-8283. Visit Contract CADD Group's website at:

The Module code:

Option Explicit

Public Sub LoadBlockReport()

frmBlockReport.Show

End Sub

The Form Code:

Option Explicit

Private Sub cmdExit_Click()

Unload Me

End Sub

Private Sub cmdFindBlocks_Click()

' Find all blocks in a Drawing an display in the list box

Dim objDoc As Document

Dim objBlock As Object

Dim FoundBlockInList As Boolean

Dim strBlockName As String

Dim Counter As Integer

Dim I As Integer

Dim NewSS As SelectionSet

Dim FilterType(0) As Integer

Dim FilterData(0) As Variant

FilterType(0) = 0

FilterData(0) = "INSERT"

' Hide this form

Me.Hide

Set objDoc = Application.ActiveDocument

Set NewSS = objDoc.SelectionSets.Add("VBA")

' Create a selection set filtering for blocks

NewSS.Select vicSelectionSetAll, , , FilterType, FilterData

' If are in this drawing go the the gracefull exit below

If Not NewSS.Count > 0 Then GoTo GracefullExit

' remove the ' below to make the listt box invisible for faster processing

' lstResults.Visible = False

' Put a new header on the List Box

Call ListBoxHeader

Counter = NewSS.Count

txtStatus.Visible = True

cmdFindBlocks.Visible = False

cmdExit.Visible = False

cmdRemove.Visible = False

cmdMtextPlace.Visible = False

' Change the Status Text

txtStatus.Caption = "Processing: " & Counter & " Blocks"

' Repaint the Form

frmBlockReport.Repaint

' Typicaly this is should read:

' For I = 0 to NewSS.Count -1

For I = 1 To NewSS.Count

Set objBlock = NewSS.Item(I)

FoundBlockInList = False

' Find the block name in the list

strBlockName = objBlock.Name

FoundBlockInList = BlockListLookUp(strBlockName)

If FoundBlockInList = False Then

lstResults.AddItem "", lstResults.ListCount

lstResults.List(lstResults.ListCount - 1, 0) = objBlock.Name

lstResults.List(lstResults.ListCount - 1, 1) = 1

End If

Next

Set NewSS = Nothing

' objDoc.SelectionSets.Item("VBA").Delete

' Make the List box Visible

lstResults.Visible = True

' Start the enable remove button program

Call EnableRemoveButton

If Not lstResults.ListCount >= 2 Then MsgBox "No blocks found in drawing", vbInformation

txtStatus.Visible = False

cmdFindBlocks.Visible = True

cmdExit.Visible = True

cmdRemove.Visible = True

cmdMtextPlace.Visible = True

GracefullExit:

' Display this form

Me.Show

End Sub

Function BlockListLookUp(strBlockName As String) As Boolean

' Check to see if a block is in the list box

Dim I As Integer

Dim FoundBlockInList As Boolean

For I = 0 To lstResults.ListCount - 1

If lstResults.List(I, 0) = strBlockName Then

lstResults.List(I, 1) = lstResults.List(I, 1) + 1

FoundBlockInList = True

End If

Next

If FoundBlockInList = True Then

BlockListLookUp = True

Else

BlockListLookUp = False

End If

End Function

Private Sub cmdMtextPlace_Click()

' Place an MText object in to the drawing containing the block names and the count

Dim objDoc As Document

Dim objUtil As Object

Dim strDrawingName As String

Dim intCounter As Integer

Dim strMTextOutPut As String

Dim MtextWidth As Double

Dim objNewMtextReport As Object

Dim strSetting As String

Dim strOUT1 As String

Dim strOUT2 As String

Dim Qt As String

Dim NewMText As Mtext

Dim PT1 As Point

' set Qt to equal chr(34)

' this is the ASCII character "

Qt = Chr(34)

'Set objCADD = GetObject(, "icad.application")

Set objDoc = Application.ActiveDocument

' Width of the Mtext object

If objDoc.GetVariable("dimscale") 0 Then

MtextWidth = 525 * objDoc.GetVariable("dimscale")

Else

MtextWidth = 525

End If

' Header info for the block report

strMTextOutPut = " Block Report: \P Qty. Block "

' Process every row in the Block results list

For intCounter = 1 To lstResults.ListCount - 1

' set the length of the string to 10 characters

strOUT1 = "12345"

' Get the text from the flexgrid at this location

RSet strOUT1 = lstResults.List(intCounter, 1)

' add text to the mtext out string

strMTextOutPut = strMTextOutPut & "\P" & strOUT1

strOUT2 = lstResults.List(intCounter, 0)

' add text to the mtext out string

strMTextOutPut = strMTextOutPut & Space(5) & strOUT2

' Process the next intCounter

Next intCounter

'Hide the Block Report form

frmBlockReport.Hide

' Get the insert point for the block report

Me.Hide

Set PT1 = objDoc.Utility.GetPoint(, "Insert Block Report -- Upper Left corner:")

If objDoc.ActiveSpace = vicModelSpace Then

Set NewMText = objDoc.ModelSpace.AddMText(PT1, MtextWidth, strMTextOutPut)

Else

Set NewMText = objDoc.PaperSpace.AddMText(PT1, MtextWidth, strMTextOutPut)

End If

NewMText.Update

' Show the block report form

frmBlockReport.Show

End Sub

Private Sub cmdRemove_Click()

On Error GoTo ClearForm

lstResults.SetFocus

'Ensure ListBox contains list items

If lstResults.ListCount >= 1 Then

'If no selection, choose last list item.

If lstResults.ListIndex = -1 Then

lstResults.ListIndex = lstResults.ListCount - 1

End If

lstResults.RemoveItem (lstResults.ListIndex)

End If

' run the Enable Remove Button sub

Call EnableRemoveButton

Exit Sub

ClearForm:

Call ListBoxHeader

Exit Sub

End Sub

Sub EnableRemoveButton()

' make the Remove button and Place button enabled or disabled

If lstResults.ListCount >= 2 Then

cmdRemove.Enabled = True

cmdMtextPlace.Enabled = True

Else

cmdRemove.Enabled = False

cmdMtextPlace.Enabled = False

End If

End Sub

Private Sub UserForm_Initialize()

Call ListBoxHeader

Call EnableRemoveButton

End Sub

Private Sub ListBoxHeader()

' Setup the list box header information

With lstResults

' Clear the list box

.Clear

' Set the number of columns

.ColumnCount = 2

' Set the column widths

.ColumnWidths = "80;20"

' Text Tip

.ControlTipText = "Blocks and Quantity"

.AddItem "", 0

.List(0, 0) = "Block Name:"

.List(0, 1) = "Qty:"

End With

End Sub

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches