# Code Import and Code Generation

Umbrello is a UML modelling tool, and as such its main purpose is to help you in the analysis and design of your systems. However, to make the transition between your design and your implementation, Umbrello allows you to generate source code in different programming languages to get you started. Also, if you want to start using UML in an already started project, Umbrello can help you create a Model of your system from the source code by analysing your source code and importing the classes found in it.

## Code Generation

Umbrello can generate source code for various programming languages based on your UML Model to help you get started with the implementation of your project. The code generated consists of the class declarations, with their methods and attributes so you can fill in the blanks by providing the functionality of your classes' operations.

Umbrello 1.1 comes with Code Generation support for C++, Java and PHP. Other languages can be easily added at a later time as plug-in modules to the application.

### Adding Languages

Umbrello 1.1 ships with generators for C++, Java, and PHP which will be added to the list of supported languages the first time you start Umbrello. You can later on download generators for other languages and add them as plug-ins to the application.

Basically, the first time Umbrello finds a new Code Generation plug in, it will ask you to confirm which of the languages offered by the new plug in you want to install (one plug in may offer support for more than one language). You can always add or remove languages from your list of available languages by selecting the option Add/Remove Languages available in the Code menu.

Add / Remove Language Add and Remove languages for Code Generation in Umbrello

Add and Remove languages for Code Generation in Umbrello

### Generating Code

In order to generate code with Umbrello, you first need to create or load a Model containing at least one class. When you are ready to start writing some code, select the Code Generation Wizard entry from the Code menu to start a Wizard which will guide you trough the code generation process.

The first step is to select the classes for which you want to generate source code. By default all the classes of your model are selected, and you can remove the ones for which you do not want to generate code by moving them to the left-hand side list.

The next step of the wizard allows you to modify the parameters the Code Generator uses while writing your code. The following options are available:

Code Generation Options Options for the Code Generation in Umbrello

Options for the Code Generation in Umbrello

#### Generation Options

##### Code Verbosity

The option Write documentation comments even if empty instructs the Code Generator to write comments of the /** blah */ style even if the comment blocks are empty. If you added documentation to your classes, methods or attributes in your Model, the Code Generator will write these comments as Doxygen documentation regardless of what you set here, but if you select this option Umbrello will write comment blocks for all classes, methods and attributes even if there is no documentation in the Model, in which case you should document your classes later directly in the source code.

Write comments for sections even if section is empty: Umbrello writes comments in the source code to delimit the different sections of a class. For example public methods or Attributes before the corresponding sections. If you select this option Umbrello will write comments for all sections of the class even if the section is empty. For example, it would write a comment saying protected methods even if there are no protected methods in your class.

##### Directories

Write all generated files to directory.Here you should select the directory where you want Umbrello to put the generated sources.

The Include heading files from directory option allows you to insert a heading at the beginning of each generated file. Heading files can contain copyright or licensing information and contain variables that are evaluated at generation time. You can take a look at the template heading files shipped with Umbrello to see how to use this variables for replacing your name or the current date at generation time.

##### Overwrite Policy

This option tells Umbrello what to do if the file it wants to create already exists in the destination directory. Umbrello 1.1 cannot modify existing source files, so you have to choose between overwriting the existing file, skipping the generation of that particular file or letting Umbrello choose a different file name. If you choose the option to use a different name, Umbrello will add a suffix to the file name.

##### Language

Umbrello will by default generate code in the language you have selected as Active Language, but with the Code Generation Wizard you have the option to change this to another language.

#### Generation Wizard Generation

The third and last step of the wizard shows the status of the Code Generation process. You need only to click on the Generate button to get your classes written for you.

Note that the Options you select during the Code Generation wizard are only valid for the current generation. The next time you run the Wizard you will need to re-select all the options (your headings directory, overwrite policy, and so on). You can set the defaults used by Umbrello in the Code Generation section of the Umbrello settings, available at Settings Configure Umbrello...

If you have set your Code Generation options to the right settings and want to generate some code right away without going through the wizard, you can select the entire Generate All Code from the Code menu. This will generate code for all the classes in your Model using the current settings (including Output Directory and Overwrite Policy, so use with care).

## Code Import

Umbrello can import source code from your existing projects to help you build Model of your systems. Version 1.1 of the application supports only C++ source code, but other languages should be available in future versions.

To import classes into your Model, select the entry Import Classes... from the Code menu. In the file dialog select the files containing the C++ class declarations and press OK. The classes will be imported and you will find them as part of your Model in the Tree View. Note that Umbrello will not create any kind of Diagram for showing your classes, they will only be imported into your Model so that you can use them later in any diagram you want.

Code Import Menu for importing source code in Umbrello

Menu for importing source code in Umbrello