Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
<html>
<head>
<title>Create Tracker 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,trackers">
<meta name=MS-HKWD content="C# examples,temporary entities">
</head>
<body>
<h1>Create Tracker Example (C#)</h1>
<p>This example shows how to create a tracker and add a temporary entity to the
tracker.</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 example into the C# IDE.</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 references to <b>System</b> and <b>System.Windows.Forms</b>.</span>
<span style="color:green;">// 5. Start DraftSight and open a document.</span>
<span style="color:green;">// 6. Start debugging the project.</span>
<span style="color:green;">//</span>
<span style="color:green;">// Postconditions: </span>
<span style="color:green;">// 1. Creates a temporary Circle for the tracker.</span>
<span style="color:green;">// 2. Creates a tracker and adds the temporary Circle to the</span>
<span style="color:green;">// the tracker. </span>
<span style="color:green;">// 3. Examine the pointer in the drawing to verify. </span>
<span style="color:green;">// 4. Prompts you to click in the drawing to insert a Circle.</span>
<span style="color:green;">// 5. Inserts a Circle in the drawing at the point where you clicked.</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> TrackerCSharp
{
<span style="color:blue;">class</span> <span style="color:#2b91af;">Program</span>
{
<span style="color:blue;">private</span> <span style="color:blue;">static</span> DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">Application</span> dsApp;
<span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">Circle</span> dsTempCircle;
<span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args)
{
<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:green;">// Abort any command currently running in DraftSight to avoid nested commands</span>
dsApp.<b>AbortRunningCommand</b>();
<span style="color:blue;">if</span> (<span style="color:blue;">null</span> == dsApp)
{
<span style="color:blue;">return</span>;
}
<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;">//Get Sketch Manager</span>
<span style="color:#2b91af;">SketchManager</span> dsSketchMgr = dsDoc.<b>GetModel</b>().<b>GetSketchManager</b>();
<span style="color:green;">//Create temporary Circle</span>
dsApp.<b>TemporaryEntityMode</b> = <span style="color:blue;">true</span>;
dsTempCircle = dsSketchMgr.<b>InsertCircle</b>(0, 0, 0, 10);
dsApp.<b>TemporaryEntityMode</b> = <span style="color:blue;">false</span>;
<span style="color:green;">//Create tracker and add temporary Circle</span>
<span style="color:#2b91af;"> Tracker</span> dsTracker = dsApp.<b>CreateTracker</b>();
dsTracker.<b>AddTemporaryEntity</b>(dsTempCircle);
<span style="color:green;">//Prompt for point where to insert Circle</span>
<span style="color:green;">//Before prompt, add tracker to command message</span>
<span style="color:#2b91af;">CommandMessage</span> dsCmdMsg = dsApp.<b>GetCommandMessage</b>();
dsCmdMsg.<b>AddTracker</b>(dsTracker);
<span style="color:blue;">double</span> X = 0.0;
<span style="color:blue;">double</span> Y = 0.0;
<span style="color:blue;">double</span> Z = 0.0;
dsTracker.<b>UpdateNotify</b> += <span style="color:blue;">new</span> <span style="color:#2b91af;">_ITrackerEvents_UpdateNotifyEventHandler</span>(dsTracker_UpdateNotify);
<span style="color:blue;">bool</span> res = dsCmdMsg.<b>PromptForPoint</b>(<span style="color:#a31515;">"Click to insert Circle"</span>, <span style="color:blue;">out</span> X, <span style="color:blue;">out</span> Y, <span style="color:blue;">out</span> Z);
dsTracker.<b>UpdateNotify</b> -= <span style="color:blue;">new</span> <span style="color:#2b91af;">_ITrackerEvents_UpdateNotifyEventHandler</span>(dsTracker_UpdateNotify);
<span style="color:blue;">if</span> (res == <span style="color:blue;">true</span>)
{
<span style="color:green;">//Set the center of the Circle where user clicked</span>
dsTempCircle.<b>SetCenter</b>(X, Y, Z);
<span style="color:green;">//Add temporary Circle to drawing</span>
dsSketchMgr.<b>AddTemporaryEntity</b>(dsTempCircle);
}
<span style="color:green;">//Remove tracker from command message if no longer needed</span>
dsCmdMsg.<b>RemoveTracker</b>(dsTracker);
}
<span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> dsTracker_<b>UpdateNotify</b>(<span style="color:#2b91af;">MathPoint</span> CursorPosition)
{
<span style="color:blue;">if</span> (CursorPosition == <span style="color:blue;">null</span>)
<span style="color:blue;">return</span>;
<span style="color:blue;">double</span> x = 0.0, y = 0.0, z = 0.0;
CursorPosition.<b>GetPosition</b>(<span style="color:blue;">out</span> x, <span style="color:blue;">out</span> y, <span style="color:blue;">out</span> z);
dsTempCircle.<b>SetCenter</b>(x, y, z);
}
}
}</pre>
</body>
</html>