Q How can I export line data as a CSV file for consumption by an external app such as Excel?
Questions similar to this, posed by MicroStation users and VBA developers, appear on the Bentley Discussion Groups, typically the MicroStation Forum or Programming Forum.
A This VBA project implements a utility for writing DGN line element data to a CSV file..
MicroStation users sometimes want something different from the set of tools provided in the product. The question that prompted the development of this project was something like: A cross section file has several hundred cross sections with varied ditch widths on level ditchbot. Is there a way to tabulate selected elements with desired properties?"
What that MicroStation user really wanted, it turns out, was an Excel worksheet with line end-points. In that worksheet he could perform further calculations, such as find the centre point of each line.
The Line Exporter first scans the active DGN model for line elements on a specified level. Then it opens a CSV file for writing and copied line element data into that file. The CSV file looks something like this (the first line is the header with column names) …
Level Name,Start.X,Start.Y,Start.Z,End.X,End.Y,End.Z Lines 3,4.16,-1.22,0.00,7.93,-1.22,0.00 Lines 3,4.14,-1.60,0.00,7.91,-1.60,0.00
It's straightforward to write a CSV file and include element data.
The CSV writer references the Scripting.FileSystem
object,
which supplies the useful TextStream
class.
The scripting DLL is almost certainly already installed on your Windows computer.
The VBA project discussed here references that DLL.
It uses the Scripting.FileSystemObject
in the CSV text writer.
To use those objects, which are not delivered with VBA, you must add a reference to
Microsoft Scripting Runtime in your VBA project.
In the VBA IDE, choose menu Tools|References to open the VBA References dialog.
The above code is available in this MicroStation
VBA project.
Unpack the ZIP archive and copy LineExporter.mvba
to a location where MicroStation
can find it.
A good place to copy it would be \Workspace\Standards\vba
.
To start exporting line information, type the following into MicroStation's key-in dialog …
vba run [LineExporter]modMain.Main
Post questions about MicroStation programming to the MicroStation Programming Forum.