Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
<html>
<head>
<title>Insert Shapes 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,shapes">
<meta name=MS-HKWD content="C# examples,Shape Manager">
</head>
<body>
<h1>Insert Shapes Example (C#)</h1>
<p>This example shows how to load a shape file and insert the shapes in the
loaded shape file into a drawing.</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. Gets the Shape Manager.</span>
<span style="color:green;">// 2. Loads a shape file.</span>
<span style="color:green;">// 3. Gets the shapes in the loaded shape file.</span>
<span style="color:green;">// 4. Inserts the shapes, with an obliquing angle of 30, in the drawing.</span>
<span style="color:green;">// 5. Examine the drawing.</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> ShapeCSharp
{
<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;">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 the Shape Manager</span>
<span style="color:#2b91af;">ShapeManager</span> dsShapeMgr = dsDoc.<b>GetShapeManager</b>();
<span style="color:green;">//Load shape file LTypeShp.shx</span>
<span style="color:#2b91af;">SystemFiles</span> dsSysFile = dsApp.GetSystemFiles();
<span style="color:blue;">object</span>[] FontsPathObj = (<span style="color:blue;">object</span>[])dsSysFile.FontsPaths;
<span style="color:blue;">string</span> fontPath = <span style="color:#a31515;">""</span>;
<span style="color:blue;">if</span> (FontsPathObj.Length > 0)
fontPath = FontsPathObj[0].ToString();
<span style="color:blue;">string</span> shapeFileName = System.IO.<span style="color:#2b91af;">Path</span>.Combine(fontPath, <span style="color:#a31515;">"LTypeShp.shx"</span>);
dsShapeMgr.<b>Load</b>(shapeFileName);
<span style="color:green;">//Get shapes</span>
<span style="color:blue;">object</span>[] shapesStrList = (<span style="color:blue;">object</span>[])dsShapeMgr.<b>GetAvailableShapes</b>();
<span style="color:blue;">int</span> shapesCount = shapesStrList.Length;
<span style="color:green;">//Get Sketch Manager</span>
<span style="color:#2b91af;">SketchManager</span> dsSketchMgr = dsDoc.GetModel().<b>GetSketchManager</b>();
<span style="color:green;">//Insert shapes</span>
<span style="color:blue;">double</span> PositionX = 0.0, PositionY = 0.0, PositionZ = 0.0;
<span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i < shapesCount; ++i)
{
<span style="color:#2b91af;">Shape</span> dsShape = dsSketchMgr.<b>InsertShape</b>(shapesStrList[i].ToString(), PositionX, PositionY, PositionZ, 1, 0.0);
<span style="color:green;">//Change obliquing property</span>
dsShape.<b>Obliquing</b> = 30 * <span style="color:#2b91af;">Math</span>.PI / 180;
<span style="color:blue;">double</span> X1, Y1, Z1, X2, Y2, Z2;
dsShape.<b>GetBoundingBox</b>(<span style="color:blue;">out</span> X1, <span style="color:blue;">out</span> Y1, <span style="color:blue;">out</span> Z1, <span style="color:blue;">out</span> X2, <span style="color:blue;">out</span> Y2, <span style="color:blue;">out</span> Z2);
PositionX += (X2 - X1) + 5;
}
}
}
}</pre>
</body>
</html>