Deswik Scripting Training Manual
Deswik Scripting Training Manual
MANUAL
1. INTRODUCTION ..................................................................................................................................................................... 3
4. ENTITIES ...................................................................................................................................................................................... 4
5. LAYERS ......................................................................................................................................................................................... 5
7. SELECTIONS ............................................................................................................................................................................... 7
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.
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
Modern versions of the language allow it to be dimensioned and a default value set on the same line.
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.
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.
Is identical to
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.
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).
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.
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”.
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.
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.
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.
Selections in Deswik.CAD are named and accessed through the selections collection. This can be found under:
CurrentDoc.Selections
CurrentDoc.Selections.AddByLayerTypeVisibility
The method is extensively overloaded to provide lots of flexibility in getting the selections.
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.
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.
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.
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.
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.
The List (Of TKey) generic class provides dynamic array functionality. Items can be added and deleted from the
list.
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.
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.
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.
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.
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)
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.
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.
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.
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.
Page 12