-
Notifications
You must be signed in to change notification settings - Fork 28
/
AddIn.cs
78 lines (64 loc) · 2.07 KB
/
AddIn.cs
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
78
using System;
using System.IO;
using System.Runtime.InteropServices;
using Xarial.XCad;
using Xarial.XCad.Data;
using Xarial.XCad.Documents;
using Xarial.XCad.Documents.Enums;
using Xarial.XCad.Documents.Services;
using Xarial.XCad.Documents.Structures;
using Xarial.XCad.SolidWorks;
using Xarial.XCad.SolidWorks.Documents;
namespace PropertyAsFileName
{
public class SaveDocHandler : IDocumentHandler
{
private const string FILE_NAME_PRP = "Title";
private IXDocument m_Model;
public void Init(IXApplication app, IXDocument model)
{
m_Model = model;
m_Model.Saving += OnModelSaving;
}
private void OnModelSaving(IXDocument doc, DocumentSaveType_e type, DocumentSaveArgs args)
{
if (type == DocumentSaveType_e.SaveAs)
{
var tempDir = Path.GetTempPath();
var ext = Path.GetExtension(args.FileName);
IXProperty titlePrp = null;
if (doc is ISwDocument3D)
{
titlePrp = (doc as ISwDocument3D).Configurations.Active.Properties.GetOrPreCreate(FILE_NAME_PRP);
}
if (titlePrp == null || !titlePrp.Exists())
{
titlePrp = doc.Properties.GetOrPreCreate(FILE_NAME_PRP);
}
var prpVal = "";
if (titlePrp.Exists())
{
prpVal = titlePrp.Value?.ToString();
}
if (string.IsNullOrEmpty(prpVal))
{
prpVal = Guid.NewGuid().ToString();
}
var destFile = Path.Combine(tempDir, prpVal + ext);
args.FileName = destFile;
}
}
public void Dispose()
{
m_Model.Saving -= OnModelSaving;
}
}
[ComVisible(true)]
public class AddIn : SwAddInEx
{
public override void OnConnect()
{
Application.Documents.RegisterHandler<SaveDocHandler>();
}
}
}