|
Patch WPF Tutorial
Patch Tutorial demonstrates incorporation of DGKCwpfn10 DG Kernel Control into a simple WPF .NET 10.0 application using Microsoft
Visual Studio. Source code for Patch sample is available in
Patch folder of WPF samples.
See also: Sample List
This tutorial assumes basic familiarity with Visual Studio. It is based on Visual Studio 2026.
The Application
Patch application creates a new truncated cone and allows modification of its parameters.
Step 1. Creating the project
- Open Visual Studio. Select File > New > Project in the menu. In the "Create a new project" make sure C#, Windows and Desktop are current at the top.
Select "WPF Application" project type. Click Next. Enter "Patch" for the project name. Select location and make sure .NET 10.0 is selected. Click Create.
- Close any open files. In Solution Explorer right-click the MainWindow.xaml created by default and rename it
(using context menu on right-mouse click) to PatchForm.xaml.
- In the PatchForm.xaml.cs rename the class from MainWindow to PatchForm.
- In the Window element of the PatchForm.xaml replace Class="Patch.MainWindow" with Class="Patch.PatchForm"
- Open App.xaml and replace StartupUri="MainWindow.xaml" with StartupUri="PatchForm.xaml".
Step 2. Inserting the DG Kernel WPF control
- In the Project menu of Visual Studio select "Manage NuGet Packages".
- Make sure 'nuget.org' is the current source in the package source combo.
- Open Browse tab of the NuGet Package Manager and type DGKCwpfn10 in the search box. This should display a single item
- Select the item and click Install in the right-hand panel.
- Click Apply in the displayed dialogue. Close the NuGet manager.
- Double-click PatchForm.xaml in the Solution Explorer to open the form in Designer.
- In the XAML view (the code window) in the Window element change the title to "Patch - Hello World (WPF)"
- Change size in the element to: Width="1020" Height="800".
- Add DGKCwpfn10 assembly reference to the Window element by adding
xmlns:wpfcontr="clr-namespace:DGKC;assembly=DGKCwpfn10"
line below the last xmlns line.
- To add DGKCwpfn10 control, copy the whole code of the Grid element from the distributed WPF Patch sample.
The main element there, which defines the instance of the control, is the
<wpfcontr:DGKCwpfn10 x:Name="m_dgkc" RenderSolid="True" ShowGlobalAxes="True" BackgroundColor="204,255,234"/>
- You can specify properties directly in the .xaml file or by using the Property Window.
- Right-click on the control and select Properties. The form shows properties of the control. The properties can also be accessed via the Properties Window of Visual Studio. The main one, ModelPath, can point to an .mdg model file or a file in any other supported format. We will leave it empty as our entity will be created in the code.
- Open the Object Browser (View > Object Browser menu option). Expand DGKCwpfn10 and its child node. This node displays all necessary information about types, interfaces and their methods available in the component.
Step 3. Creating the Entity
- To create a handler for the Load event of the window, add property Loaded="OnLoad" inside the Window tag. Right-click on event handler name and select "Go To Definition". A new OnLoad() method will be added to the Patch class.
- Add line:
using DGKC;
at the top of the Patch.cs
- Add the following three lines just below the line (A):
var iStdShape = iModel.Query<IStdShape_DG>();
m_iEntity = iStdShape.Cylinder(m_baseRadius, m_topRadius, m_height);
iModel.AddEntity(m_iEntity);
The first line obtains an interface for creation of basic shapes. The second line creates a cylindrical object and stores its reference in m_iEntity.
The third line adds the entity to the model to be displayed in the 3D view.
- Add the following two lines at the end of OnLoad():
var iView = m_dgkc.GetView();
iView.Reset(true, true);
The first line accesses IView_DG, which is the root of the view-related functionality and the second line refits the view to include the new object.
Step 4. Implementing Modifications of the Object.
- At the end of Patch constructor add the following to populate the edit boxes with default values:
editBaseRad.Text = m_baseRadius.ToString();
editTopRad.Text = m_topRadius.ToString();
editHeight.Text = m_height.ToString();
- In the handler of the Apply button, add:
m_baseRadius = double.Parse(editBaseRad.Text, CultureInfo.InvariantCulture);
m_topRadius = double.Parse(editTopRad.Text, CultureInfo.InvariantCulture);
m_height = double.Parse(editHeight.Text, CultureInfo.InvariantCulture);
var iModel = m_dgkc.GetModel();
var iStdShapeRef = iModel.Query<IStdShapeRef_DG>();
iStdShapeRef.Cylinder(m_baseRadius, m_topRadius, m_height, ref m_iEntity);
m_dgkc.UpdateView();
Notice the different version of interface used. IStdShapeRef_DG is designed for modification (and creation) rather than creation only.
The last line redraws the window.
- Add an empty handler for the Help item to satisfy the compiler.
- In handler of the OK button add Close();.
- Run the application and test modifications of the entity.
|