|
Patch C# Tutorial
Patch Tutorial demonstrates incorporation of DG Kernel
ActiveX control into a simple Windows Form application using Microsoft
Visual Studio. Source code for Patch sample is available in
Samples\NET\CS\Patch\ folder of the installation directory.
The KerCADne.ocx control is provided in v7 for backward compability of legacy applications. For new applications we recommend using DGKC pure .NET control.
See also: Sample List
This tutorial assumes basic familiarity with Visual Studio. It is based on Visual Studio 2019.
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, Desktop are current on top.
Select "Windows Forms App (.NET Framework) C#" project type. Click Next.
Enter "Patch" for the project name. Select location and make sure .NET framework 4.5 is selected. Click Create.
- In Solution Explorer select the Form1.cs created by default and rename it
(using context menu on right-mouse click) to Patch.cs. Accept the suggestion to rename related elements when asked.
Step 2. Inserting the DG Kernel control
- In the menu select Project > "Add Reference...".
- In the dialogue open COM tab and select "KernelCAD .NET Pro ActiveX Control". Press OK.
- Open Patch form in design mode and adjust size of the form to 890, 532 by
pasting or typing the pair in Size property of the Properties Window (View
> "Properties Window"). Change also Text property to
"Patch".
- In the Toolbox (View > Toolbox) open Components tab. If "KernCADnet Control" is not there, right-click on the Components tab and select "Choose items". In COM Componetnts tab turn on the "KernCADnet Control" check box and click OK.
- In the Toolbox open Components tab, select "KernCADnet Control" and place the control on the left in the form. Save All. Close the from designer. A new member of type AxKernCADnet.AxKernCADnet called axKernCADnet1 will be added to the Patch class.
- Rename axKernCADnet1 field of the Patch class to m_kernCAD. Use Find And Replace, Replace in Files tab Visuall Studio dialogue to performe the renaming. Select Current project as Look in folder and *.cs as File types
- Open the form. Select the control and change its size to 698, 482 using Properties Window.
On rare occasions control may throw an exception at this point. Please save all then,
close the solution and restart Visual Studio. Generally, it is recommended to save often and close the form
designer as soon as possible.
- 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
Interop.KernCADnet 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 handler for the Load event of the form, in design mode, click on the form outside the
control. Click Events icon on the top of Properties Window. Select Load row
in the list and double-click the row on the right. A new Patch_Load()
method will be added to the Patch class.
- Add line:
using KernCADnet;
at the top of the Patch.cs
- Add the following three lines just below the line (A):
IStdShape_DG iStdShape = iModel as 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 Patch_Load():
IView_DG iView = m_kernCAD.GetView() as IView_DG;
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.
- Add three edit boxes for the parameters and an Apply button to the form.
Please make sure the text in edit boxes coincides with the initial values.
The controls can be copied in design mode from the installed completed version of the sample
- In handler of the Apply button, add:
m_baseRadius = Double.Parse(editBaseRad.Text);
m_topRadius = Double.Parse(editTopRad.Text);
m_height = Double.Parse(editHeight.Text);
IModel_DG iModel = m_kernCAD.GetModel() as IModel_DG;
IStdShapeRef_DG iStdShapeRef = iModel as IStdShapeRef_DG;
iStdShapeRef.Cylinder(m_baseRadius, m_topRadius, m_height, ref m_iEntity);
m_kernCAD.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.
- Run the application and test modifications of the entity.
|