You are on page 1of 12

DESWIK SCRIPTING TRAINING

MANUAL

RELEASE DATE: APRIL 2, 2012


DOCUMENT VERSION: 1.0

2 / 428 Upper Edward St., Spring Hill, QLD, Australia, 4000


Tel: +61 (0)7 3218 2915 • WITHIN AUSTRALIA 1300 DESWIK (337 945)
Fax: +61 (0)7 3218 7377 • Email: info@deswiksoftware.com
ABN 37 136 642 450
TABLE OF CONTENTS

1. INTRODUCTION ..................................................................................................................................................................... 3

2. NAMESPACES AND REFERENCES ...................................................................................................................................... 3

3. VARIABLE DECLARATIONS .................................................................................................................................................. 4

4. ENTITIES ...................................................................................................................................................................................... 4

5. LAYERS ......................................................................................................................................................................................... 5

6. XPROPERTIES AND ATTRIBUTES ...................................................................................................................................... 6

7. SELECTIONS ............................................................................................................................................................................... 7

7.1. Selection through code .................................................................................................................................................... 7

7.1.1. Select polyface objects on layer1 and layer2 ..................................................................................................... 7

7.1.2. select polyline objects on layer1........................................................................................................................... 7

7.2. Interactive selection .......................................................................................................................................................... 8

7.3. Iterating through selections ............................................................................................................................................ 8

8. DICTIONARIES AND LISTS ................................................................................................................................................... 9

8.1. Overview of dictionaries ................................................................................................................................................. 9

8.2. Overview of lists................................................................................................................................................................ 9

8.3. Generating grouping dictionaries .................................................................................................................................. 9

9. USING THE STREAMREADER AND STREAMWRITER ............................................................................................... 10

9.1. Reading Comma delimited text files ........................................................................................................................... 10

9.1.1. Strange encoding .................................................................................................................................................... 10

9.1.2. Numbers, CSV and excel ..................................................................................................................................... 10

9.2. Streamreader .................................................................................................................................................................... 11

9.3. Streamwriter .................................................................................................................................................................... 11

10. SHOWING PROGRESS ..................................................................................................................................................... 12

Page 2
1. INTRODUCTION

This document provides an overview of the Deswik.CAD object model and scripting interface. The document
does cover some specifics of the programming and vb language uses, but is not designed as an in depth
programming reference.

The Deswik Scripting language closely follows the VB.NET Framework 4 as much as possible. Bearing in mind that
it does NOT execute the code in the .NET JIT compiler (it has its own compiler). For this reason it is possible to
use the Microsoft VB.NET web resources when looking for syntax information. There are likely some code
syntaxes and methods which Deswik has not encountered. Should you find one of these cases please contact
support and we will rectify as quickly as possible.

It is assumed that the user is familiar with basic programming.

2. NAMESPACES AND REFERENCES

A namespace is defined as ‘a container that provides context for the identifiers (names, or technical terms, or
words) it holds, and allows the disambiguation of homonym identifiers residing in different namespaces.

By example, in Deswik.CAD there are two types of Point object. A Deswik.Graphics.Geometry.Point (a Point
object under the Deswik.Graphics.Geometry namespace) and a Deswik.Graphics.Figures.Point (a Point object
under the Deswik.Graphics.Figures namespace). The two objects are quite different. A
Deswik.Graphics.Figures.Point is visually drawn inside of model space while a Deswik.Graphics.Geometry.Point
not drawn, it simply governs the location (X,Y,Z).

The Deswik scripting Integrated Development Environment (IDE) allows access to all of the .NET namespaces as
well as all of the namespaces under “Deswik”. Deswik is the root name of all Deswik namespaces.

Other dlls may be referenced in the IDE by selecting Edit | References and selecting any .NET or COM dll.

Namespaces are great at compartmentalizing objects however, they can be quite lengthy to write. By using the
keywork Imports at the top of the script, it is not necessary to type the entire namespace. For example

Private Sub
Dim p as new Deswik.Graphics.Geometry.Point(0,0,0)
End Sub

Is the same as

Imports Deswik.Graphics.Geometry
Private Sub
Dim p as new Point(0,0,0)
End Sub

Page 3
3. VARIABLE DECLARATIONS

In previous versions of VB it was necessary to declare a variable and then set its value on a different line. Eg

Dim myInteger as Integer


myInteger=1000

Modern versions of the language allow it to be dimensioned and a default value set on the same line.

Dim myInteger as Integer=1000

Likewise in loops it is possible to declare the variable inside of the loop

For x as integer=0 to 1000


‘Do Something
Next x

4. ENTITIES

In the scripting IDE the “CurrentDoc” object gives access to the current Deswik.CAD document.

The Deswik object model is closely designed to align with the graphical user interface of the application.

The 3D modeling area is known as the ModelSpace, accessed through CurrentDoc.ModelSpace. Layouts are the
paper printing areas of the document. Each layout has a unique name and can be access through the
CurrentDoc.Layouts collection.

Page 4
Each object inModelSpace resides under the Deswik.Graphics.Figures namespace. (eg
Deswik.Graphics.Figures.Circle). These objects are called entities and inherit from the
Deswik.Graphics.Primaries.Figure object.

To create a new object simply declare the object

Dim myCircle as new Deswik.Graphics.Figures.Circle()


Dim myPolyline as new Deswik.Graphics.Figures.Polyline()

Each object has an overloaded constructor, so when the first bracket is created a list of the overloads is shown.
Pressing the up and down arrows will cycle through the overloads. It is important to set some of the basic
properties for an object otherwise when it is added to the document it will not be visible.

In the case of a circle the following line of code.

Dim myCircle as new Deswik.Graphics.Figures.Circle(new Deswik.Graphics.Geometry.Point(0,0,0),1)

Is identical to

Dim myCircle as new Deswik.Graphics.Figures.Circle()


myCircle.Centerpoint=new Deswik.Graphics.Geometry.Point(0,0,0)
myCircle.Radius=1

Once an object has been created it needs to be added into the entities collection in order for it to be rendered.

CurrentDoc.ModelSpace.Entities.additem(myCircle)

Once added into the entities collection a redraw is required in order for it to become visible. If lots of entities are
being added the redraw only needs to be called once at the end.

CurrentDoc.Redraw(true)

5. LAYERS

The layers in a Deswik.CAD document act as containers to hold the entities which exist in the document. Each
document must contain at least one layer and this layer must have the name “0”. It is not possible to delete the
“0” layer.

If a layer is deleted so are the entities which exist in the layer.

The layers in Deswik.CAD are displayed as a hierarchy, however, they are sorted as a simple list. The name of the
layer contains the backslash (“\”) character. This works in much the same way as windows explorer and the
windows file system. The hierarchy of layers is only for storage purposes. A child of a layer does not necessarily
bear any resemblance to its parent if that is not the user’s intention.

Page 5
Each layer can contain its own list of attributes and properties (discussed in next section).

Deswik.CAD has the concept of an active layer (when a user adds an entity to the document it goes to the active
layer) and a selected layer (used in many operations).

A get/set operation on these can be performed under the CurrentDoc object.

CurrentDoc.ActiveLayerName
CurrentDoc.SelectedLayerName

A layer may be referenced by using the “FindName” method or by referring to its index in the document. By using
the “Add” method the resultant layer is returned.

When an entity is added to the document through code, if the Layer property has not been set then the entity is
placed onto the active layer.

The following code adds the entity onto the active layer.

Dim myCircle as new Deswik.Graphics.Figures.Circle(new Deswik.Graphics.Geometry.Point(0,0,0),1)


CurrentDoc.ModelSpace.Entities.AddItem(myCircle)

The following code adds the entity onto an existing layer “MYLAYER”. Note that in this example if the layer
“MYLAYER” does not exist an exception will be returned as “FindName” will return “null”.

Dim myCircle as new Deswik.Graphics.Figures.Circle(new Deswik.Graphics.Geometry.Point(0,0,0),1)


myCircle.Layer=CurrentDoc.Layers.FindName(“MYLAYER”)
CurrentDoc.ModelSpace.Entities.AddItem(myCircle)

Layer names are always stored as uppercase characters. There are a number of illegal characters which will be
stripped from the name, when creating layers.

6. XPROPERTIES AND ATTRIBUTES

Deswik.CAD makes extensive use of Attributes (these are sometimes referred to as METADATA in other
systems). These are essentially user defined data which can be stored on every entity to describe them more
appropriately and allow for detailed reporting. Every entity can have an unlimited number of attributes, each with
a unique name. On an entity, attributes are referred to as XProperties. When you wish to have the user be able
to alter an XProperty, the layer object must have an Attribute of the same name added. If this attribute does not
exist on the layer then the XProperty is invisible to the user, and cannot be used in standard Deswik.CAD
functions.

The following code adds a RAMP attribute with a value of R1 and a strip attribute as 25 to a circle entity and adds
it to the MYLAYER layer.

Dim myCircle as new Deswik.Graphics.Figures.Circle(new Deswik.Graphics.Geometry.Point(0,0,0),1)


myCircle.Layer=CurrentDoc.Layers.FindName(“MYLAYER”)
myCircle.Xproperties.Add(“RAMP”,R1)
myCircle.Xproperties.Add(“STRIP”,25)
CurrentDoc.ModelSpace.Entities.AddItem(myCircle)

Page 6
If the user were to click on this entity, the attribute information would NOT show up in the property panel. For
them to show up a “RAMP’ and “STRIP” attribute would need to be added to the layer.

CurrentDoc.Layers.FindName("MYLAYER").Attributes.Add("RAMP",Objects.Attribute.AttributeType.String,"")
CurrentDoc.Layers.FindName("MYLAYER").Attributes.Add("STRIP",Objects.Attribute.AttributeType.String,""
)

7. SELECTIONS

One of the most important functions in Deswik.CAD is the ability to select entities and then process these
entities. Selection could be from the user selecting one or more entities interactively or selecting all of the entities
on a layer through code. The Deswik.Collections.Selection object stores all of the entities from a selection. The
Selection object acts like any collection and can be iterated through to process the contained entities. The
Selection object collection contains only Deswik.Graphics.Primaries.Figure objects. These are the base object of
every entity in Deswik.CAD. In order to access the methods and properties of a specified entity type, the Figure
object must be cast to that entity type. If a figure is cast to the incorrect entity type and exception will be thrown.

7.1. SELECTION THROUGH CODE

Selections in Deswik.CAD are named and accessed through the selections collection. This can be found under:

CurrentDoc.Selections

The following method is used to select entities on a layer:

CurrentDoc.Selections.AddByLayerTypeVisibility

The method is extensively overloaded to provide lots of flexibility in getting the selections.

Following are some typical selection calls:

7.1.1. SELECT POLYFACE OBJECTS ON LAYER1 AND LAYER2


Dim mysel As Deswik.Graphics.Collections.Selection
=CurrentDoc.Selections.AddByLayerTypeVisibility("_T",New String(){"LAYER1","LAYER2"},New
String(){"Polyface"},Enums.EntitiesSelectionByVisibility.All)

7.1.2. SELECT POLYLINE OBJECTS ON LAYER1


Dim mysel As Deswik.Graphics.Collections.Selection
=CurrentDoc.Selections.AddByLayerTypeVisibility("_T","LAYER1","Polyline",Enums.EntitiesSelectionByVisi
bility.All)

The final parameter in the method allows for “All”, “Visible” or “Invisible” entities to be selected.

Page 7
7.2. INTERACTIVE SELECTION

The primary method for prompting the user to select entities is located here:

CurrentDoc.UserCommands.SelectionInteractiveGet

The “Prompt” parameter of the method is for the text which will be displayed in the prompt popup on the
bottom right hand corner of the application.

7.3. ITERATING THROUGH SELECTIONS

Once a selection has been created, iterating through with a For loop is possible.

The following example loops through the selection and writes the volume of any polyface entities to the output
window.

For i As Integer = 0 To mysel.Count-1


If mysel(i).TypeName.ToUpper()="POLYFACE" Then
Dim thisFace As Deswik.Graphics.Figures.Polyface=mysel(i).asPolyface
CurrentDoc.OnMessageOutput("Volume = " + thisFace.Volume.ToString("#,##0"),False)
End If
Next

The selection object can also be iterated

For Each fig As Deswik.Graphics.Primaries.Figure In mysel


If fig.TypeName.ToUpper()="POLYFACE" Then
Dim thisFace As Deswik.Graphics.Figures.Polyface=fig.asPolyface
CurrentDoc.OnMessageOutput("Volume = " + thisFace.Volume.ToString("#,##0"),False)
End If
Next

It is important to check the typename of the entity before casting it to the figure as casting to an incorrect
figure will throw an exception.

Page 8
8. DICTIONARIES AND LISTS

With the advent of the .NET framework, Microsoft introduced generic collection classes; the Deswik.CAD
framework has complete access to all of these classes.

8.1. OVERVIEW OF DICTIONARIES

The Dictionary (Of TKey, TValue) generic class provides a mapping from a set of keys to a set of values. Each
addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast,
close to O(1), because the Dictionary(Of TKey, TValue) class is implemented as a hash table.

For more information on Dictionaries please visit http://msdn.microsoft.com/en-us/library/xfhwa508.aspx#Y11302

In the context of Deswik.CAD it is possible to extract entities based on “Groups” and then process these
entities. This can be quite useful, for instance, when calculating the margin on a column of blocks, or extracting
volume and tonnage values from “areas” of the mine.

8.2. OVERVIEW OF LISTS

The List (Of TKey) generic class provides dynamic array functionality. Items can be added and deleted from the
list.

For more information on Lists please visit http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

8.3. GENERATING GROUPING DICTIONARIES

The selection object contains methods which will build the dictionary for you. The following code snippet shows
how to create a group dictionary where the key is the values contained in the “BLOCK” XProperty. Note that
the dictionary has a key which is of type “String” and the values are lists of Deswik.Primaries.Figure objects.

Dim myDict As System.Collections.Generic.Dictionary(Of String,List(Of


Deswik.Graphics.Primaries.Figure))
myDict=mysel.GetPropertyGroups("BLOCK")

Once the dictionary has been created it is then possible to iterate through the dictionary and process the entities.
The following example shows how, with the above dictionary, it is possible to iterate through and calculate the
volume of each ‘BLOCK” and write the results to the output window.

For Each key As String In myDict.Keys


Dim myFigs As List(Of Deswik.Graphics.Primaries.Figure)=myDict(key)'get the list of figures
associated with this key
Dim volume As Double=0'dimension the volume variable to zero
For j As Integer =0 To myFigs.Count-1'loop through the list of figures
If myFigs(j).typename.toUpper()="POLYFACE" Then
volume=volume+myFigs(j).asPolyface.Volume'if the entity is a polyface add the volume
onto the volume variable
End If

Page 9
Next
CurrentDoc.OnMessageOutput("Volume of " + key + " is " + volume.ToString("#,##0"),False)
'write the volume of the key to the output window
Next

Deswik also provides a number of objects which can reduce the number of lines of code which need to be
written in a script. An example of this is the “PolyfaceList” object. This essentially packages up a number of
methods and properties which can be run when there is a list of polyfaces. The above code would then condense
to this.

For Each key As String In myDict.Keys


Dim myPolyfaceList As New Deswik.Graphics.Collections.PolyfaceList(myDict(key))'get the list
of figures associated with this key and put it in a polyfacelist
CurrentDoc.OnMessageOutput("Volume of " + key + " is " +
myPolyfaceList.Volume.ToString("#,##0"),False) 'write the volume of the key to the output window
Next

9. USING THE STREAMREADER AND STREAMWRITER

9.1. READING COMMA DELIMITED TEXT FILES

Interpreting comma delimited text files can be difficult and time consuming without existing knowledge or
experience. Reading this section will assist users to be aware of common complications. Some troubleshooting
suggestions have been included.

9.1.1. STRANGE ENCODING

Some text files have encoding in them which can confuse the streamreader object. Generally these come from
mining packages which were originally written for Unix or, specifically, XPAC output paths. There is a method in
Deswik.CAD to repair these encodings and it is suggested that you call this function to repair the file before using
the stream reader.

Deswik.Common.FileTools.FixTextFileCarriageReturns(file)

9.1.2. NUMBERS, CSV AND EXCEL

If a file has been saved to a CSV from excel it is common that a line may look like this.

“A”,”B”,”1,000”

The 1,000 is the number one thousand, but it has been written with the comma. If we read the file using a straight
parse it will be confused and see this as 4 separate fields.

The following line of code will correctly parse the above line into a string array.

Page 10
Dim line As String="“A”,”B”,”1,000”"
Dim linesplit() As
String=Deswik.Common.TextParser.Parser.ParseStringWithQuotes(line,","C,True).ToArray()

9.2. STREAMREADER

The streamreader is a .NET object which can be used to read text files. The following snippet shows how to read
a text file and write the results to the output window of Deswik.CAD.

Dim sr As New System.IO.StreamReader(file)


While sr.EndOfStream=False
Dim line As String=sr.ReadLine()
CurrentDoc.OnMessageOutput(line,False)
End While
sr.Close()

9.3. STREAMWRITER

The steamwriter is a .NET object which can be used to write text files. The following snippet extends the
previous examples to write the BLOCK volumes to a comma delimited text file.

Dim sw As New System.IO.StreamWriter(file,False,System.Text.Encoding.ASCII)


sw.WriteLine("BLOCK,VOLUME")
For Each key As String In myDict.Keys
Dim myPolyfaceList As New Deswik.Graphics.Collections.PolyfaceList(myDict(key))'get the list
of figures associated with this key and put it in a polyfacelist
sw.WriteLine(key + "," +myPolyfaceList.Volume.ToString("#,##0"))
Next
sw.Close()

Page 11
10. SHOWING PROGRESS

During processes it is important to show the user that work is being done. Deswik provides a generic progress
bar for performing this.

This example extends the previous example to show a progress bar while writing and then to close it after
completion.

Dim prog As New Deswik.Common.Progress(myDict.Count)


prog.Text="Writing Text File"
prog.Show(CurrentDoc)
Dim sw As New System.IO.StreamWriter(file,False,System.Text.Encoding.ASCII)
sw.WriteLine("BLOCK,VOLUME")
For Each key As String In myDict.Keys
Dim myPolyfaceList As New Deswik.Graphics.Collections.PolyfaceList(myDict(key))'get the list
of figures associated with this key and put it in a polyfacelist
sw.WriteLine(key + "," +myPolyfaceList.Volume.ToString("#,##0"))
prog.UpdateProgress(True)
Next
sw.Close()
prog.Close()

If, while running the macro, you press the stop button it is likely that the progress bar will not close. To close it
simply select it and press “Alt-F4” and it will close.

It is also possible to extend the above to include user cancelling.

Dim prog As New Deswik.Common.Progress(myDict.Count)


prog.Text="Writing Text File"
prog.Show(CurrentDoc)
Dim sw As New System.IO.StreamWriter(file,False,System.Text.Encoding.ASCII)
sw.WriteLine("BLOCK,VOLUME")
For Each key As String In myDict.Keys
Dim myPolyfaceList As New Deswik.Graphics.Collections.PolyfaceList(myDict(key))'get the list
of figures associated with this key and put it in a polyfacelist
sw.WriteLine(key + "," +myPolyfaceList.Volume.ToString("#,##0"))
prog.UpdateProgress(True)
If prog.Cancel Then
Exit For
End If
Next
sw.Close()
prog.Close()

Page 12

You might also like