chr
2024-11-02 b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//**********************
//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");
        }
    }
}