OpenRoads Designer CONNECT Edition SDK Help

Foresight point creator

Description

  • This is a custom interactive tool for creating the civil point using distance from origin, angle and existing civil points feature definition .
  • The user is prompted for two civil points as Occupied point and Backsight point which user needs to select from UI .
  • Hard coded values are used for angle and distance. The feature definition is first picked from list of existing point's feature names.
  • The ForesightPointCreator class which extends DgnElementSetTool which handles different events to interact with UI, the ForesightPointCreator class overrides the events here in this tool

Note: Use OpenRoads Designer and SDK version 10.12 or above

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"
  • The method OnDataButton() handles the point selection from User interface.

Source Code

using System;
using System.Collections.Generic;
using Bentley.CifNET.SDK.Edit;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.GeometryNET;
using Bentley.CifNET.GeometryModel.SDK.Edit;
using Bentley.CifNET.Formatting;

namespace ExampleSnippetAddIn.Examples
{
    class ForesightPointCreator : DgnElementSetTool
    {
        enum State
        {
            Occupied = 0,
            BackSight,
        }

        internal static DgnModel m_activeModel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
        internal static double m_defaultUnitsToMeters = FormatSettingsConstants.GetMasterUnitsToMeters();

        State m_state = State.Occupied;
        PointEntity2dInPlan m_occupied, m_backsight;
        string m_featureDefinition;
        double m_angle = 0.0;
        double m_distance = 0.0;

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

        protected override void ExitTool()
        {
            base.ExitTool();
        }

        protected override void OnPostInstall()
        {
            base.BeginPickElements();
            Bentley.DgnPlatformNET.AccuSnap.LocateEnabled = true;
            Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
            ConsensusConnection con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
            if (con == null)
                return;
            base.OnPostInstall();
            NotificationManager.OutputPrompt("Select Occupied Point.");
            BeginDynamics();
        }

        protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 0);
            if (hitPath == null)
                return false;

            Element el = hitPath.GetCursorElement();
            if (el == null)
                return false;

            ConsensusConnection con = ConsensusConnectionEdit.GetActive();
            if (con == null)
                return false;


            PointEntity2dInPlan ptEntFromElm = null;
            PointEntity2dInPlan pointEntity2d = null;
            try
            {
                ptEntFromElm = PointEntity2dInPlan.CreateFromElement(con, el);
                pointEntity2d = (el.ParentElement == null) ? PointEntity2dInPlan.CreateFromElement(con, el) : PointEntity2dInPlan.CreateFromElement(con, el.ParentElement);
            }
            catch (Exception exc)
            {
                NotificationManager.OutputPrompt("Exception: " + exc.Message);
                return false;
            }

            if (ptEntFromElm == null)
                return false;
            if (pointEntity2d == null)
                return false;

            switch (m_state)
            {
                case State.Occupied:
                    m_occupied = pointEntity2d;
                    NotificationManager.OutputPrompt("Select Backsight Point.");
                    m_state = State.BackSight;
                    break;
                case State.BackSight:
                    m_backsight = pointEntity2d;

                    DefinePoint();
                    NotificationManager.OutputPrompt("Foresight Point created.");
                    //InstallNewInstance();

                    break;
                default:
                    break;
            }
            return true;
        }
        private void DefinePoint()
        {

            ConsensusConnectionEdit con = ConsensusConnectionEdit.GetActive();
            IEnumerable<Bentley.CifNET.GeometryModel.SDK.GeometricModel> geometricModels = con.GetAllGeometricModels();
            foreach (Bentley.CifNET.GeometryModel.SDK.GeometricModel gm in geometricModels)
            {
                foreach (Bentley.CifNET.GeometryModel.SDK.FeatureDefinition feature in gm.FeatureDefinitions)
                {
                    Bentley.CifNET.ContentManagementModel.ObjectSettings obs = feature.DomainObject as Bentley.CifNET.ContentManagementModel.ObjectSettings;
                    if (obs.Name.Contains("Point\\"))
                    {
                        m_featureDefinition = obs.Name;
                        break;
                    }

                }
            }
            GetAngle();
            GetDistance();
            CreatePoint();
        }

        protected override bool OnPostLocate(HitPath path, out string cantAcceptReason)
        {
            if (path == null)
            {
                cantAcceptReason = "HitPath is null.";
                return false;
            }

            Element element = path.GetCursorElement();
            if (element == null)
            {
                cantAcceptReason = "There is no element at cursor.";
                return false;
            }

            if (element.ElementType == MSElementType.Line)
            {
                cantAcceptReason = "This is not a Civil 2d point.";
                return false;
            }

            Bentley.CifNET.SDK.ConsensusConnection con = ConsensusConnectionEdit.GetActive();
            if (con == null)
            {
                cantAcceptReason = "There was an error connecting to the Civil SDK model.";
                return false;
            }

            PointEntity2dInPlan pointEntity2d = null;
            try
            {
                if (element.ParentElement == null)
                    pointEntity2d = PointEntity2dInPlan.CreateFromElement(con, element);
                else
                    pointEntity2d = PointEntity2dInPlan.CreateFromElement(con, element.ParentElement);
            }
            catch (Exception exc)
            {
                cantAcceptReason = "Exception: " + exc.Message;
                return false;
            }

            if (pointEntity2d == null)
            {
                cantAcceptReason = "This is not a Civil 2d point.";
                return false;
            }

            if (m_state == State.BackSight && m_occupied == pointEntity2d)
            {
                cantAcceptReason = "Occupied Point cannot also be Backsight Point.";
                return false;
            }

            cantAcceptReason = System.String.Empty;
            return true;
        }

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

        protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            ExitTool();
            return true;
        }

        public static void InstallNewInstance()
        {
            ForesightPointCreator tool = new ForesightPointCreator();
            tool.InstallTool();
        }

        //Helper functions 
        private DPoint3d ComputeDPoint3d()
        {
            double y, x, bx, by;
            Bentley.DgnPlatformNET.ModelInfo info = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel().GetModelInfo();
            DVector3d occupiedToBacksight = new DVector3d(m_occupied.Coordinates, m_backsight.Coordinates);
            y = Convert.ToDouble(ConvertMeterToMaster(m_distance * Math.Sin(occupiedToBacksight.AngleXY.Radians - m_angle)));
            x = Convert.ToDouble(ConvertMeterToMaster(m_distance * Math.Cos(occupiedToBacksight.AngleXY.Radians - m_angle)));

            bx = Convert.ToDouble(ConvertMeterToMaster(m_occupied.Coordinates.X / info.UorPerMeter));
            by = Convert.ToDouble(ConvertMeterToMaster(m_occupied.Coordinates.Y / info.UorPerMeter));

            DPoint3d dpoint3d = new DPoint3d(ConvertMasterToMeter(x + bx), ConvertMasterToMeter(y + by));
            return dpoint3d;
        }
        private void CreatePoint()
        {
            DPoint3d dpoint3d = ComputeDPoint3d();
            ConsensusConnectionEdit con = ConsensusConnectionEdit.GetActive();

            con.StartTransientMode();
            PointEntity2dInPlanEdit point = PointEntity2dInPlanEdit.CreateByDPoint3d(con, dpoint3d);

            if (m_featureDefinition != null && m_featureDefinition != string.Empty)
            {
                point.SetFeatureDefinition(m_featureDefinition);
            }
            point.SetDetails("NewCivilPoint", "Pt");
            con.PersistTransients();
        }
        public static double ConvertMeterToMaster(double num)
        {
            return num / m_defaultUnitsToMeters;
        }
        public static double ConvertMasterToMeter(double num)
        {
            return num * m_defaultUnitsToMeters;
        }

        private void GetAngle()
        {
            double angle = 0;
            try
            {
                angle = (double)Bentley.CifNET.Formatting.FormattingProvidersManager.Instance[Bentley.CifNET.Formatting.ECAngleTypeConverter.FormatGuid].Parse("0.0", 0);

            }
            catch
            {
            }
            m_angle = angle;

        }
        private void GetDistance()
        {
            m_distance = (double)Bentley.CifNET.Formatting.FormattingProvidersManager.Instance[Bentley.CifNET.Formatting.ECDistanceTypeConverter.FormatGuid].Parse("100", 0);
        }
    }
}