-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
195 lines (146 loc) · 9.5 KB
/
Main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;
using System.Linq;
namespace ProjectPolylineFromProfileView
{
public class Main
{
[CommandMethod("PROJECT_POLYLINE")]
public void ProjectPolylineFromProfileView()
{
DocumentCollection docCol = Application.DocumentManager;
Database database = docCol.MdiActiveDocument.Database;
Editor editor = docCol.MdiActiveDocument.Editor;
CivilDocument doc = CivilApplication.ActiveDocument;
using (Transaction tx = database.TransactionManager.StartTransaction())
{
try
{
// Select Polyline
PromptEntityOptions promptPolyline = new PromptEntityOptions("\nSelect a Polyline: ");
promptPolyline.SetRejectMessage("\n Polyline not Select");
promptPolyline.AddAllowedClass(typeof(Polyline), true);
PromptEntityResult entityPolyline = editor.GetEntity(promptPolyline);
if (entityPolyline.Status != PromptStatus.OK) return;
// Select Profile View
PromptEntityOptions promptProfileView = new PromptEntityOptions("\nSelect a profile view: ");
promptProfileView.SetRejectMessage("\nProfileView not select");
promptProfileView.AddAllowedClass(typeof(ProfileView), true);
PromptEntityResult entityProfileView = editor.GetEntity(promptProfileView);
if (entityProfileView.Status != PromptStatus.OK) return;
ProfileView profileView = tx.GetObject(entityProfileView.ObjectId, OpenMode.ForWrite) as ProfileView;
double x = 0.0;
double y = 0.0;
if (profileView.ElevationRangeMode == ElevationRangeType.Automatic)
{
profileView.ElevationRangeMode = ElevationRangeType.UserSpecified;
profileView.FindXYAtStationAndElevation(profileView.StationStart, profileView.ElevationMin, ref x, ref y);
}
else
profileView.FindXYAtStationAndElevation(profileView.StationStart, profileView.ElevationMin, ref x, ref y);
ProfileViewStyle profileViewStyle = tx.GetObject(profileView.StyleId, OpenMode.ForRead) as ProfileViewStyle;
ObjectId layerId = (tx.GetObject(profileView.AlignmentId, OpenMode.ForRead) as Alignment).LayerId;
ObjectId profileStyleId = doc.Styles.ProfileStyles.FirstOrDefault();
ObjectId profileLabelSetStylesId = doc.Styles.LabelSetStyles.ProfileLabelSetStyles.FirstOrDefault();
ObjectId profByLayout = Profile.CreateByLayout("New Profile", profileView.AlignmentId, layerId, profileStyleId, profileLabelSetStylesId);
Profile profile = tx.GetObject(profByLayout, OpenMode.ForWrite) as Profile;
BlockTableRecord blockTableRecord = tx.GetObject(database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
ObjectId polylineObjId = entityPolyline.ObjectId;
Polyline polyline = tx.GetObject(polylineObjId, OpenMode.ForWrite, false) as Polyline;
double intervalMajorTick = profileViewStyle.BottomAxis.MajorTickStyle.Interval;
double gridPadding = profileViewStyle.GridStyle.GridPaddingLeft * intervalMajorTick;
//Invert the Polyline if the start coordinate is greater than the end coordinate
if (polyline != null && (polyline.StartPoint.X > polyline.EndPoint.X))
{
polyline.ReverseCurve();
}
if (polyline != null)
{
int numOfVert = polyline.NumberOfVertices - 1;
Point2d startSegment;
Point2d endSegment;
Point2d sampleSegment;
Point2d coodStartSeg;
Point2d coodEndSeg;
Point2d coodSampleSegment;
for (int i = 0; i < numOfVert; i++)
{
switch (polyline.GetSegmentType(i))
{
case SegmentType.Line:
LineSegment2d lineSegment2dAt = polyline.GetLineSegment2dAt(i);
startSegment = lineSegment2dAt.StartPoint;
double difStartSegment_X = (startSegment.X - gridPadding) - x;
double difStartSegment_Y = (startSegment.Y - y) / profileViewStyle.GraphStyle.VerticalExaggeration + profileView.ElevationMin;
coodStartSeg = new Point2d(difStartSegment_X, difStartSegment_Y);
endSegment = lineSegment2dAt.EndPoint;
double difEndSegment_X = (endSegment.X - gridPadding) - x;
double difEndSegment_Y = (endSegment.Y - y) / profileViewStyle.GraphStyle.VerticalExaggeration + profileView.ElevationMin;
coodEndSeg = new Point2d(difEndSegment_X, difEndSegment_Y);
profile.Entities.AddFixedTangent(coodStartSeg, coodEndSeg);
break;
case SegmentType.Arc:
CircularArc2d arcSegment2dAt = polyline.GetArcSegment2dAt(i);
startSegment = arcSegment2dAt.StartPoint;
double difStartSegmentArc_x = (startSegment.X - gridPadding) - x;
double difStartSegmentArc_y = (startSegment.Y - y) / profileViewStyle.GraphStyle.VerticalExaggeration + profileView.ElevationMin;
coodStartSeg = new Point2d(difStartSegmentArc_x, difStartSegmentArc_y);
endSegment = arcSegment2dAt.EndPoint;
double difEndSegmentArc_x = endSegment.X - gridPadding - x;
double difEndSegmentArc_y = (endSegment.Y - y) / profileViewStyle.GraphStyle.VerticalExaggeration + profileView.ElevationMin;
coodEndSeg = new Point2d(difEndSegmentArc_x, difEndSegmentArc_x);
sampleSegment = arcSegment2dAt.GetSamplePoints(11)[5];
double difMidSegmentArc_x = (sampleSegment.X - gridPadding) - x;
double difMidSegmentArc_y = (sampleSegment.Y - y) / profileViewStyle.GraphStyle.VerticalExaggeration + profileView.ElevationMin;
coodSampleSegment = new Point2d(difMidSegmentArc_x, difMidSegmentArc_y);
profile.Entities.AddFixedSymmetricParabolaByThreePoints(coodStartSeg, coodSampleSegment, coodEndSeg);
break;
case SegmentType.Coincident:
break;
case SegmentType.Point:
break;
case SegmentType.Empty:
break;
default:
break;
}
}
}
Polyline3d polyline3D = new Polyline3d();
blockTableRecord.AppendEntity(polyline3D);
ObjectId alignmentId = profileView.AlignmentId;
Alignment alignment = tx.GetObject(alignmentId, OpenMode.ForRead, false) as Alignment;
foreach (ProfilePVI profilePVI in profile.PVIs)
{
double offset = 0.0;
double northing = 0.0;
double easting = 0.0;
double station = profilePVI.RawStation;
double elevation = profilePVI.Elevation;
// Get the coordinates easting and northing from Alignment
alignment.PointLocation(station, offset, ref easting, ref northing);
// Add the profile entity vertices to the 3D polyline
Point3d point = new Point3d(easting, northing, elevation);
PolylineVertex3d point3dVertex = new PolylineVertex3d(point);
polyline3D.AppendVertex(point3dVertex);
}
// Add the 3D polyline to the model space
tx.AddNewlyCreatedDBObject(polyline3D, true);
// Remove profile
profile.Erase();
}
catch (Exception ex)
{
editor.WriteMessage("\n" + ex.Message);
}
tx.Commit();
}
}
}
}