Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
draftsight
/
opt
/
dassault-systemes
/
DraftSight
/
APISDK
/
Rotate_and_Copy_Entities_Example_CSharp.htm
|
|---|
<html>
<head>
<title>Rotate and Copy Entities 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,rotate entities">
<meta name=MS-HKWD content="C# examples,copy entities">
<meta name=MS-HKWD content="C# examples,command window">
<meta name=MS-HKWD content="C# examples,selection filter">
<meta name=MS-HKWD content="C# examples,Sketch Manager">
<meta name=MS-HKWD content="C# examples,Selection Manager">
</head>
<body>
<h1>Rotate and Copy Entities Example (C#)</h1>
<p>This example shows how to rotate and copy selected entities.</p>
<div style="font-family: Monospace; font-size: 10pt; background-color: white">
<p class="APICODE"><span style="color: Green">
//--------------------------------------------------------------<br>
// Preconditions:<br>
// 1. Create a C# Windows console project.<br>
// 2. Copy and paste this example into the C# IDE.<br>
// 3. Add a reference to:<br>
// <i>install_dir</i>\<b>APISDK\tlb\DraftSight.Interop.dsAutomation.dll</b>.<br>
// 4. Add references to <b>System</b> and <b>System.Windows.Forms</b>.<br>
// 5. Start DraftSight and open a new document.<br>
// 6. Construct multiple entities (e.g., circle, rectangle, 2D PolyLine, <br>
// etc.).<br>
// 7. Start debugging the project.<br>
//<br>
// Postconditions: <br>
// 1. When the prompt appears in the DraftSight command window,<br>
// select the entities to rotate and press the Enter key. The<br>
// selected entities are rotated.<br>
// 2. Execution stops so that you can examine the drawing to<br>
// verify that the selected entities were rotated. Click the <br>
// Continue button in the IDE to continue.<br>
// 3. The selected entities are copied.<br>
//----------------------------------------------------------------<br>
<br>
</span><span style="color: Blue">using</span><span style="color: Black">
System;<br>
</span><span style="color: Blue">using</span><span style="color: Black">
System.Collections.Generic;<br>
</span><span style="color: Blue">using</span><span style="color: Black">
System.Text;<br>
</span><span style="color: Blue">using</span><span style="color: Black">
DraftSight.Interop.dsAutomation;<br>
</span><span style="color: Blue">using</span><span style="color: Black">
System.Runtime.InteropServices;<br>
</span><span style="color: Blue">using</span><span style="color: Black">
System.Windows.Forms;<br>
</span><span style="color: Blue">using</span><span style="color: Black">
System.Diagnostics;<br>
<br>
</span><span style="color: Blue">namespace</span><span style="color: Black">
RotateSelectedEntities<br>
{<br>
</span><span style="color: Blue">class</span><span style="color: Black">
</span><span style="color: #2b91af">Program<br>
</span><span style="color: Black"> {<br>
</span><span style="color: Blue">static</span><span style="color: Black">
</span><span style="color: Blue">void</span><span style="color: Black">
Main(</span><span style="color: Blue">string</span><span style="color: Black">[]
args)<br>
{<br>
</span><span style="color: Green">//Connect to DraftSight
application<br>
</span><span style="color: Black"> DraftSight.Interop.dsAutomation.</span><span style="color: #2b91af">Application</span><span style="color: Black">
dsApp = ConnectToDraftSight();<br>
</span><span style="color: Blue">if</span><span style="color: Black">
(</span><span style="color: Blue">null</span><span style="color: Black"> ==
dsApp)<br>
{<br>
</span><span style="color: Blue">return</span><span style="color: Black">;<br>
}<br>
<br>
</span>dsApp.<b>AbortRunningCommand</b>(); // abort any command currently running in DraftSight to avoid nested commands<span style="color: Black"><br>
<br>
</span><span style="color: Green">//Get active document<br>
</span><span style="color: Black"> </span><span style="color: #2b91af">Document</span><span style="color: Black">
dsDoc = dsApp.<b>GetActiveDocument</b>();<br>
</span><span style="color: Blue">if</span><span style="color: Black">
(</span><span style="color: Blue">null</span><span style="color: Black"> ==
dsDoc)<br>
{<br>
</span><span style="color: #2b91af">MessageBox</span><span style="color: Black">.Show(</span><span style="color: #a31515">"There
are no open documents in DraftSight."</span><span style="color: Black">);<br>
</span><span style="color: Blue">return</span><span style="color: Black">;<br>
}<br>
<br>
</span><span style="color: Green">//Get model space<br>
</span><span style="color: Black"> </span><span style="color: #2b91af">Model</span><span style="color: Black">
dsModel = dsDoc.<b>GetModel</b>();<br>
<br>
</span><span style="color: Green">//Get Sketch Manager<br>
</span><span style="color: Black"> </span><span style="color: #2b91af">SketchManager</span><span style="color: Black">
dsSketchMgr = dsModel.<b>GetSketchManager</b>();<br>
<br>
</span><span style="color: Green">//Get selection manager<br>
</span><span style="color: Black"> </span><span style="color: #2b91af">SelectionManager</span><span style="color: Black">
dsSelectionMgr = dsDoc.<b>GetSelectionManager</b>();<br>
<br>
</span><span style="color: Green">//Get selection filter<br>
</span><span style="color: Black"> </span><span style="color: #2b91af">SelectionFilter</span><span style="color: Black">
dsSelectionFilter = dsSelectionMgr.<b>GetSelectionFilter</b>();<br>
<br>
</span><span style="color: Green">//Clear selection filter<br>
</span><span style="color: Black"> dsSelectionFilter.<b>Clear</b>();<br>
<br>
</span><span style="color: Green">//Add all entities to the
selection filter<br>
</span><span style="color: Black"> </span><span style="color: Blue">foreach</span><span style="color: Black">
(</span><span style="color: #2b91af">dsObjectType_e</span><span style="color: Black">
entityType </span><span style="color: Blue">in</span><span style="color: Black">
</span><span style="color: #2b91af">Enum</span><span style="color: Black">.GetValues(</span><span style="color: Blue">typeof</span><span style="color: Black">(</span><span style="color: #2b91af">dsObjectType_e</span><span style="color: Black">)))<br>
{<br>
dsSelectionFilter.<b>AddEntityType</b>(entityType);<br>
}<br>
<br>
</span><span style="color: Green">//Activate selection filter<br>
</span><span style="color: Black"> dsSelectionFilter.<b>Active</b>
= </span><span style="color: Blue">true</span><span style="color: Black">;<br>
<br>
</span><span style="color: Green">//Get command message object<br>
</span><span style="color: Black"> </span><span style="color: #2b91af">CommandMessage</span><span style="color: Black">
dsCommandMessage = dsApp.<b>GetCommandMessage</b>();<br>
<br>
</span><span style="color: Green">//Clear previous selection<br>
</span><span style="color: Black"> dsSelectionMgr.<b>ClearSelections</b>(</span><span style="color: #2b91af">dsSelectionSetType_e</span><span style="color: Black">.dsSelectionSetType_Previous);<br>
<br>
</span><span style="color: Green">//Run prompt to select
entities<br>
</span><span style="color: Black"> </span><span style="color: Blue">bool</span><span style="color: Black">
singleSelection = </span><span style="color: Blue">false</span><span style="color: Black">;<br>
</span><span style="color: Blue">string</span><span style="color: Black">
prompt = </span><span style="color: #a31515">"Select entities"</span><span style="color: Black">;<br>
</span><span style="color: Blue">string</span><span style="color: Black">
errorMessage = </span><span style="color: #a31515">"Unknown entity"</span><span style="color: Black">;<br>
</span><span style="color: Blue">if</span><span style="color: Black">
(dsCommandMessage.<b>PromptForSelection</b>(singleSelection, prompt,
errorMessage))<br>
{<br>
</span><span style="color: Green">//Get number of selected
objects<br>
</span><span style="color: Black"> </span><span style="color: Blue">int</span><span style="color: Black">
count = dsSelectionMgr.<b>GetSelectedObjectCount</b>(</span><span style="color: #2b91af">dsSelectionSetType_e</span><span style="color: Black">.dsSelectionSetType_Previous);<br>
<br>
</span><span style="color: #2b91af">DispatchWrapper</span><span style="color: Black">[]
dsEntities = </span><span style="color: Blue">new</span><span style="color: Black">
</span><span style="color: #2b91af">DispatchWrapper</span><span style="color: Black">[count];<br>
</span><span style="color: Blue">int</span><span style="color: Black">[]
dsEntityTypes = </span><span style="color: Blue">new</span><span style="color: Black">
</span><span style="color: Blue">int</span><span style="color: Black">[count];<br>
<br>
</span><span style="color: Green">//Get selected entities<br>
</span><span style="color: Black"> </span><span style="color: Blue">for</span><span style="color: Black">
(</span><span style="color: Blue">int</span><span style="color: Black">
index = 0; index < count; ++index)<br>
{<br>
</span><span style="color: #2b91af">dsObjectType_e</span><span style="color: Black">
entityType;<br>
</span><span style="color: Blue">object</span><span style="color: Black">
selectedEntity = dsSelectionMgr.<b>GetSelectedObject</b>(</span><span style="color: #2b91af">dsSelectionSetType_e</span><span style="color: Black">.dsSelectionSetType_Previous,
index, </span><span style="color: Blue">out</span><span style="color: Black">
entityType);<br>
dsEntities[index] = </span><span style="color: Blue">new</span><span style="color: Black">
</span><span style="color: #2b91af">DispatchWrapper</span><span style="color: Black">(selectedEntity);<br>
<br>
dsEntityTypes[index] = (</span><span style="color: Blue">int</span><span style="color: Black">)entityType;<br>
}<br>
<br>
</span><span style="color: Green">//Rotation parameters<br>
</span><span style="color: Black"> </span><span style="color: Blue">double</span><span style="color: Black">
pivotPointX = 0.0;<br>
</span><span style="color: Blue">double</span><span style="color: Black">
pivotPointY = 0.0;<br>
</span><span style="color: Blue">double</span><span style="color: Black">
rotateAngle = </span><span style="color: #2b91af">Math</span><span style="color: Black">.PI
/ 4; </span><span style="color: Green">//In radians<br>
<br>
</span><span style="color: Black"> </span><span style="color: Green">//Rotate
entities<br>
</span><span style="color: Black"> dsSketchMgr.<b>RotateEntities</b>(pivotPointX,
pivotPointY, rotateAngle, dsEntityTypes, dsEntities);<br>
<br>
</span><span style="color: Green">//Stop execution<br>
</span><span style="color: Black"> </span><span style="color: Green">//Examine
the document<br>
</span><span style="color: Black"> System.Diagnostics.</span><span style="color: #2b91af">Debugger</span><span style="color: Black">.Break();<br>
<br>
</span><span style="color: Green">//Click the Continue
button in the IDE<br>
<br>
</span><span style="color: Black"> </span><span style="color: Green">//Copy
parameters<br>
</span><span style="color: Black"> </span><span style="color: Blue">double</span><span style="color: Black">
displacementX = 2.0;<br>
</span><span style="color: Blue">double</span><span style="color: Black">
displacementY = 2.0;<br>
</span><span style="color: Blue">double</span><span style="color: Black">
displacementZ = 0.0;<br>
<br>
</span><span style="color: Green">//Copy entities<br>
</span><span style="color: Black"> dsSketchMgr.<b>CopyEntities</b>(displacementX,
displacementY, displacementZ, dsEntityTypes, dsEntities);<br>
<br>
<br>
}<br>
}<br>
<br>
</span><span style="color: Blue">private</span><span style="color: Black">
</span><span style="color: Blue">static</span><span style="color: Black">
DraftSight.Interop.dsAutomation.</span><span style="color: #2b91af">Application</span><span style="color: Black">
ConnectToDraftSight()<br>
{<br>
</span><span style="color: Green">//DraftSight.Interop.dsAutomation.Application
dsApp = null;<br>
</span><span style="color: Black"> DraftSight.Interop.dsAutomation.</span><span style="color: #2b91af">Application</span><span style="color: Black">
dsApp = </span><span style="color: Blue">null</span><span style="color: Black">;<br>
<br>
</span><span style="color: Blue">try<br>
</span><span style="color: Black"> {<br>
</span><span style="color: Green">//Connect to DraftSight<br>
</span><span style="color: Black"> dsApp = (DraftSight.Interop.dsAutomation.</span><span style="color: #2b91af">Application</span><span style="color: Black">)</span><span style="color: #2b91af">Marshal</span><span style="color: Black">.GetActiveObject(</span><span style="color: #a31515">"DraftSight.Application"</span><span style="color: Black">);<br>
}<br>
</span><span style="color: Blue">catch</span><span style="color: Black">
(</span><span style="color: #2b91af">Exception</span><span style="color: Black">
ex)<br>
{<br>
</span><span style="color: #2b91af">MessageBox</span><span style="color: Black">.Show(</span><span style="color: #a31515">"Failed
to connect to DraftSight. Cause: "</span><span style="color: Black"> +
ex.Message);<br>
dsApp = </span><span style="color: Blue">null</span><span style="color: Black">;<br>
}<br>
<br>
</span><span style="color: Blue">return</span><span style="color: Black">
dsApp;<br>
}<br>
}<br>
}</span></div>
</body>
</html>