OpenRoads Designer CONNECT Edition SDK Help

Corridor items complex alignment creator

Description

  • This is a custom interactive tool for creating alignment using multi points.

  • The user places data points, and then right-clicks to complete the command which creates a complex horizontal alignment with multi straight lines.

  • The CorridorItemsComplexAlignmentCreator class which extends DgnElementSetTool which handles different events to interact with UI, the CorridorItemsComplexAlignmentCreator class overrides the events here in this tool .

Remarks

  • This sample code is a part of ManagedSDKExample which you get with SDK installation under "examples" section in SDK installation directory.

  • If you encounter any error while using DgnElementSetTool class, make sure to add a reference to Bentley.DgnDisplayNet.dll by selecting Project > Add Reference or change the projects .csproj file to add reference to this dll .

  • The default dll location will be "C:\Program Files\Bentley\OpenRoads Designer CE 10.11\OpenRoadsDesigner\Bentley.DgnDisplayNet.dll".

Source Code


//Required References
using System.Collections.Generic;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.GeometryNET;
using Bentley.CifNET.LinearGeometry;
using BCGMSDK = Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.GeometryModel.SDK.Edit;
using Bentley.CifNET.GeometryModel.SDK;

namespace ManagedSDKExample
{
    class CorridorItemsComplexAlignmentCreator : DgnElementSetTool
    {
        private IList<DPoint3d> mInputPoints = new List<DPoint3d>();
        /*----------------------------------------------------------------------------------------------**/
        /* Write Function | The user is prompted for multi data points. A complex horizontal alignment
         *                  is then created from code that contains multi straight line.
        /*--------------+---------------+---------------+---------------+---------------+----------------*/
        protected override void OnPostInstall()
        {
            NotificationManager.OutputPrompt("Select first data point.");
        }
        public static void InstallNewInstance()
        {
            CorridorItemsComplexAlignmentCreator tool = new CorridorItemsComplexAlignmentCreator();
            tool.InstallTool();
        }

        protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            mInputPoints.Add(ev.Point);

            if (mInputPoints.Count == 0)
            {
                NotificationManager.OutputPrompt("Select second data point.");
            }
            else if (mInputPoints.Count == 1)
            {
                BeginDynamics();
            }
            else
            {
                NotificationManager.OutputPrompt("Select next data point or right click to complete the command.");
            }

            return true;
        }

        protected override bool OnResetButton(DgnButtonEvent ev)
        {
            EndDynamics();
            CreateAlignment();
            mInputPoints.Clear();
            NotificationManager.OutputPrompt("Command complete. Select first data point or pick element selection tool to exit command.");
            return true;
        }

        protected override void OnRestartTool()
        {
            InstallNewInstance();
        }

        public override StatusInt OnElementModify(Element element)
        {
            return Bentley.DgnPlatformNET.StatusInt.Error;
        }

        protected override void OnDynamicFrame(DgnButtonEvent ev)
        {
            if (DynamicsStarted && mInputPoints.Count > 0)
            {
                DPoint3d stoppoint = ev.Point;
                DgnModel model = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
                var redraw = new RedrawElems();
                redraw.DrawMode = DgnDrawMode.TempDraw;
                redraw.DrawPurpose = DrawPurpose.Dynamics;
                redraw.SetDynamicsViewsFromActiveViewSet(ev.Viewport);

                Bentley.DgnPlatformNET.Elements.ComplexStringElement polyLine = new ComplexStringElement(model, null);
                for (int i = 1; i < mInputPoints.Count; i++)
                {
                    DSegment3d seg_ = new DSegment3d(mInputPoints[i - 1], mInputPoints[i]);
                    LineElement line_ = new LineElement(model, null, seg_);
                    polyLine.AddComponentElement(line_);
                }

                DSegment3d seg = new DSegment3d(mInputPoints[mInputPoints.Count - 1], stoppoint);
                LineElement line = new LineElement(model, null, seg);
                polyLine.AddComponentElement(line);
                polyLine.AddComponentComplete();

                redraw.DoRedraw(polyLine);
            }
        }

        protected override bool WantDynamics()
        {
            return base.WantDynamics();
        }

        private void CreateAlignment()
        {
            if (mInputPoints.Count < 2)
            {
                return;
            }

            Bentley.DgnPlatformNET.DgnModel currentmodel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
            double UPM = currentmodel.GetModelInfo().UorPerMeter;

            List<LinearElement> lines = new List<LinearElement>();
            DPoint3d startPoint = new DPoint3d(mInputPoints[0]);
            startPoint.X /= UPM;
            startPoint.Y /= UPM;
            startPoint.Z /= UPM;
            for (int i = 1; i < mInputPoints.Count; i++)
            {
                DPoint3d endPoint = new DPoint3d(mInputPoints[i]);
                endPoint.X /= UPM;
                endPoint.Y /= UPM;
                endPoint.Z /= UPM;
                Line line = Line.Create1(startPoint, endPoint);
                lines.Add(line);
                startPoint = endPoint;
            }

            Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();

            GeometricModel gm = con.GetOrCreateGeometricModel();

            if (gm == null)
            {
                con.Close();
                con.Dispose();
                return;
            }

            con.StartTransientMode();
            BCGMSDK.Alignment alignmentEdit = gm.CreateAlignmentByLinearElements(lines);
            con.PersistTransients();
        }
    }

}