Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
draftsight
/
opt
/
dassault-systemes
/
DraftSight
/
APISDK
/
Create_and_Change_EntityGroups_Example_VBNET.htm
|
|---|
<html>
<head>
<title>Create and Change EntityGroups 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,EntityGroups">
<meta name=MS-HKWD content="VB.NET examples,groups">
</head>
<body>
<h1> Create and Change EntityGroups Example (VB.NET)</h1>
<p>This example shows how to create and change EntityGroups.</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. 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. Inserts entities, two lines and two circles, in the drawing.</span>
<span style="color:green;">' 2. Creates two EntityGroups:</span>
<span style="color:green;">' * SampleGroup1 contains two lines, line1 and line2.</span>
<span style="color:green;">' * SampleGroup2 contains two circles, circle1 and circle2.</span>
<span style="color:green;">' 3. Changes the color of the lines from white to red.</span>
<span style="color:green;">' 4. Removes an entity, circle1, from SampleGroup2.</span>
<span style="color:green;">' 5. Adds an entity, circle2, to SampleGroup1.</span>
<span style="color:green;">' 6. Reorders SampleGroup1.</span>
<span style="color:green;">' 7. Explodes SampleGroup2, which removes the definition</span>
<span style="color:green;">' from the drawing; however, circle1 remains as an entity</span>
<span style="color:green;">' in the drawing.</span>
<span style="color:green;">' 8. Renames SampleGroup1 and changes its description.
'
</span><span style="color:green;">' <b>NOTE</b>: Execution of the macro stops several times. Follow the
' instructions in the macro each time execution stops.</span>
<span style="color:green;">'----------------------------------------------------------------</span>
<span style="color:blue;">Imports</span> System
<span style="color:blue;">Imports</span> DraftSight.Interop.dsAutomation
<span style="color:blue;">Imports</span> System.Runtime.InteropServices
<span style="color:blue;">Imports</span> System.Windows.Forms
<span style="color:blue;">Imports</span> System.Diagnostics
<span style="color:blue;">Module</span> <span style="color:#2b91af;">Module1</span>
<span style="color:blue;">Public</span> dsApp <span style="color:blue;">As</span> DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">Application</span>
<span style="color:blue;">Public</span> dsDoc <span style="color:blue;">As</span> <span style="color:#2b91af;">Document</span>
<span style="color:blue;">Sub</span> Main()
<span style="color:green;">'Connect to DraftSight application</span>
dsApp = <span style="color:blue;">DirectCast</span>(<span style="color:#2b91af;">Marshal</span>.GetActiveObject(<span style="color:#a31515;">"DraftSight.Application"</span>), DraftSight.Interop.dsAutomation.<span style="color:#2b91af;">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>
<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:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Insert entities</span>
<span style="color:blue;">Dim</span> dsSketchMgr <span style="color:blue;">As</span> <span style="color:#2b91af;">SketchManager</span> = dsApp.<b>GetActiveDocument</b>().GetModel().GetSketchManager()
<span style="color:blue;">Dim</span> line1 <span style="color:blue;">As</span> <span style="color:#2b91af;">Line</span> = dsSketchMgr.<b>InsertLine</b>(0, 0, 0, 10, 10, 0)
<span style="color:blue;">Dim</span> line2 <span style="color:blue;">As</span> <span style="color:#2b91af;">Line</span> = dsSketchMgr.<b>InsertLine</b>(5, 0, 0, 15, 10, 0)
<span style="color:blue;">Dim</span> circle1 <span style="color:blue;">As</span> <span style="color:#2b91af;">Circle</span> = dsSketchMgr.<b>InsertCircle</b>(5, 5, 0, 10)
<span style="color:blue;">Dim</span> circle2 <span style="color:blue;">As</span> <span style="color:#2b91af;">Circle</span> = dsSketchMgr.<b>InsertCircle</b>(10, 5, 0, 10)
<span style="color:blue;">Dim</span> EntitiesArray1 <span style="color:blue;">As</span> <span style="color:#2b91af;">DispatchWrapper</span>() = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(1) {}
EntitiesArray1(0) = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(line1)
EntitiesArray1(1) = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(line2)
<span style="color:blue;">Dim</span> EntitiesArray2 <span style="color:blue;">As</span> <span style="color:#2b91af;">DispatchWrapper</span>() = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(1) {}
EntitiesArray2(0) = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(circle1)
EntitiesArray2(1) = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(circle2)
CreateGroups(EntitiesArray1, EntitiesArray2)
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break()
<span style="color:green;">'Type GROUP at the command-window prompt</span>
<span style="color:green;">'to verify that two EntityGroups, SampleGroup1</span>
<span style="color:green;">'and SampleGroup2, were created</span>
<span style="color:green;">'Press OK to close the dialog </span>
<span style="color:green;">'Press F5 in the IDE to continue</span>
<span style="color:green;">'Get SampleGroup2 and remove circle2</span>
<span style="color:blue;">Dim</span> dsGroup2 <span style="color:blue;">As</span> <span style="color:#2b91af;">Group</span> = dsDoc.<b>GetGroup</b>(<span style="color:#a31515;">"SampleGroup2"</span>)
<span style="color:blue;">If</span> dsGroup2.<b>HasEntity</b>(circle2) <span style="color:blue;">Then</span>
<span style="color:blue;">Dim</span> index <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = dsGroup2.<b>GetIndex</b>(circle2)
dsGroup2.<b>RemoveEntityAt</b>(index)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Get SampleGroup1 and add circle1 at last position</span>
<span style="color:blue;">Dim</span> dsGroup1 <span style="color:blue;">As</span> <span style="color:#2b91af;">Group</span> = dsDoc.<b>GetGroup</b>(<span style="color:#a31515;">"SampleGroup1"</span>)
<span style="color:blue;">Dim</span> EntitiesArray <span style="color:blue;">As</span> <span style="color:#2b91af;">DispatchWrapper</span>() = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(0) {}
EntitiesArray(0) = <span style="color:blue;">New</span> <span style="color:#2b91af;">DispatchWrapper</span>(circle1)
<span style="color:blue;">Dim</span> count <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = dsGroup1.<b>GetEntitiesCount</b>()
dsGroup1.<b>InsertEntitiesAt</b>(count, EntitiesArray)
<span style="color:blue;">Dim</span> newCount <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = dsGroup1.<b>GetEntitiesCount</b>()
<span style="color:blue;">If</span> newCount <> count + 1 <span style="color:blue;">Then</span>
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Circle1 was not inserted."</span>)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Move circle1 to second position (index 1)</span>
<span style="color:blue;">Dim</span> circleIndex <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = dsGroup1.<b>GetIndex</b>(circle1)
<span style="color:blue;">If</span> circleIndex <> count <span style="color:blue;">Then</span>
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Circle1 inserted at wrong position."</span>)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
dsGroup1.Reorder(circleIndex, 1, 1)
circleIndex = dsGroup1.<b>GetIndex</b>(circle1)
<span style="color:blue;">If</span> circleIndex <> 1 <span style="color:blue;">Then</span>
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Circle1 inserted at wrong position."</span>)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Explode SampleGroup2</span>
dsGroup2.Explode()
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break()
<span style="color:green;">'Type GROUP at the command-window prompt</span>
<span style="color:green;">'to verify that only SampleGroup1 exists</span>
<span style="color:green;">'Press OK to close the dialog </span>
<span style="color:green;">'Press F5 in the IDE to continue</span>
<span style="color:blue;">Dim</span> dsGroups <span style="color:blue;">As</span> <span style="color:blue;">Object</span> = <span style="color:blue;">Nothing</span>
dsGroups = dsDoc.<b>GetGroups</b>()
<span style="color:blue;">Dim</span> groupObjects <span style="color:blue;">As</span> <span style="color:blue;">Object</span>() = <span style="color:blue;">DirectCast</span>(dsGroups, <span style="color:blue;">Object</span>())
<span style="color:blue;">If</span> groupObjects.Length <> 1 <span style="color:blue;">Then</span>
<span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"Group2 was not exploded."</span>)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:green;">'Rename SampleGroup1 and change description</span>
dsGroup1.<b>Rename</b>(<span style="color:#a31515;">"SampleGroup"</span>)
dsGroup1.<b>Description</b> = <span style="color:#a31515;">"My sample group."</span>
System.Diagnostics.<span style="color:#2b91af;">Debugger</span>.Break()
<span style="color:green;">'Type GROUP at the command-window prompt</span>
<span style="color:green;">'to verify that SampleGroup1 was renamed</span>
<span style="color:green;">'to SampleGroup and its description</span>
<span style="color:green;">'changed to "My sample group"</span>
<span style="color:green;">'Press OK to close the dialog </span>
<span style="color:green;">'Press F5 in the IDE to continue</span>
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>
<span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> CreateGroups(<span style="color:blue;">ByVal</span> EntitiesArray1 <span style="color:blue;">As</span> <span style="color:#2b91af;">DispatchWrapper</span>(), <span style="color:blue;">ByVal</span> EntitiesArray2 <span style="color:blue;">As</span> <span style="color:#2b91af;">DispatchWrapper</span>())
<span style="color:green;">'Create group</span>
<span style="color:blue;">Dim</span> dsGroup1 <span style="color:blue;">As</span> <span style="color:#2b91af;">Group</span> = dsDoc.<b>CreateGroup</b>(<span style="color:#a31515;">"SampleGroup1"</span>, <span style="color:blue;">False</span>, <span style="color:#a31515;">"This is my first sample group."</span>, EntitiesArray1)
<span style="color:blue;">Dim</span> dsEntities <span style="color:blue;">As</span> <span style="color:blue;">Object</span> = <span style="color:blue;">Nothing</span>
<span style="color:blue;">Dim</span> dsEntityTypes <span style="color:blue;">As</span> <span style="color:blue;">Object</span> = <span style="color:blue;">Nothing</span>
dsEntities = dsGroup1.<b>GetEntities</b>(dsEntityTypes)
<span style="color:blue;">Dim</span> entityObjects <span style="color:blue;">As</span> <span style="color:blue;">Object</span>() = <span style="color:blue;">DirectCast</span>(dsEntities, <span style="color:blue;">Object</span>())
<span style="color:blue;">Dim</span> dsEntityTypesArray <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>() = <span style="color:blue;">DirectCast</span>(dsEntityTypes, <span style="color:blue;">Integer</span>())
<span style="color:blue;">If</span> entityObjects <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">AndAlso</span> dsEntityTypesArray <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;">'Change color of lines from white to red</span>
<span style="color:blue;">Dim</span> dsEntityHelper <span style="color:blue;">As</span> <span style="color:#2b91af;">EntityHelper</span> = dsApp.<b>GetEntityHelper</b>()
<span style="color:blue;">Dim</span> dsColor <span style="color:blue;">As</span> <span style="color:#2b91af;">Color</span> = dsApp.<b>GetNamedColor</b>(<span style="color:#2b91af;">dsNamedColor_e</span>.dsNamedColor_Red)
<span style="color:blue;">For</span> <span style="color:blue;">Each</span> dsEntity <span style="color:blue;">As</span> <span style="color:blue;">Object</span> <span style="color:blue;">In</span> entityObjects
<span style="color:blue;">If</span> dsEntity <span style="color:blue;">IsNot</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span>
dsEntityHelper.<b>SetColor</b>(dsEntity, dsColor)
<span style="color:blue;">End</span> <span style="color:blue;">If</span>
<span style="color:blue;">Next</span>
<span style="color:blue;">Dim</span> dsGroup2 <span style="color:blue;">As</span> <span style="color:#2b91af;">Group</span> = dsDoc.<b>CreateGroup</b>(<span style="color:#a31515;">"SampleGroup2"</span>, <span style="color:blue;">False</span>, <span style="color:#a31515;">"This is my second sample group."</span>, EntitiesArray2)
<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>