OpenRoads Designer CONNECT Edition SDK Help

Create your custom report and export to .csv

The below code creates a data for horizontal geometries, vertical geometries, corridors, and superelevation sections from current DGN file and exports it into the .csv file.

 //Required References
using System;
using System.Collections.Generic;
using Bentley.CifNET.SDK;
using Bentley.CifNET.GeometryModel.SDK;
using System.Diagnostics;
using System.IO;
using Bentley.CifNET.Formatting;

 public void CreateCustomReportAndExportToCSV()
        {
            try
            {
                //Path for .csv file
                string path = @"D:\CustomReport.csv";

                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path))
                {
                  
                    double defaultUnitsToMeters = FormatSettingsConstants.GetMasterUnitsToMeters();

                    //Get connection object
                    ConsensusConnection sdkCon = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
                    if (sdkCon == null)
                        return;

                    //Get active geometric model
                    GeometricModel geomModel = sdkCon.GetActiveGeometricModel();
                    if (geomModel == null)
                        return;

                    Dictionary<string, string> header = new Dictionary<string, string>();
                    sw.WriteLine("DGN File Name : " + geomModel.DgnModel.GetDgnFile().GetFileName());
                    sw.WriteLine("Model Name : " + geomModel.DgnModel.ModelName);

                    //Write Horizontal alignments data to file
                    sw.WriteLine("");
                    sw.WriteLine("Horizontal Alignments");

                    int alignmentCount = 0;
                    foreach (Alignment al in geomModel.Alignments)
                    {
                        if (!al.IsFinalElement) continue;
                        alignmentCount++;
                    }
                    if (alignmentCount > 0)
                    {
                        sw.WriteLine(string.Format("{0}|{1}|{2}|{3}", "Name", "Length", "StartPoint", "EndPoint"));
                    }
                    else
                    {
                        sw.WriteLine("No alignments found");
                    }

                    foreach (Alignment al in geomModel.Alignments)
                    {
                        if (!al.IsFinalElement) continue;

                        string alignmentName = string.IsNullOrEmpty(al.Name) ? "Unnamed" : al.Name;
                        string length = FormatForDisplay.Double(al.LinearGeometry.Length / defaultUnitsToMeters);

                        string startX = FormatForDisplay.Double(al.LinearGeometry.StartPoint.Coordinates.X / defaultUnitsToMeters);
                        string startY = FormatForDisplay.Double(al.LinearGeometry.StartPoint.Coordinates.Y / defaultUnitsToMeters);
                        string startZ = FormatForDisplay.Double(al.LinearGeometry.StartPoint.Coordinates.Z / defaultUnitsToMeters);
                        string startPoint = startX + ":" + startY + ":" + startZ;

                        string endX = FormatForDisplay.Double(al.LinearGeometry.EndPoint.Coordinates.X / defaultUnitsToMeters);
                        string endY = FormatForDisplay.Double(al.LinearGeometry.EndPoint.Coordinates.Y / defaultUnitsToMeters);
                        string endZ = FormatForDisplay.Double(al.LinearGeometry.EndPoint.Coordinates.Z / defaultUnitsToMeters);
                        string endPoint = endX + ":" + endY + ":" + endZ;

                        sw.WriteLine(string.Format("{0}|{1}|{2}|{3}", alignmentName, length, startPoint, endPoint));

                    }

                    //Write Vertical alignments data to file
                    sw.WriteLine("");
                    sw.WriteLine("Vertical Alignments");

                    int profileCount = 0;
                    foreach (Alignment al in geomModel.Alignments)
                    {
                        if (!al.IsFinalElement) continue;

                        foreach (Profile profile in al.Profiles)
                        {
                            if (!profile.IsFinalElement) continue;
                            profileCount++;
                        }
                    }
                    if (profileCount > 0)
                    {
                        sw.WriteLine(string.Format("{0}|{1}|{2}|{3}", "Name", "Length", "StartPoint", "EndPoint"));
                    }
                    else
                    {
                        sw.WriteLine("No profiles found");
                    }


                    foreach (Alignment al in geomModel.Alignments)
                    {
                        if (!al.IsFinalElement) continue;

                        foreach (Profile profile in al.Profiles)
                        {
                            if (!profile.IsFinalElement) continue;

                            string profileName = string.IsNullOrEmpty(profile.Name) ? "Unnamed" : profile.Name;
                            string length = FormatForDisplay.Double(profile.ProfileGeometry.Length / defaultUnitsToMeters);

                            string startX = FormatForDisplay.Double(profile.ProfileGeometry.StartPoint.Coordinates.X / defaultUnitsToMeters);
                            string startY = FormatForDisplay.Double(profile.ProfileGeometry.StartPoint.Coordinates.Y / defaultUnitsToMeters);
                            string startZ = FormatForDisplay.Double(profile.ProfileGeometry.StartPoint.Coordinates.Z / defaultUnitsToMeters);
                            string startPoint = startX + ":" + startY + ":" + startZ;

                            string endX = FormatForDisplay.Double(profile.ProfileGeometry.EndPoint.Coordinates.X / defaultUnitsToMeters);
                            string endY = FormatForDisplay.Double(profile.ProfileGeometry.EndPoint.Coordinates.Y / defaultUnitsToMeters);
                            string endZ = FormatForDisplay.Double(profile.ProfileGeometry.EndPoint.Coordinates.Z / defaultUnitsToMeters);
                            string endPoint = endX + ":" + endY + ":" + endZ;

                            sw.WriteLine(string.Format("{0}|{1}|{2}|{3}", profileName, length, startPoint, endPoint));
                        }
                    }

                    //Write Corridors data to file
                    sw.WriteLine("");
                    sw.WriteLine("Corridors");

                    int corridorsCount = 0;
                    foreach (Corridor corridor in geomModel.Corridors)
                    {
                        corridorsCount++;
                    }
                    if (corridorsCount > 0)
                    {
                        sw.WriteLine(string.Format("{0}|{1}|{2}", "Name", "StartDistance", "StopDistance"));
                    }
                    else
                    {
                        sw.WriteLine("No corridors found");
                    }

                    foreach (Corridor corridor in geomModel.Corridors)
                    {
                        string corridorName = string.IsNullOrEmpty(corridor.Name) ? "Unnamed" : corridor.Name;
                        string startDistance = FormatForDisplay.Double(corridor.StartDistance / defaultUnitsToMeters);
                        string stopDistance = FormatForDisplay.Double(corridor.EndDistance / defaultUnitsToMeters);
                        sw.WriteLine(string.Format("{0}|{1}|{2}", corridorName, startDistance, stopDistance));
                    }

                    //Write SuperElevationSections data to file
                    sw.WriteLine("");
                    sw.WriteLine("SuperElevationSections");

                    int superElevationSectionsCount = 0;
                    foreach (SuperElevationSection seSection in geomModel.SuperElevationSections)
                    {
                        superElevationSectionsCount++;
                    }
                    if (superElevationSectionsCount > 0)
                    {
                        sw.WriteLine(string.Format("{0}|{1}|{2}|{3}|{4}", "Name", "StartDistance", "StopDistance", "RuleFile", "SuperElevationCount"));
                    }
                    else
                    {
                        sw.WriteLine("No Superelevation sections found");
                    }

                    foreach (SuperElevationSection seSection in geomModel.SuperElevationSections)
                    {
                        string seSectionName = string.IsNullOrEmpty(seSection.Name) ? "Unnamed" : seSection.Name;
                        string startDistance = FormatForDisplay.Double(seSection.StartDistance / defaultUnitsToMeters);
                        string stopDistance = FormatForDisplay.Double(seSection.EndDistance / defaultUnitsToMeters);
                        string ruleFileName = string.IsNullOrEmpty(seSection.RuleFile) ? "Unnamed" : seSection.RuleFile;
                        int superElevationsCount = 0;
                        foreach (SuperElevation superElevation in seSection.SuperElevations)
                        {
                            superElevationsCount++;
                        }

                        sw.WriteLine(string.Format("{0}|{1}|{2}|{3}|{4}", seSectionName, startDistance, stopDistance, ruleFileName, superElevationsCount));
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                return;
            }
        }