Python Example Scripts

1 - DGD Reading/Writing


The example scripts found on this page are complete scripts and can all be run as written as long as the specific file names have been edited to match your particular needs. There are many ways to accomplish the same thing when using Python, and these examples show one way of completing the tasks described.

Run the scripts using File > Python Script, or by using the TCSH window.

Steps to run the scripts from TCSH

  1. Copy and paste the script text into a text editor, then save it using a (.py) extension.

  2. Open the TCSH window.

  3. Type the word python at the prompt, followed by a space, then the name of the script with the extension.

    Prompt>python field_list.py

Steps to run Python from the interpreter without a script

  1. Open the TCSH window.

  2. Type the word python and the prompt, then press ENTER.

    Note:  If using the Integrated Shell, type python -i.

  3. Type in your commands at the prompt.


1.1 Imports

from maptek import vulcan
import numpy as np
import pandas as pd

1.2 Create a dgd

Creates a dgd, adds a layer called DEMO, then adds a single point at location 0,0,0. The colour of the object can be changed using the set_colour() method with an integer. The integer represents the colour number in the Vulcan (.scd) colour pallete.

# Creates a dgd, adds a layer called DEMO, then adds a single point at location 0,0,0. 

from maptek import vulcan

with vulcan.dgd('pydemo.dgd.isis', 'create') as dgd: # create a design database called pydemo
    layer = vulcan.layer('DEMO') # create a new layer called DEMO
    obj = vulcan.polyline([0,0,0]) # add a single point
    obj.name = 'POINT' # change the name of the point to "POINT"
    obj.set_colour(1) # change the colour to colour 1
    layer.append(obj) # add to the layer
    dgd.append(layer) # add the layer to the dgd

1.3 Open existing dgd and add data

This example opens the dgd and layer created previously and adds a polyline. The line consists of two points with a line between them created using the set_connected() method.

# open dgd in write mode and add a line

from maptek import vulcan
import numpy as np
import pandas as pd 

with vulcan.dgd('pydemo.dgd.isis','w') as dgd:
    layer = dgd.get_layer('DEMO') # get the "DEMO" layer
   
    # create a diagonal line
    obj = vulcan.polyline([[5,5,5],[10,10,10]])
    obj.name = 'LINE'
    obj.set_colour(3)
    
    # set the line as connected so it appears as a line
    obj.set_connected()

    # add the line to the layer then save
    layer.append(obj)
    dgd.save_layer(layer)

1.4 Create a closed polygon

Open an existing dgd and layer and add a closed polygon in the shape of a square.

This example illustrates the use of the set_closed() method to close a polygon.

# open dgd in write mode and add a closed polygon

from maptek import vulcan
import numpy as np
import pandas as pd 

with vulcan.dgd('pydemo.dgd.isis','w') as dgd:
    layer = dgd.get_layer('DEMO') # get the "DEMO" layer
    
    # create a closed polygon
    obj = vulcan.polyline([[5,0],[7,0],[7,2],[5,2]])
    obj.name = 'SQUARE'
    obj.set_colour(5)
    obj.set_connected()
    obj.set_closed()
    
    # add the polygon to the layer then save
    layer.append(obj)
    dgd.save_layer(layer)

1.5 Get object details

This example illustrates how to get details for each object in a layer.

# Get details for each polyline object in a layer.

from maptek import vulcan

with vulcan.dgd('pydemo.dgd.isis','r') as dgd:
    layer = dgd.get_layer('DEMO')
    print('Layer Objects: {0}'.format(layer.num_objects()))
    
    for obj in layer.get_objects([vulcan.polyline]):
        name = obj.get_name()
        desc = obj.description
        closed = obj.closed
        print(f"Layer name: {name}, Description: {desc}, Closed: {closed}")

1.6 Reading from a dgd

# Open the dgd for read access and print all the layers and all of the objects in it.

from maptek import vulcan

dgd_file_name = 'pydemo.dgd.isis'

with vulcan.dgd(dgd_file_name, "r") as dgd:      
    for layer in dgd:          
        print(layer.name)    
        for obj in layer:              
            print(obj)

1.7 Editing objects in a dgd

# Edit objects in dgd

from maptek import vulcan    

dgd_file_name = 'pydemo.dgd.isis'

with vulcan.dgd(dgd_file_name, "w") as dgd:      
    my_layer = None      
    # Find the layer we want      
    layer_name = "TEMP"      
    
    # If it exists, modify an object in it      
    if dgd.is_layer(layer_name):          
        my_layer = dgd.get_layer(layer_name)          
        idx = len(my_layer)-1          
        if idx > 0:              
            obj = my_layer[idx]              
            obj.name = "LastObject"              
            my_layer[idx] = obj              
            dgd.save_layer(my_layer)          
            
    # If the layer doesn't exist, make one and add it.          
    else:              
        my_layer = vulcan.layer(layer_name)              
        obj = vulcan.polyline([0,0,0])              
        obj.name = "Origin point"              
        my_layer.append(obj)              
        dgd.append(my_layer)

1.8 2D text manipulation

# >>> [Indicates sample output.]  

from maptek import vulcan  

# Text can be initialized with input data or not  
text = vulcan.text("Single line")  
text.origin = [78000,6000,0]  
text.framed = True  
text.angle = 55  
text.scale = "1:15000"  
text.font = "Arial"  
text.height = 0.5  
text.width = 0.5  

print (text)  
# >>>  Object (TEXT)...edited...  
# >>>    Description: Created   in Python  
# >>>    Feature    :  
# >>>    Group      :  
# >>>    Primitive  :  
# >>>    Value      :   0.000000  
# >>>    Colour     :   0  
# >>>    IsEdited   :   1  
# >>>    Type       :   TEXT  
# >>>    Origin     :   (500.000, 500.000, 0.000)  
# >>>    Angle      :   55.000  
# >>>    Scale      :   1:15000  
# >>>    Height     :   0.500  
# >>>    Width      :   0.500  
# >>>    Font       :   Arial  
# >>>    Box        :   Yes  
# >>>    Text data  :   1  
# >>>               :   Single line  

print (list(text))  
# >>> ['Single line']  

text.append("Second line")  
text.append("Third line")
  
print (list(text))  
# >>> ['Single line', 'Second line', 'Third   line']  

del(text[1]) 
 
print (list(text))  
# >>> ['Single line', 'Third line']

1.9 3D text manipulation

# >>> [Indicates sample output.]
from maptek import vulcan

# Text can be initialized with input data or not
text = vulcan.text3d("Single line")
text.origin = [78000,6000,0]
text.direction = [0.5,0.5,0.5]
text.normal = [0.5,0.5,0]
text.origin = [0.5,-0.5,0]
text.framed = True
text.angle = 55
text.scale = "1:500"
text.font = "Arial"
text.height = 0.5
text.width = 0.5
text.italic = True
text.mirrored_vertical = True
text.mirrored_horizontal = True
text.horizontal_align = "LEFT"
text.vertical_align = "CENTRE"
print (text)

# >>>  Object (TEXT3D)...edited...
# >>>     Description:   Created in Python
# >>>     Feature    :
# >>>     Group      :
# >>>     Primitive  :
# >>>     Value      :   0.000
# >>>     Colour     :   0
# >>>     IsEdited   :   1
# >>>     Type       :   3DTEXT
# >>>     Origin     :   (0.500, -0.500, 0.000)
# >>>     Direction  :   (0.500, 0.500, 0.500)
# >>>     Normal     :   (0.500, 0.500, 0.000)
# >>>     Scale      :   1:500
# >>>     Width      :   0.500
# >>>     Height     :   0.500
# >>> (H) Mirrored   :   Yes
# >>> (V) Mirrored   :   Yes
# >>>     Italic     :   Yes
# >>> (H) Position   :   Left
# >>> (V) Position   :   Centre
# >>>     Font       :   Arial
# >>>     Text data  :   1
# >>>                :   Single line

1.10 Polyline manipulation

# >>> [Indicates sample output.]
from maptek import vulcan
origin = (0,0)
size = 10
square_points = ([origin[0], origin[1] ],
[origin[0]+size, origin[1] ],
[origin[0]+size, origin[1]+size],
[origin[0], origin[1]+size])
poly = vulcan.polyline(square_points)
poly.set_connected()
poly.closed = True
poly.group = "PLOT_MARKERS"
poly.feature = "SQUARE"
poly.colour = 5
print (poly)
# >>> Object (POLYLINE)...edited...
# >>> Description: Created in Python
# >>> Feature : SQUARE
# >>> Group : PLOT_MARKERS
# >>> Primitive :
# >>> Value : 0.000000
# >>> Colour : 5
# >>> IsEdited : 1
# >>> Type : POLYLINE
# >>> Line index : 0
# >>> Pattern : 0
# >>> Closed : true
# >>> Points : 4
# >>> 0 : ( 0.000, 0.000, 0.000, 0.000) t(0) []
# >>> 1 : ( 10.000, 0.000, 0.000, 0.000) t(0) []
# >>> 2 : ( 10.000, 10.000, 0.000, 0.000) t(0) []
# >>> 3 : ( 0.000, 10.000, 0.000, 0.000) t(0) []
# >>> Link data : 1
# >>> 0 : type( 17) => (24262895156382685135231)
# >>> * : Variant => ()
print(poly.centroid())
# >>> point(5.0,5.0,0.0,0.0,0,'')
### Attributes
attr = poly.attributes
attr["template"] = { "attrib" : "value" }
print (poly.attributes)
# >>> {'template': {'attrib': 'value'}}
### Metadata
print (poly.metadata)# >>> None
poly.metadata['NewMeta'] = "Value"
print (poly.metadata)
>>> {'NewMeta': 'Value'}
### Section widths
poly.section_widths.front_width = 10
poly.section_widths.back_width = 10
poly.section_widths.enable = True
print (poly.section_widths)
# >>> Section widths:
# >>> Enabled : 1
# >>> Front width : 10.000000
# >>> Back width : 10.000000
# >>> Cross sections: 0
# >>> Cross points : 0

1.11 Arrow manipulation

# >>> [Indicates sample output.]
from maptek import vulcan
arrow = vulcan.arrow()
# Default type is 3D, Type can be changed by:
# arrow.arrow_type = "2D"
arrow.start = [10,10,0]
arrow.end = [100,75,0]
arrow.filled = True
# Head length/width defaults to relative
# Override by doing the following:
# arrow.head_length = 1
# arrow.head width = 0.5
arrow.relative_head_length = 20 # Percent
arrow.relative_head_width = 10 # Percent
print (arrow)
# >>>  Object (ARROW)...edited...
# >>>     Description: Created in Python
# >>>     Feature    :
# >>>     Group      :
# >>>     Primitive  :
# >>>     Value      : 0.000000
# >>>     Colour     : 0
# >>>     IsEdited   : 1
# >>>     Type       : Arrow
# >>>     Start      : (0.000, 0.000, 0.000)
# >>>     End        : (100.000, 75.000, 0.000)
# >>>     Normal     : (0.000, 0.000, 0.000)
# >>>     Scale      : 1:1250
# >>>     Auto Scale : Yes
# >>>     Arrow type : 3D
# >>>     Filled     : Yes
# >>>     Facets     : 20
# >>>     Head Length: 0.000
# >>>     Head Width : 0.000
# >>>     Head Length: 20.000 % relative
# >>>     Head Width : 10.000 % relative

1.12 Apply feature code to object

This adds a way to define the feature file (.ftd) so that it apply features automatically.

set-feature(self, feature: "std::string const &") -> "void':

Sets the object feature. Defaults to searching the paths: VULCAN_CORPORATE > CWD > ENVIS_RESO. First found will be used.

Note:  Layer information is not set.

return _vulcan.obj_set>feature(self, feature)
set_feature_with_file(self, *args, **kwargs) -> "void':

Sets the object feature with a specifc feature file. Blank file will search the same as set_feature. If ignore flag is set, missing files and invalid feature codes will be set without attributes. If IgnoreMissingFeatures-False, valueError will be thtown with missing feature codes, FileNotFoundError for missing files.

Note:  Layer information is not set.

return _vulcan.obj_set_feature_with_file(self, *args, **kwargs)
# >>> [Indicates sample output.]
from maptek import vulcan # feature file contains DS1 # which has a linestyle and colour enabled. # these do not get applied when using either of # the two examples. # method 1 with vulcan.dgd('ephtest.dgd.isis', 'w') as dgd: layer = dgd.get_layer('TEST') for idx, obj in enumerate(layer.get_objects([vulcan.polyline])): obj.feature = 'DS1' layer[idx] = obj dgd.save_layer(layer) # method 2 with vulcan.dgd('ephtest.dgd.isis', 'w') as dgd: layer = dgd.get_layer('TEST') for idx, obj in enumerate(layer.get_objects([vulcan.polyline])): obj.set_feature('DS1') layer[idx] = obj dgd.save_layer(layer)

1.13 Set object visibility

modes = ['SHADOW', 'INVISIBLE', 'VISIBLE']
for obj in objs:
    for mode in modes:
        print (vulcan_gui.get_object_visibility(obj))
        if vulcan_gui.set_object_visibility(obj, mode)
            print (f'Set visibility to: {mode}')

def set_object_visibility(object: 'obj', mode: 'std::string const &') -> 'bool':
    #set_object_visibility(object, mode) -> bool
    #Parameters
    #----------
    #object: obj const &
    #Gets the visibility of an object in Vulcan. Only supports objects from
    #the GUI with the visibility modes of VISIBLE, SHADOW, and INVISIBLE
    return _vulcan_gui.set_object_visibility(object, mode)

def get_object_visibility(object: 'obj') -> 'std::string':
    #get_object_visibility(object) -> std::string
    #Parameters
    #----------
    #object: obj const &
    #Gets the visibility of an object in Vulcan. 
    return _vulcan_gui.set_object_visibility(object