summaryrefslogtreecommitdiff
path: root/modules/xcos/macros/generateBlockImage.sci
blob: 58a3d6a2e86f1c7b6450e90b1bd12a185506d02c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) DIGITEO - 2010-2010 - Clément DAVID
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
//
//

// Generate a block image from the instance.
//
// @param block the block instance
// @param path output file path
// @param[opt] filename the file name to use (without extension).
//             The default is `block.gui'.
// @param[opt] imageType the exported image type. only "svg", "gif", "jpg" is
//             supported. The default is to use "gif".
// @param[opt] withPort true if the exported image should contains the port,
//             false otherwise. The default is value is true.
// @return status %T if the operation has been sucessfull, %F otherwise.
function status = generateBlockImage(block, path, filename, imageType, withPort)
    status = %f;

    // call loadXcosLibs if not loaded
    if exists("scicos_diagram", "a") == 0 then loadXcosLibs(); end

    [lhs,rhs] = argn(0)
    if rhs < 2 | rhs > 6 then
        error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"), "generateBlockImage", 2, 4));
    end

    if typeof(block) <> "Block" then
        error(msprintf(gettext("%s: Wrong type for input argument ""%s"": Block type expected.\n"), "generateBlockImage", "block"));
    end

    if typeof(path) <> "string" | ~isdir(path) then
        error(msprintf(gettext("%s: Wrong type for input argument ""%s"": directory path string expected.\n"), "generateBlockImage", "path"));
    end

    // generate a default graphic or clear the existing one
    if exists("imageType", "l") == 0 then
        imageType = "gif";
    else
        if typeof(imageType) <> "string" | and(imageType <> ["svg", "gif", "jpg"]) then
            error(msprintf(gettext("%s: Wrong input argument ""%s"": ""svg"", ""gif"" or ""jpg"" expected.\n"), "generateBlockImage", "imageType"));
        end
    end

    if exists("withPort", "l") == 0 then
        withPort = %t;
    end

    // set the default outFile
    if exists("filename", "l") == 0 then
        outFile = path + "/" + block.gui + "." + imageType;
    else
        if typeof(filename) <> "string" | size(filename, "*") <> 1 then
            error(msprintf(gettext("%s: Wrong type for input argument ""%s"": string expected.\n"), "generateBlockImage", "filename"));
        end
        outFile = path + "/" + filename + "." + imageType;
    end

    // if the gr_i value of a bloc is empty, return
    if (block.graphics.gr_i(1) == []) then
        mputl("<svg/>", outFile);
        status = %t;
        return
    end

    // set export properties before creating any graphic object (including any figure)
    previous_driver = driver(imageType);
    xinit(outFile);

    handle = gcf();

    if ~withPort then
        prot = funcprot();
        function standard_draw_port(varargin)
        endfunction
        standard_draw_port_up = standard_draw_port;
        funcprot(prot);
    end

    // constants
    diagram = scicos_diagram();
    options = diagram.props.options;
    options("3D")(1) = %f;
    sz = block.graphics.sz;
    orig = block.graphics.orig;

    gh_axes = gca();
    gh_curwin = handle;

    // draw settings
    // note that the gh_axes variable have to be known on the "plot" call
    gh_axes.fractional_font = "off";
    gh_axes.arc_drawing_method = "lines";
    o_size = size(gh_axes.children);
    gh_axes.data_bounds = [orig(1), orig(2); sz(1), orig(2)+sz(2)];
    gh_axes.isoview = "on";
    gh_axes.margins = 0.01 * ones(1, 4);
    gh_axes.box ="off";
    handle.axes_size = [max(20, 20 * sz(1)), max(20, 20 * sz(2))];

    // Create variable o because needed inside "plot"
    o = block;
    ierr = execstr("block  = " + o.gui + "(""plot"",o)", "errcatch");
    if (ierr <> 0) then
        return;
    end

    // export
    try
        xend();
        status = %t;
    catch
        status = %f;
    end

    // post operations
    driver(previous_driver);
endfunction