extern alias DgnPlatformNetAli;
extern alias DDUI;

using DgnPlatformNetAli::Bentley.DgnPlatformNET;
using DgnPlatformNetAli::Bentley.DgnPlatformNET.Elements;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AddinsOri
{
    public partial class FormForTestUIcs : Form
    {
        public FormForTestUIcs()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //获取当前DgnModel
            DgnModel dgnmodel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
            try
            {
                //构造元素集
                ElementAgenda showElements = new ElementAgenda();
                //元素集来源为选择
                showElements.Source = ModifyElementSource.Selected;
                //获取当前DgnModel所有图形元素
                ModelElementsCollection elements = dgnmodel.GetGraphicElements();
                //遍历所有图形元素
                IEnumerator<Element> allelement = elements.GetEnumerator();
                while (allelement.MoveNext())
                {
                    showElements.Insert(allelement.Current, false);
                }
                //获取当前DgnModel的IntPtr
                IntPtr modelPtr = dgnmodel.GetNative();
                unsafe
                {
                    //构造PreviewControl,注意此处DDUI的使用
                    DDUI.Bentley.DgnPlatform.PreviewControl pc = new DDUI.Bentley.DgnPlatform.PreviewControl((DDUI.Bentley.DgnPlatform.DgnModel*)modelPtr.ToPointer());
                    this.elementHost1.Child = pc;
                    pc.ShowPreview(showElements);
                    pc.Loaded += Pc_Loaded;

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message + ":" + ex.StackTrace);
            }
        }

        private void Pc_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            DDUI.Bentley.DgnPlatform.PreviewControl pc = sender as DDUI.Bentley.DgnPlatform.PreviewControl;
            if(null!=pc)
            {
                pc.FitToView();
                pc.Rotate(DDUI.Bentley.DgnPlatform.RotationMode.Isometric);
                pc.SetRenderMode(DDUI.Bentley.DgnPlatform.RenderMode.WireFrame);
            }
        }
    }
}
