[MSCE C#] 如何在WPF或者在WINFORM中显示一个microstation cell的三维几何图形?

 类似这种效果

Parents
  • 1. 将如下代码保存为MyElementPreviewControl.cs

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using Bentley.GeometryNET;
    using Bentley.DgnPlatformNET;
    using Bentley.DgnPlatformNET.Elements;
    using Bentley.MstnPlatformNET.WinForms.Controls;
    using System.Windows.Forms;
    
    namespace MSAppNET
    {
        public class MyElementPreviewControl : ElementPreview
        {
            private DisplayableElement m_elem;
            private MSRenderMode m_renderMode;
            public MyElementPreviewControl(ref DisplayableElement elem, MSRenderMode renderMode)
            {
                m_elem = elem;
                m_renderMode = renderMode;
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
    
                ViewFlags myViewFlags = ViewInformation.GetDefaultFlags();
                myViewFlags.Camera = false;
                myViewFlags.RenderMode = (int)m_renderMode;
                myViewFlags.Grid = false;
                DRange3d elemRng;
                m_elem.CalcElementRange(out elemRng);
                DPoint3d myOrg = elemRng.Low;
                DPoint3d myRng = DPoint3d.Subtract(elemRng.High, elemRng.Low);
                Rectangle myRect = new Rectangle(Location, Size);
                DisplayElemHandle(BytesToIntptr(m_elem.ElementHandle), myViewFlags, myRect, null, myOrg, myRng);
            }
            public static IntPtr BytesToIntptr(byte[] bytes)
            {
                int size = bytes.Length;
                IntPtr buffer = Marshal.AllocHGlobal(size);
                Marshal.Copy(bytes, 0, buffer, size);
                return buffer;
            }
        }
    }
    

    2. 在你的Winform中添加一个Panel控件;

    3. 在你的WinForm.cs中大致如下写:

    public partial class MstnControlsForm : //Bentley.MstnPlatformNET.WinForms.Adapter
                                                System.Windows.Forms.Form
        {
            private MyElementPreviewControl myElemPreview;
    
            public MstnControlsForm()
            {
                InitializeComponent();
    
                DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                DPoint3d[] pts = { new DPoint3d(0, 0, 0), new DPoint3d(1000, 1000,0),
                                   new DPoint3d(2000, -1000,0), new DPoint3d(3000, 0, 0)};
                DVector3d sTangent = DVector3d.FromXY(1000, 1000);
                DVector3d eTangent = DVector3d.FromXY(2000 - 3000, -1000);
                MSBsplineCurve curve = MSBsplineCurve.CreateFromInterpolatePoints(pts, 3, true, ref sTangent, ref eTangent, true, 3);
                DisplayableElement myElem = new BSplineCurveElement(dgnModel, null, curve);
                //DisplayableElement myElem = dgnModel.FindElementById((ElementId)6645L) as DisplayableElement;
                if (null == myElem)
                {
                    MessageCenter.Instance.ShowErrorMessage("myElem is invalid", null, true);
                    return;
                }
                myElemPreview = new MyElementPreviewControl(ref myElem, MSRenderMode.Wireframe);
                myElemPreview.Dock = DockStyle.Fill;
                this.panel1.Controls.Add(myElemPreview);
            }

    4. 当然,我这里是在代码中临时创建的一个元素,你可以将Cell读入到元素中带入即可。

    5. MyElementPreviewControl仅仅演示了TopView的显示情况,你可以改造传入合适的RotMatrix。



    Answer Verified By: 鑫 杨 

Reply Children