Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
draftsight
/
opt
/
dassault-systemes
/
DraftSight
/
APISDK
/
Create_and_Remove_Dictionaries_Example_CSharp.htm
|
|---|
<html>
<head>
<title>Create and Remove Dictionaries 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,dictionaries">
<meta name=MS-HKWD content="C# examples,XRecords">
<meta name=MS-HKWD content="C# examples,extension dictionaries">
</head>
<body>
<h1>Create and Release Dictionaries Example (C#)</h1>
<p>This example shows how to create and release dictionaries.</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. Constructs a Circle. </span>
<span style="color: green">// 2. Gets the root dictionary of the document.</span>
<span style="color: green">// 3. Gets the existing table-style dictionary of the document.</span>
<span style="color: green">// 4. Gets the active style in the table-style dictionary and</span>
<span style="color: green">// prints the style type to the command window.</span>
<span style="color: green">// 5. Creates a dictionary in the root dictionary called <b>Our_Dict</b></span>
<span style="color: green">// and prints the name of the dictionary to the command window.</span>
<span style="color: green">// 6. Adds an XRecord entry to <b>Out_Dict</b> dictionary and prints</span>
<span style="color: green">// the name of the XRecord to the command window.</span>
<span style="color: green">// 7. Creates an extension dictionary for the Circle.</span>
<span style="color: green">// 8. Adds XRecord entries to the Circle's extension dictionary.</span>
<span style="color: green">// 9. Reads the XRecord entries in the Circle's extension dictionary</span>
<span style="color: green">// and prints their data to the command window.</span>
<span style="color: green">// 10. Removes the XRecord entries from the Circle's extension dictionary.</span>
<span style="color: green">// 11. Releases and erases the Circle's extension dictionary and prints</span>
<span style="color: green">// confirmation to the command window.</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> DictionariesAndXRecords
{
<span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">Program</span>
{
<span style="color: blue">static</span> <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args)
{
DraftSight.Interop.dsAutomation.<span style="color: #2b91af">Application</span> dsApp;
<span style="color: #2b91af">Document</span> dsDoc;
<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">//Get command message</span>
<span style="color: #2b91af">CommandMessage</span> dsCmdMsg = dsApp.<b>GetCommandMessage</b>();
<span style="color: green">//Construct Circle</span>
<span style="color: #2b91af">Model</span> dsModel;
<span style="color: #2b91af">SketchManager</span> dsSketchMgr;
dsModel = dsDoc.<b>GetModel</b>();
dsSketchMgr = dsModel.<b>GetSketchManager</b>();
<span style="color: #2b91af">Circle</span> dsCircle;
dsCircle = dsSketchMgr.<b>InsertCircle</b>(5, 5, 0, 10);
<span style="color: green">//Get drawing's root dictionary</span>
<span style="color: #2b91af">Dictionary</span> dsRootDict;
dsRootDict = dsDoc.<b>GetNamedObjectsDictionary</b>();
<span style="color: green">// Get an existing dictionary (e.g., each drawing has a table-style dictionary)</span>
<span style="color: blue">bool</span> hasEntry;
hasEntry = dsRootDict.<b>HasEntry</b>( <span style="color: #a31515">"ACAD_TABLESTYLE"</span> );
<span style="color: blue">if</span>( hasEntry )
{
<span style="color: #2b91af">dsObjectType_e</span> entityType;
<span style="color: blue">object</span> entity;
entity = dsRootDict.<b>GetEntry</b>( <span style="color: #a31515">"ACAD_TABLESTYLE"</span>, <span style="color: blue">out</span> entityType );
<span style="color: green">//Dictionary entries can be of arbitrary entity types</span>
<span style="color: green">//In this case, the arbitrary entity type should be a dictionary</span>
<span style="color: blue">if</span>( entityType == <span style="color: #2b91af">dsObjectType_e</span>.dsDictionaryType )
{
<span style="color: #2b91af">Dictionary</span> dict;
dict = (<span style="color: #2b91af">Dictionary</span>)entity;
<span style="color: green">//Table-style dictionary should contain an active style</span>
<span style="color: #2b91af">TableStyleManager</span> dsTblStyleMgr;
dsTblStyleMgr = dsDoc.<b>GetTableStyleManager</b>();
<span style="color: #2b91af">TableStyle</span> dsActiveTblStyle;
dsActiveTblStyle = dsTblStyleMgr.<b>GetActiveTableStyle</b>();
<span style="color: blue">string</span> activeTblStyleEntryName;
activeTblStyleEntryName = dict.<b>GetNameOf</b>( dsActiveTblStyle );
dsCmdMsg.<b>PrintLine</b>( <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">"Active table-style entry: {0}"</span>, activeTblStyleEntryName ) );
}
}
<span style="color: green">//Create a dictionary in root dictionary</span>
<span style="color: #2b91af">Dictionary</span> dsOurDict;
dsOurDict = dsRootDict.<b>CreateDictionary</b>( <span style="color: #a31515">"Our_Dict"</span> );
<span style="color: green">//New dictionary is entry in root dictionary </span>
<span style="color: green">//Check if dictionary has new entry</span>
<span style="color: blue">bool</span> hasOurDict;
hasOurDict = dsRootDict.<b>HasEntry</b>( <span style="color: #a31515">"Our_Dict"</span> );
<span style="color: blue">if</span> (hasOurDict)
dsCmdMsg.<b>PrintLine</b>(<span style="color: #a31515">"\"Our_Dict\" dictionary added."</span>);
<span style="color: green">//Add XRecord entry</span>
<span style="color: #2b91af">XRecord</span> dsOurXRecord;
dsOurXRecord = dsOurDict.<b>CreateXRecord</b>( <span style="color: #a31515">"Our_XRecord"</span> );
<span style="color: green">//Check if dictionary has new entry</span>
<span style="color: blue">bool</span> hasOurXRecord;
hasOurXRecord = dsOurDict.<b>HasEntry</b>(<span style="color: #a31515">"Our_XRecord"</span>);
<span style="color: blue">if</span> (hasOurXRecord)
dsCmdMsg.<b>PrintLine</b>(<span style="color: #a31515">"\"Our_XRecord\" XRecord added."</span>);
<span style="color: green">//XRecords can contain arbitrary data</span>
<span style="color: blue">int</span> dataCount;
dataCount = dsOurXRecord.<b>GetDataCount</b>();
<span style="color: green">//Add double data</span>
dsOurXRecord.<b>InsertDoubleData</b>( dataCount, 20, 1.42 );
dataCount = dsOurXRecord.<b>GetDataCount</b>();
<span style="color: green">//Add string data</span>
dsOurXRecord.<b>InsertStringData</b>( dataCount, 3, <span style="color: #a31515">"XRecordstring data"</span> );
<span style="color: green">//Each entity can have its own extension dictionary</span>
<span style="color: green">//Create extension dictionary for Circle entity</span>
<span style="color: #2b91af">Dictionary</span> extDict;
extDict = dsCircle.<b>CreateExtensionDictionary</b>();
<span style="color: green">//Add XRecords to Circle's extension dictionary</span>
<span style="color: #2b91af">XRecord</span> dsXRecord1;
dsXRecord1 = extDict.<b>CreateXRecord</b>( <span style="color: #a31515">"XRecord1"</span> );
dsXRecord1.<b>InsertStringData</b>( 0, 1, <span style="color: #a31515">"part number"</span> );
dsXRecord1.<b>InsertInteger32Data</b>( 1, 90, 1 );
<span style="color: #2b91af">XRecord</span> dsXRecord2;
dsXRecord2 = extDict.<b>CreateXRecord</b>( <span style="color: #a31515">"XRecord2"</span> );
dsXRecord2.<b>InsertStringData</b>( 0, 1, <span style="color: #a31515">"Description"</span> );
dsXRecord2.<b>InsertStringData</b>( 1, 3, <span style="color: #a31515">"Circle"</span> );
<span style="color: green">//Read entries of Circle's extension dictionary</span>
<span style="color: blue">object</span> entitytypes = <span style="color: blue">null</span>;
<span style="color: blue">object</span>[] entries = <span style="color: blue">null</span>;
entries = extDict.<b>GetEntries</b>(<span style="color: blue">out</span> entitytypes) <span style="color: blue">as</span> <span style="color: blue">object</span>[];
<span style="color: blue">int</span>[] dsEntityTypes = (<span style="color: blue">int</span>[])entitytypes;
<span style="color: blue">for</span> ( <span style="color: blue">int</span> index = 0; index < dsEntityTypes.Length; ++index )
{
<span style="color: blue">if</span>( (<span style="color: #2b91af">dsObjectType_e</span>)dsEntityTypes[index] == <span style="color: #2b91af">dsObjectType_e</span>.dsXRecordType )
{
<span style="color: #2b91af">XRecord</span> xRecord;
xRecord = (<span style="color: #2b91af">XRecord</span>)entries[index];
<span style="color: blue">if</span>( xRecord == <span style="color: blue">null</span> )
<span style="color: blue">continue</span>;
<span style="color: blue">int</span> count;
count = xRecord.<b>GetDataCount</b>();
<span style="color: blue">for</span>( <span style="color: blue">int</span> i = 0; i < count; ++i )
{
<span style="color: #2b91af">dsCustomDataType_e</span> type = xRecord.<b>GetDataType</b>(i);
<span style="color: blue">if</span>( type == <span style="color: #2b91af">dsCustomDataType_e</span>.dsCustomDataType_String )
{
<span style="color: blue">string</span> data;
data = xRecord.<b>GetStringData</b>(i);
dsCmdMsg.<b>PrintLine</b>( <span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">"String data: {0}"</span>,data));
}
<span style="color: blue">else</span> <span style="color: blue">if</span> ( type == <span style="color: #2b91af">dsCustomDataType_e</span>.dsCustomDataType_Integer32 )
{
<span style="color: blue">int</span> intData;
intData = xRecord.<b>GetInteger32Data</b>(i);
dsCmdMsg.<b>PrintLine</b>(<span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">"Int data: {0}"</span>, intData));
}
}
}
}
<font color="#008000"> //Remove the XRecords in the Circle's extension dictionary</font>
extDict.<b>RemoveEntry</b>(<span style="color: #a31515">"XRecord1"</span>);
extDict.<b>RemoveEntry</b>(<span style="color: #a31515">"XRecord2"</span>);
<span style="color: green">//Release and erase the Circle's extension dictionary</span>
<span style="color: blue">bool</span> removed;
removed = dsCircle.<b>ReleaseExtensionDictionary</b>();
<span style="color: blue">if</span> (removed)
{
dsCmdMsg.<b>PrintLine</b>(<span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">"Circle's extension dictionary released and erased."</span>));
}
}
}
}</pre>
</body>
</html>