Home Page > Programming Samples and Tutorials > .NET Samples > WPF > WPF
WpfCAD Sample Overview
WpfCAD C# WPF Tutorial
Source code for the sample is located in the Samples\NET\C#\WPF\WpfCAD folder of the KernelCAD installation directory.
The application demonstrates usage of KernelCAD in a Windows Presentation Framework application.
The tutorial is based on Visual Studio 2008. It also assumes KernelCAD 4.3 or
later is installed on the computer.
The application will depend on the controls library <KernelCAD Installation
Directory>Samples\NET\C#\WPF\KcAxLib
Creating the application
- Build 32 bit Debug version of the KcAxLib project
- Close KcAxLib solution
- Create new C# WPF application in Samples\NET\C#\WPF\ folder
- Open the project properties for the project and in the Build page select
Platform Target x86
- Add references to all dlls in the KcAxLib\Bin\Debug folder: Right-click on the
project file in the solution explorer, Select Add Reference. Browse to
KcAxLib\Bin\Debug folder. Select all .dll files in the folder. Click OK
- Add a reference to the WindowsFormsIntegration assembly, which is named
WindowsFormsIntegration.dll. To do that in the Add Reference dialog select the
.NET tab, scroll down and select WindowsFormsIntegration item
- Add a reference to System.Windows.Forms assembly in the same manner
- Open Window1.xaml and replace lines containing Title and Grid element with:
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid Name="grid1">
</Grid>
- Open Window1.xaml.cs and add the following three lines at the top
using KcAxLib; using AxKernCADnet; using KernCADnet;
- In the same file add the method below to the Window1 class
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the ActiveX control.
KcAxLib.KcAxControl axKc = new KcAxLib.KcAxControl();
// Assign the
ActiveX control as the host control's child.
host.Child = axKc;
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
axKc.ModelPath = "Models\\Torus.glm";
IModel iModel = axKc.GetModel();
int countObjects = iModel.GetSectionCount();
if (countObjects > 0)
{
ISection iSection = iModel.GetSection(0);
string objName = iSection.GetName();
int countChildren = iSection.GetChildCount(); } }
- Build the application
- Set a break point in the Window_Loaded method and check that
everything works as expected, namely countObjects is not zero and the objName
is "Torus_Knot01"
Samples\NET\C#\WPF\ folder contains a finished copy of the tutorial in
WpfCAD_Completed folder.
See also "Walkthrough: Hosting an ActiveX Control in WPF" MSDN topic:
|