//********************** //Hosting SOLIDWORKS eDrawings control in Windows Presentation Foundation (WPF) //Copyright(C) 2019 www.codestack.net //License: https://github.com/codestack-net-dev/solidworks-api-examples/blob/master/LICENSE //Product URL: https://www.codestack.net/edrawings-api/gettings-started/wpf/ //********************** using EModelView; using System; using System.Diagnostics; using System.IO; using System.Windows; using System.Windows.Controls; namespace PdmSwPlugin.MaterialSelect.EDrawing { public partial class EDrawingsHostControl : UserControl { private EModelViewControl m_Ctrl; public EDrawingsHostControl() { InitializeComponent(); EDrawingsHost ctrl = new EDrawingsHost() { Dock = System.Windows.Forms.DockStyle.Fill }; ctrl.ControlLoaded += OnControlLoaded; winHost.Child = ctrl; } public string FilePath { get { return (string)GetValue(FilePathProperty); } set { SetValue(FilePathProperty, value); } } public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(nameof(FilePath), typeof(string), typeof(EDrawingsHostControl), new FrameworkPropertyMetadata(OnFilePathPropertyChanged)); private static void OnFilePathPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as EDrawingsHostControl).OpenFile(e.NewValue as string); } private void OpenFile(string filePath) { if (m_Ctrl == null) { throw new NullReferenceException("eDrawings control is not loaded"); } if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) { m_Ctrl.CloseActiveDoc(""); } else { m_Ctrl.OpenDoc(filePath, false, false, true, ""); } } private void OnControlLoaded(EModelViewControl ctrl) { m_Ctrl = ctrl; m_Ctrl.OnFinishedLoadingDocument += OnFinishedLoadingDocument; m_Ctrl.OnFailedLoadingDocument += OnFailedLoadingDocument; } private void OnFailedLoadingDocument(string fileName, int errorCode, string errorString) { Trace.WriteLine($"{fileName} failed to loaded: {errorString}"); } private void OnFinishedLoadingDocument(string fileName) { Trace.WriteLine($"{fileName} loaded"); } } }