MicroStation and Application Development

MicroStation® is a computer-aided-design (CAD) tool produced by Bentley Systems. It is used by engineers, architects, and draughting technicians to create 3D models and 2D drawings.

MicroStation can be customised. Bentley Systems themselves have many applications for specialised markets that use MicroStation as a platform. Third-parties likewise may use MicroStation as a platform on which to develop their application software. Bentley Systems provide the MicroStation Software Development Kit (SDK) that software developers use to create their applications.

The MicroStation SDK has three development libraries from 2023 …

This article concerns the DgnPlatformNet API. The DgnPlatformNet contains thousands of functions that provide a .NET interface.

This article provides a note about programming idioms used with the DgnPlatformNet. In particular, what we do when editing a DGN element.

A MicroStation DGN file is a container: it contains one or more DGN models. A DGN model is also a container: it contains DGN elements, reference attachments and other objects. Programmatically, we store a DGN element in a .NET Element.

EditElementRewriter

These are the steps we use to edit a DGN element …

  1. Read the element into an Element
    • Save the element's elementRef
  2. Modify the element
  3. Replace the element in its DGN model
    • Remember to supply the saved elementRef

The reason for using the saved elementRef is to help MicroStation's transaction logic. Using the saved elementRef lets MicroStation save the transaction for use with a subsequent Undo and Redo.

Thanks go to Jan Šlegr for posting that idiom to the Be Communities MicroStation Programming Forum.

Class Wraps Idiom

However, it's a hassle — a minor hassle, but nonetheless a hassle — to remember the above idiom when writing code. Being a lazy programmer, I wrote a class that helps to write the above idiom. It's not a large or complex class …

///    EditElementRewriter simplifies the task of rewriting an Element.
///    EditElementRewriter's constructor stores the elementRef of an Element when instantiated.
///    It subsequently uses that saved elementRef when it comes to ReplaceInModel().
class EditElementRewriter : Element
{
	///    ElementRef stored by the constructor.
	Element original;
	///    EditElementRewriter constructor stores the elementRef of the inherited Element.
	EditElementRewriter (ElementId id, DgnModelRef modelRef)
	:   Element (id, modelRef),
		original (GetNativeElementRef()) {}

	///    Use the saved original elementRef when rewriting the Element.
	StatusInt  Rewrite () { return ReplaceInModel (original); }
};

EditElementRewriter is an Element, so you can use it anywhere you need one. When first instantiated, it saves its internal elementRef in a member variable. When you call the Rewrite() method, it has that elementRef ready to go.

Usage couldn't be simpler …

void SomeFunctionThatModifiesAnElement (ElementId elementId, DgnModelRef model)
{
    EditElementRewriter  elementRewriter (elementId, model);
    …
    //  Modify the element
    …
    //	Save changes
    elementRewriter.Rewrite ();
}

Questions

Post questions about .NET to the MicroStation Programming Forum.