Questions similar to this appear on the MicroStation Programming Forum. This problem appeared in the VBA discussion group.
Q How do I create a shape element using C#?
A
Create a class module that inherits the DgnPrimitiveTool
interface. Use a member variable
to store an elastic array of DPoint3d
: GPArray
or List<DPoint3d>
.
A GPArray
or List<>
knows its size, so you don't have to store the point count in a variable.
With those pieces of data, you have enough information to display rubber banding as the user places datapoints.
A legacy approach, following its MDL ancestry, creates a ShapeElement
or LineStringElement
explicitly.
In the example code, the CreateShapeTool
follows that idiom.
A 21st century approach uses a GPArray
to accumulate datapoints.
The CreateCurveVectorShapeTool
uses that implicit element creation idiom: you just let the DgnPlatformNet classes do the thinking for you.
When user indicates she has finished (with a reset), we create a DgnCurveVector
from the points.
Because we want a closed shape, we set the curve's boundary type to CurveVector.BoundaryType.Outer
.
Finally we invoke DraftingElementSchema.ToElement
to create an element from the DgnCurveVector
.
We don't specify an element type, because we don't have to.
Those DgnPlatformNet classes do whatever they have to to create an element: in this case a ShapeElement
or ComplexShapeElement
.
private Element MakeShape(GPArray points, DgnModel model) { DgnCurveVector curves = (DgnCurveVector)points.ToCurveVector(); curves.SetBoundaryType((int)CurveVector.BoundaryType.Outer); return DraftingElementSchema.ToElement(model, curves, null); }
The example CreateShape
class uses the DgnPrimitiveTool
interface by providing
a number of methods that MicroStation calls when certain events happen. Events that are interesting in this example are …
Initialise our variables and issue a prompt to the user.
Store the DPoint3d
in our list (List<DPoint3d>
) of vertices.
There's no need to store the point count, because a List<>
knows its own size.
If the operator has placed the first point, then draw a line from that point to the current cursor position.
If the operator has place more than one point, create a temporary shape from our list of vertices and the current cursor position.
Each time the user moves the mouse, MicroStation calls this subroutine to draw a new element. The element is assigned the active settings.
This example provides key-ins to create a line or shape using DgnCurveVector
and GPArray
, and others to create a legacy shape or line-string …
CREATE PRIMITIVE LINE
CREATE PRIMITIVE SHAPE
CREATE CURVEVECTOR LINE
CREATE CURVEVECTOR SHAPE
The CreateShape C# Project is available in this ZIP file.
Unpack the ZIP file and put CreatePrimitive.dll
somewhere where MicroStation can find it, such as
C:\Program Files\Bentley\MicroStation\mdlapps
(or wherever your MS_MDLAPPS
configuration variable is pointing).
You can run the sample using the keyin …
mdl load CreatePrimitive
See this article about how to create a shape with VBA.
If you're interested in moving from C# to C++, then the MicroStationAPI will help. The MicroStationAPI arrived with MicroStation CONNECT, and is a first-class C++ interface. It's a development and fusion of the MicroStation Development Library (MDL) and the MicroStationAPI provided with the SDK of MicroStation V8i.
See this article about how to create a shape with C++ using the MicroStationAPI.
Post questions about MicroStation programming to the MicroStation Programming Forum.