-
Notifications
You must be signed in to change notification settings - Fork 11
/
TextureBuilderPro.cs
65 lines (58 loc) · 2.83 KB
/
TextureBuilderPro.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
using System;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Threading.Tasks;
namespace TexTruder
{
internal class MainButton : Button
{
protected override async void OnClick()
{
const string gdbName = @"F:\TexTest.gdb";
const string fcName = @"Me104";
const string textPath = @"F:\Me-104.jpg";
const long linOid = 1;
const double extrusion = -2000;
await QueuedTask.Run(() =>
{
IGeometryEngine engine = GeometryEngine.Instance;
using Geodatabase gdb = new(
connectionPath: new FileGeodatabaseConnectionPath(
path: new Uri(gdbName, UriKind.Absolute)));
using FeatureClass fc = gdb.OpenDataset<FeatureClass>(fcName);
using RowCursor cursor = fc.Search(
queryFilter: new QueryFilter() { ObjectIDs = new long[1] { linOid } },
useRecyclingCursor: false);
if (cursor.MoveNext())
{
using Feature feature = (Feature)cursor.Current;
Polyline polyline = (Polyline)engine.SetMsAsDistance((Polyline)feature.GetShape(), AsRatioOrLength.AsRatio);
ReadOnlyPointCollection coords = polyline.Points;
int len = coords.Count;
Coordinate2D[] textureCoordinates = GC.AllocateUninitializedArray<Coordinate2D>(2 * len);
for (int i = 0, j = 0; i < len;)
{
double distanceAlong = 1 - coords[i++].M;
textureCoordinates[j++].SetComponents(distanceAlong, 0);
textureCoordinates[j++].SetComponents(distanceAlong, 1);
}
MultipatchBuilderEx mpBuilder = new(engine.ConstructMultipatchExtrude(polyline, extrusion));
Patch patch = mpBuilder.Patches[0];
patch.TextureCoords2D = textureCoordinates;
patch.Material = new BasicMaterial()
{
TextureResource = new TextureResource(new JPEGTexture(
buffer: System.IO.File.ReadAllBytes(textPath)))
};
using FeatureClass MPStore = gdb.OpenDataset<FeatureClass>(@"MPStore");
using InsertCursor insertCursor = MPStore.CreateInsertCursor();
using RowBuffer rowBuffer = MPStore.CreateRowBuffer();
rowBuffer[1] = mpBuilder.ToGeometry();
insertCursor.Insert(rowBuffer);
insertCursor.Flush();
}
});
}
}
}