Using the Insert Block Method

Use the InsertBlock method to add a BlockReference object to a drawing or to nest a block in another Block object. This can be either a block in the Blocks collection or an external drawing file. In either case, this method returns a BlockReference object. The PaperSpace, ModelSpace, and Block objects expose this method.

Set BlockReferenceObject = Object.InsertBlock(InsertionPoint, BlockName, _ Xscale, Yscale, ZScale, RotationAngle)

Table 13-4 explains this method's parameters.

Table 13-4. The InsertBlock Method's Parameters

Name

Data Type

Description

InsertionPoint

Variant

A three-element array of doubles that specifies the 3-D WCS coordinates where the block will be inserted into Object.

BlockName

String

The name of a Block object in the Blocks collection or the path and file name of an AutoCAD drawing file to import.

Xscale

Double

A scaling factor for the block's X direction. It may not be 0. Negative numbers mirror the insertion on this axis.

Yscale

Double

A scaling factor for the block's Y direction. It may not be 0. Negative numbers mirror the insertion on this axis.

Zscale

Double

A scaling factor for the block's Z direction. It may not be 0. Negative numbers mirror the insertion on this axis.

RotationAngle

Double

The rotation angle relative to the WCS X-axis, expressed in radians.

The following example creates a block reference based on user input:

Public Sub TestInsertBlock()

Dim strName As String

Dim varInsertionPoint As Variant

Dim dblX As Double

Dim dblY As Double

Dim dblZ As Double

Dim dblRotation As Double

'' get input from user With ThisDrawing.Utility .InitializeUserInput 1

strName = .GetString(True, vbCr & "Block or file name: ") .InitializeUserInput 1

varInsertionPoint = .GetPoint(, vbCr & "Pick the insert point: ") .InitializeUserInput 1 + 2

dblX = .GetDistance(varInsertionPoint, vbCr & "X scale: ") .InitializeUserInput 1 + 2

dblY = .GetDistance(varInsertionPoint, vbCr & "Y scale: ") .InitializeUserInput 1 + 2

dblZ = .GetDistance(varInsertionPoint, vbCr & "Z scale: ") .InitializeUserInput 1

dblRotation = .GetAngle(varInsertionPoint, vbCr & "Rotation angle: ") End With

'' create the object On Error Resume Next

ThisDrawing.ModelSpace.InsertBlock varInsertionPoint, strName, dblX, _

dblY, dblZ, dblRotation If Err Then MsgBox "Unable to insert this block."

End Sub

Try this example with the name of a block in the current drawing and with an external file name (including the .dwg file extension).

To give the user a friendlier way to input the path and file name, you can build a GUI with a UserForm and use a Windows CommonDialog control. This control is not available on the UserForm Toolbox, but you can add it by right-clicking the Toolbox, selecting Additional Controls, and checking the Microsoft Common Dialog Control, Version 6.0 option.

If you add a CommandButton control named cmdInsertBlock and a CommonDialog control named dlgOpenFile to a UserForm, as shown in Figure 13-3, you could use the following code to see the familiar Open dialog box, as shown in Figure 13-4, when you choose a drawing file.

I Ed

Insert 3lcc-

Figure 13-3. Creating a UserForm with a CommandButton and a CommonDialog control

Private Sub CommandButton1_Click()

Dim objBlockRef As AcadBlockReference

Dim varInsertionPoint As Variant

Dim dblX As Double

Dim dblY As Double

Dim dblZ As Double

Dim dblRotation As Double

'' get input from user dlgOpenFile.Filter = "AutoCAD Blocks (*.DWG) | *.dwg" dlgOpenFile.InitDir = Application.Path dlgOpenFile.ShowOpen

If dlgOpenFile.FileName = "" Then Exit Sub Me.Hide

With ThisDrawing.Utility .InitializeUserInput 1

varInsertionPoint = .GetPoint(, vbCr & "Pick the insert point: ") .InitializeUserInput 1 + 2

dblX = .GetDistance(varInsertionPoint, vbCr & "X scale: ") .InitializeUserInput 1 + 2

dblY = .GetDistance(varInsertionPoint, vbCr & "Y scale: ") .InitializeUserInput 1 + 2

dblZ = .GetDistance(varInsertionPoint, vbCr & "Z scale: ") .InitializeUserInput 1

dblRotation = .GetAngle(varInsertionPoint, vbCr & "Rotation angle: ") End With

'' create the object On Error Resume Next

Set objBlockRef = ThisDrawing.ModelSpace.InsertBlock _ (varInsertionPoint, dlgOpenFile.FileName, dblX, _ dblY, dblZ, dblRotation)

If Err Then

MsgBox "Unable to insert this block" Exit Sub End If objBlockRef.Update Me.Show End Sub

Figure 13-4. Viewing the Open dialog box when choosing a drawing file

To control the displayed file types, set the CommonDialog control's Filter property. You must hide the form before you can access the AutoCAD interface, because AutoCAD 2006 VBA UserForms are modal by default. The alternative is to set the ShowModal property of the UserForm to False.

+1 0

Post a comment