3D Graphics Extrusion

Download Samples Repository

Description

Demonstrates how to use graphics extrusion to stretch a flat 2D shape vertically to create a 3D object.

"Desktop" Available for Desktop

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.GraphicsExtrusion"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
			 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
			 xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
			 mc:Ignorable="d" 
			 d:DesignHeight="300" d:DesignWidth="300">
	<Grid>
		<esri:SceneView x:Name="MySceneView">
			<esri:Scene>
				<esri:ArcGISTiledMapServiceLayer ID="AGOLayer"
					ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
			</esri:Scene>
			<esri:SceneView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="statesOverlay">
					<esri:GraphicsOverlay.Renderer>
						<esri:SimpleRenderer>
							<esri:SimpleRenderer.SceneProperties>
								<esri:RendererSceneProperties ExtrusionMode="BaseHeight"  ExtrusionExpression="[]"/>
							</esri:SimpleRenderer.SceneProperties>
							<esri:SimpleFillSymbol>
								<esri:SimpleFillSymbol.Outline>
									<esri:SimpleLineSymbol Color="Transparent"/>
								</esri:SimpleFillSymbol.Outline>
							</esri:SimpleFillSymbol>
						</esri:SimpleRenderer>
					</esri:GraphicsOverlay.Renderer>
				</esri:GraphicsOverlay>
			</esri:SceneView.GraphicsOverlays>
		</esri:SceneView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20">
			<Grid>
				<StackPanel>
					<TextBlock Text="Select Age Range" FontWeight="Bold" 
							   HorizontalAlignment="Center"/>
					<StackPanel Orientation="Horizontal" Margin="0,10">
						<ComboBox x:Name="statisticsComboBox" DisplayMemberPath="DisplayName"
							SelectionChanged="statisticsComboBox_SelectionChanged"/>
					</StackPanel>
				</StackPanel>
			</Grid>
		</Border>
	</Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Query;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// Demonstrates how to use graphics extrusion to stretch a flat 2D shape vertically to create a 3D object.
	/// </summary>
	/// <title>3D Graphics Extrusion</title>
	/// <category>Scene</category>
	/// <subcategory>Graphics</subcategory>
	public partial class GraphicsExtrusion : UserControl
	{
		public GraphicsExtrusion()
		{
			InitializeComponent();
			Initialize();
		}

		private async void Initialize()
		{
			try
			{
				CreateExtrusionInfos();

				// Set initial viewpoint
				var viewpoint = new ViewpointCenter(new MapPoint(-96, 39), 15000000);
				await MySceneView.SetViewAsync(viewpoint);

				// Query states with statistical attributes
				var queryTask = new QueryTask(
					new Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"));
				Query query = new Query("1=1");
				query.OutFields.Add("STATE_NAME");
				query.OutFields.Add("AGE_5_17");
				query.OutFields.Add("AGE_18_21");
				query.OutFields.Add("AGE_22_29");
				query.OutFields.Add("AGE_30_39");
				query.OutFields.Add("AGE_40_49");
				query.OutFields.Add("AGE_50_64");
				query.OutFields.Add("AGE_65_UP");

				var result = await queryTask.ExecuteAsync(query);

				var states = new GraphicCollection();
				foreach (var state in result.FeatureSet.Features)
					states.Add(new Graphic(state.Geometry, state.Attributes));

				// Make sure that all layers are loaded
				await MySceneView.LayersLoadedAsync();

				// Set graphics to the overlay
				var statesOverlay = MySceneView.GraphicsOverlays["statesOverlay"];
				statesOverlay.GraphicsSource = states;

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Graphics Extrusion Sample");
			}
		}

		// Create list of extrusion infos that contains rendering info
		private void CreateExtrusionInfos()
		{
			var renderers = new List<RenderingInfo>();
			renderers.Add(new RenderingInfo(
				"Age 5 to 17", "[AGE_5_17]", Colors.DarkBlue));

			renderers.Add(new RenderingInfo(
				"Age 18 to 21", "[AGE_18_21]", Colors.DarkCyan));

			renderers.Add(new RenderingInfo(
				"Age 22 to 29", "[AGE_22_29]", Colors.DarkGoldenrod));

			renderers.Add(new RenderingInfo(
				"Age 30 to 39", "[AGE_30_39]", Colors.DarkGray));

			renderers.Add(new RenderingInfo(
				"Age 40 to 49", "[AGE_40_49]", Colors.DarkGreen));

			renderers.Add(new RenderingInfo(
				"Age 50 to 64", "[AGE_50_64]", Colors.DarkMagenta));

			renderers.Add(new RenderingInfo(
				"Age 65 and up", "[AGE_65_UP]", Colors.DarkOliveGreen));

			statisticsComboBox.ItemsSource = renderers;
			statisticsComboBox.SelectedIndex = 0;
		}

		private void statisticsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			var statesOverlay = MySceneView.GraphicsOverlays["statesOverlay"];
			var renderingInfo = (e.AddedItems[0] as RenderingInfo);
			var renderer = statesOverlay.Renderer as SimpleRenderer;

			// Change rendering information based on the selection
			(renderer.Symbol as SimpleFillSymbol).Color = renderingInfo.Color;
			renderer.SceneProperties.ExtrusionExpression = renderingInfo.ExtrusionExpression;
		}

		// Simple container class for rendering information
		public class RenderingInfo
		{
			public RenderingInfo(string displayName, string extrusionExpression, Color color)
			{
				DisplayName = displayName;
				ExtrusionExpression = extrusionExpression;
				Color = color;
			}

			public string DisplayName { get; private set; }
			public Color Color { get; private set; }
			public string ExtrusionExpression { get; private set; }
		}
	}
}
Feedback on this topic?