Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
draftsight
/
opt
/
dassault-systemes
/
DraftSight
/
APISDK
/
Create_and_Remove_Dictionaries_Example_VBNET.htm
|
|---|
<html>
<head>
<title>Create and Remove Dictionaries Example (VB.NET)</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="VB.NET examples">
<meta name=MS-HKWD content="VB.NET examples,dictionaries">
<meta name=MS-HKWD content="VB.NET examples,extension dictionaries">
<meta name=MS-HKWD content="VB.NET examples,XRecords">
</head>
<body>
<h1> Create and Remove Dictionaries Example (VB.NET)</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 VB.NET Windows console project.</span>
<span style="color:green;">' 2. Copy and paste this example into the VB.NET 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. Start DraftSight and open a document.</span>
<span style="color:green;">' 5. 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;">Imports</span> DraftSight.Interop.dsAutomation
<span style="color:blue;">Imports</span> System.Runtime.InteropServices
<span style="color:blue;">Module</span> <span style="color:#2b91af;">Module1</span>
<span style="color:blue;">Sub</span> Main()
<span style="color:blue;">Dim</span> dsApp <span style="color:blue;">As</span> <span style="color:#2b91af;">Application</span>
<span style="color:blue;">Dim</span> dsDoc <span style="color:blue;">As</span> <span style="color:#2b91af;">Document</span>
<span style="color:green;">'Connect to DraftSight application</span>
dsApp = GetObject(, <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> dsApp <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span>
<span style="color:blue;">Return</span>
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Get active document</span>
dsDoc = dsApp.<b>GetActiveDocument</b>()
<span style="color:blue;">If</span> dsDoc <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span>
MsgBox(<span style="color:#a31515;">"There are no open documents in DraftSight."</span>)
<span style="color:blue;">Return</span>
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Get command message</span>
<span style="color:blue;">Dim</span> dsCmdMsg <span style="color:blue;">As</span> <span style="color:#2b91af;">CommandMessage</span> = dsApp.<b>GetCommandMessage</b>()
<span style="color:green;">'Construct Circle</span>
<span style="color:blue;">Dim</span> dsModel <span style="color:blue;">As</span> <span style="color:#2b91af;">Model</span>
<span style="color:blue;">Dim</span> dsSketchMgr <span style="color:blue;">As</span> <span style="color:#2b91af;">SketchManager</span>
dsModel = dsDoc.<b>GetModel</b>()
dsSketchMgr = dsModel.<b>GetSketchManager</b>()
<span style="color:blue;">Dim</span> dsCircle <span style="color:blue;">As</span> <span style="color:#2b91af;">Circle</span>
dsCircle = dsSketchMgr.<b>InsertCircle</b>(5, 5, 0, 10)
<span style="color:green;">'Get drawing's root dictionary</span>
<span style="color:blue;">Dim</span> dsRootDict <span style="color:blue;">As</span> <span style="color:#2b91af;">Dictionary</span>
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;">Dim</span> hasEntry <span style="color:blue;">As</span> <span style="color:blue;">Boolean</span>
hasEntry = dsRootDict.<b>HasEntry</b>(<span style="color:#a31515;">"ACAD_TABLESTYLE"</span>)
<span style="color:blue;">If</span> hasEntry <span style="color:blue;">Then</span>
<span style="color:blue;">Dim</span> entityType <span style="color:blue;">As</span> <span style="color:#2b91af;">dsObjectType_e</span>
<span style="color:blue;">Dim</span> entity <span style="color:blue;">As</span> <span style="color:blue;">Object</span>
entity = dsRootDict.<b>GetEntry</b>(<span style="color:#a31515;">"ACAD_TABLESTYLE"</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:blue;">Then</span>
<span style="color:blue;">Dim</span> dict <span style="color:blue;">As</span> <span style="color:#2b91af;">Dictionary</span>
dict = <span style="color:blue;">DirectCast</span>(entity, <span style="color:#2b91af;">Dictionary</span>)
<span style="color:green;">'Table-style dictionary should contain an active style</span>
<span style="color:blue;">Dim</span> dsTblStyleMgr <span style="color:blue;">As</span> <span style="color:#2b91af;">TableStyleManager</span>
dsTblStyleMgr = dsDoc.<b>GetTableStyleManager</b>()
<span style="color:blue;">Dim</span> dsActiveTblStyle <span style="color:blue;">As</span> <span style="color:#2b91af;">TableStyle</span>
dsActiveTblStyle = dsTblStyleMgr.<b>GetActiveTableStyle</b>()
<span style="color:blue;">Dim</span> activeTblStyleEntryName <span style="color:blue;">As</span> <span style="color:blue;">String</span>
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:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Create a dictionary in root dictionary</span>
<span style="color:blue;">Dim</span> dsOurDict <span style="color:blue;">As</span> <span style="color:#2b91af;">Dictionary</span>
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;">Dim</span> hasOurDict <span style="color:blue;">As</span> <span style="color:blue;">Boolean</span>
hasOurDict = dsRootDict.<b>HasEntry</b>(<span style="color:#a31515;">"Our_Dict"</span>)
<span style="color:blue;">If</span> hasOurDict <span style="color:blue;">Then</span>
dsCmdMsg.<b>PrintLine</b>(<span style="color:#a31515;">"""Our_Dict"" dictionary added."</span>)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Add XRecord entry</span>
<span style="color:blue;">Dim</span> dsOurXRecord <span style="color:blue;">As</span> <span style="color:#2b91af;">XRecord</span>
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;">Dim</span> hasOurXRecord <span style="color:blue;">As</span> <span style="color:blue;">Boolean</span>
hasOurXRecord = dsOurDict.<b>HasEntry</b>(<span style="color:#a31515;">"Our_XRecord"</span>)
<span style="color:blue;">If</span> hasOurXRecord <span style="color:blue;">Then</span>
dsCmdMsg.<b>PrintLine</b>(<span style="color:#a31515;">"""Our_XRecord"" XRecord added."</span>)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'XRecords can contain arbitrary data</span>
<span style="color:blue;">Dim</span> dataCount <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>
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:blue;">Dim</span> extDict <span style="color:blue;">As</span> <span style="color:#2b91af;">Dictionary</span>
extDict = dsCircle.<b>CreateExtensionDictionary</b>()
<span style="color:green;">'Add XRecords to Circle's extension dictionary</span>
<span style="color:blue;">Dim</span> dsXRecord1 <span style="color:blue;">As</span> <span style="color:#2b91af;">XRecord</span>
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:blue;">Dim</span> dsXRecord2 <span style="color:blue;">As</span> <span style="color:#2b91af;">XRecord</span>
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;">Dim</span> entitytypes <span style="color:blue;">As</span> <span style="color:blue;">Object</span> = <span style="color:blue;">Nothing</span>
<span style="color:blue;">Dim</span> entries <span style="color:blue;">As</span> <span style="color:blue;">Object</span>() = <span style="color:blue;">Nothing</span>
entries = <span style="color:blue;">TryCast</span>(extDict.<b>GetEntries</b>(entitytypes), <span style="color:blue;">Object</span>())
<span style="color:blue;">Dim</span> dsEntityTypes <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>() = <span style="color:blue;">DirectCast</span>(entitytypes, <span style="color:blue;">Integer</span>())
<span style="color:blue;">For</span> index <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = 0 <span style="color:blue;">To</span> dsEntityTypes.Length - 1
<span style="color:blue;">If</span> <span style="color:blue;">DirectCast</span>(dsEntityTypes(index), <span style="color:#2b91af;">dsObjectType_e</span>) = <span style="color:#2b91af;">dsObjectType_e</span>.dsXRecordType <span style="color:blue;">Then</span>
<span style="color:blue;">Dim</span> xRecord <span style="color:blue;">As</span> <span style="color:#2b91af;">XRecord</span>
xRecord = <span style="color:blue;">DirectCast</span>(entries(index), <span style="color:#2b91af;">XRecord</span>)
<span style="color:blue;">If</span> xRecord <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span>
<span style="color:blue;">Continue For</span>
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:blue;">Dim</span> count <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>
count = xRecord.<b>GetDataCount</b>()
<span style="color:blue;">For</span> i <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = 0 <span style="color:blue;">To</span> count - 1
<span style="color:blue;">Dim</span> type <span style="color:blue;">As</span> <span style="color:#2b91af;">dsCustomDataType_e</span> = 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;">Then</span>
<span style="color:blue;">Dim</span> data <span style="color:blue;">As</span> <span style="color:blue;">String</span>
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;">ElseIf</span> type = <span style="color:#2b91af;">dsCustomDataType_e</span>.dsCustomDataType_Integer32 <span style="color:blue;">Then</span>
<span style="color:blue;">Dim</span> intData <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>
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))
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:blue;">Next</span>
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:blue;">Next</span>
<span style="color:green;">'Remove the XRecords in the Circle's extension dictionary</span>
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;">Dim</span> removed <span style="color:blue;">As</span> <span style="color:blue;">Boolean</span>
removed = dsCircle.<b>ReleaseExtensionDictionary</b>()
<span style="color:blue;">If</span> removed <span style="color:blue;">Then</span>
dsCmdMsg.<b>PrintLine</b>(<span style="color:#2b91af;">[String]</span>.Format(<span style="color:#a31515;">"Circle's extension dictionary released and erased."</span>))
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>
<span style="color:blue;">End</span> <span style="color:blue;">Module</span></pre>
</body>
</html>