Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
draftsight
/
opt
/
dassault-systemes
/
DraftSight
/
APISDK
/
Create_and_Change_EntityGroups_Example_CSharp.htm
|
|---|
<html>
<head>
<title>Create and Change EntityGroups 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,EntityGroups">
<meta name=MS-HKWD content="C# examples,groups">
</head>
<body>
<h1>Create and Change EntityGroups Example (C#)</h1>
<p>This example shows how to create and change EntityGroups.</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. Inserts entities, two lines and two circles, in the drawing.</span>
<span style="color:green;">// 2. Creates two EntityGroups:</span>
<span style="color:green;">// * SampleGroup1 contains two lines, line1 and line2.</span>
<span style="color:green;">// * SampleGroup2 contains two circles, circle1 and circle2.</span>
<span style="color:green;">// 3. Changes the color of the lines from white to red.</span>
<span style="color:green;">// 4. Removes an entity, circle1, from SampleGroup2.</span>
<span style="color:green;">// 5. Adds an entity, circle2, to SampleGroup1.</span>
<span style="color:green;">// 6. Reorders SampleGroup1.</span>
<span style="color:green;">// 7. Explodes SampleGroup2, which removes the definition</span>
<span style="color:green;">// from the drawing; however, circle1 remains as an entity</span>
<span style="color:green;">// in the drawing.</span>
<span style="color:green;">// 8. Renames SampleGroup1 and changes its description.
//
// <b>NOTE</b>: Execution of the macro stops several times. Follow the
// instructions in the macro each time execution stops.</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;">using</span> System.Diagnostics;
<span style="color:blue;">namespace</span> EntityGroup
{
<span style="color:blue;">static</span> <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;">Document</span> dsDoc;
<span style="color:blue;">static</span> <span style="color:blue;">void</span> Main()
{
<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>
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;">//Insert entities</span>
<span style="color:#2b91af;">SketchManager</span> dsSketchMgr = dsApp.<b>GetActiveDocument</b>().GetModel().GetSketchManager();
<span style="color:#2b91af;">Line</span> line1 = dsSketchMgr.<b>InsertLine</b>(0, 0, 0, 10, 10, 0);
<span style="color:#2b91af;">Line</span> line2 = dsSketchMgr.<b>InsertLine</b>(5, 0, 0, 15, 10, 0);
<span style="color:#2b91af;">Circle</span> circle1 = dsSketchMgr.<b>InsertCircle</b>(5, 5, 0, 10);
<span style="color:#2b91af;">Circle</span> circle2 = dsSketchMgr.<b>InsertCircle</b>(10, 5, 0, 10);
<span style="color:#2b91af;">DispatchWrapper</span>[] EntitiesArray1 = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>[2];
EntitiesArray1[0] = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>(line1);
EntitiesArray1[1] = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>(line2);
<span style="color:#2b91af;">DispatchWrapper</span>[] EntitiesArray2 = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>[2];
EntitiesArray2[0] = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>(circle1);
EntitiesArray2[1] = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>(circle2);
CreateGroups(EntitiesArray1, EntitiesArray2);
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break();
<span style="color:green;">//Type GROUP at the command window</span>
<span style="color:green;">//to verify that two EntityGroups, SampleGroup1</span>
<span style="color:green;">//and SampleGroup2, were created</span>
<span style="color:green;">//Press OK to close the dialog </span>
<span style="color:green;">//Press F5 in the IDE to continue</span>
<span style="color:green;">//Get SampleGroup2 and remove circle2</span>
<span style="color:#2b91af;">Group</span> dsGroup2 = dsDoc.<b>GetGroup</b>(<span style="color:#a31515;">"SampleGroup2"</span>);
<span style="color:blue;">if</span> (dsGroup2.<b>HasEntity</b>(circle2))
{
<span style="color:blue;">int</span> index = dsGroup2.<b>GetIndex</b>(circle2);
dsGroup2.<b>RemoveEntityAt</b>(index);
}
<span style="color:green;">//Get SampleGroup1 and add circle1 at last position</span>
<span style="color:#2b91af;">Group</span> dsGroup1 = dsDoc.<b>GetGroup</b>(<span style="color:#a31515;">"SampleGroup1"</span>);
<span style="color:#2b91af;">DispatchWrapper</span>[] EntitiesArray = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>[1];
EntitiesArray[0] = <span style="color:blue;">new</span> <span style="color:#2b91af;">DispatchWrapper</span>(circle1);
<span style="color:blue;">int</span> count = dsGroup1.<b>GetEntitiesCount</b>();
dsGroup1.<b>InsertEntitiesAt</b>(count, EntitiesArray);
<span style="color:blue;">int</span> newCount = dsGroup1.<b>GetEntitiesCount</b>();
<span style="color:blue;">if</span> (newCount != count + 1)
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Circle1 was not inserted."</span>);
<span style="color:green;">//Move circle1 to second position (index 1)</span>
<span style="color:blue;">int</span> circleIndex = dsGroup1.<b>GetIndex</b>(circle1);
<span style="color:blue;">if</span> (circleIndex != count)
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Circle1 inserted at wrong position."</span>);
dsGroup1.Reorder(circleIndex, 1, 1);
circleIndex = dsGroup1.<b>GetIndex</b>(circle1);
<span style="color:blue;">if</span> (circleIndex != 1)
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Circle1 inserted at wrong position."</span>);
<span style="color:green;">//Explode SampleGroup2</span>
dsGroup2.<b>Explode</b>();
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break();
<span style="color:green;">//Type GROUP at the command window</span>
<span style="color:green;">//to verify that only SampleGroup1 exists</span>
<span style="color:green;">//Press OK to close the dialog </span>
<span style="color:green;">//Press F5 in the IDE to continue</span>
<span style="color:blue;">object</span> dsGroups = <span style="color:blue;">null</span>;
dsGroups = dsDoc.<b>GetGroups</b>();
<span style="color:blue;">object</span>[] groupObjects = (<span style="color:blue;">object</span>[])dsGroups;
<span style="color:blue;">if</span> (groupObjects.Length != 1)
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Group2 was not exploded."</span>);
<span style="color:green;">//Rename SampleGroup1 and change description</span>
dsGroup1.<b>Rename</b>(<span style="color:#a31515;">"SampleGroup"</span>);
dsGroup1.<b>Description</b> = <span style="color:#a31515;">"My sample group."</span>;
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break();
<span style="color:green;">//Type GROUP at the command window</span>
<span style="color:green;">//to verify that SampleGroup1 was renamed</span>
<span style="color:green;">//to SampleGroup and its description</span>
<span style="color:green;">//changed to "My sample group"</span>
<span style="color:green;">//Press OK to close the dialog </span>
<span style="color:green;">//Press F5 in the IDE to continue</span>
}
<span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> CreateGroups(<span style="color:#2b91af;">DispatchWrapper</span>[] EntitiesArray1, <span style="color:#2b91af;">DispatchWrapper</span>[] EntitiesArray2)
{
<span style="color:green;">//Create group</span>
<span style="color:#2b91af;">Group</span> dsGroup1 = dsDoc.<b>CreateGroup</b>(<span style="color:#a31515;">"SampleGroup1"</span>, <span style="color:blue;">false</span>, <span style="color:#a31515;">"This is my first sample group."</span>, EntitiesArray1);
<span style="color:blue;">object</span> dsEntities = <span style="color:blue;">null</span>;
<span style="color:blue;">object</span> dsEntityTypes = <span style="color:blue;">null</span>;
dsEntities = dsGroup1.<b>GetEntities</b>(<span style="color:blue;">out</span> dsEntityTypes);
<span style="color:blue;">object</span>[] entityObjects = (<span style="color:blue;">object</span>[])dsEntities;
<span style="color:blue;">int</span>[] dsEntityTypesArray = (<span style="color:blue;">int</span>[])dsEntityTypes;
<span style="color:blue;">if</span> (<span style="color:blue;">null</span> == entityObjects && <span style="color:blue;">null</span> == dsEntityTypesArray)
{
<span style="color:blue;">return</span>;
}
<span style="color:green;">//Change color of lines from white to red</span>
<span style="color:#2b91af;">EntityHelper</span> dsEntityHelper = dsApp.<b>GetEntityHelper</b>();
<span style="color:#2b91af;">Color</span> dsColor = dsApp.<b>GetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Red);
<span style="color:blue;">foreach</span> (<span style="color:blue;">object</span> dsEntity <span style="color:blue;">in</span> entityObjects)
{
<span style="color:blue;">if</span> (dsEntity != <span style="color:blue;">null</span>)
{
dsEntityHelper.<b>SetColor</b>(dsEntity, dsColor);
}
}
<span style="color:#2b91af;">Group</span> dsGroup2 = dsDoc.<b>CreateGroup</b>(<span style="color:#a31515;">"SampleGroup2"</span>, <span style="color:blue;">false</span>, <span style="color:#a31515;">"This is my second sample group."</span>, EntitiesArray2);
}
}
}</pre>
</body>
</html>