-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSampleCsHistoryDivide.cs
117 lines (90 loc) · 3.37 KB
/
SampleCsHistoryDivide.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
using System;
using Rhino;
using Rhino.Commands;
namespace SampleCsCommands
{
[System.Runtime.InteropServices.Guid("74528b07-baba-48c0-9be4-2659de76f01b")]
public class SampleCsHistoryDivide : Command
{
static int _historyVersion = 20121101;
public SampleCsHistoryDivide()
{
}
public override string EnglishName
{
get { return "SampleCsHistoryDivide"; }
}
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
const Rhino.DocObjects.ObjectType filter = Rhino.DocObjects.ObjectType.Curve;
Rhino.DocObjects.ObjRef objref;
Rhino.Commands.Result rc = Rhino.Input.RhinoGet.GetOneObject("Select curve to divide", false, filter, out objref);
if (rc != Rhino.Commands.Result.Success || objref == null)
return rc;
Rhino.Geometry.Curve curve = objref.Curve();
if (null == curve || curve.IsShort(Rhino.RhinoMath.ZeroTolerance))
return Rhino.Commands.Result.Failure;
int segmentCount = 2;
rc = Rhino.Input.RhinoGet.GetInteger("Number of segments", false, ref segmentCount, 2, 100);
if (rc != Rhino.Commands.Result.Success)
return rc;
Rhino.Geometry.Point3d[] points;
curve.DivideByCount(segmentCount, true, out points);
if (null == points)
return Rhino.Commands.Result.Failure;
// Create a history record
Rhino.DocObjects.HistoryRecord history = new Rhino.DocObjects.HistoryRecord(this, _historyVersion);
WriteHistory(history, objref, segmentCount, points.Length);
for (int i = 0; i < points.Length; i++)
doc.Objects.AddPoint(points[i], null, history, false);
doc.Views.Redraw();
return Rhino.Commands.Result.Success;
}
/// <summary>
/// Rhino calls this function to remake objects when inputs have changed.
/// </summary>
protected override bool ReplayHistory(Rhino.DocObjects.ReplayHistoryData replay)
{
Rhino.DocObjects.ObjRef objref = null;
int segmentCount = 0;
int pointCount = 0;
if (!ReadHistory(replay, ref objref, ref segmentCount, ref pointCount))
return false;
Rhino.Geometry.Curve curve = objref.Curve();
if (null == curve)
return false;
if (pointCount != replay.Results.Length)
return false;
Rhino.Geometry.Point3d[] points;
curve.DivideByCount(segmentCount, true, out points);
if (null == points)
return false;
for (int i = 0; i < points.Length; i++)
replay.Results[i].UpdateToPoint(points[i], null);
return true;
}
private bool ReadHistory(Rhino.DocObjects.ReplayHistoryData replay, ref Rhino.DocObjects.ObjRef objref, ref int segmentCount, ref int pointCount)
{
if (_historyVersion != replay.HistoryVersion)
return false;
objref = replay.GetRhinoObjRef(0);
if (null == objref)
return false;
if (!replay.TryGetInt(1, out segmentCount))
return false;
if (!replay.TryGetInt(2, out pointCount))
return false;
return true;
}
private bool WriteHistory(Rhino.DocObjects.HistoryRecord history, Rhino.DocObjects.ObjRef objref, int segmentCount, int pointCount)
{
if (!history.SetObjRef(0, objref))
return false;
if (!history.SetInt(1, segmentCount))
return false;
if (!history.SetInt(2, pointCount))
return false;
return true;
}
}
}