TechDraw TemplateGenerator/de

Einleitung
Diese Anleitung beschreibt, wie man eine einfache Vorlage zur Benutzung mit dem TechDraw-Arbeitsbereich aus ein paar Zeilen Python-Kode erstellt.

Jeder Texteditor kann zum Erstellen von Kode genutzt werden. Mein Favorit ist Atom... Aber FreeCADs eingebauter Editor funktioniert auch bestens.

Die folgenden Kode-Beispiele können kopiert, in eine leere Textdatei eingefügt und dann unter einem selbstgewählten Namen als *.py oder *.FCMacro abgespeichert werden.

Eine Vorlage stellt einen Hintergrund für Zeichnungsaufgaben bereit und ihre Maße werden vom Druckertreiber für die korrekte Skalierung der Zeichnung verwendet.

Die Vorlagen sind SVG-Dateien, daher muss ein Makro einige Zeilen SVG-Kode (eine Untermenge von XML-Kode) zusammenstellen.

Struktur einer einfachen leeren Seite
Das SVG-Format ist eine Untermenge des XML-Formats. Daher besteht eine SVG-Datei, wie auch jede XML-Datei, aus zwei Teilen:
 * Einem Kopf mit eine Formatdeklaration
 * Einem Körper, der die Informationen darüber enthält, was anzuzeigen ist und wo es platziert wird
 * (Ich weiß nicht, warum die Kopfzeile enthalten sein soll; die SVG-Datei funktioniert auch ohne sie als Vorlagedatei)

Kopf
Der Kopf besteht aus nur einer Zeile zur Angabe der Version der XML-Sprache, die verwendet werden soll, um die im Körper befindlichen Anweisungen zu verarbeiten.

Körper
The Body starts with an opening tag which contains information about name spaces and about the size of the template and where to place it. And it finishes with a closing tag.


 * xmlns="http://www.w3.org/2000/svg": External link to the xml name space to look up standard xml commands
 * version="1.1": Used xml version is 1.1
 * xmlns:freecad="Svg Namespace": External link to FreeCAD's name space extension
 * to look up special commands that are only used inside a FreeCAD environment e.g. for editable texts
 * width="420mm": Width of the drawing area
 * height="297mm": Height of the drawing area
 * viewBox="0 0 420 297": Position of the upper left corner (0;0) and the lower right corner (420;297) in the svg construction space (in svg units).
 * Width, height, and viewBox in this combination set 1 svg-unit to 1 mm for the whole document. A dimensional unit can be omitted from now on.
 * In this case 420 and 297 give an A3 page. Customise these values to generate other page sizes

For a blank page size DIN A3 in landscape orientation that's all.

Python-Kode...
Before any code is generated a folder is needed to store the new template file.

The user should have selected a template folder. Its path is then stored in the TechDraw preferences. It is not necessary to know where the preferences are store, because FreeCAD has commands to address needed parameters directly.

parameter_path receives the path to the "folder" within the configuration file where "TemplateDir" parameter can be found. template_path receives the content of "TemplateDir" which is the path to the template directory. template_name recieves the name of the template to be created.

Now the template name needs to be linked to the template path in a way that is compatible to unix based OSs and Windows. This is done with the "os.path.join" command and stored into the template_file.

... zum Erstellen einer leeren Seite
This macro shows the principle how an svg-file can be put together. (Format is A3)


 * The main principle is:
 * to open a file for writing and so start an svg-file from scratch, write the header line and close the file as a first step.
 * and then to repeatedly open the file to append further segments and then close it again after appending.
 * The macro is made of several functions which are called from the main section.
 * Additional functions could be inserted before the EndSvg function and the needed calls are put before the EndSvg call.
 * We need to have an eye on the number of spaces used with the write operations for a correct indentation.
 * We need to have an eye on the number of spaces used with the write operations for a correct indentation.

... zum Erstellen einer Seite mit ein paar Strichen
To make a drawing out of a blank page we need:
 * Frames i.e. rectangles created with the rect instruction
 * a title block and more made of lines created with the path instruction
 * simple texts for indexes and labeling title block cells
 * editable texts like part number or part name

Normally these graphical elements are used repeatedly and so the generating code is put into functions.

svgrect function
To draw a rectangle we just need to call the svgrect function and hand over the values for width, height, and position of the upper left corner. That is better readable than the content of the svgline which had to be used instead.

svgpath function
To draw a line we just need to call the svgpath function and hand over the coordinates of the start point and the endpoint of a line.

Instead of the end point coordinates we can hand over a tag to draw a horizontal (h) or vertical (v) line followed by the length of the line or the tag to draw a horizontal (H) or vertical (V) line followed by the x or y ordinate of the end point.

Each path starts at the origin and the first action is a movement with "raised pen" (not drawing) to the start point. In this case the relative movement and the absolute movement are the same and so it is irrelevant whether the m-tag is upper or lower case.

svgtext function
To draw a piece of text we just need to call the svgtext function and hand over the coordinates of the text's anchor point and the text string itself.

(The text alignment is controlled by the surrounding group tag or left-aligned as default).

FCeditext function
To draw a piece of editable text we just need to call the FCeditext function and hand over a name, the coordinates of the text's anchor point, and the text string itself.

FreeCAD creates a dictionary object with every inserted template, and each entry has a name (key) and a value.

(The text alignment is controlled by the surrounding group tag or left-aligned as default).

CreateXxxx functions
These functions start with code to open a file in append mode and to write the group opening tag.

Then follows a section to set and calculate values and with write instructions that call the above functions to generate svg-code.

And finally the group closing tag followed by an instruction to close the file.

Resulting macro
This macro adds some basic graphical elements needed for proper templates i.e. line elements, texts, and editable texts.

And this is the svg-code coming out of this macro:

And what it should look like when inserted (plus magnified title block):