Questions similar to this appear on the Be Communities MicroStation Programming Forum.

MicroStation Fence

The fence idiom has been part of MicroStation's user interface for decades. When a user places a fence, she designates a spatial area for subsequent graphic element manipulation.

MicroStation VBA provides the Fence object. There is only ever one Fence, and it is created in a specific view. The contents of the fence are governed by that view and the fence boundary. A fence is 2D in a 2D DGN model and 3D in a 3D model.

Q Questions about MicroStation VBA and fences …

A  User wants to draw a shape and then convert the shape to a fence. In other words, a VBA tool that mimics MicroStation's place fence command. We wrote an example to demonstrate that idiom: see Create Fence VBA article.

A  To qualify the question, the MicroStation user wants to select an element without having to datapoint (click) in a MicroStation view, and create a fence also without having to click. The goal is to perform batch processing — without a human available to click the mouse — and choose an element somehow before creating a fence and processing its contents.

VBA Fence Object

Get MicroStation VBA's fence object like this …

Dim oFence As Fence
Set oFence = ActiveDesignFile.Fence

You may want to see if a fence is defined and, if so, undefine it …

Dim oFence As Fence
Set oFence = ActiveDesignFile.Fence
If oFence.IsDefined Then oFence.Undefine

Create a fence from points, an element, or from a list of fence names. In this example, suppose we have a ShapeElement, and want to create a fence from that shape …

Sub CreateFenceFromShapeElement (ByVal nView As Long, ByVal oShape As ShapeElement)
  Dim oView As View
  Set oView = ActiveDesignFile.Views (nView)
  Dim oFence As Fence
  Set oFence = ActiveDesignFile.Fence
  oFence.DefineFromElement oView, oShape
End Sub

VBA KeyinArguments

MicroStation VBA's KeyinArguments stores information passed in the key-in you use to run a macro. KeyinArguments is a global variable supplied automatically by VBA, meaning that you just use it. For example, suppose you want to pass a view number to a macro. You would append the view number to the key-in that runs the macro like this …

vba run [project-name]module-name.procedure-name arguments

Collect the string from the key-in in VBA like this …

Dim args As String
args = KeyinArguments
Debug.Print "Key-in suffix '" & args & "'"

In the Fence Tools utility KeyinArguments conveys a view number. The view number is used when the tool creates a fence. KeyinArguments provides a string, so we must convert it to a number, and check that it represents a valid view index …

Dim args As String
args = KeyinArguments
If IsNumeric (args) Then
  Dim n As Long
  n = CLng(args)
  Debug.Print "Key-in view number " & cstr(n)
  If n > 0 And n <= 8 Then
    ' View number is valid
  End If
End If

Fence Tools

The VBA Fence Tools project implements the create-fence-from-shape-element idiom. When you run the macro, it determines if a selection set is active, and if so whether it contains a single closed element. Then it creates a fence using the selected shape and the view number passed in the key-in.

Here's the key-in to run Fence Tools. In this example we're specifying view index 2 to create the fence in MicroStation view 2 …

vba run [FenceTools]modMain.Main 2
Download

How to Obtain Fence Tools

The complete code is provided in the FenceTools MVBA project, supplied in a ZIP archive. Unpack the ZIP archive to your ..\Organization\Standards\macros folder, where MicroStation can find it.