-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSwSketchEntityCollection.cs
176 lines (136 loc) · 4.89 KB
/
SwSketchEntityCollection.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//*********************************************************************
//xCAD
//Copyright(C) 2021 Xarial Pty Limited
//Product URL: https://www.xcad.net
//License: https://xcad.xarial.com/license/
//*********************************************************************
using SolidWorks.Interop.sldworks;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xarial.XCad.Base;
using Xarial.XCad.Geometry;
using Xarial.XCad.Geometry.Curves;
using Xarial.XCad.Geometry.Wires;
using Xarial.XCad.Sketch;
using Xarial.XCad.SolidWorks.Documents;
using Xarial.XCad.SolidWorks.Features;
using Xarial.XCad.SolidWorks.Geometry;
using Xarial.XCad.SolidWorks.Geometry.Curves;
namespace Xarial.XCad.SolidWorks.Sketch
{
public interface ISwSketchEntityCollection : IXSketchEntityRepository
{
}
internal class SwSketchEntityCollection : ISwSketchEntityCollection
{
public int Count => m_Sketch.IsCommitted ? 0 : m_Cache.Count;
public IXSketchEntity this[string name] => throw new NotImplementedException();
public bool TryGet(string name, out IXSketchEntity ent) => throw new NotImplementedException();
private readonly ISwSketchBase m_Sketch;
private readonly List<IXSketchEntity> m_Cache;
private readonly ISwApplication m_App;
private readonly ISwDocument m_Doc;
private readonly ISketchManager m_SkMgr;
internal SwSketchEntityCollection(ISwSketchBase sketch, ISwDocument doc, ISwApplication app)
{
m_Doc = doc;
m_App = app;
m_Sketch = sketch;
m_SkMgr = doc.Model.SketchManager;
m_Cache = new List<IXSketchEntity>();
}
public void AddRange(IEnumerable<IXSketchEntity> segments)
{
if (m_Sketch.IsCommitted)
{
CreateSegments(segments, m_Sketch.Sketch);
}
else
{
m_Cache.AddRange(segments);
}
}
internal void CommitCache(ISketch sketch)
{
CreateSegments(m_Cache, sketch);
m_Cache.Clear();
}
private void CreateSegments(IEnumerable<IXSketchEntity> segments, ISketch sketch)
{
var addToDbOrig = m_SkMgr.AddToDB;
m_Sketch.SetEditMode(sketch, true);
m_SkMgr.AddToDB = true;
foreach (SwSketchEntity seg in segments)
{
seg.Commit();
}
m_SkMgr.AddToDB = addToDbOrig;
m_Sketch.SetEditMode(sketch, false);
}
public IEnumerator<IXSketchEntity> GetEnumerator()
{
if (m_Sketch.IsCommitted)
{
return new SwSketchEntitiesEnumerator(m_Doc, m_Sketch);
}
else
{
return m_Cache.GetEnumerator();
}
}
public IXLine PreCreateLine() => new SwSketchLine(null, m_Doc, m_App, false);
public IXPoint PreCreatePoint() => new SwSketchPoint(null, m_Doc, m_App, false);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void RemoveRange(IEnumerable<IXSketchEntity> ents)
{
//TODO: implement removing of entities
}
public IXCircle PreCreateCircle() => new SwSketchCircle(null, m_Doc, m_App, false);
public IXPolylineCurve PreCreatePolyline()
=> throw new NotSupportedException();
public IXCurve Merge(IXCurve[] curves)
=> throw new NotSupportedException();
public IXArc PreCreateArc() => new SwSketchArc(null, m_Doc, m_App, false);
}
internal class SwSketchEntitiesEnumerator : IEnumerator<ISwSketchEntity>
{
public ISwSketchEntity Current => m_Doc.CreateObjectFromDispatch<SwSketchEntity>(m_Entities[m_CurIndex]);
object IEnumerator.Current => Current;
private readonly ISwDocument m_Doc;
private readonly ISwSketchBase m_Sketch;
private List<object> m_Entities;
private int m_CurIndex;
internal SwSketchEntitiesEnumerator(ISwDocument doc, ISwSketchBase sketch)
{
m_Doc = doc;
m_Sketch = sketch;
m_Entities = new List<object>();
Reset();
}
public void Dispose()
{
}
public bool MoveNext()
{
m_CurIndex++;
return m_CurIndex < m_Entities.Count;
}
public void Reset()
{
m_CurIndex = -1;
m_Entities.Clear();
var segs = m_Sketch.Sketch.GetSketchSegments() as object[];
if (segs != null)
{
m_Entities.AddRange(segs);
}
var pts = m_Sketch.Sketch.GetSketchPoints2() as object[];
if (pts != null)
{
m_Entities.AddRange(pts);
}
}
}
}