Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
draftsight
/
opt
/
dassault-systemes
/
DraftSight
/
APISDK
/
Create_Context-sensitive_Menu_Items_Example_CSharp.htm
|
|---|
<html>
<head>
<title>Create Context-sensitive Menu Items Example (C#)</title>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="ApiHelp.css">
<meta name=MS-HKWD content="C# examples">
<meta name=MS-HKWD content="C# examples,context-sensitive menu items">
</head>
<body>
<h1>Create Context-sensitive Menu Items Example (C#)</h1>
<p>This example shows how to create default, command, and object
context-sensitive menu items.</p>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"><span style="color:green;">//--------------------------------------------------------------</span>
<span style="color:green;">// Preconditions:</span>
<span style="color:green;">// 1. Create a C# Windows console project.</span>
<span style="color:green;">// 2. Copy and paste this code into the C# project.</span>
<span style="color:green;">// 3. Add a reference to</span>
<span style="color:green;">// <i>install_dir</i>\<b>APISDK\tlb\DraftSight.Interop.dsAutomation.dll</b>.</span>
<span style="color:green;">// 4. Add a reference to <b>System.Windows.Forms</b>.</span>
<span style="color:green;">// 5. Start DraftSight.</span>
<span style="color:green;">// 6. Press F5.</span>
<span style="color:green;">//</span>
<span style="color:green;">// Postconditions: </span>
<span style="color:green;">// 1. Connects to DraftSight.</span>
<span style="color:green;">// 2. Gets the active document.</span>
<span style="color:green;">// 3. Creates three user commands.</span>
<span style="color:green;">// 4. Adds a default context-sensitive menu item for the first user command.</span>
<span style="color:green;">// 5. Adds a command context-sensitive menu item for the second user command.</span>
<span style="color:green;">// 6. Adds an object context-sensitive menu item for the third user command.</span>
<span style="color:green;">// 7. Displays a message box informing you to perform steps 7a - 7f. Click <b>OK</b></span>
<span style="color:green;">// to close the message box before performing these steps.</span>
<span style="color:green;">// a. Right-click anywhere in the the graphics area and </span>
<span style="color:green;">// select <b>Execute line command</b> in the context-sensitive menu.</span>
<span style="color:green;">// The crosshair pointer is displayed.</span>
<span style="color:green;">// b. Right-click anywhere in the graphics area and select <b>Open Help</b> </span>
<span style="color:green;">// in the context-sensitive menu. The user-interface DraftSight </span>
<span style="color:green;">// Help system opens and displays the<b> Constructing Lines</b> Help topic.</span>
<span style="color:green;">// c. Read the Help topic and close the Help.</span>
<span style="color:green;">// d. Construct a line and select the line.</span>
<span style="color:green;">// e. Right-click anywhere in the graphics area and </span>
<span style="color:green;">// select <b>Do pattern</b> in the context-sensitive menu.</span>
<span style="color:green;">// A pattern of the selected Line is constructed.</span>
<span style="color:green;">// f. Press the Enter key and exit DraftSight.</span>
<span style="color:green;">//----------------------------------------------------------------</span>
<span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> DraftSight.Interop.dsAutomation;
<span style="color:blue;">using</span> System.Runtime.InteropServices;
<span style="color:blue;">using</span> System.Windows.Forms;
<span style="color:blue;">namespace</span> ContextMenuItemCSharp
{
<span style="color:blue;">class</span> <span style="color:#2b91af;">Program</span>
{
<span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main()
{
DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">Application</span> dsApp;
<span style="color:green;">//Connect to DraftSight application</span>
dsApp = (DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">Application</span>)<span style="color:#2b91af;">Marshal</span>.GetActiveObject(<span style="color:#a31515;">"DraftSight.Application"</span>);
<span style="color:blue;">if</span> (<span style="color:blue;">null</span> == dsApp)
{
<span style="color:blue;">return</span>;
}</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="COLOR: green">//Abort any command currently running in DraftSight to avoid nested commands</span>
dsApp.<b>AbortRunningCommand</b>();
<span style="color:green;">//Get active document</span>
<span style="color:#2b91af;">Document</span> dsDoc = dsApp.<b>GetActiveDocument</b>();
<span style="color:blue;">if</span> (<span style="color:blue;">null</span> == dsDoc)
{
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"There are no open documents in DraftSight."</span>);
<span style="color:blue;">return</span>;
}
<span style="color:green;">//Create three user commands for context-sensitive menus</span>
<span style="color:#2b91af;">dsCreateCommandError_e</span> error;
<span style="color:#2b91af;">UserCommand</span> lineUserCmd = dsApp.<b>CreateUserCommand</b>(<span style="color:#a31515;">"APIUUID"</span>, <span style="color:#a31515;">"TEST_LINE"</span>, <span style="color:#a31515;">"^C^C_LINE\n"</span>, <span style="color:#a31515;">"Line"</span>, <span style="color:blue;">string</span>.Empty, <span style="color:blue;">string</span>.Empty, <span style="color:#2b91af;">dsUIState_e</span>.dsUIState_Document, <span style="color:blue;">out</span> error);
<span style="color:#2b91af;">UserCommand</span> helpUserCmd = dsApp.<b>CreateUserCommand</b>(<span style="color:#a31515;">"APIUUID"</span>, <span style="color:#a31515;">"TEST_HELP"</span>, <span style="color:#a31515;">"'_HELP\n"</span>, <span style="color:#a31515;">"Help"</span>, <span style="color:blue;">string</span>.Empty, <span style="color:blue;">string</span>.Empty, <span style="color:#2b91af;">dsUIState_e</span>.dsUIState_Document, <span style="color:blue;">out</span> error);
<span style="color:#2b91af;">UserCommand</span> patternUserCmd = dsApp.<b>CreateUserCommand</b>(<span style="color:#a31515;">"APIUUID"</span>, <span style="color:#a31515;">"TEST_PATTERN"</span>, <span style="color:#a31515;">"_-PATTERN _L 3 3 5 5\n\n"</span>, <span style="color:#a31515;">"Pattern"</span>, <span style="color:blue;">string</span>.Empty, <span style="color:blue;">string</span>.Empty, <span style="color:#2b91af;">dsUIState_e</span>.dsUIState_Document, <span style="color:blue;">out</span> error);
<span style="color:#2b91af;">dsMenuItemType_e</span> menuItemType = <span style="color:#2b91af;">dsMenuItemType_e</span>.dsMenuItemType_UserCommand;
<span style="color:green;">//Add default context-sensitive menu item for first user command</span>
<span style="color:#2b91af;">ContextMenuItem</span> dsDefaultContextMenuItem = dsApp.<b>AddDefaultContextMenu</b>(<span style="color:#a31515;">"APIUUID"</span>, menuItemType, <span style="color:#a31515;">"Execute line command"</span>, lineUserCmd.<b>GetID</b>());
<span style="color:green;">//Add command context-sensitive menu item for second user command</span>
<span style="color:#2b91af;">ContextMenuItem</span> dsCommandContextMenuItem = dsApp.<b>AddCommandContextMenu</b>(<span style="color:#a31515;">"APIUUID"</span>, menuItemType, <span style="color:#a31515;">"Open Help"</span>, helpUserCmd.<b>GetID</b>(), <span style="color:#a31515;">"_LINE"</span>);
<span style="color:green;">//Add object context-sensitive menu item for third user command</span>
<span style="color:#2b91af;">ContextMenuItem</span> dsObjectContextMenuItem = dsApp.<b>AddObjectContextMenu</b>(<span style="color:#a31515;">"APIUUID"</span>, menuItemType, <span style="color:#a31515;">"Do pattern"</span>, patternUserCmd.<b>GetID</b>(), <span style="color:#2b91af;">dsObjectType_e</span>.dsLineType);
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Perform steps 7a - 7f."</span>);
}
}
}
</pre>
</body>
</html>