diff options
Diffstat (limited to 'modules/xcos/macros')
57 files changed, 2897 insertions, 0 deletions
diff --git a/modules/xcos/macros/%Block_xcosUpdateBlock.bin b/modules/xcos/macros/%Block_xcosUpdateBlock.bin Binary files differnew file mode 100755 index 000000000..f33ba0d67 --- /dev/null +++ b/modules/xcos/macros/%Block_xcosUpdateBlock.bin diff --git a/modules/xcos/macros/%Block_xcosUpdateBlock.sci b/modules/xcos/macros/%Block_xcosUpdateBlock.sci new file mode 100755 index 000000000..0819fe20c --- /dev/null +++ b/modules/xcos/macros/%Block_xcosUpdateBlock.sci @@ -0,0 +1,32 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2011 - Scilab Enterprises - Clement 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 +// +// + +function %Block_xcosUpdateBlock(blk) + // Overload function when calling xcosUpdateBlock with a block + + // generate a unique temp path + timestamp = getdate(); + tempfile = TMPDIR + filesep() + "xcosUpdateBlock_" + string(timestamp(10)) + ".sod"; + while isfile(tempfile) + timestamp = timestamp + 1; + tempfile = TMPDIR + filesep() + "xcosUpdateBlock_" + string(timestamp(10)) + ".sod"; + end + + // export to hdf5 + blk = blk; + if export_to_hdf5(tempfile, "blk") then + xcosUpdateBlock(tempfile) + else + error(msprintf(gettext("%s: Unable to export %s to %s.\n"), "xcosUpdateBlock", "`blk''", tempfile)); + end + +endfunction diff --git a/modules/xcos/macros/%diagram_xcos.bin b/modules/xcos/macros/%diagram_xcos.bin Binary files differnew file mode 100755 index 000000000..f2d0f9ac1 --- /dev/null +++ b/modules/xcos/macros/%diagram_xcos.bin diff --git a/modules/xcos/macros/%diagram_xcos.sci b/modules/xcos/macros/%diagram_xcos.sci new file mode 100755 index 000000000..e4136a0f5 --- /dev/null +++ b/modules/xcos/macros/%diagram_xcos.sci @@ -0,0 +1,27 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009-2009 - DIGITEO - Vincent COUVERT <vincent.couvert@scilab.org> +// Copyright (C) 2010-2010 - DIGITEO - Clément DAVID <clement.david@scilab.org> +// +// 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 +// +// + +function %diagram_xcos(scs_m) + // Overload function when calling xcos with a diagram + // + // Calling Sequence + // xcos(scs_m); + // + // Parameters + // scs_m: the diagram instance + + // allocate a local copy + scs_m = scs_m; + // call xcos with the copy and name + xcos(scs_m, "scs_m"); +endfunction diff --git a/modules/xcos/macros/buildmacros.bat b/modules/xcos/macros/buildmacros.bat new file mode 100755 index 000000000..23a827489 --- /dev/null +++ b/modules/xcos/macros/buildmacros.bat @@ -0,0 +1,11 @@ + +rem Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +rem Copyright (C) 2009 - DIGITEO +rem +rem This file must be used under the terms of the CeCILL. +rem This source file is licensed as described in the file COPYING, which +rem you should have received as part of this distribution. The terms +rem are also available at +rem http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + +@..\..\..\bin\scilex -nwni -ns -e exec('buildmacros.sce');quit; diff --git a/modules/xcos/macros/buildmacros.sce b/modules/xcos/macros/buildmacros.sce new file mode 100755 index 000000000..cf891e9ca --- /dev/null +++ b/modules/xcos/macros/buildmacros.sce @@ -0,0 +1,16 @@ + +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Allan CORNET <allan.cornet@inria.fr> +// +// 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 + +if (isdef("genlib") == %f) then + exec(SCI+"/modules/functions/scripts/buildmacros/loadgenlib.sce"); +end + +genlib("xcoslib","SCI/modules/xcos/macros",%f,%t); + diff --git a/modules/xcos/macros/cleanmacros.bat b/modules/xcos/macros/cleanmacros.bat new file mode 100755 index 000000000..053cf56f2 --- /dev/null +++ b/modules/xcos/macros/cleanmacros.bat @@ -0,0 +1,13 @@ + +rem Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +rem Copyright (C) 2009 - DIGITEO +rem +rem This file must be used under the terms of the CeCILL. +rem This source file is licensed as described in the file COPYING, which +rem you should have received as part of this distribution. The terms +rem are also available at +rem http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + +@del *.bin 2>NUL +@del lib 2>NUL +@del names 2>NUL diff --git a/modules/xcos/macros/generateBlockImage.bin b/modules/xcos/macros/generateBlockImage.bin Binary files differnew file mode 100755 index 000000000..65e65f472 --- /dev/null +++ b/modules/xcos/macros/generateBlockImage.bin diff --git a/modules/xcos/macros/generateBlockImage.sci b/modules/xcos/macros/generateBlockImage.sci new file mode 100755 index 000000000..58a3d6a2e --- /dev/null +++ b/modules/xcos/macros/generateBlockImage.sci @@ -0,0 +1,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 + diff --git a/modules/xcos/macros/generateBlockImages.bin b/modules/xcos/macros/generateBlockImages.bin Binary files differnew file mode 100755 index 000000000..7570489fe --- /dev/null +++ b/modules/xcos/macros/generateBlockImages.bin diff --git a/modules/xcos/macros/generateBlockImages.sci b/modules/xcos/macros/generateBlockImages.sci new file mode 100755 index 000000000..444a5ba58 --- /dev/null +++ b/modules/xcos/macros/generateBlockImages.sci @@ -0,0 +1,118 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) DIGITEO - 2009-2009 - Vincent COUVERT +// 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 xcos palette block icons and graph images from scicos pal files. +// +// @param palFiles set of string of palette files +// @param iconsOutPath output path of the palette icons (GIF files). +// The default is the common `palFiles' dir. +// @param imagesOutPath output path of the graph images (SVG files). +// The default is the common `palFiles' dir. +// @param[opt] traceEnable %T if a trace output must be printed, %F if not (default=%F). +function generateBlockImages(palFiles, iconsOutPath, imagesOutPath, traceEnable) + [lhs, rhs] = argn(0); + if rhs < 3 then + error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected.\n"), "generateBlockImages", 3)); + end + + if typeof(iconsOutPath) <> "string" | ~isdir(iconsOutPath) then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": directory path string expected.\n"), "generateBlockImages", "iconsOutPath")); + end + + if typeof(imagesOutPath) <> "string" | ~isdir(imagesOutPath) then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": directory path string expected.\n"), "generateBlockImages", "imagesOutPath")); + end + + if exists("traceEnable", "l") == 0 then + traceEnable = %f; + else + if typeof(traceEnable) <> "boolean" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": boolean expected.\n"), "generateBlockImage", "traceEnable")); + end + end + + // call loadXcosLibs if not loaded + if exists("scicos_diagram", "a") == 0 then loadXcosLibs(); end + + if traceEnable then + ncl = lines(), lines(0); + end + + // iterator on all blocks + for fIndex = 1:size(palFiles, "*") + + if ~isfile(palFiles(fIndex)) then + if traceEnable then + mprintf(gettext("%s: File ''%s'' does not exist.\n"), "generateBlockImages", palFiles(fIndex)); + end + continue; + end + + exec(palFiles(fIndex), -1); + + if isempty("scs_m") then + if traceEnable then + mprintf(gettext("%s: File ''%s'' is not a valid palette file.\n"), "generateBlockImages", palFiles(fIndex)); + end + continue; + end + + for iBlock = 1:size(scs_m.objs) + block = scs_m.objs(iBlock); + + if typeof(block)=="Block" & block.gui == "PAL_f" then + // Add PAL_f children blocks + children = block.model.rpar.objs; + for jBlock = 1:size(children) + varsToLoad($+1) = children(jBlock).gui; + end + elseif typeof(block)=="Block" then + // old scicos palettes doesn't have a PAL_f block but directly + // the reference instances instead. + varsToLoad($+1) = block.gui + else + if traceEnable then + mprintf(gettext("%s: Found ''%s'' instead of a block.\n"), "generateBlockImages", typeof(block)); + end + continue; + end + end + + clear scs_m; + end + + varsToLoad = gsort(varsToLoad, "r", "i"); + for kBlock = 1 : size(varsToLoad, "*") + ierr = execstr("scs_m = " + varsToLoad(kBlock) + "(""define"")", "errcatch"); + if traceEnable then + mprintf("%d: %s", kBlock, varsToLoad(kBlock)); + end + if ierr == 0 then + status = generateBlockImage(scs_m, imagesOutPath, imageType=imageType, %f); + if status & traceEnable then + mprintf(" SUCCEED\n"); + elseif traceEnable then + mprintf(" FAILED\n"); + end + elseif traceEnable then + mprintf(" FAILED\n"); + end + end + + if traceEnable then + lines(ncl); + end +endfunction + +function c=scs_color(c) +endfunction diff --git a/modules/xcos/macros/getDiagramVersion.bin b/modules/xcos/macros/getDiagramVersion.bin Binary files differnew file mode 100755 index 000000000..2ed4a29bd --- /dev/null +++ b/modules/xcos/macros/getDiagramVersion.bin diff --git a/modules/xcos/macros/getDiagramVersion.sci b/modules/xcos/macros/getDiagramVersion.sci new file mode 100755 index 000000000..b685825af --- /dev/null +++ b/modules/xcos/macros/getDiagramVersion.sci @@ -0,0 +1,43 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2010-2010 - DIGITEO - Clément DAVID <clement.david@scilab.org> +// +// 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 +// +// + +function version = getDiagramVersion(diagram) + // Return the current diagram version + // + // Calling Sequence + // version = getDiagramVersion(diagram) + // + // Parameters + // diagram: the diagram instance + // version: the version of the diagram + // + // Example + // loadXcosLibs(); + // version = getDiagramVersion(scicos_diagram()) + + [lhs,rhs] = argn(0); + version = get_scicos_version(); + if rhs <> 1 then + error(999, msprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "getDiagramVersion", 1)); + end + + if typeof(diagram) <> "diagram" then //check inputs + error(999 ,msprintf(_("%s: Wrong type for argument #%d: diagram structure expected"), "getDiagramVersion", 1)); + end + + // check version + if find(getfield(1, diagram) == "version") <> [] & diagram.version <> "" then + version=diagram.version; + else + version=find_scicos_version(diagram); + end +endfunction diff --git a/modules/xcos/macros/getModelicaPath.bin b/modules/xcos/macros/getModelicaPath.bin Binary files differnew file mode 100755 index 000000000..da4467446 --- /dev/null +++ b/modules/xcos/macros/getModelicaPath.bin diff --git a/modules/xcos/macros/getModelicaPath.sci b/modules/xcos/macros/getModelicaPath.sci new file mode 100755 index 000000000..9e6d781b4 --- /dev/null +++ b/modules/xcos/macros/getModelicaPath.sci @@ -0,0 +1,59 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2010 - DIGITEO - Jérôme PICARD +// Copyright (C) 2010 - DIGITEO - Pierre MARECHAL + +// +// 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 +// + +function [modelica_path, modelica_directory] = getModelicaPath() + // Return the Modelica directory path used by Xcos diagram containing Modelica blocks + // + // Calling Sequence + // [modelica_path, modelica_directory] = getModelicaPath() + // + // Parameters + // modelica_path: Indicates path for generic modelica blocks, user + // can add paths with %MODELICA_USER_LIBS for his own + // blocks. + // modelica_directory: output directory for generated modelica files + // %MODELICA_USER_LIBS: string path global variable used by the user to + // modify modelica_path. All paths should exist. + + // Init + modelica_path = "SCI/modules/scicos_blocks/macros/" + ["Electrical","Hydraulics"]; + modelica_directory = pathconvert(TMPDIR+"/modelica/",%T,%T); + + // user-defined %MODELICA_USER_LIBS + global %MODELICA_USER_LIBS + if exists("%MODELICA_USER_LIBS") & ~isempty(%MODELICA_USER_LIBS) then + + if type(%MODELICA_USER_LIBS)<>10 then + error(msprintf(gettext("%s: Wrong type for %s variable: A string array expected.\n"),"getModelicaPath","%MODELICA_USER_LIBS")); + end + + if or(~isdir(%MODELICA_USER_LIBS)) then + error(msprintf(gettext("%s: All paths defined by %s variable must exist.\n"),"getModelicaPath","%MODELICA_USER_LIBS")); + end + + // reshape %MODELICA_USER_LIBS to get a row vector + %MODELICA_USER_LIBS = matrix(%MODELICA_USER_LIBS,[1 prod(size(%MODELICA_USER_LIBS))]) + + // Remove duplicate paths + %MODELICA_USER_LIBS = unique(%MODELICA_USER_LIBS); + + // Concatenate %MODELICA_USER_LIBS with modelica_path + modelica_path = [ %MODELICA_USER_LIBS modelica_path ]; + end + + // create modelica directory if it doesn't exist + if ~isdir(modelica_directory) & mkdir(modelica_directory)<>1 then + error(msprintf(gettext("%s: The directory %s cannot been created, please check if you have write access on this directory.\n"),"getModelicaPath",modelica_directory)); + end + +endfunction + diff --git a/modules/xcos/macros/importScicosDiagram.bin b/modules/xcos/macros/importScicosDiagram.bin Binary files differnew file mode 100755 index 000000000..bed1f4cc4 --- /dev/null +++ b/modules/xcos/macros/importScicosDiagram.bin diff --git a/modules/xcos/macros/importScicosDiagram.sci b/modules/xcos/macros/importScicosDiagram.sci new file mode 100755 index 000000000..156aceca9 --- /dev/null +++ b/modules/xcos/macros/importScicosDiagram.sci @@ -0,0 +1,53 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2010 - DIGITEO - 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 +// + +// +// Import a scicos diagram. +// +// This method wrap all the scicos entry points (load, version, etc...). +// +// @source the filename or a diagram instance +// @out the scicos diagram structure (scs_m) +function out = importScicosDiagram(source) + out = []; + + [lhs,rhs] = argn(0); + if rhs <> 1 then + error(999, msprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "importScicosDiagram", 1)); + return; + end + + if typeof(source) == "diagram" then //import scs_m structure + out = source; + elseif typeof(source) == "string" then //import cos or cosf file + [path,name,ext] = fileparts(source); + ext2 = convstr(ext, "u"); + if ext2 == ".COS" then + load(source); + out = scs_m; + clear scs_m; + elseif ext2 == ".COSF" then + exec(source, -1); + out = scs_m; + clear scs_m; + else + error(999 ,msprintf(_("%s: Wrong type for argument #%d: String or diagram structure expected"), "importScicosDiagram", 1)); + return; + end + else + error(999, msprintf(_("%s: Wrong type for argument #%d: String or diagram structure expected"), "importScicosDiagram", 1)); + return; + end + + version = getDiagramVersion(out); + out = do_version(out, version); +endfunction + diff --git a/modules/xcos/macros/importScicosPal.bin b/modules/xcos/macros/importScicosPal.bin Binary files differnew file mode 100755 index 000000000..1160c21f1 --- /dev/null +++ b/modules/xcos/macros/importScicosPal.bin diff --git a/modules/xcos/macros/importScicosPal.sci b/modules/xcos/macros/importScicosPal.sci new file mode 100755 index 000000000..070c10d2d --- /dev/null +++ b/modules/xcos/macros/importScicosPal.sci @@ -0,0 +1,141 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009-2009 - DIGITEO - Vincent COUVERT +// Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS +// Copyright (C) 2009-2010 - DIGITEO - 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 +// + +function importScicosPal(palFiles, outPath) + // Export all palettes to a path as H5 files + // + // Calling Sequence + // importScicosPal(palFiles, outPath); + // + // Parameters + // palFiles: string array; the palette files to export + // outPath: string; path where to export the palettes + // + // Description + // To export Scicos palettes to an HDF5 instance, use this macro. + // + // Examples + // palFiles = ls(SCI + "/modules/scicos/palettes/*.cosf"); + // importScicosPal(palFiles, SCI + "/modules/scicos_blocks/blocks"); + // + // See also + // xcosPal + // xcosPalAddBlock + // + // Authors + // Vincent COUVERT + // Antoine ELIAS + // Clément DAVID + + rhs = argn(2); + + if ~exists("scicos_diagram") then + loadXcosLibs(); + end + + if rhs < 2 then + error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected.\n"), "importScicosPal", 2)); + return + end + + // global variables + exportedBlocks = 0; + + // foreach file + for fIndex = 1:size(palFiles, "*") + if ~isfile(palFiles(fIndex)) then + error(msprintf(gettext("%s: File ''%s'' does not exist.\n"), "importScicosPal", palFiles(fIndex))); + return + end + + mprintf("%s\n", palFiles(fIndex)); + + // get the block names + varsToLoad = []; + exec(palFiles(fIndex), -1); + + blockLst = scs_m.objs; + lstIndex = 1; + while length(blockLst) >= lstIndex, + block = blockLst(lstIndex); + + // add PAL_f children blocks + if typeof(block)=="Block" & block.gui == "PAL_f" then + blockLst = lstcat(blockLst, block.model.rpar.objs); + blockLst(lstIndex) = null() + mprintf("\t%s\n", block.model.sim) + elseif typeof(block)=="Block" then + // old scicos palettes doesn't have a PAL_f block but directly + // the reference instances instead. + varsToLoad($+1) = block.gui + end + + lstIndex = lstIndex + 1; + end + + // export the blocks + for i = 1:size(varsToLoad, "*") + block_name = varsToLoad(i) + ".sod"; + blockFile = outPath + "/" + block_name + + // instanciate a block + // /!\ may cause an error depending on the implementation + execstr("out = " + varsToLoad(i) + "(""define"")"); + + // Update on super block + if out.model.sim == "super" | out.model.sim == "csuper" then + [ierr,scicos_ver,scs_m]=update_version(out.model.rpar); + + if ierr <> 0 then + mprintf("FAILED TO UPDATE AND EXPORT: %s\n", out.gui); + else + mprintf("%s updated from %s\n", out.gui, scicos_ver); + end + + out.model.rpar = scs_m; + end + + doExport = %t; + if isfile(blockFile) then + //if the file already exists try to load data and compare + out2 = out; + bImport = import_from_hdf5(blockFile); + + //data are identical + if bImport == %t & isequal(out, out2) then + doExport = %f; + else + out = out2; + end + end + + if doExport == %t then + mprintf("%d: %s\n", i, block_name); + bexport = export_to_hdf5(blockFile, "out"); + if (~bexport) then + mprintf("FAILED TO EXPORT: %s\n", out.gui); + end + + out2 = out; + bImport = import_from_hdf5(blockFile); + + if bImport == %f | or(out2 <> out) then + mprintf("FAILED TO EXPORT: %s\n", out.gui); + end + exportedBlocks = exportedBlocks + 1; + end + end + end + disp("exportedBlocks : " + string(exportedBlocks)); +endfunction + diff --git a/modules/xcos/macros/importXcosDiagram.bin b/modules/xcos/macros/importXcosDiagram.bin Binary files differnew file mode 100755 index 000000000..7e589779c --- /dev/null +++ b/modules/xcos/macros/importXcosDiagram.bin diff --git a/modules/xcos/macros/importXcosDiagram.sci b/modules/xcos/macros/importXcosDiagram.sci new file mode 100755 index 000000000..9a5e5ca42 --- /dev/null +++ b/modules/xcos/macros/importXcosDiagram.sci @@ -0,0 +1,46 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Antoine ELIAS +// +// 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 +// + +//import xcos diagram in Scilab environment +//parameters : +//xcosFile : xcos diagram file +//result : boolean +function result = importXcosDiagram(xcosFile) + + result = %f; + rhs = argn(2); + + if(rhs == 1) then + //save hdf5 file with xcos filename, just change extension + [path,fname,extension] = fileparts(xcosFile); + h5File = TMPDIR + filesep() + fname + ".sod"; + // open the file to check for permissions + [a, err] = mopen(xcosFile, "r"); + if (err <> 0) then + error(msprintf(gettext("Unable to open %s" + "\n"), xcosFile)); + return; + end + // construct a full path string + fullPathName = get_absolute_file_path(fname + extension) + fname + extension; + mclose(a); + else + error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected." + "\n"), "importXcosDiagram", 1)); + return; + end + + // import the real file + convertStatus = xcosDiagramToScilab(fullPathName); + + //return scs_m in Scilab environment + result = %t; + scs_m = resume(scs_m); +endfunction + diff --git a/modules/xcos/macros/lib b/modules/xcos/macros/lib Binary files differnew file mode 100755 index 000000000..644336e89 --- /dev/null +++ b/modules/xcos/macros/lib diff --git a/modules/xcos/macros/loadXcosLibs.bin b/modules/xcos/macros/loadXcosLibs.bin Binary files differnew file mode 100755 index 000000000..0fe34ec53 --- /dev/null +++ b/modules/xcos/macros/loadXcosLibs.bin diff --git a/modules/xcos/macros/loadXcosLibs.sci b/modules/xcos/macros/loadXcosLibs.sci new file mode 100755 index 000000000..0fc82aa40 --- /dev/null +++ b/modules/xcos/macros/loadXcosLibs.sci @@ -0,0 +1,70 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009 - DIGITEO - Allan CORNET +// Copyright (C) 2011 - DIGITEO - 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 +// +// +function loadXcosLibs() + // Extracted from initial_scicos_tables + scicos_pal_libs = ["Branching","Events","Misc","Sinks","Threshold","Linear","MatrixOp","NonLinear","Sources","Electrical","Hydraulics","PDE","IntegerOp"]; + + // list of scicos libraries that we need at xcos launch + listlibsname = []; + + for theLib = scicos_pal_libs + if isfile("SCI/modules/scicos_blocks/macros/" + theLib + "/lib") then + load("SCI/modules/scicos_blocks/macros/" + theLib + "/lib"); + listlibsname = [listlibsname, theLib]; + end + end + clear theLib; + + if isfile("SCI/modules/scicos/macros/scicos_scicos/lib") then + load("SCI/modules/scicos/macros/scicos_scicos/lib"); + listlibsname = [listlibsname, "scicos_scicos"]; + end + + // name and libname + listlibsname(listlibsname == "MatrixOp") = "Matrixop"; + listlibsname(listlibsname == "NonLinear") = "Nonlinear"; + listlibsname(listlibsname == "IntegerOp") = "Integerop"; + + if listlibsname <> [] then + resumedLibs = listlibsname + "lib"; + else + resumedLibs = string([]); + end + + // Compatibility interface functions + function [x,y,typ]=COMPAT_BLOCK(job,arg1,arg2) + // Throw an error on block access + x=[];y=[];typ=[] + if ~exists("arg1") then + arg1 = mlist(["Block", "gui"], "COMPAT_BLOCK"); + end + error(msprintf(gettext("%s: the block ""%s"" is no more available, please update the diagram with a compatible one."), "loadXcosLibs", arg1.gui)); + endfunction + + // removed blocks + removed = [ + "AFFICH_f" + "RFILE" + "WFILE"]; + prot = funcprot(); + funcprot(0); + execstr(strcat(removed + "=COMPAT_BLOCK; ")); + funcprot(prot); + + resumedBlocks = removed'; + + // Put all resumed symbols into the parent scope + // Take care: resume() will also return to the parent scope + execstr("[" + strcat([resumedLibs resumedBlocks], ", ") + "] = resume(" + strcat([resumedLibs resumedBlocks], ", ") + ");"); +endfunction + diff --git a/modules/xcos/macros/names b/modules/xcos/macros/names new file mode 100755 index 000000000..efa3dc39e --- /dev/null +++ b/modules/xcos/macros/names @@ -0,0 +1,26 @@ +%Block_xcosUpdateBlock +%diagram_xcos +generateBlockImage +generateBlockImages +getDiagramVersion +getModelicaPath +importScicosDiagram +importScicosPal +importXcosDiagram +loadXcosLibs +xcosBlockEval +xcosBlockInterface +xcosCodeGeneration +xcosConfigureModelica +xcosPal +xcosPalAdd +xcosPalAddBlock +xcosPalExport +xcosPalGenerateAllIcons +xcosShowBlockWarning +xcosValidateBlockSet +xcosValidateCompareBlock +xcos_compile +xcos_debug_gui +xcos_run +xcos_simulate diff --git a/modules/xcos/macros/xcosBlockEval.bin b/modules/xcos/macros/xcosBlockEval.bin Binary files differnew file mode 100755 index 000000000..08c6217b0 --- /dev/null +++ b/modules/xcos/macros/xcosBlockEval.bin diff --git a/modules/xcos/macros/xcosBlockEval.sci b/modules/xcos/macros/xcosBlockEval.sci new file mode 100755 index 000000000..6186aa571 --- /dev/null +++ b/modules/xcos/macros/xcosBlockEval.sci @@ -0,0 +1,50 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS <antoine.elias@scilab.org> +// Copyright (C) 2010-2010 - DIGITEO - Clément DAVID <clement.david@scilab.org> +// +// 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 +// +// + +// Evaluate a block with the context input. +// +// @param interfaceAlias block interface function ( ex IN_f ) +// @param blk the block +// @param context the context string +function blk = xcosBlockEval(interfaceAlias, blk, context) + + //replace scicos_getvalue by setvalue (call by interfaceAlias) + %mprt = funcprot() + funcprot(0) + scicos_getvalue = setvalue; + getvalue = setvalue; + function message(txt) + messagebox(.. + ["In block " + o.gui + ": " ; txt ; "current parameter value kept"],.. + "error","modal"); + [str,n,line,func]=lasterror(); + printf("do_eval: error %d - %s in %s at line %d\n", n, str, func, line); + endfunction + funcprot(%mprt) + + // define scicos variables + %scicos_prob = %f + %scicos_debug_gr = %f + needcompile = 4; + [modelica_libs, scicos_pal_libs, %scicos_with_grid, %scs_wgrid] = initial_scicos_tables(); + + // allocate the context + %scicos_context = struct(); + [%scicos_context, ierr] = script2var(context, %scicos_context) + + // Every parameter settings is done, perform block update + + //create a structure with the new context + [new_blk, y, typ] = interfaceAlias("set", blk, []); + blk = new_blk; +endfunction + diff --git a/modules/xcos/macros/xcosBlockInterface.bin b/modules/xcos/macros/xcosBlockInterface.bin Binary files differnew file mode 100755 index 000000000..09ef4bc96 --- /dev/null +++ b/modules/xcos/macros/xcosBlockInterface.bin diff --git a/modules/xcos/macros/xcosBlockInterface.sci b/modules/xcos/macros/xcosBlockInterface.sci new file mode 100755 index 000000000..40044e031 --- /dev/null +++ b/modules/xcos/macros/xcosBlockInterface.sci @@ -0,0 +1,53 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET +// Copyright (C) 2009-2010 - DIGITEO - 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 +// +// + +function blk = xcosBlockInterface(interfaceAlias, job, blk, context) + + // push some old scicos variable in environment. + needcompile = 0; + alreadyran = %f; + + // define context + %scicos_context = struct(); + [%scicos_context, ierr] = script2var(context, %scicos_context) + + //for backward compatibility for scifunc + if ierr==0 then + %mm = getfield(1,%scicos_context) + for %mi=%mm(3:$) + ierr = execstr(%mi+"=%scicos_context(%mi)","errcatch") + if ierr<>0 then + break; //** in case of error exit + end + end + end + //end of for backward compatibility for scifunc + + ierr = execstr("[new_blk, y, typ] = interfaceAlias(job, blk, [])", "errcatch"); + if ierr <> 0 then + [msg, err] = lasterror(); + disp(msg); + return blk; + end + + // Check if the block has been updated or not. + // If the data has changed then we don't need to recompile (indicated by + // no file creation). + updated = and([needcompile == 0, and(new_blk == blk)]) <> %t; + if updated then + blk = new_blk; + else + blk = []; + end +endfunction + diff --git a/modules/xcos/macros/xcosCodeGeneration.bin b/modules/xcos/macros/xcosCodeGeneration.bin Binary files differnew file mode 100755 index 000000000..7f7235e41 --- /dev/null +++ b/modules/xcos/macros/xcosCodeGeneration.bin diff --git a/modules/xcos/macros/xcosCodeGeneration.sci b/modules/xcos/macros/xcosCodeGeneration.sci new file mode 100755 index 000000000..73681741d --- /dev/null +++ b/modules/xcos/macros/xcosCodeGeneration.sci @@ -0,0 +1,40 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET +// Copyright (C) 2012 - Scilab Enterprises - Clement 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 +// +// + +function blk = xcosCodeGeneration(blk) + // define scicos variables + %scicos_prob = %f + %scicos_debug_gr = %f + needcompile = 4; + + ierr = execstr("[ok, XX] = do_compile_superblock42(blk, [], [], %f); ", "errcatch"); + if ierr <> 0 then + [msg, err] = lasterror(); + disp(msg); + + // push blk error + blk = []; + blk = resume(blk) + end + + if ~ok then + // push blk error + blk = []; + blk = resume(blk) + end + + blk = XX; + // push the results (and interface function) on the upper scope + execstr("[blk, " + XX.gui + "]=resume(blk, "+ XX.gui +")"); +endfunction + diff --git a/modules/xcos/macros/xcosConfigureModelica.bin b/modules/xcos/macros/xcosConfigureModelica.bin Binary files differnew file mode 100755 index 000000000..4388f4f16 --- /dev/null +++ b/modules/xcos/macros/xcosConfigureModelica.bin diff --git a/modules/xcos/macros/xcosConfigureModelica.sci b/modules/xcos/macros/xcosConfigureModelica.sci new file mode 100755 index 000000000..2ed10cbec --- /dev/null +++ b/modules/xcos/macros/xcosConfigureModelica.sci @@ -0,0 +1,71 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET +// Copyright (C) 2010-2010 - DIGITEO - 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 +// +// + +function xcosConfigureModelica() + // Configure the current diagram with the modelica setting UI. + // + // Description + // On modelica diagrams, variable initialization has to be performed by a + // specific UI. This achieve on this macro which rely on an existing scs_m + // diagram variable. + // + + modelica_libs = getModelicaPath() + + name=scs_m.props.title(1); + if ~validvar(name) then + messagebox(msprintf(_("%s is not a valid name, please change the title of the diagram."),name),"error","modal"); + return + end + + name=stripblanks(name)+"_im"; + path=TMPDIR+"/"; + path=pathconvert(stripblanks(path),%t,%t) + + + mofile=path+name+".mo"; + xmlfile_init=path+name+"f_init.xml"; + xmlfile_relations=path+name+"f_relations.xml"; + + [info,err1]=fileinfo(xmlfile_init); + [info,err2]=fileinfo(xmlfile_relations); + [info,err3]=fileinfo(mofile); + + if (err1==0 & err2==0 & err3 == 0) then, + if (newest(xmlfile_init,mofile)==2) then compile=%t;end; + else + compile=%t; + end + + compile=%t; // Very conservative + + needcompile=4; + + if compile then + %Modelica_Init=%t + // in order to generate *_im.mo -> *_im_f.mo -> *_im.xml + [bllst,connectmat,clkconnect,cor,corinv,ok]=c_pass1(scs_m); + %Modelica_Init=%f + end + [info,err1]=fileinfo(xmlfile_init); + [info,err2]=fileinfo(xmlfile_relations); + + if err1==0 & err2==0 then + xcosConfigureXmlFile(xmlfile_init, xmlfile_relations); + end + + // variables needed by compile_init_modelica + [modelica_libs,bllst,connectmat,clkconnect,cor,corinv]=resume(modelica_libs,bllst,connectmat,clkconnect,cor,corinv); + +endfunction + diff --git a/modules/xcos/macros/xcosPal.bin b/modules/xcos/macros/xcosPal.bin Binary files differnew file mode 100755 index 000000000..dc8306d87 --- /dev/null +++ b/modules/xcos/macros/xcosPal.bin diff --git a/modules/xcos/macros/xcosPal.sci b/modules/xcos/macros/xcosPal.sci new file mode 100755 index 000000000..cdce8fabf --- /dev/null +++ b/modules/xcos/macros/xcosPal.sci @@ -0,0 +1,117 @@ +// +// 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 +// +// + +function pal = xcosPal(name, scs_m) + // Instanciate a new Xcos palette on Scilab. + // + // Calling Sequence + // pal = xcosPal(); + // pal = xcosPal(name); + // pal = xcosPal(scs_m); + // pal = xcosPal([], scs_m); + // pal = xcosPal(name, scs_m); + // + // Parameters + // name: string; the optional palette name + // scs_m: diagram mlist; the optional source diagram + // pal: palette tlist; the palette instance + // + // Description + // Instanciate a new palette diagram. + // + // The optional name argument can be used to set a name to the palette. The optional scs_m argument can be used to import diagrams as palettes. + // + // Examples + // loadXcosLibs(); + // + // // from scratch + // pal = xcosPal("My sum palette"); + // pal = xcosPalAddBlock(pal, "SUM_f"); + // pal = xcosPalAddBlock(pal, "BIGSOM_f"); + // xcosPalAdd(pal); + // + // // from an old palette + // exec(SCI + "/modules/scicos/palettes/Integer.cosf", -1); + // pal = xcosPal(scs_m); + // xcosPalAdd(pal); + // + // See also + // xcosPal + // xcosPalAddBlock + // + // Authors + // Clément DAVID + + + [lhs,rhs] = argn(0); + + if rhs > 2 then + error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"), "xcosPal", 0, 2)); + end + + if exists("name", "l") == 0 then + name = "New palette"; + elseif isempty(name) then + name = "New palette"; + elseif typeof(name) == "diagram" then + scs_m = name; + name = "New palette"; + end + // loading the scicos_diagram macro + if exists("scicos_diagram", "a") == 0 then loadXcosLibs(), end + if exists("scs_m", "l") == 0 then scs_m = scicos_diagram(), end + + if typeof(name) <> "string" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": string type expected.\n"), "xcosPal","name")); + end + + if typeof(scs_m) <> "diagram" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": diagram type expected.\n"), "xcosPal","scs_m")); + end + + // default instance + pal = tlist(["palette", "name", "blockNames", "icons", "style"],.. + [], [], [], []); + + // find palette name + if ~isempty(scs_m.objs) then + name = scs_m.props.title(1); + end + + // getting a block name list + varsToLoad = []; + for block = scs_m.objs + if typeof(block)=="Block" & block.gui == "PAL_f" then + // customize palette name + name = block.model.rpar.props.title(1); + + // Add PAL_f children blocks + children = block.model.rpar.objs; + for jBlock = 1:size(children) + varsToLoad($+1) = children(jBlock).gui; + end + elseif typeof(block)=="Block" then + // old scicos palettes doesn't have a PAL_f block but directly + // the reference instances instead. + varsToLoad($+1) = block.gui + end + end + + // set name + pal.name = name; + + // add block instance by interfunction (will generate a new reference instance). + for index = 1:size(varsToLoad, "*") + pal = xcosPalAddBlock(pal, varsToLoad(index)); + end +endfunction + diff --git a/modules/xcos/macros/xcosPalAdd.bin b/modules/xcos/macros/xcosPalAdd.bin Binary files differnew file mode 100755 index 000000000..86c57899c --- /dev/null +++ b/modules/xcos/macros/xcosPalAdd.bin diff --git a/modules/xcos/macros/xcosPalAdd.sci b/modules/xcos/macros/xcosPalAdd.sci new file mode 100755 index 000000000..6271f22b6 --- /dev/null +++ b/modules/xcos/macros/xcosPalAdd.sci @@ -0,0 +1,92 @@ +// +// 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 +// +// + +function [status, msg] = xcosPalAdd(pal, category) + // Add a palette to the Scilab/Xcos palette manager. Optional property can be added to set the category of the palette. + // + // Calling Sequence + // xcosPalAdd(pal); + // xcosPalAdd(pal, category); + // status = xcosPalAdd(pal); + // status = xcosPalAdd(pal, category); + // [status, msg] = xcosPalAdd(pal); + // [status, msg] = xcosPalAdd(pal, category); + // + // Parameters + // pal: palette tlist or path string; the palette to add + // category: string array; the optional category path to use + // status: boolean; the status of the operation + // msg: string; the error message + // + // Description + // After setting the blocks into the palette, the user add this function to add a Scilab palette to the Xcos palette manager. + // + // The optional category argument can be used to add the palette to a customized category path. If not specified, the root category is used. + // + // Examples + // loadXcosLibs(); + // pal = xcosPal(); + // pal = xcosPalAddBlock(pal, "SUM_f"); + // pal = xcosPalAddBlock(pal, "BIGSOM_f"); + // + // xcosPalAdd(pal, "my Summation blocks") + // + // See also + // xcosPal + // xcosPalAddBlock + // + // Authors + // Clément DAVID + + status = %F; + msg = ""; + + // Checking arguments + [lhs,rhs] = argn(0) + if rhs < 1 | rhs > 2 then + error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"), "xcosPalAdd", 1, 2)); + end + + if lhs > 3 then + error(msprintf(gettext("%s: Wrong number of output arguments: %d to %d expected.\n"), "xcosPalAdd", 1, 2)); + end + + if typeof(pal) <> "palette" & typeof(pal) <> "string" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": palette type or path expected.\n"), "xcosPalAdd", "pal")); + end + + if typeof(pal) == "string" & ~isfile(pal) then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": File not found.\n"), "xcosPalAdd", "pal")); + end + + if exists("category", "l") == 0 then category="", end + + if typeof(category) <> "string" & or(size(category) == [1 1]) then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": string vector expected.\n"), "xcosPalAdd", "pal")); + end + + if typeof(pal) == "string" & isfile(pal) then + path = pal; + + clear pal + import_from_hdf5(path); + if ~exists("pal", "l") then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": palette type or path expected.\n"), "xcosPalAdd", "pal")); + end + end + + // call the gateway with the variable name and the category as + // a string vector + xcosPalLoad("pal", category); + status = %T; +endfunction + diff --git a/modules/xcos/macros/xcosPalAddBlock.bin b/modules/xcos/macros/xcosPalAddBlock.bin Binary files differnew file mode 100755 index 000000000..8c129fde5 --- /dev/null +++ b/modules/xcos/macros/xcosPalAddBlock.bin diff --git a/modules/xcos/macros/xcosPalAddBlock.sci b/modules/xcos/macros/xcosPalAddBlock.sci new file mode 100755 index 000000000..5c523a578 --- /dev/null +++ b/modules/xcos/macros/xcosPalAddBlock.sci @@ -0,0 +1,181 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) DIGITEO - 2010 - Clément DAVID +// Copyright (C) - 2011 - Scilab Enterprises - 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 + +function pal = xcosPalAddBlock(pal, block, pal_block_img, style) + + // Add a block to a Scilab/Xcos palette instance. Some optional properties can be added to customize the palette icon and the style of the block. + // + // Calling Sequence + // pal = xcosPalAddBlock(pal, block); + // pal = xcosPalAddBlock(pal, block, pal_block_img); + // pal = xcosPalAddBlock(pal, block, [], style); + // pal = xcosPalAddBlock(pal, block, pal_block_img, style); + // + // Parameters + // pal: the palette to update + // block: the block to add to the palette + // pal_block_img: the block icon to use on the palette manager. + // style: the style to apply to the block + // + // Description + // This macros add a block instance to a palette. This block parameter can be an instantiated block or a name (interface-function) or a path to a saved instance. Some operations are performed to load this block and check it's availability so it's interface-function must be loaded on Scilab. Some temporary files are also generated without full path arguments. + // + // The optional pal_block_img argument is generated on the <link linkend="TMPDIR">TMPDIR</link> using Scilab graphics if not specified.Be careful that if you use our palette to be persistent you then need to specify it. Otherwise the generated image will be deleted at the end of the Scilab session. + // + // The optional style argument allow the user to determine the kind of style to be used by this block. This argument can be typed as a path <link linkend="string">string</link> or a <link linkend="struct">struct</link>. If it is a string then a default style value is generated and formatted as a style else a struct is wrapped to a key-value jgraphx settings. The <link linkend="jgraphx-style-list">following style</link> correspond to jgraphx version 1.4.0.2. These style keys can change with new version of jgraphx without any warranty. + // + // + // Examples + // loadXcosLibs(); + // pal = xcosPal(); + // + // sumPath = TMPDIR + "/sum.sod"; + // bigSomPath = TMPDIR + "/sum.sod"; + // + // scs_m = SUM_f("define"); + // export_to_hdf5(sumPath, "scs_m"); + // scs_m = BIGSOM_f("define"); + // export_to_hdf5(bigSomPath, "scs_m"); + // + // pal = xcosPalAddBlock(pal, sumPath); + // pal = xcosPalAddBlock(pal, bigSomPath); + // + // xcosPalAdd(pal); + // + // See also + // xcosPal + // xcosPalAdd + // + // Authors + // Clément DAVID + // Yann COLLETTE + + // Checking arguments + [lhs,rhs] = argn(0) + if rhs < 2 | rhs > 5 then + error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"), "xcosPalAddBlock", 2, 5)); + end + + if lhs > 1 then + error(msprintf(gettext("%s: Wrong number of output arguments: %d expected.\n"), "xcosPalAddBlock", 1)); + end + + if typeof(pal) <> "palette" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": palette type expected.\n"), "xcosPalAddBlock", "pal")); + end + + // check and tranform block argument + if typeof(block) == "Block" then + scs_m = block; + elseif typeof(block) == "string" & isfile(block) then + fd = mopen(block, "rb"); + [err, msg] = merror(fd); + if err <> 0 then + error(msg); + end + block = fullpath(block); + mclose(fd); + + // store the block instance + status = import_from_hdf5(block); + if ~status then + error(msprintf(gettext("%s: Unable to load block from ""%s"": hdf5 file expected.\n"), "xcosPalAddBlock", block)); + end + + if exists("scs_m", "l") == 0 then + error(msprintf(gettext("%s: Unable to load block from ""%s"": no `scs_m'' variable found.\n"), "xcosPalAddBlock", block)); + end + elseif typeof(block) == "string" & exists(block) <> 0 & typeof(evstr(block)) == "function" then + execstr("scs_m = " + block + "(""define"");"); + else + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": function as string or Block type or full path string expected.\n"), "xcosPalAddBlock", "block")); + end + + // at this point we can assert that `scs_m' is a full path string to a + // block reference instance. + + // now handle pal_block_img argument + if ~exists("pal_block_img", "l") | isempty(pal_block_img) then + // block icon by default + pal_block_img = TMPDIR + "/" + scs_m.gui + ".gif"; + if isfile(pal_block_img) then + error(msprintf(gettext("%s: Unable to generate the palette icon : ""%s"" already exists.\n"), "xcosPalAddBlock", pal_block_img)); + end + else + // specified block icon + if typeof(pal_block_img) <> "string" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": path string expected.\n"), "xcosPalAddBlock", "pal_block_img")); + end + + if ~isfile(pal_block_img) then + error(msprintf(gettext("%s: Wrong value for input argument ""%s"": An existing file expected.\n"), "xcosPalAddBlock", "pal_block_img")); + end + + valid_ext = ["png" "jpg" "gif" "PNG" "JPG" "JPEG" "GIF"]; + ext = strrchr(pal_block_img, "."); + if isempty(strstr(emptystr(valid_ext) + ext, valid_ext)) then + error(msprintf(gettext("%s: Wrong value for input argument ""%s"": A valid file format (png, jpg, gif) expected.\n"), "xcosPalAddBlock", "pal_block_img")); + end + clear valid_ext ext; + pal_block_img = fullpath(pathconvert(pal_block_img, %f)); + end + + + + // now handle style argument + if ~exists("style", "l") | isempty(style) then + // style by default + block_img = TMPDIR + "/" + scs_m.gui + ".svg"; + // protect drive letter + if getos() == "Windows" then + block_img = "/" + block_img; + end + style = "noLabel=1;image=file://" + block_img + ";"; + status = generateBlockImage(scs_m, TMPDIR, imageType="svg", withPort=%f); + if ~status then + error(msprintf(gettext("%s: Unable to generate the image ""%s"".\n"), "xcosPalAddBlock", block_img)); + end + else + // apply the specified style to the block + if and(typeof(style) <> ["st" "string"]) then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": string or struct expected.\n"), "xcosPalAddBlock", "style")); + elseif typeof(style) == "st" then + formattedStyle = ""; + fields = fieldnames(style); + fieldsSize = size(fields, "*"); + for i=1:fieldsSize + formattedStyle = formattedStyle + fields(i); + fieldValue = getfield(fields(i), style); + if ~isempty(fieldValue) then + formattedStyle = formattedStyle+ "=" + string(fieldValue); + end + formattedStyle = formattedStyle + ";"; + end + style = formattedStyle; + elseif typeof(style) == "string" then + if isfile(style) then + // protect drive letter + if getos() == "Windows" then + style = "/" + style; + end + style = "shape=label;image=file://" + style + ";"; + // else + // assume a well formatted string, do nothing + end + end + end + + // Store the data into the palette structure + pal.blockNames($+1) = scs_m.gui // block named class + pal.icons($+1) = pal_block_img; // palette icon full path string + pal.style($+1) = style; // block style (linked to style definition) + +endfunction + diff --git a/modules/xcos/macros/xcosPalExport.bin b/modules/xcos/macros/xcosPalExport.bin Binary files differnew file mode 100755 index 000000000..e3d848cd6 --- /dev/null +++ b/modules/xcos/macros/xcosPalExport.bin diff --git a/modules/xcos/macros/xcosPalExport.sci b/modules/xcos/macros/xcosPalExport.sci new file mode 100755 index 000000000..d4b3c2a8a --- /dev/null +++ b/modules/xcos/macros/xcosPalExport.sci @@ -0,0 +1,103 @@ +// +// 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 +// +// + +// Export a palette instance to an hdf5 file. +// +// @param pal the "palette" type +// @param path Fullpath to the hdf5 file (.sod) +// @return status %T if the export has been successful, %F otherwise +// @return[opt] msg Contains the error message in case of %F status or an empty +// string when no error as occurred. +function [status, msg] = xcosPalExport(pal, path) + // Export a palette instance to a path. + // + // Calling Sequence + // xcosPalExport(pal, path); + // status = xcosPalExport(pal, path); + // [status, message] = xcosPalExport(pal, path); + // + // Parameters + // pal: palette tlist; the palette to export + // path: string path; where to export the palette (HDF5 formatted file) + // status: boolean; status of the export + // msg: string; error message + // + // Description + // Export Scilab palette instance to a file. + // + // Examples + // loadXcosLibs(); + // pal = xcosPal(); + // + // sumPath = TMPDIR + "/sum.sod"; + // bigSomPath = TMPDIR + "/sum.sod"; + // + // scs_m = SUM_f("define"); + // export_to_hdf5(sumPath, "scs_m"); + // scs_m = BIGSOM("define"); + // export_to_hdf5(bigSomPath, "scs_m"); + // + // pal = xcosPalAddBlock(pal, sumPath); + // pal = xcosPalAddBlock(pal, bigSomPath); + // + // xcosPalExport(pal, TMPDIR + "/palette.sod"); + // + // See also + // xcosPal + // xcosPalAdd + // + // Authors + // Clément DAVID + + status = %F; + msg = ""; + + // Checking arguments + [lhs,rhs] = argn(0) + if rhs <> 2 then + error(msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"), "xcosPalExport", 2)); + end + + if lhs > 2 then + error(msprintf(gettext("%s: Wrong number of output arguments: %d to %d expected.\n"), "xcosPalExport", 1, 2)); + end + + if typeof(pal) <> "palette" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": palette type expected.\n"), "xcosPalExport", "pal")); + end + + if typeof(path) <> "string" then + error(msprintf(gettext("%s: Wrong type for input argument ""%s"": full path string expected.\n"), "xcosPalExport", "path")); + end + fd = mopen(path, "wb"); + [err, msg] = merror(fd); + if err <> 0 then + mclose(fd); + error(msg); + end + + path = fullpath(path); + mclose(fd); + + // workaround bug 7242 + pal = pal; + + // export the data to the temp file + status = export_to_hdf5(path, "pal"); + if ~status then + msg = msprintf(gettext("%s: Unable to export the palette to hdf5.\n"), "xcosPalExport"); + if lhs == 1 then + error(msg); + end + end +endfunction + diff --git a/modules/xcos/macros/xcosPalGenerateAllIcons.bin b/modules/xcos/macros/xcosPalGenerateAllIcons.bin Binary files differnew file mode 100755 index 000000000..edf9366f1 --- /dev/null +++ b/modules/xcos/macros/xcosPalGenerateAllIcons.bin diff --git a/modules/xcos/macros/xcosPalGenerateAllIcons.sci b/modules/xcos/macros/xcosPalGenerateAllIcons.sci new file mode 100755 index 000000000..00e527749 --- /dev/null +++ b/modules/xcos/macros/xcosPalGenerateAllIcons.sci @@ -0,0 +1,53 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) - 2013 - Scilab Enterprises - 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 + +function blocks = xcosPalGenerateAllIcons(path) + // 0. Check number of arguments + [lhs,rhs]=argn(); + if rhs<>1 then + error(msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"),"xcosPalGenerateAllIcons",1)); + end + if lhs<>1 then + error(msprintf(gettext("%s: Wrong number of output arguments: %d expected.\n"),"xcosPalGenerateAllIcons",1)); + end + // 1. Check type + if typeof(path)<>"string" then + error(sprintf(gettext("%s: Wrong type for argument #%d: Matrix of strings expected.\n"), "xcosPalGenerateAllIcons", 1)); + end + // 2. Check size + if and(size(path)<>1) then + error(sprintf(gettext("%s: Wrong size for input argument #%d: Vector expected.\n"), "xcosPalGenerateAllIcons" , 1)); + end + + // 3. check the argument validity + xcosPalGet(path); // will push a 'pal' mlist + + if size(pal.blockNames, "*") <= 0 then + error(sprintf(gettext("%s: Wrong value for input argument #%d: No block found.\n"), "xcosPalGenerateAllIcons" ,1)); + end + + blocks = []; + for i=1:size(pal.blockNames, "*") + name = pal.blockNames(i); + iconPath = pal.icons(i); + + // load the block + execstr("blk="+name+"(''define'');"); + + // filter-out the text block + if typeof(blk) == "Text" then + continue, + end + + // generate the icon + xcosPalGenerateIcon(iconPath); + + blocks($+1) = name; + end +endfunction diff --git a/modules/xcos/macros/xcosShowBlockWarning.bin b/modules/xcos/macros/xcosShowBlockWarning.bin Binary files differnew file mode 100755 index 000000000..894f4fdf3 --- /dev/null +++ b/modules/xcos/macros/xcosShowBlockWarning.bin diff --git a/modules/xcos/macros/xcosShowBlockWarning.sci b/modules/xcos/macros/xcosShowBlockWarning.sci new file mode 100755 index 000000000..8d53ee2d8 --- /dev/null +++ b/modules/xcos/macros/xcosShowBlockWarning.sci @@ -0,0 +1,20 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET +// +// 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 +// +// + +function xcosShowBlockWarning(k, win) + for i = k' + if typeof(scs_m.objs(i)) == "Block" .. + & size(scs_m.objs(i).doc) >= 1 + warnBlockByUID(scs_m.objs(i).doc(1), "ERROR"); + end + end +endfunction diff --git a/modules/xcos/macros/xcosValidateBlockSet.bin b/modules/xcos/macros/xcosValidateBlockSet.bin Binary files differnew file mode 100755 index 000000000..2d7bbbdd7 --- /dev/null +++ b/modules/xcos/macros/xcosValidateBlockSet.bin diff --git a/modules/xcos/macros/xcosValidateBlockSet.sci b/modules/xcos/macros/xcosValidateBlockSet.sci new file mode 100755 index 000000000..c0dcab8dc --- /dev/null +++ b/modules/xcos/macros/xcosValidateBlockSet.sci @@ -0,0 +1,99 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2010 - DIGITEO - Clément DAVID +// Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET +// +// 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 +// +// +function [status, msg] = xcosValidateBlockSet(interfFunctionName) + status = %t; + msg = ""; + + if typeof(interfFunctionName) <> "string" | size(interfFunctionName) <> [1, 1] + error(999, sprintf(_("%s: Wrong type for argument #%d: A String expected."), "xcosValidateBlockSet", 1)) + end + + // Check function is defined + ierr = execstr("funType = typeof("+interfFunctionName+");", "errcatch") + if ierr <> 0 | and(funType <> ["fptr", "function"]) + status = %f; + msg = _("Interface function does not exist or can not be called."); + return + end + + // Check for signature + vars=macrovar(evstr(interfFunctionName)); + if or([size(vars(1)) <> [3 1] , size(vars(2)) <> [3 1]]) then + status = %f; + msg = sprintf(_("%s is not a valid block descriptor."), interfFunctionName); + continue; + end + + // Overload usefull functions + // Stubbing the x_mdialog method + // checking it's arguments size only + function [result]=x_mdialog(title, labelsv, labelsh, default_inputs_vector) + [lhs, rhs] = argn(); + if rhs == 3 then + default_inputs_vector = labelsh; + result = x_dialog(labelsv, default_inputs_vector); + elseif rhs == 4 then + vSize = size(labelsv, "*"); + hSize = size(labelsh, "*"); + if size(default_inputs_vector) <> [vSize, hSize] then + error(999, sprintf(_("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n"), "x_mdialog", 4, vSize, hSize)); + end; + result = default_inputs_vector; + else + error(999, sprintf(_("%s: Wrong number of input arguments: %d or %d expected.\n"), "x_mdialog", 3, 4)); + end + endfunction + + // Stubbing the x_dialog method + // checking it's arguments size only + function [result]=x_dialog(labels, default_inputs_vector) + result = default_inputs_vector; + endfunction + + // Stubbing the edit_curv method + function [xx, yy, ok, gc] = edit_curv(xx, yy, axis, args, gc) + ok = %T; + if ~exists("gc", "l") then + rect=[0 0 1 1]; + axisdata=[2 10 2 10]; + gc = list(rect, axisdata); + end + endfunction + + // Stubbing the msgbox method + function [btn] = msgbox(msg, msgboxtitle, msgboxicon, buttons, ismodal) + btn=1; + endfunction + + // Stubbing the message method + function [btn] = message(strings ,buttons, modal) + btn=1; + endfunction + + ierr = execstr("scs_m = "+interfFunctionName+"(""define"", [], [])", "errcatch"); + if ierr <> 0 + status = %f; + msg = sprintf(_("Block definition with function [%s] failed."), interfFunctionName); + return + end + + ierr = execstr("scs_m = "+interfFunctionName+"(""set"", scs_m, [])", "errcatch") + if ierr <> 0 + errmsg = lasterror(); + status = %f; + msg = sprintf(_("Block configuration with function [%s] failed."), interfFunctionName); + msg = [msg;errmsg]; + return + end + +endfunction diff --git a/modules/xcos/macros/xcosValidateCompareBlock.bin b/modules/xcos/macros/xcosValidateCompareBlock.bin Binary files differnew file mode 100755 index 000000000..f57918d71 --- /dev/null +++ b/modules/xcos/macros/xcosValidateCompareBlock.bin diff --git a/modules/xcos/macros/xcosValidateCompareBlock.sci b/modules/xcos/macros/xcosValidateCompareBlock.sci new file mode 100755 index 000000000..74e928fa0 --- /dev/null +++ b/modules/xcos/macros/xcosValidateCompareBlock.sci @@ -0,0 +1,84 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET +// +// 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 +// +// + +function [status, message] = xcosValidateCompareBlock(block1, block2) + status = %t; + message = ""; + + if (typeof(block1) <> "Block") + error(999, sprintf(_("%s: Wrong type for argument #%d: A Block expected."), "xcosValidateCompareBlock", 1)) + end + if (typeof(block2) <> "Block") + error(999, sprintf(_("%s: Wrong type for argument #%d: A Block expected."), "xcosValidateCompareBlock", 2)) + end + + unconsistantFields = []; + + if or(block1.gui <> block2.gui) + status = %f; + unconsistantFields = [ unconsistantFields ; "gui"] + end + + if or(block1.doc <> block2.doc) + status = %f; + unconsistantFields = [ unconsistantFields ; "doc"] + end + + // + // Compare block.graphics + // + graphics1 = block1.graphics; + graphics2 = block2.graphics; + graphicFields = ["orig" ; "sz" ; "flip" ; "theta" ; "exprs" ; + "pin" ; "pout" ; "pein" ; "peout" ; "gr_i" ; + "id"; "in_implicit" ; "out_implicit"] + + for i=1:size(graphicFields, "*") + if or(graphics1(graphicFields(i)) <> graphics2(graphicFields(i))) + status = %f; + unconsistantFields = [ unconsistantFields ; "graphics." + graphicFields(i)] + end + end + + // + // Compare block.model + // + model1 = block1.model; + model2 = block2.model; + modelFields = [ "sim" ; + "in" ; "in2" ; "intyp" ; + "out" ; "out2" ; "outtyp" ; + "evtin" ; "evtout" ; + "state" ; "dstate" ; "odstate" ; + "rpar" ; "ipar" ; "opar" ; + "blocktype" ; + "firing" ; "dep_ut" ; + "label" ; + "nzcross" ; "nmode" ; + "equations" + ] + for i=1:size(modelFields, "*") + if or(model1(modelFields(i)) <> model2(modelFields(i))) + status = %f; + unconsistantFields = [ unconsistantFields ; "model." + modelFields(i)] + end + end + + if ~isempty(unconsistantFields) + message = []; + for i=1:size(unconsistantFields, "*") + msg = sprintf(_("Field %s has different values."), unconsistantFields(i)); + message = [message; msg]; + end + end + +endfunction diff --git a/modules/xcos/macros/xcos_compile.bin b/modules/xcos/macros/xcos_compile.bin Binary files differnew file mode 100755 index 000000000..1a1019d00 --- /dev/null +++ b/modules/xcos/macros/xcos_compile.bin diff --git a/modules/xcos/macros/xcos_compile.sci b/modules/xcos/macros/xcos_compile.sci new file mode 100755 index 000000000..842d1a834 --- /dev/null +++ b/modules/xcos/macros/xcos_compile.sci @@ -0,0 +1,94 @@ +// Scicos +// +// Copyright (C) INRIA - METALAU Project <scicos@inria.fr> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// +// See the file ../license.txt +// + +function [%cpr,ok] = xcos_compile(scs_m) + + //////////////////////////////////////////////////////////////// + // Add global environment variable so that scicos is not lost // + //////////////////////////////////////////////////////////////// + %state0 = list(); + needcompile = 4; + curwin = 1000; + %cpr = struct(); + %tcur = 0; + %cpr.state = %state0; + tf = scs_m.props.tf; + alreadyran = %f; + + show_trace = %f; //** tracing and profiling (probably by Alan L. ) + + if show_trace then + disp("c_pass0:"+string(timer())); + end + + if ~exists("%scicos_solver") then + %scicos_solver = 0 ; + end + + if ~exists("%scicos_debug_gr") then + %scicos_debug_gr = %f; //** debug mode : default is "%f" + end + + // modelica_libs needed to compile Modelica blocks + if ~exists("modelica_libs") then + modelica_libs = getModelicaPath(); + end + + par = scs_m.props; + + if alreadyran then + // terminate current simulation + do_terminate(); + end + + //** extract tolerances from scs_m.props.tol + tolerances = scs_m.props.tol ; + //** extract solver type from tolerances + solver = tolerances(6) ; + + // Propagate context through all blocks + %scicos_context = struct(); + context = scs_m.props.context; + //** context eval here + [%scicos_context, ierr] = script2var(context, %scicos_context); + + // For backward compatibility for scifunc + if ierr==0 then + %mm = getfield(1,%scicos_context) + for %mi=%mm(3:$) + ierr = execstr(%mi+"=%scicos_context(%mi)","errcatch") + if ierr<>0 then + break; //** in case of error exit + end + end + end + // End of for backward compatibility for scifuncpagate context values + + [scs_m,%cpr,needcompile,ok] = do_eval(scs_m, %cpr, %scicos_context); + if ~ok then + %cpr=list(); + return + end + + //** update parameters or compilation results + [%cpr,%state0_n,needcompile,alreadyran,ok] = do_update(%cpr,%state0,needcompile) + +endfunction diff --git a/modules/xcos/macros/xcos_debug_gui.bin b/modules/xcos/macros/xcos_debug_gui.bin Binary files differnew file mode 100755 index 000000000..946dec5fb --- /dev/null +++ b/modules/xcos/macros/xcos_debug_gui.bin diff --git a/modules/xcos/macros/xcos_debug_gui.sci b/modules/xcos/macros/xcos_debug_gui.sci new file mode 100755 index 000000000..7e0ee28f7 --- /dev/null +++ b/modules/xcos/macros/xcos_debug_gui.sci @@ -0,0 +1,567 @@ +function flag=xcos_debug_gui(flag,block) + // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + // Copyright (C) 2013 - INRIA - Serge Steer <serge.steer@inria.fr> + // + // 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 + + if scicos_debug()==0 then return;end + gui = findobj("tag","Xcos_debug_gui"); + init=gui==[] + + if init then + gui=xcos_debug_create_gui() + before=%t + t=0 + cb=curblock() + f=%cpr.sim.funs(cb) + path=%cpr.corinv(cb); + end + ud=gui.user_data; + if ~init then + //at least one flags must be selected + Flags=find(ud.Flags.value==1) + if Flags==[] then + Flags=2 + ud.Flags(2).value=1 + end + //at least before or after must be selected + When=find(ud.When.value==1) + if When==[] then + When=1 + ud.When(1).value=1 + end + + if and(flag+1<>Flags) then return,end + t=scicos_time(); + time_to_wait=evstr(ud.Time.string); + if t< time_to_wait then return,end + + instr=stripblanks(ud.Condition.string); + if instr<>"" then + if execstr("Cond="+instr,"errcatch")==0 then + if ~Cond then return,end + end + end + + cb=curblock();// l'index du block dans la structure résultant de la compilation + f=%cpr.sim.funs(cb);//fonction de calcul + + path=%cpr.corinv(cb); + + before=ud.previous==[]; + if before then + ud.previous=path + if and(When<>1) then set(gui,"user_data",ud);return,end + else + ud.previous=[] + if and(When<>2) then set(gui,"user_data",ud);return,end + end + end + //break asked + handles=ud.handles; + if before then //before block + handles(4).string=_("Stopped before block:") + else //after + handles(4).string=_("Stopped after block:") + end + Actions=[_("State derivative computation") + _("Regular outputs update") + _("States update on discrete event") + _("Output activation dates") + _("Initialization") + _("Ending") + _("ReInitialization") + _("Continous states properties update") + "" + _("Zero crossing surfaces computation") + _("Residual computation")]; + + handles(1).string=msprintf("%.3g",t); + handles(2).string=string(cb); + handles(3).string=string(flag)+" ("+Actions(flag+1)+")" + handles(5).string=sci2exp(path,0) + //Highlight current block + blk=scs_m.objs(path(1)); + uids=blk.model.label; + for k=2:size(path,"*") + blk=blk.model.rpar.objs(path(k)); + uids=[uids blk.model.label]; + end + ud.Buttons.enable="on"; + warnBlockByUID(uids, "Error"); + while %t //To handle pause actions + realtimeinit(0.01); + tt=0; + global ret; + ret=0; + while ret==0 then + //Wait till user select a button + //slow down the waiting loop + tt=tt+1; + realtime(tt); + end + for k=size(uids,"*"):-1:1 + warnBlockByUID(uids(1:k), "") + end + + r=ret;clearglobal ret;clear ret + select r + case 1 then //Next + if xcos_debug_check_time(ud.Time)&xcos_debug_check_cond(ud.Condition) then + if before then ud.previous=path;end + ud.Buttons.enable="off" + set(gui,"user_data",ud) + return + end + case 2 then //End debug + scicos_debug(0) + delete(gui.parent) + return + case 3 then //pause + Y=list() + if block.nout>0 then + outtb=%cpr.state.outtb + sz=matrix(block.outsz,block.nout,-1) + for i=1:block.nout + Y($+1)=%cpr.state.outtb(block.outptr(i)+(1:prod(sz(i,1:2)))) + end + end + U=list() + if block.nin>0 then + outtb=%cpr.state.outtb + sz=matrix(block.insz,block.nin,-1) + for i=1:block.nout + U($+1)=%cpr.state.outtb(block.inptr(i)+(1:prod(sz(i,1:2)))) + end + end + ud.Buttons.enable="off" + pause + ud.Buttons.enable="on" + if before then ud.previous=path;end + set(gui,"user_data",ud) + // end + end + end +endfunction + +function gui=xcos_debug_create_gui() + margin_x = 5; // Horizontal margin between each elements + margin_y = 5; // Vertical margin between each elements + button_w = 80; + button_h = 20; + label_h = 20; + label_h1 = 30; + label_w1 = 200; + label_w2 = 80; + frame_w = max(4*margin_x+label_w1+label_w2,4*margin_x+3*button_w,12*margin_x+10*label_h) + frame_h = 16*margin_y+10*label_h+3*label_h1+button_h + + axes_w = 2*margin_x+frame_w; + axes_h = 2*margin_y+frame_h;// Frame height + defaultfont = "arial"; // Default Font + + fig_id=max(winsid())+1 + fig = scf(fig_id) + fig.visible = "off"; + toolbar(fig.figure_id, "off"); + fig.axes_size = [axes_w axes_h]; + // Remove Scilab graphics menus & toolbar + delmenu(fig.figure_id, gettext("&File")); + delmenu(fig.figure_id, gettext("&Tools")); + delmenu(fig.figure_id, gettext("&Edit")); + delmenu(fig.figure_id, gettext("&?")); + fig.figure_size(2)=axes_h + fig.axes_size = [axes_w axes_h]; + fig.background = addcolor([0.8 0.8 0.8]); + fig.figure_name = _("Xcos debug"); + fig.closerequestfcn = "xcos_debug_gui_close()" + ax=fig.children; + ax.background= fig.background ; + + gui=uicontrol( ... + "parent" , fig,... + "style" , "frame",... + "units" , "pixels",... + "position" , [margin_x margin_y frame_w frame_h],... + "background" , [1 1 1]*0.8, ... + "tag" , "Xcos_debug_gui",... + "visible" , "on"); + + yo=axes_h-button_h; + + + y=frame_h-margin_y-label_h1 + x=margin_x; + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("Break point conditions"),... + "units" , "pixels",... + "position" , [x y frame_w-2*margin_x label_h1],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 16,... + "fontweight" , "bold", ... + "horizontalalignment" , "center", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + y=y-label_h-margin_y; + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("Break on selected flags"),... + "units" , "pixels",... + "position" , [x y frame_w-2*margin_x label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + yl=y-label_h-margin_y; + y=yl-label_h-margin_y; + xk=x + Flags=[] + for k=0:9 + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , string(k),... + "units" , "pixels",... + "position" , [xk yl label_h label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + Flags=[Flags,uicontrol( ... + "parent" , gui,... + "style" , "checkbox",... + "value" , 0,... + "units" , "pixels",... + "position" , [xk y label_h label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on")]; + xk=xk+label_h+margin_x + end + Flags(2).value=1; + yl=y-label_h-margin_y; + y=yl-label_h-margin_y; + xk=x; + When=[] + L=[_("Before call"),_("After call")] + for k=1:2 + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , L(k),... + "units" , "pixels",... + "position" , [xk yl label_w2 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + When=[When,uicontrol( ... + "parent" , gui,... + "style" , "checkbox",... + "value" , 0,... + "units" , "pixels",... + "position" , [xk y label_h label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on")]; + xk=xk+label_w2+margin_x + end + When(1).value=1; + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("After date"),... + "units" , "pixels",... + "position" , [xk yl label_w2 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + Time=uicontrol( ... + "parent" , gui,... + "style" , "edit",... + "string" , "0",... + "units" , "pixels",... + "position" , [xk y label_w2 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "callback" , "xcos_debug_check_time();" ,... + "visible" , "on"); + y=y-label_h-2*margin_y; + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("On the Scilab condition above"),... + "units" , "pixels",... + "position" , [x y frame_w-2*margin_x label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + y=y-label_h-2*margin_y; + Condition=uicontrol( ... + "parent" , gui,... + "style" , "edit",... + "string" , "",... + "units" , "pixels",... + "position" , [x y frame_w-6*margin_x label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "callback" , "xcos_debug_check_cond();" ,... + "visible" , "on"); + y=y-label_h1-margin_y; + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("Current simulation state"),... + "units" , "pixels",... + "position" , [x y frame_w-2*margin_x label_h1],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 16,... + "fontweight" , "bold", ... + "horizontalalignment" , "center", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + y=y-label_h-margin_y; + X=uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , "",... + "units" , "pixels",... + "position" , [x y label_w1 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "horizontalalignment" , "left", ... + "fontweight" , "bold", .. + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + + B=uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , "0",... + "units" , "pixels",... + "position" , [x+label_w1+margin_x y label_w2 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "horizontalalignment" , "right", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + y=y-label_h-margin_y; + //%%%%%% + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("scs_m block path:"),... + "units" , "pixels",... + "position" , [x y label_w1 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + + P=uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , "0",... + "units" , "pixels",... + "position" , [x+label_w1+margin_x y label_w2 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "horizontalalignment" , "right", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + y=y-label_h-margin_y; + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("Time:"),... + "units" , "pixels",... + "position" , [x y label_w1 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + + T=uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , "0",... + "units" , "pixels",... + "position" , [x+label_w1+margin_x y label_w2 label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "horizontalalignment" , "right", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + + y=y-label_h-margin_y; + uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , _("Flag:"),... + "units" , "pixels",... + "position" , [x y 4*label_h label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "left", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + + F=uicontrol( ... + "parent" , gui,... + "style" , "text",... + "string" , "6",... + "units" , "pixels",... + "position" , [x+4*label_h+margin_x y label_w2+label_w1+-4*label_h label_h],.... + "fontname" , defaultfont,... + "fontunits" , "points",... + "fontsize" , 12,... + "horizontalalignment" , "right", ... + "background" , [1 1 1]*0.8, ... + "visible" , "on"); + + //buttons + y=y-button_h-margin_y; + xb=x; + Next = uicontrol( ... + "parent" , gui,... + "relief" , "groove",... + "style" , "pushbutton",... + "string" , _("Next"),... + "units" , "pixels",... + "position" , [xb,y,button_w button_h],.. + "fontname" , "arial",... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "center", ... + "visible" , "on", ... + "callback" , "global ret;ret=1;"); + xb=xb+margin_x+button_w + Stop = uicontrol( ... + "parent" , gui,... + "relief" , "groove",... + "style" , "pushbutton",... + "string" , _("End debug"),... + "units" , "pixels",... + "position" , [xb,y,button_w button_h],.. + "fontname" , "arial",... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "center", ... + "visible" , "on", ... + "callback" , "global ret;ret=2;"); + xb=xb+margin_x+button_w + Pause = uicontrol( ... + "parent" , gui,... + "relief" , "groove",... + "style" , "pushbutton",... + "string" , _("Pause"),... + "units" , "pixels",... + "position" , [xb,y,button_w button_h],.. + "fontname" , "arial",... + "fontunits" , "points",... + "fontsize" , 12,... + "fontweight" , "bold", ... + "horizontalalignment" , "center", ... + "visible" , "on", ... + "callback" , "global ret;ret=3;"); + + set(gui,"user_data",struct("handles",[T,B,F,X,P],"previous",[],... + "Flags",Flags,"When",When,"Time",Time,... + "Condition",Condition,... + "Buttons",[Next,Stop,Pause])) + fig.visible = "on"; +endfunction +function ok=xcos_debug_check_time(Time) + if argn(2)<1 then Time=gcbo;end + ud=Time.parent.user_data + ok=execstr("t="+Time.string,"errcatch")==0 + if ~ok|type(t)<>1|size(t,"*")<>1 then + Time.ForegroundColor=[1 0 0]; + ud.Buttons.enable="off" + ok=%f + else + Time.ForegroundColor=[0 0 0]; + ud.Buttons.enable="on" + ok=%t + end +endfunction +function ok=xcos_debug_check_cond(Condition) + if argn(2)<1 then Condition=gcbo;end + ud=Condition.parent.user_data + Cond=stripblanks(Condition.string) + if Cond=="" then + ok=%t + c=%f + else + ok=execstr("c="+Cond,"errcatch")==0 + end + if ~ok|and(type(c)<>[1 4])|size(c,"*")<>1 then + Condition.ForegroundColor=[1 0 0]; + ud.Buttons.enable="off" + ok=%f + else + Condition.ForegroundColor=[0 0 0]; + ud.Buttons.enable="on" + ok=%t + end + +endfunction +function xcos_debug_gui_close() + messagebox(msprintf(_("Use the ""%s"" button to end the debugging"),_("End debug")),"modal") +endfunction diff --git a/modules/xcos/macros/xcos_run.bin b/modules/xcos/macros/xcos_run.bin Binary files differnew file mode 100755 index 000000000..61b7e3948 --- /dev/null +++ b/modules/xcos/macros/xcos_run.bin diff --git a/modules/xcos/macros/xcos_run.sci b/modules/xcos/macros/xcos_run.sci new file mode 100755 index 000000000..747f95661 --- /dev/null +++ b/modules/xcos/macros/xcos_run.sci @@ -0,0 +1,36 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET +// +// 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 +// +// + +// +// Simulate the diagram +// @xcosFile String Path to the diagram +// +function xcos_run(xcosFile) + + // Check that the Scicos libs are loaded + if isdef("haltscicos") <> %t then + error(msprintf(gettext("The Scicos libraries are not loaded"))); + return; + end + + // import the file + result = importXcosDiagram(xcosFile); + if result <> %t then + error(msprintf(gettext("Unable to simulate %s"), xcosFile)); + return; + end + + // simulate + scicos_simulate(scs_m, list(), []); + +endfunction + diff --git a/modules/xcos/macros/xcos_simulate.bin b/modules/xcos/macros/xcos_simulate.bin Binary files differnew file mode 100755 index 000000000..b25960e12 --- /dev/null +++ b/modules/xcos/macros/xcos_simulate.bin diff --git a/modules/xcos/macros/xcos_simulate.sci b/modules/xcos/macros/xcos_simulate.sci new file mode 100755 index 000000000..d29b1940f --- /dev/null +++ b/modules/xcos/macros/xcos_simulate.sci @@ -0,0 +1,456 @@ +// +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) Scilab Enterprises - 2013 - Bruno JOFRET +// Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET +// +// 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 +// +// + +function %cpr = xcos_simulate(scs_m, needcompile) + + // Load the block libs if not defined + prot = funcprot(); + funcprot(0); + if ~exists("scicos_diagram") then + loadXcosLibs(); + end + funcprot(prot); + [modelica_libs, scicos_pal_libs, %scicos_with_grid, %scs_wgrid] = initial_scicos_tables(); + + // Hook according to SEP066 + function [ok]=invoke_pre_simulate(fun, scs_m, needcompile) + ok=%f; + ierr=execstr("[continueSimulation]="+fun+"(scs_m, needcompile);", "errcatch"); + if ierr<>0 then + disp(_("Error occurred in pre_xcos_simulate: Cancelling simulation.")); + [str,n,line,func]=lasterror(); + mprintf(" at line %d of function %s\n", line, func); + return + end + if ~continueSimulation then + return + end + ok=%t; + + // Force update on the parent in case of scoped modification + scs_m=resume(scs_m); + endfunction + + if isdef("pre_xcos_simulate") then + if type(pre_xcos_simulate) == 15 then + // If has a multiple implementation (on a list) + for f=pre_xcos_simulate; + ok=invoke_pre_simulate(f, scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end + else + // If has a unique implementation + ok=invoke_pre_simulate("pre_xcos_simulate", scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end + end + + //**---- prepare from and to workspace stuff ( "From workspace" block ) + scicos_workspace_init() + + + //** extract tolerances from scs_m.props.tol + tolerances = scs_m.props.tol ; + //** extract solver type from tolerances + solver = tolerances(6) ; + //** initialize a "scicos_debug_gr" variable + %scicos_debug_gr = %f; + + //////////////////////////////////////////////////////////////// + // Add global environment variable so that scicos is not lost // + //////////////////////////////////////////////////////////////// + if needcompile == 4 then + %state0 = list(); + needcompile = 4; + curwin = 1000; + %cpr = struct(); + %tcur = 0; + %cpr.state = %state0; + alreadyran = %f; + else + %state0 = %cpr.state; + alreadyran = %f; + end + + tf = scs_m.props.tf; + %zoom = 1.4; + Select = []; + + //** extract tolerances from scs_m.props.tol + tolerances = scs_m.props.tol ; + //** extract solver type from tolerances + solver = tolerances(6) ; + + // Propagate context through all blocks + %scicos_context = struct(); + context = scs_m.props.context; + //** context eval here + [%scicos_context, ierr] = script2var(context, %scicos_context); + + // For backward compatibility for scifunc + if ierr==0 then + %mm = getfield(1,%scicos_context) + for %mi=%mm(3:$) + ierr = execstr(%mi+"=%scicos_context(%mi)","errcatch") + if ierr<>0 then + break; //** in case of error exit + end + end + end + // End of for backward compatibility for scifuncpagate context values + + [scs_m,%cpr,needcompile,ok] = do_eval(scs_m, %cpr, %scicos_context); + if ~ok then + msg = msprintf(gettext("%s: Error during block parameters evaluation.\n"), "Xcos"); + messagebox(msg, "Xcos", "error"); + error(msprintf(gettext("%s: Error during block parameters evaluation.\n"), "xcos_simulate")); + end + + //** update parameters or compilation results + [%cpr,%state0_n,needcompile,alreadyran,ok] = do_update(%cpr,%state0,needcompile) + if ~ok then + error(msprintf(gettext("%s: Error during block parameters update.\n"), "xcos_simulate")); + end + + //** if alreadyran then set the var choice + if alreadyran then + choix = ["Continue";"Restart";"End"] + else + choix = [] ; + end + + issequal = %t; + //** initial state has been changed + if ~isequal(%state0_n,%state0) then + issequal = %f + else + //** test typeof outtb element + for i=1:lstsize(%state0_n.outtb) + if typeof(%state0_n.outtb(i))<>typeof(%state0.outtb(i)) + issequal = %f + break + end + end + //** test typeof oz element + for i=1:lstsize(%state0_n.oz) + if typeof(%state0_n.oz(i))<>typeof(%state0.oz(i)) + issequal = %f + break + end + end + end + + //** if state have changed + //** finish the simulation via do_terminate() + if ~issequal then + %state0 = %state0_n + [alreadyran,%cpr] = do_terminate() + choix = [] + end + + //** switch to appropriate solver + if %cpr.sim.xptr($)-1<size(%cpr.state.x,"*") & solver<100 then + warning(["Diagram has been compiled for implicit solver" + "switching to implicit Solver"]) + solver = 100 ; //** Magic number + tolerances(6) = solver ; //** save Magic number solver type + elseif (%cpr.sim.xptr($)-1==size(%cpr.state.x,"*")) & ((or (solver == [100 101 102])) & size(%cpr.state.x,"*")<>0) then + message(["Diagram has been compiled for explicit solver" + "switching to explicit Solver"]) + solver = 0 ; //** Magic number + tolerances(6) = solver ; //** save Magic number solver type + end + + //** ask user what to do + if choix<>[] then + //** open dialog box + to_do = choose(choix,"What do you want to do") + + //** if cancel then exit + if to_do==0 then + ok = %f + return + end + + select choix(to_do) + + case "Continue" then + needstart = %f ; + state = %cpr.state ; + + case "Restart" then + needstart = %t ; + state = %state0 ; + + case "End" then + state = %cpr.state ; + needstart = %t ; + tf = scs_m.props.tf; + + //Alan: Cannot call do_terminate() here ? + //Answer: No, because do_terminate() doesn't + // return control to the user + + //** run scicosim via 'finish' flag + ierr = execstr("[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,"+.. + "''finish'',tolerances)","errcatch") + + %cpr.state = state + alreadyran = %f + + //** error case + if ierr<>0 then + str_err = split_lasterror(lasterror()); + kfun = curblock() + corinv = %cpr.corinv + + if kfun<>0 & length(corinv) > kfun then //** block error + path = corinv(kfun) + //** get error cmd for the block + get_errorcmd(path,"End problem.",str_err); + + else //** simulator error + message(["End problem:";str_err]) + //scf(curwin); + end + ok = %f + end + + return + end + + else //** Normal first start simulation + + needstart = %t + state = %state0 + + end + + //gh_win = gcf(); + + //** scicos initialization + if needstart then + //** if the simulation has already run + //** and is not finished then call do_terminate + if alreadyran then + [alreadyran,%cpr] = do_terminate() + alreadyran = %f ; + end + //** set initial values for a new simulation + %tcur = 0 + %cpr.state = %state0 + tf = scs_m.props.tf; + if tf*tolerances==[] then + message(["Simulation parameters not set";"use setup button"]); + return; + end + + //** Run the normal first start simulation here + + //** run scicosim via 'start' flag + ierr = execstr("[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,"+.. + "''start'',tolerances)","errcatch") + + %cpr.state = state ; //** save the state + //** error case + if ierr<>0 then + str_err=split_lasterror(lasterror()); + kfun=curblock() + corinv=%cpr.corinv + + if kfun<>0 & length(corinv) > kfun then //** block error + path=corinv(kfun) + //** get error cmd for the block + disp(str_err); + get_errorcmd(path,gettext("Initialisation problem"),str_err); + + + else //** simulator error + message(["Initialisation problem:";str_err]) + //scf(curwin); + end + + ok = %f; + //xset('window',curwin) + return + end + //scf(gh_win); + //xset('window',win); + end + + //** scicos simulation + tf = scs_m.props.tf + + // Inform Xcos the simulator is going to run + xcosSimulationStarted(); + + //** run scicosim via 'start' flag + ierr = execstr("[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,"+.. + "''run'',tolerances)","errcatch") + + %cpr.state = state + + //** no error + if ierr==0 then + alreadyran = %t; + //** we are at the end of the simulation + //** finish the simulation via do_terminate() + if tf-t<tolerances(3) then + //disp('fin'); + //Alan : removing do_terminate() here because it + // doesn't return control + //[alreadyran,%cpr]=do_terminate() + needstart = %t; + alreadyran = %f; + //** run scicosim via 'finish' flag + ierr = execstr("[state,t]=scicosim(%cpr.state,tf,tf,%cpr.sim,"+.. + "''finish'',tolerances)","errcatch") + + %cpr.state = state; + + //** error case + if ierr<>0 then + str_err = split_lasterror(lasterror()); + kfun = curblock() + corinv = %cpr.corinv + + if kfun<>0 & length(corinv) > kfun then //** block error + path = corinv(kfun) + //** get error cmd for the block + get_errorcmd(path,gettext("End problem"),str_err); + else //** simulator error + message(["End problem:";str_err]) + //scf(curwin); + end + end + else + %tcur = t; + end + //** error case + else + str_err = split_lasterror(lasterror()); + + alreadyran = %f; + kfun = curblock(); + corinv = %cpr.corinv; + if kfun<>0 & length(corinv) > kfun then //** block error + path = corinv(kfun); + //** get error cmd for the block + execstr(get_errorcmd(path,gettext("Simulation problem"),str_err)); + else //** simulator error + message(["Simulation problem:";str_err]) + //scf(curwin); + end + ok = %f; + end + + // Restore saved variables in Scilab environment ("To workspace" block) + + // First step: Search the %cpr tree for TOWS_c blocks, and extract the variable names. + path = %cpr.sim; + Names = []; + buff_sizes = []; + increment = 1; + for i=1:size(path.funs) + increment2 = path.ipptr(i+1); + if (increment2 - increment <> 0) then // ipar has at least a few elements + space = increment2 - increment; // The number of elements that the current block pushed into ipar + if (path.funs(i) == "tows_c") then // Found a Tow_workspace block + varNameLen = space - 2; // Space minus 'buffer_size' and 'var length' ipar elements + varNameCode = path.ipar(increment+2:increment+2+varNameLen-1); // varName is stored in Scilab code + varName = ascii(varNameCode); + Names = [Names varName]; // Append varName in Names + buff_size = path.ipar(increment); // Retrieve buffer size + buff_sizes = [buff_sizes buff_size]; + end + increment = increment2; + end + end + // At the end, Names contains the string names of the variables and buff_sizes the respective buffer sizes + + // Second step: Link the variable names to their values vectors, + //and call '[names(1), names(2), ...] = resume(names(1), names(2), ...)' to save the variable into Scilab + if ~isempty(Names) then + for i=1:size(Names, "c") + execstr("NamesIval = "+Names(i)+"_val;"); + execstr("NamesIvalt = "+Names(i)+"_valt;"); + // If input is a matrix, use function matrix() to reshape the saved values + // Check condition using time vector, if we have more values than time stamps, split it + if (size(NamesIval, "r") > size(NamesIvalt, "r")) then + nRows = size(NamesIvalt, "r"); + nCols = size(NamesIval, "c"); + nCols2 = size(NamesIval, "r") / nRows; + NamesIval = matrix(NamesIval, nCols, nCols2, nRows); + end + ierr = execstr(Names(i)+" = struct(''values'', NamesIval, ''time'', NamesIvalt)", "errcatch"); + if ierr <> 0 then + str_err = split_lasterror(lasterror()); + message(["Simulation problem" ; "Unable to resume To Workspace Variable {"+Names(i)+"}:" ; str_err]); + break; + end + if i == 1 then + Resume_line_args = Names(1); + else + Resume_line_args = Resume_line_args + ", " + Names(i); // Concatenate the variable names up to the last one + end + end + Resume_line = "[" + Resume_line_args + "] = resume(" + Resume_line_args + ");"; // Build the message + // Will execute Resume_line at the end of the function, to run the following Hook instructions + end + + // Hook according to SEP066 + function ok=invoke_post_simulate(fun, %cpr, scs_m, needcompile) + ok=%f; + ierr=execstr(fun+"(%cpr, scs_m, needcompile);", "errcatch"); + if ierr<>0 then + disp(_("Error in post_xcos_simulate: ending simulation.")) + return + end + ok=%t + // Force update on the parent in case of scoped modification + scs_m=resume(scs_m); + endfunction + + if isdef("post_xcos_simulate") then + if type(post_xcos_simulate) == 15 then + // If has a multiple implementation (on a list) + for f=post_xcos_simulate; + ok=invoke_post_simulate(f, %cpr, scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end + else + // If has a unique implementation + ok=invoke_post_simulate("post_xcos_simulate", %cpr, scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end + end + + // Executing the resume() function at the end, because it does not return control + if ~isempty(Names) then + execstr(Resume_line); + end + +endfunction + |