Products Consultancy

Check variant and prevent hard coding


Friday, August 12, 2011

Each modeller experiences that he runs a job which is by accident executed on the wrong variant. Another often occurring problem are moved OmniTRANS projects including jobs with hard coded links to files. These two situations are time consuming, but can be prevented easily.

 

The class OtSystem allows you to get information like the active variant name or a string with the path to the variant. We will discuss these properties in this article, more properties can be found in the OmniTRANS manual.

 

Important: instead of other OmniTRANS classes, the OtSystem class is called by "$Ot" like:

$Ot.omnitransVersion

Check the variant

A job might be created only to be executed on a specific variant. Therefore it is useful to add a check in first lines of your job to be sure that the job is executed on the correct variant. The property OtSystem.requireVariant checks whether the current variant has a certain name, and if not, raises an exception (essentially halting the job). When your job is created to only run on the "Base” variant, you use this property like:

$Ot.requireVariant "Base"

It might be necessary to create your own error message to indicate what the user should do. The property OtSystem.variant allows you to get the name of the active variant, like:

writeln $Ot.variant

We want to compare this name with the active variant name. This is possible by using an if-statement like:

if $Ot.variant != "Base"
    raise "It is not allowed to execute the job on this variant, " +
    "because the job will overwrite current data assignments"
end

The if-statement is triggered when the variant name is not equal (!=) to "Base". In case the variant name is not equal, a raise message will appear which will stop the job immediately.

 

When the job is created for a specific variant and is also applicable for other variants, you can use the OtSystem.confirmVariant property. This property allows you to set an accepted variant name. If the variant's name is equal, the job will continue otherwise a dialog is displayed asking if the user wants to continue. The job exits if the user chooses "No". You can use the property like below:

$Ot.confirmVariant "Base"

 

Prevent hard coding

Hard coding refers to the embedding of external files by supplying a fixed path ("C:\users\john\omnitransprj\shape.dbf”). When the shape.dbf is moved to George’s PC ("C:\users\george\ omnitransprj\shape.dbf”), the job cannot find the file anymore and will return an error. To get flexible jobs, it is useful to prevent hard coded links and use variables to refer to a directory. The OtSystem class contains approximately ten default variables which refer to OmniTRANS related directories. We will show several examples to get the variant directory, project directory or the job directory:

writeln $Ot.variantDirectory
writeln $Ot.projectDirectory
writeln $Ot.jobDirectory

To include such a property in a reference, you have to define it like:

filename = "#{$Ot.projectDirectory}shape.dbf"

The method #{} allows you to refer to an variable within a string.

 

This also allow you to write output including OtSystem properties like writing variant name and the used OmniTRANS version.

writeln "The job will be applied on the #{$Ot.variant} variant " +
"with OmniTRANS version #{$Ot.omnitransVersion[0]}. #{$Ot.omnitransVersion[1]}"

In summary, using properties of the OtSystem class can make your jobs more robust and flexible. It's a small investment and has great benefits.