Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
<html>
<head>
<title>Create Table 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,Tables">
<meta name=MS-HKWD content="C# examples,TableStyles">
<meta name=MS-HKWD content="C# examples,TableStyle Manager">
<meta name=MS-HKWD content="C# examples,Layers">
<meta name=MS-HKWD content="C# examples,transparency">
<meta name=MS-HKWD content="C# examples,Colors">
</head>
<body>
<h1>Create Table Example (C#)</h1>
<p>This example shows how to change the transparency of the active Layer, create
a TableStyle and Table, and change the background colors of the top row and a cell
in the Table.</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. Open the Immediate window.</span>
<span style="color:green;">// 5. Add references to <b>System</b> and <b>System.Windows.Forms</b>.</span>
<span style="color:green;">// 6. Start DraftSight and open a new document.</span>
<span style="color:green;">// 7. Press F5.
//</span>
<span style="color: #008000">//</span>
<span style="color:green;">// Postconditions:
// 1. Gets the transparency object for the active Layer and prints its
// transparency percentage to the Immediate window.
// 2. Changes the transparency percentage of the active Layer and prints
// its new percentage to the Immediate window.
// 3. Creates and activates a new TableStyle named <b>Sample Table Style</b>.
// 4. Inserts a Table and applies the <b>Sample Table Style</b> TableStyle</span>.<span style="color:green;">
// 5. Gets the transparency object for the Table.
// 6. Prints whether the transparency object for the Table is <b>ByLayer</b>.
// 7. If not, then changes and updates the transparency object
// of the Table to <b>ByLayer</b>.
// 8. Changes the top row's background color.
// 9. Changes the background color of the cell in the second row
// and second column to match the top row's background color.
//10. Prints the name of the TableStyle to the Immediate window.
//11. Examine the drawing and the Immediate window.
//----------------------------------------------------------------</span> </pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white">
<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;">using</span> System.Diagnostics;
<span style="color:blue;">static</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Module1</span>{
<span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main() {
DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">Application</span> dsApp;
<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</span>
<span style="color:green;">//to avoid nested commands</span>
dsApp.<b>AbortRunningCommand</b>();
<span style="color:green;">//Get active document</span>
<span style="color:#2b91af;">Document</span> dsDoc = <span style="color:blue;">default</span>(<span style="color:#2b91af;">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>;
}</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:green;"> //Get how transparency is defined</span>
<span style="color:#2b91af;"> LayerManager</span> dsLayerManager = <span style="color:blue;">default</span>(<span style="color:#2b91af;">LayerManager</span>);
<span style="color:#2b91af;"> Layer</span> dsLayer = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Layer</span>);
dsLayerManager = dsDoc.<b>GetLayerManager</b>();
dsLayer = dsLayerManager.<b>GetActiveLayer</b>();
<span style="color:#2b91af;"> Debug</span>.Print(<span style="color:#a31515;">"Transparency percentage for active Layer: "</span> + <span style="color:#2b91af;">Convert</span>.ToString(dsLayer.<b>Transparency</b>));
<span style="color:green;"> //Set new transparency percentage for the active Layer</span>
dsLayer.<b>Transparency</b> = 50;
<span style="color:#2b91af;"> Debug</span>.Print(<span style="color:#a31515;">"New transparency percentage for active Layer: "</span> + <span style="color:#2b91af;">Convert</span>.ToString(dsLayer.<b>Transparency</b>));
<span style="color:green;">//Get the TableStyle Manager</span>
<span style="color:#2b91af;">TableStyleManager</span> dsTableStyleManager = <span style="color:blue;">default</span>(<span style="color:#2b91af;">TableStyleManager</span>);
dsTableStyleManager = dsDoc.<b>GetTableStyleManager</b>();
<span style="color:green;">//Create the TableStyle</span>
<span style="color:#2b91af;">TableStyle</span> dsTableStyle = <span style="color:blue;">default</span>(<span style="color:#2b91af;">TableStyle</span>);
<span style="color:blue;">string</span> sampleTableStyle = <span style="color:blue;">null</span>;
sampleTableStyle = <span style="color:#a31515;">"Sample Table Style"</span>;
<span style="color:#2b91af;"> dsCreateObjectResult_e</span> result;
dsTableStyleManager.<b>CreateTableStyle</b>(sampleTableStyle, <span style="color:blue;">out</span> dsTableStyle, <span style="color:blue;">out</span> result);
<span style="color:green;">//Set the TableStyle styles</span>
<span style="color:green;">//Create the Table in a downward direction</span>
dsTableStyle.<b>HeaderOrientation</b> = <span style="color:#2b91af;">dsTableHeaderOrientation_e</span>.dsTableHeaderOrientation_Down;
<span style="color:green;">//Set the Colors for the Table </span><span style="color: #008000">backgrounds</span>
<span style="color:#2b91af;">Color</span> dsColorHeader = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Color</span>);
<span style="color:#2b91af;">Color</span> dsColorData = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Color</span>);
<span style="color:#2b91af;">Color</span> dsColorTitle = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Color</span>);
</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> dsColorHeader = dsTableStyle.<b>GetBackgroundColor</b>(<span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Header);
dsColorHeader.<b>SetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Cyan);
dsTableStyle.<b>SetBackgroundColor</b>(<span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Header, dsColorHeader);
dsColorData = dsTableStyle.<b>GetBackgroundColor</b>(<span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Data);
dsColorData.<b>SetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Green);
dsTableStyle.<b>SetBackgroundColor</b>(<span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Data, dsColorData);
dsColorTitle = dsTableStyle.<b>GetBackgroundColor</b>(<span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Title);
dsColorTitle.<b>SetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_White);
dsTableStyle.<b>SetBackgroundColor</b>(<span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Title, dsColorTitle);</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white">
<span style="color:green;">//Activate the TableStyle</span>
dsTableStyle.<b>Activate</b>();
<span style="color:green;">//Get the model document</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 the Sketch Manager</span>
<span style="color:#2b91af;">SketchManager</span> dsSketchManager = <span style="color:blue;">default</span>(<span style="color:#2b91af;">SketchManager</span>);
dsSketchManager = dsModel.<b>GetSketchManager</b>();
<span style="color:green;">//Insert the Table</span>
<span style="color:#2b91af;">Table</span> dsTable = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Table</span>);
<span style="color:blue;">double</span> x = 0;
x = 6;
<span style="color:blue;">double</span> y = 0;
y = 6;
<span style="color:blue;">int</span> row = 0;
row = 4;
<span style="color:blue;">int</span> col = 0;
col = 5;
<span style="color:blue;">double</span> rowHeight = 0;
rowHeight = 2;
<span style="color:blue;">double</span> colWidth = 0;
colWidth = 4;
dsTable = dsSketchManager.<b>InsertTable</b>(x, y, row, col, rowHeight, colWidth, <span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Title, <span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Header, <span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Data);</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:green;"> //Get transparency object for Table</span>
<span style="color:#2b91af;"> Transparency</span> dsTransparency = <span style="color:blue;">default</span>(<span style="color:#2b91af;">Transparency</span>);
dsTransparency = dsTable.<b>Transparency</b>;
<span style="color:green;"> //Find out if Table's transparency is ByLayer</span>
<span style="color:#2b91af;"> Debug</span>.Print (<span style="color:#a31515;">"Transparency defined ByLayer: "</span> + <span style="color:#2b91af;">Convert</span>.ToString(dsTransparency.<b>IsByLayer</b>()));
<span style="color:green;"> //If not, then change Table's transparency to ByLayer and</span>
<span style="color:green;"> //update Table's transparency object</span>
<span style="color:blue;"> if</span> (dsTransparency.<b>IsByLayer</b>() == <span style="color:blue;">false</span>)
{
dsTransparency.<b>SetByLayer</b>();
<span style="color:#2b91af;"> Debug</span>.Print(<span style="color:#a31515;">"Transparency now ByLayer: "</span> + <span style="color:#2b91af;">Convert</span>.ToString(dsTransparency.<b>IsByLayer</b>()));
<span style="color:green;"> //Update Table's transparency object</span>
dsTable.Transparency = <b>dsTransparency</b>;
}
<span style="color:green;"> //Change Table's top row's background color</span>
dsColorTitle.<b>SetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Yellow);
dsTable.<b>SetBackgroundColor</b>(<span style="color:#2b91af;">dsTableCellType_e</span>.dsTableCellType_Title, dsColorTitle);
<span style="color:green;"> //Change background color of cell in second row and</span>
<span style="color:green;"> //second column to match the top row's background color</span>
dsTable.<b>SetCellBackgroundColor</b>(1,1,dsColorTitle);
<span style="color:green;">//Get the name of the TableStyle for the just-inserted Table</span>
<span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">"TableStyle applied to this table: "</span> + dsTableStyle.<b>Name</b>);</pre>
<pre style="font-family: Consolas; font-size: 13; color: black; background: white"> <span style="color:green;"> //Zoom to fit</span>
dsApp.Zoom(<span style="color:#2b91af;">dsZoomRange_e</span>.dsZoomRange_Fit, <span style="color:blue;">null</span>, <span style="color:blue;">null</span>);
}
}</pre>
</body>
</html>