Q How can I create a MicroStation color table from a set of RGB values?
Q How can I create a MicroStation color table from a CSV file of RGB values?
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 creating a MicroStation color table. It reads a CSV file of RGB colours and creates a MicroStation Color Table.
MicroStation users sometimes want something different from the set of tools provided in the product. The Color Table is familiar to all MicroStation users. It provides 255 colours made up of red-green-blue (RGB) triplets.
Sometimes the default Color Table is not enough, and a user wants to substitute her own table.
MicroStation provides tools to attach a Color Table.
The tools presuppose that you have a Color Table in the correct (.tbl
) format.
But maybe you want to match the Color Table to a set of RGB values defined externally.
There's no provision in MicroStation to read a list of RGB values to create a new Color Table.
The VBA project decribed here overcomes that omission. The app reads a CSV file and, if the format matches our expectations, creates a MicroStation Color Table in the active DGN file.
The question originally posted on Bentley Discussion Groups showed this data (I've abbreviated the list because you don't want to read 255 RGB values, do you?) …
1 255 0 0 2 255 255 0 3 0 255 0 … 253 186 186 186 254 220 220 220 255 0 0 0
I converted that list manually to the well-known CSV format. Now it looks like this (the first line is a comment) …
#Index, R, G, B 1,255,0,0 2,255,255,0 3,0,255,0 … 254,220,220,220 255,0,0,0
It's straightforward to parse a CSV file and create colour objects.
I wrote VBA class clsColor
to store RGB data and handle conversion from the
text supplied by the CSV file and conversion to a Long
value used in a MicroStation Color Table.
The CSV reader references the Scripting.FileSystem
object,
which supplies the useful TextStream
class.
The macro stores a set of clsColor
objects in a dictionary keyed by the colour index.
The dictionary is supplied by the Microsoft Scripting Runtime
utility.
Using a dictionary makes it easy to check for duplicate indices.
The app's dialog (VBA UserForm
) prompts user to locate a color CSV file, using the Browse button.
When user clicks the Import Colour Table button, the macro reads the file and attempt to convert its
contents into a collection of RBG colour triplets and the Color Table index of each.
The macro checks the CSV file for valid entries — it expects to receive 255 lines. Each line must have four fields …
Long
) in range 1..255
Byte
)
Byte
)
Byte
)
If the CSV file is parsed successfully, the macro converts the set of values into a
MicroStation VBA ColorTable
object.
Finally, it attaches the new table to MicroStation.
Note: the existing MicroStation color table will be overwritten! If you want to re-use the existing color table, save it before running this macro. You can re-attach it later, using MicroStation's built-in tools.
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 reader.
It uses the Scripting.Dictionary
object when it builds a collection of
RGB values.
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 ColorTableCsvImporter.mvba
to a location where MicroStation
can find it.
A good place to copy it would be \Workspace\Standards\vba
.
To create a new color table, type the following into MicroStation's key-in dialog …
vba run [ColorTableCsvImporter]modMain.Main
Post questions about MicroStation programming to the MicroStation Programming Forum.