Dgn

Namespace: Digi21.DigiNG.IO.Dgn
Assembly: Digi21.DigiNG.IO.Dgn

Drawing file in MicroStation V8 DGN format (.dgn). Reads and writes lines, points, text, polygons and complexes using the same engine as Digi3D.AI's DGNv8 files importer/exporter.

public class Dgn : IDrawingFile, IDisposable

Implements: IDrawingFile, which in turn implements IReadOnlyDrawingFile, IEnumerable<Entity> and IWindow3D.

Each entity's code is obtained from the code table, which translates DGN element attributes (level, color, style, weight, cell) into a Digi3D.AI code. An element with no translation in the table receives a code using the format in DgnParameters.FormatForUnknownCodes.

Entities are read from the file as they are iterated with foreach. Changes (Add and Delete) are written to disk when Dispose is called, and only if the file was not opened read-only and has been modified. If writing fails, the error is not reported.

Constructors

public Dgn(string path, DgnParameters parameters)
public Dgn(string path, DgnParameters parameters, bool readOnly)
public Dgn(string path, DgnParameters parameters, DelegateGetWkt GetWkt)
public Dgn(string path, DgnParameters parameters, DelegateGetWkt GetWkt, bool readOnly)
public Dgn(string path, DgnParameters parameters, DigiTab digiTab)
public Dgn(string path, DgnParameters parameters, DigiTab digiTab, bool readOnly)
public Dgn(string path, DgnParameters parameters, DigiTab digiTab, DelegateGetWkt GetWkt)
public Dgn(string path, DgnParameters parameters, DigiTab digiTab, DelegateGetWkt GetWkt, bool readOnly)

Opens the file path. Overloads without readOnly open it for reading and writing.

Parameter Description
path .dgn file path.
parameters DgnParameters with the template, cell file, code criterion, and cell and curve conversion.
GetWkt DelegateGetWkt delegate returning the coordinate reference system's WKT. Called only if the .prj file does not exist.
digiTab Code table that translates the file's elements. Without this parameter, an empty code table is used.
readOnly true to open the file read-only.

If path does not exist and parameters.TemplateFile is an existing file, the template is copied to path and the copy is opened. If path does not exist and there is no template, a new 3D drawing is created in memory with meters as the master unit and millimeters as the subunit. This drawing is written to disk when Dispose is called, only if an entity has been added.

The file's coordinate reference system is obtained as follows:

  1. If the .prj file with the same name as the .dgn exists, its first line is read.
  2. If it does not exist and there is a GetWkt delegate, the text returned by the delegate is used.
  3. If it does not exist and there is no delegate, a local (unknown) system is used.

In cases 2 and 3, if the file is not opened read-only, the .prj file is created with that WKT.

Constructors throw ArgumentOutOfRangeException if parameters.CodeCriteria is not a value of DgnCodeCriteria, and Exception, with the error message, if the .dgn or .prj file cannot be opened or created.

Warnings arising inside the constructor are reported through the TaskAdded event before the constructor finishes. A handler subscribing after the Dgn is created does not receive those warnings.

Properties

Property Description
string Path File path.
bool CanRead true if the import engine allows the file to be read.
bool CanWrite false if the file was opened read-only.
string Wkt WKT of the file's coordinate reference system.
string ConnectionString Connection string for the database associated with the file, or an empty string if the file has no database.
IDictionary<string, int> DatabaseTables Tables in the database associated with the file: table name and table number. Empty if the file has no database.
bool Visible File visibility in Digi3D.AI. Does not affect reading or writing.
double? Xmin, double Ymin, double Zmin, double Xmax, double Ymax, double Zmax Minimum and maximum coordinates of the file's entities. Xmin is null if Valid is false. The set accessor on all six properties throws InvalidOperationException.
bool Valid true if the minimum and maximum coordinates are valid.
double Width, double Height, double Depth Xmax - Xmin, Ymax - Ymin and Zmax - Zmin.
Point3D Center Center of the box defined by the minimum and maximum coordinates.
Point3D NW, N, NE, E, SE, S, SW, W Corners and edge midpoints of the box bounded by the minimum and maximum coordinates in the X, Y plane. The Z of all eight points is Zmin.

Methods

Method Description
IEnumerator<Entity> GetEnumerator() Returns the enumerator for the entities in the file. Can only be obtained once per Dgn: the second call throws InvalidOperationException. Entities returned by GetEnumerator() are read-only (ReadOnlyLine, ReadOnlyPoint, ReadOnlyText, ReadOnlyPolygon or ReadOnlyComplex) and each entity's Owner property is the file represented by this Dgn object.
void Add(Entity entity) Adds the entity to the file. Before adding it, copies attributes from entity.Attributes whose values are not null to the native entity. If the format does not support the geometry, the entity is not written. After the call, entity.Owner is this file.
void Add(IEnumerable<Entity> entities) Adds entities in enumeration order. Unlike Add(Entity), does not copy attributes. If an entity belongs to another file, throws the exception on reaching that entity: the previous entities have already been added.
ReadOnlyLine Add(Line line)
ReadOnlyPoint Add(Point point)
ReadOnlyText Add(Text text)
ReadOnlyPolygon Add(Polygon polygon)
ReadOnlyComplex Add(Complex complex)
Call Add(Entity) and return the same instance with the read-only type.
void Delete(Entity entity) Marks the entity as deleted and stores it in the file again.
void Delete(IEnumerable<Entity> entities) Calls Delete(Entity) for each entity.
int IndexOf(Entity entity) Position of the entity among the file's entities, or -1 if it does not belong to this file or cannot be found.
void Dispose() Writes changes to disk and closes the file.

Exceptions from Add and Delete:

Exception When
ArgumentNullException entity or entities is null. Delete(IEnumerable<Entity>) does not check whether entities is null.
InvalidOperationException The file was opened read-only, or the entity belongs to another file (its Owner is another file). To copy an entity from one file to another, add the copy returned by Clone().

Events

Event Description
ProgressEventHandler ProgressChanged Percentage of a lengthy operation, in ProgressEventArgs.Progress.
ResultsAddedEventHandler ResultsAdded Informational text, in ResultsAddedEventArgs.Message.
TaskAddedEventHandler TaskAdded Warning or error, in TaskAddedEventArgs.Task: an ITask with Title, Severity (Error, Warning or Message), DrawingFile and Module. For example, a cell that is not in the cell file.

Example

This program copies non-deleted lines from a .bin file to a new .dgn file, with the specified code table and the .bin file's coordinate reference system:

using Digi21.DigiNG.DigiTab;
using Digi21.DigiNG.Entities;
using Digi21.DigiNG.IO.Bin;
using Digi21.DigiNG.IO.Dgn;
using Digi21.Math;
using Digi21.Tasks;

if (args.Length != 3)
{
    Console.WriteLine("Uso: BinADgn <entrada.bin> <salida.dgn> <tabla de códigos>");
    return 1;
}

void MuestraAviso(object sender, TaskAddedEventArgs e) =>
    Console.WriteLine($"{e.Task.Severity}: {e.Task.Title}");

using var digiTab = DigiTab.Load(args[2]);
using var entrada = new Bin(args[0], 2, new Point3D(0, 0, 0), digiTab, true);

var parámetros = new DgnParameters
{
    BlockCriteria = DgnBlockCriteria.Point,
    SplineIncrement = 1.0,
    FormatForUnknownCodes = "$(Level)"
};

// Changes are written to the .dgn file when the using block ends.
using (var salida = new Dgn(args[1], parámetros, digiTab, () => entrada.Wkt))
{
    salida.TaskAdded += MuestraAviso;

    foreach (var entidad in entrada)
    {
        if (entidad.Deleted || entidad is not ReadOnlyLine línea)
            continue;

        salida.Add(línea.Clone());
    }
}

Console.WriteLine("Archivo creado.");
return 0;