Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
<html>
<head>
<title>Insert Splines 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,Splines">
<meta name=MS-HKWD content="C# examples,points">
<meta name=MS-HKWD content="C# examples,prompts">
<meta name=MS-HKWD content="C# examples,commands">
<meta name=MS-HKWD content="C# examples,events">
<meta name=MS-HKWD content="C# examples,notifications">
<meta name=MS-HKWD content="C# examples,entities">
<meta name=MS-HKWD content="C# examples,Layers">
</head>
<body>
<h1>Insert Splines Example (C#)</h1>
<p>This example shows how to:</p>
<ul>
<li><font face="Verdana" style="font-size: 8pt">construct several Splines, which are displayed as
the Dassault Systèmes logo in a DraftSight drawing. </font></li>
<li><font face="Verdana" style="font-size: 8pt">fire events before
and after commands are executed.</font></li>
<li><font face="Verdana" style="font-size: 8pt">select a group of entities
and modify a member in that group of entities.</font></li>
<li><font face="Verdana" style="font-size: 8pt">execute a command using
IApplication::RunCommand.</font></li>
</ul>
<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 the DraftSight type library,</span>
<span style="color:green;">// i<i>nstall_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.</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. CommandPreNotify event is fired. Click <b>OK</b> to close</span>
<span style="color:green;">// the message box.</span>
<span style="color:green;">// 2. Click anywhere in the drawing when you are prompted</span>
<span style="color:green;">// in the command window to <b>Click to insert a point</b></span>
<span style="color:green;">// <b>for the lower-left corner for the 3DS logo</b>.</span>
<span style="color:green;">// 3. CommandPostNotify event is fired. Click <b>OK</b> to close</span>
<span style="color:green;">// the message box.</span>
<span style="color:green;">// 4. The Dassault Systemes logo is constructed in the drawing.</span>
<span style="color:green;">// a. Examine the drawing to verify.</span>
<span style="color:green;">// b. </span><span style="color:green;">Press F5 </span><span style="color:green;">in the IDE.</span>
<span style="color:green;">// The Dassault Systemes logo's letter D is changed </span>
<span style="color:green;">// from blue to yellow.</span>
<span style="color:green;">// c. </span><span style="color:green;">Press F5 to </span><span style="color:green;">in the IDE.</span>
<span style="color:green;">// The Dassault Systemes logo's letter D is changed</span>
<span style="color:green;">// back to blue.</span>
<span style="color:green;">// d. </span><span style="color:green;">Press F5 </span><span style="color:green;">in the IDE.</span>
<span style="color:green;">// 5. CommandPreNotify event is fired. Click OK to close the</span>
<span style="color:green;">// the message box.</span>
<span style="color:green;">// 6. CommandPostNotify event is fired. Click OK to close the</span>
<span style="color:green;">// message box.</span>
<span style="color:green;">// 7. The Dassault Systemes logo is deleted.</span>
<span style="color:green;">//----------------------------------------------------------------</span>
<span style="color:blue;">using</span> DraftSight.Interop.dsAutomation;
<span style="color:blue;">using</span> System;
<span style="color:blue;">using</span> System.Windows.Forms;
<span style="color:blue;">using</span> System.Runtime.InteropServices;
<span style="color:blue;">static</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Module1</span>
{</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:blue;">static</span> DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">Application</span> dsApp = <span style="color:blue;">default</span>(DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">Application</span>);
<span style="color:blue;">static</span> <span style="color:blue;">string</span> commandPreNotifyCommand = <span style="color:#a31515;">" "</span>;
<span style="color:blue;">static</span> <span style="color:blue;">string</span> commandPostNotifyCommand = <span style="color:#a31515;">" "</span>;
<span style="color:blue;">static</span> <span style="color:#2b91af;">Document</span> dsDoc = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Document</span>);
<span style="color:blue;"> public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main()
{</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:#2b91af;"> SketchManager</span> dsSketchMgr;
<span style="color:#2b91af;"> SelectionManager</span> dsSelectionManager;
<span style="color:#2b91af;"> SelectionFilter</span> dsSelectionFilter;
<span style="color:#2b91af;"> EntityHelper</span> dsEntityHelper;
<span style="color:green;">// Connect to DraftSight</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</span>
<span style="color:green;">// avoid nested commands</span>
dsApp.<b>AbortRunningCommand</b>();</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:green;"> //Set up dsApp events</span>
AttachAppEventHandlers();
<span style="color:green;">// Get command message object</span>
<span style="color:#2b91af;">CommandMessage</span> dsCommandMessage = <span style="color:blue;">default</span>(<span style="color:#2b91af;">CommandMessage</span>);
dsCommandMessage = dsApp.<b>GetCommandMessage</b>();
<span style="color:green;">//Get active document</span>
dsDoc = dsApp.<b>GetActiveDocument</b>();
<span style="color:blue;">if</span> (dsDoc == <span style="color:blue;">null</span>) {
<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 model space</span>
<span style="color:#2b91af;">Model</span> dsModel = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Model</span>);
dsModel = dsDoc.<b>GetModel</b>();
<span style="color:green;">// Get Sketch Manager</span>
dsSketchMgr = dsModel.<b>GetSketchManager</b>();
<span style="color:blue;">double</span> x = 0;
<span style="color:blue;">double</span> y = 0;
<span style="color:blue;">double</span> z = 0;
<span style="color:green;">// Prompt to insert the a point for lower-left corner of the 3DS logo</span>
<span style="color:#2b91af;">MathUtility</span> dsMathUtility = <span style="color:blue;">default</span>(<span style="color:#2b91af;">MathUtility</span>);
<span style="color:#2b91af;">MathPlane</span> dsMathPlane = <span style="color:blue;">default</span>(<span style="color:#2b91af;">MathPlane</span>);
dsMathUtility = dsApp.<b>GetMathUtility</b>();
dsMathPlane = dsMathUtility.<b>CreateXYPlane</b>();
<span style="color:blue;">bool</span> status = <span style="color:blue;">false</span>;
<span style="color:blue;"> string</span> mssge = <span style="color:#a31515;">"Click to insert a point for the lower-left corner for the 3DS logo"</span>;
status = dsCommandMessage.<b>PromptForPoint2</b>(mssge, <span style="color:blue;">true</span>, 0, 0, 0, <span style="color:blue;">out</span> x, <span style="color:blue;">out</span> y, <span style="color:blue;">out</span> z, dsMathPlane);
<span style="color:blue;">double</span>[] spArray1 = <span style="color:blue;">new</span> <span style="color:blue;">double</span>[27];
<span style="color:blue;">double</span>[] spArray2 = <span style="color:blue;">new</span> <span style="color:blue;">double</span>[24];
<span style="color:blue;">double</span>[] spArray3 = <span style="color:blue;">new</span> <span style="color:blue;">double</span>[18];
<span style="color:green;">// Construct the D</span>
spArray1[0] = x + 0.4513;
spArray1[1] = y + 0.3825;
spArray1[2] = z + 0.0;
spArray1[3] = x + 0.324;
spArray1[4] = y + 0.1912;
spArray1[5] = z + 0.0;
spArray1[6] = x + 0.1261;
spArray1[7] = y + 0.0932;
spArray1[8] = z + 0.0;
spArray1[9] = x + 0.2571;
spArray1[10] = y + 0.3839;
spArray1[11] = z + 0.0;
spArray1[12] = x + 0.0023;
spArray1[13] = y + 0.0086;
spArray1[14] = z + 0.0;
spArray1[15] = x + 0.2132;
spArray1[16] = y + 0.0711;
spArray1[17] = z + 0.0;
spArray1[18] = x + 0.5275;
spArray1[19] = y + 0.4664;
spArray1[20] = z + 0.0;
spArray1[21] = x + 0.428;
spArray1[22] = y + 0.5052;
spArray1[23] = z + 0.0;
spArray1[24] = x + 0.1237;
spArray1[25] = y + 0.4568;
spArray1[26] = z + 0.0;
<span style="color:#2b91af;">Spline</span> spline1 = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Spline</span>);
spline1 = dsSketchMgr.<b>InsertSpline</b>(spArray1, <span style="color:blue;">true</span>, 0, 0, 0, 0, 0, 0);
<span style="color:green;">// Construct the S</span>
spArray2[0] = x + 0.4659;
spArray2[1] = y + 0.1472;
spArray2[2] = 0.0;
spArray2[3] = x + 0.8218;
spArray2[4] = y + 0.2052;
spArray2[5] = z + 0.0;
spArray2[6] = x + 0.6099;
spArray2[7] = y + 0.5472;
spArray2[8] = z + 0.0;
spArray2[9] = x + 0.7898;
spArray2[10] = y + 0.6372;
spArray2[11] = z + 0.0;
spArray2[12] = x + 0.9877;
spArray2[13] = y + 0.5952;
spArray2[14] = z + 0.0;
spArray2[15] = x + 0.7158;
spArray2[16] = y + 0.5472;
spArray2[17] = z + 0.0;
spArray2[18] = x + 0.9318;
spArray2[19] = y + 0.2232;
spArray2[20] = z + 0.0;
spArray2[21] = x + 0.7818;
spArray2[22] = y + 0.1112;
spArray2[23] = z + 0.0;
<span style="color:#2b91af;">Spline</span> spline2 = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Spline</span>);
spline2 = dsSketchMgr.<b>InsertSpline</b>(spArray2, <span style="color:blue;">true</span>, 0, 0, 0, 0, 0, 0);
<span style="color:green;">// Construct the 3</span>
spArray3[0] = x + 0.6319;
spArray3[1] = y + 0.8672;
spArray3[2] = z + 0.0;
spArray3[3] = x + 0.33;
spArray3[4] = y + 0.9233;
spArray3[5] = z + 0.0;
spArray3[6] = x + 0.5;
spArray3[7] = y + 0.9642;
spArray3[8] = z + 0.0;
spArray3[9] = x + 0.7318;
spArray3[10] = y + 0.8952;
spArray3[11] = z + 0.0;
spArray3[12] = x + 0.6279;
spArray3[13] = y + 0.6892;
spArray3[14] = z + 0.0;
spArray3[15] = x + 0.369;
spArray3[16] = y + 0.5563;
spArray3[17] = z + 0.0;
<span style="color:#2b91af;">Spline</span> spline3 = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Spline</span>);
spline3 = dsSketchMgr.<b>InsertSpline</b>(spArray3, <span style="color:blue;">true</span>, 0, 0, 0, 0, 0, 0);
<span style="color:green;">// Set the colors for the logo</span>
<span style="color:#2b91af;">Color</span> color1 = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Color</span>);
<span style="color:#2b91af;">Color</span> color2 = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Color</span>);
<span style="color:#2b91af;">Color</span> color3 = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Color</span>);
color1 = spline1.<b>Color</b>;
color2 = spline2.<b>Color</b>;
color3 = spline3.<b>Color</b>;
color1.<b>SetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Blue);
color2.<b>SetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Yellow);
color3.<b>SetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Red);
spline1.<b>Color</b> = color1;
spline2.<b>Color</b> = color2;
spline3.<b>Color</b> = color3;</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:green;"> // Examine the drawing to verify that</span>
<span style="color:green;"> // the logo was created and that</span>
<span style="color:green;"> // the letter D is blue, the </span>
<span style="color:green;"> // letter S is yellow, and the non-letter </span>
<span style="color:green;"> // is red</span>
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break();
<span style="color:green;"> // Press F5 to change the colors of the logo</span>
<span style="color:green;"> //Get Selection Manager</span>
dsSelectionManager = dsDoc.<b>GetSelectionManager</b>();
<span style="color:green;"> //Get selection filter</span>
dsSelectionFilter = dsSelectionManager.<b>GetSelectionFilter</b>();
<span style="color:green;"> //Clear selection filter</span>
dsSelectionFilter.<b>Clear</b>();
<span style="color:green;"> //Add Spline entities to the selection filter</span>
dsSelectionFilter.<b>AddEntityType</b>(<span style="color:#2b91af;">dsObjectType_e</span>.dsSplineType);
<span style="color:green;"> //Activate selection filter</span>
dsSelectionFilter.<b>Active</b> = <span style="color:blue;">true</span>;
<span style="color:green;"> //Get all layer names</span>
<span style="color:blue;"> string</span>[] layerNames = GetLayers(dsDoc);
<span style="color:blue;"> object</span> entityType = <span style="color:blue;">null</span>;
<span style="color:blue;"> object</span> entityObject = <span style="color:blue;">null</span>;
<span style="color:blue;"> object</span>[] entityObjects = <span style="color:blue;">null</span>;
<span style="color:green;"> //Get Spline entities</span>
dsSketchMgr.<b>GetEntities</b>(dsSelectionFilter, layerNames, <span style="color:blue;">out</span> entityType, <span style="color:blue;">out</span> entityObject);
entityObjects = (<span style="color:blue;">object</span>[])entityObject;
<span style="color:green;"> // Get EntityHelper</span>
dsEntityHelper = (<span style="color:#2b91af;">EntityHelper</span>)dsApp.<b>GetEntityHelper</b>();
<span style="color:green;"> // Change the letter D in the logo from blue to yellow</span>
dsEntityHelper.<b>SetColor</b>(entityObjects[0], color2);
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break();
<span style="color:green;"> // Examine the drawing to verify that</span>
<span style="color:green;"> // the color of D has changed from blue to yellow</span>
<span style="color:green;"> // Press F5 to continue</span>
dsEntityHelper.<b>SetColor</b>(entityObjects[0], color1);
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break();
<span style="color:green;"> // Examine the drawing to verify that</span>
<span style="color:green;"> // the color of D has changed back to blue</span>
<span style="color:green;"> // Press F5 to delete the logo</span>
<span style="color:blue;">int</span> state;
state = (<span style="color:blue;">int</span>)dsApp.<b>RunCommand</b>(<span style="color:#a31515;">"DELETE ALL\n\n"</span>, <span style="color:blue;">false</span>);
DetachEventHandlers();</pre>
<blockquote>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white">
}
<span style="color:green;">//Attach dsApp event handlers</span>
<span style="color:blue;">static</span> <span style="color:blue;">public</span> <span style="color:blue;">void</span> AttachAppEventHandlers()
{
AttachAppEvents();
}
<span style="color:blue;">static</span> <span style="color:blue;">public</span> <span style="color:blue;">void</span> AttachAppEvents()
{
dsApp.CommandPreNotify += <span style="color:blue;">new</span> <span style="color:#2b91af;">_IApplicationEvents_CommandPreNotifyEventHandler</span>(CommandPreNotify);
dsApp.CommandPostNotify += <span style="color:blue;">new</span> <span style="color:#2b91af;">_IApplicationEvents_CommandPostNotifyEventHandler</span>(CommandPostNotify);
}
<span style="color:green;">//Detach all event handlers</span>
<span style="color:blue;">static</span> <span style="color:blue;">public</span> <span style="color:blue;">void</span> DetachEventHandlers()
{
dsApp.CommandPreNotify -= <span style="color:blue;">new</span> <span style="color:#2b91af;">_IApplicationEvents_CommandPreNotifyEventHandler</span>(CommandPreNotify);
dsApp.CommandPostNotify -= <span style="color:blue;">new</span> <span style="color:#2b91af;">_IApplicationEvents_CommandPostNotifyEventHandler</span>(CommandPostNotify);
}
<span style="color:blue;">static</span> <span style="color:blue;">public</span> <span style="color:blue;">void</span> CommandPreNotify(<span style="color:blue;">string</span> commandPreNotifyCommand, <span style="color:#2b91af;">Document</span> Document)
{
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"CommandPreNotify event was fired before "</span> + commandPreNotifyCommand + <span style="color:#a31515;">" was executed."</span>);
}
<span style="color:blue;">static</span> <span style="color:blue;">public</span> <span style="color:blue;">void</span> CommandPostNotify(<span style="color:blue;">string</span> commandPostNotifyCommand, <span style="color:#2b91af;">Document</span> Document)
{
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"CommandPostNotify event was fired after "</span> + commandPostNotifyCommand + <span style="color:#a31515;">" was executed."</span>);
} </pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">string</span>[] GetLayers(<span style="color:#2b91af;">Document</span> dsDoc)
{
<span style="color:green;">//Get Layer Manager and Layer names</span>
<span style="color:#2b91af;">LayerManager</span> dsLayerManager = dsDoc.<b>GetLayerManager</b>();
<span style="color:blue;">object</span>[] dsLayers = (<span style="color:blue;">object</span>[])dsLayerManager.<b>GetLayers</b>();
<span style="color:blue;">string</span>[] layerNames = <span style="color:blue;">new</span> <span style="color:blue;">string</span>[dsLayers.Length];
<span style="color:blue;">for</span> (<span style="color:blue;">int</span> index = 0; index < dsLayers.Length; ++index)
{
<span style="color:#2b91af;">Layer</span> dsLayer = dsLayers[index] <span style="color:blue;">as</span> <span style="color:#2b91af;">Layer</span>;
layerNames[index] = dsLayer.<b>Name</b>;
}
<span style="color:blue;">return</span> layerNames;
}</pre>
</blockquote>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white">}</pre>
</body>
</html>