S T R U C T O R I Z E R - User Guide
Export > Source code

Code Export Preparations

First of all, if you want to use the code generators, you should make sure to have filled in the parser preferences appropriately. You can find them via the menu "Preferences => Parser ...":

Parser Preferences dialog with french keys

All of the words you filled in there will be filtered out and replaced by the respective target language keywords.

For example, if your diagram contains a FOR-loop and you are writing french diagrams, you may have something like this:

If you then do a "Pascal" export, the generated code will be:
for I:=1 to 10
    { do something }
but a "C" export will result in the code:
for (I=1; I<=10; I+=(1)) {
    // do something

As you can notice, the words "pour" and "à" have been eliminated or replaced with whatever the exported code requires. For being able to do this, the generators must be fed the necessary information about the syntax you are using in your diagrams.

Interactive Code Export


To export a diagram as code, go to the menu and select "File => Export => Code ..." and the name of the language you want to export to.

The currently supported programming or script languages are the following ones (where StruktTeX is not actually a programming language but an add-on to LaTeX providing embeddable NSD drawings):

  • Pascal / Delphi
  • Oberon
  • StrukTeX
  • Perl
  • ksh
  • bash
  • C
  • C#
  • C++
  • Java
  • PHP
  • Python
  • Basic

Export Configuration

In the Export Preferences you may select your most frequently used (favourite) target language in order to accelerate the export via key combination Ctrl+Shift+X:
File menu with new menu item for export to the favourite language

You should be aware, of course, that the generated program files will usually not immediately be compilable or executable in the target language but require some manual post-processing because the generators will have to do a lot of mere guesswork: Though Structorizer tries hard to derive static type information from assignments or implicit declarations, the types of many of the used variables will not be known such that the generators will hardly be able to produce complete declaration lists for languages that require strict declaration. You will have to check for wrong, missing or mis-placed declarations and must accomplish or correct them manually. Some algorithmic construct may not directly be translatable such that the generators must try their best to compose something closely equivalent.

Usually "TODO" comments inserted in the generated code will guide you through the manual post-processing work.

The Export Option "No conversion of the expression/instruction contents" allows you to perform a kind of raw export with mere control structure conversion and mostly suppressed translation of the element contents. It is recommended to use this option if you explicitly write target code in Structorizer elements. In this case Structorizer wouldn't know how to translate your code into any other language, though.

Export Option "Involve called subroutines" is to enrich the code for the selected diagram with the routine definitions for all subroutines referred to by contained CALL elements - if the corresponding diagrams are reachable via the Arranger. Unavailable subroutines the exported diagram depends on will be reported without aborting the export process (the definitions will simply be missing in the resulting file(s)). You may find hints for missing subroutines in advance in the Analyser Report List if Analyser Preference "Check for inappropriate subroutine CALLs and missing call targets" is enabled.

See also: Code generators

Batch Export

Structorizer may also be used in batch mode to generate a source file for a supported programming language or other textual export format (like StrukTeX) from NSD files. The command syntax is given below, where the underlined pseudo program name Structorizer is to be replaced by the respective batch or shell script name for the console environment:

  • structorizer.sh for Linux, UNIX, and the like;
  • Structorizer.bat for Windows.

The scripts can be found in the Structorizer installation directory; don't try with Structorizer.exe! The Java WebStart installation is not suited, either - you need the unzipped downloadable version.

Structorizer -x generator [-a] [-b] [-c] [-l] [-t] [-e encoding] [-] [-f] [-o output-file] nsdarr-file ...

The options mean:

-x (followed by a generator name ) must be the first option and selects the target language or generator class. Currently supported language or generator class names are (case-insensitive, synonyms separated by "|"):

  • PasGenerator | Pascal | Delphi
  • OberonGenerator | Oberon
  • TexGenerator | StrukTeX
  • PerlGenerator | Perl
  • KSHGenerator | ksh
  • BASHGenerator | bash
  • CGenerator | C
  • CSharpGenerator | C#
  • CPlusPlusGenerator | C++
  • JavaGenerator | Java
  • PHPGenerator | PHP
  • PythonGenerator | Python
  • BasGenerator | Basic

-a ensures that the attributes like author, creation time, and license text or link of the diagram will also be exported to the code file(s).

-b sets the opening block brace for the body of compound statements at the beginning of the next line (otherwise to the end of the same line).

-c will export simple instructions as comments.

-l will create line numbers and more ancient-style code on BASIC export.

-t will suppress most transformations of instruction and expression contents (intended for the case that the Structorizer elements already contain code complying with the target language syntax).

-e ("encoding", followed by a charset name) determines the output file character set (UTF-8 being the default).

-f forces overwriting an existing file with same name as the designated output file (see -o), otherwise a name modification by an added or incremented counter is made (e.g. output.cpp -> output.0.cpp).

-o (followed by a file path or name) specifies a specific output file name. If not given then the output file name will be derived from the first nsd file name by replacing the file name extension with the one associated to the target language, e.g. ".java". The target-language-specific file name extension is ensured in either case. If several nsd files are given then without option -o the created file will be named after the first nsd file name; with option -o, however, the filename of the option will be used.

- (single minus sign) will direct the code to the standard output instead of to the default output file. If -o option is also given, then the result will be written both to standard output and to the specified output file.

nsdarr-file a file path/name/specification of a Structorizer diagram or aarrangement file (.nsd, .arr, or .arrz) to be converted into a source file. (Arrangement file specifications may also be among the file list since version 3.29-05. For arrangements) An arrangement file specification consists of an arrangement file name (.arr, .arrz) with an optionally appened sequence of diagram names or routine signatures, separated with exclamation marks (no blanks!), e.g. /home/bob/files/foo.arrz!test(7)!MAIN!sub(2-3). This would mean, that program diagram MAIN and routine diagrams test (with 7 arguments) and sub (with two mandatory arguments and another optional one) would be picked out of the arrangement archive foo.arrz and exported together with all directly or indirectly referenced subroutine and includable diagrams from this archive into a single file. If no signatures are appended then all program (main) diagrams in the arrangement file will be taken as export roots. If there aren't any program diagrams in the arrangement then you must specify routine signatures, otherwise nothing would be exported from the arrangement. All loose .nsd files among the files will contribute their diagrams to a single source file, which is divided by
"==== 8< ========================"
lines. Each arrangement file will also feed an own source code file, where some separator lines will be placed among the explicitly specified roots. But be aware, that the additionally exported referenced routines may be shared among them.

Note that the parser preferences configured in interactive Structorizer mode will be valid here, whereas the export options configured in interactive mode are not valid here, only the command line options described above will be observed (for binary options, the contrary of the described option effect being the default, e.g. without specifying -l option there won't be line numbers or other old-fashioned relics in BASIC output). The export option "Involve called subroutines" will automatically be enabled for arrangements and disabled for loose diagram files.

Export of Diagrams for Turtleizer

If you export diagrams that control Turtleizer, then the exported code will usually not work unless you find some more or less compatible source package for your target language. The Python language fortunately contains a native "turtle" package that fully supports all Turtleizer commands and functions. Since version 3.28-10 the Python code generator of Structorizer performs all necessary conversions automatically. For Java and C++ you may easily get identical or equivalent external Turtle support:

  • Since release 3.27, the original Turtleizer package sources from Structorizer are available for separate download and may be integrated in the Java project you set up around your exported code. You will also need the diagrcontrol package as an interface it relies on. Both packages require at least Java 8 as platform. The exported Java file will automatically be prepared to work with this package.
  • For C++, there is a functionally equivalent VisualStudio project Turtleizer_CPP available on GitHub that can at least be used by console applications under Windows -- either as static library (after having updated the project file to your VisualStudio version) or simply by integrating the header and code files in your C++ project. Your exported C++ file will just have to include the "Turtleizer.h" header and possibly to add a Turtleizer::awaitClose(); instruction (provided the other project settings are suited, see the README.md file in the download for details).