1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2012 - DIGITEO - Antoine ELIAS
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution. The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10 //2012/08/06 transform macros to string to save it
12 //called by save function, transform handle in tlist and save result
13 function [] = %_save(%__filename__, varargin)
15 function result = isList(var)
20 if or(type(var) == [15, 16, 17]) then
27 function result = isMacro(var)
29 if or(type(var) == [11]) then
36 function result = isCompiledMacro(var)
38 if or(type(var) == [13]) then
45 function result = inspectList(l)
46 if typeof(l)=="list" then
48 for %__i__ = definedfields(l)
49 if typeof(l(%__i__)) == "handle" then
50 if ~is_handle_valid(l(%__i__)) then
51 oldMode = warning("query");
53 warning(msprintf(gettext("%s: handle no more valid ignored.\n"), "save"));
57 result(%__i__) = extractMatrixHandle(l(%__i__));
59 elseif isMacro(l(%__i__)) | isCompiledMacro(l(%__i__)) then
60 //build an arbitrary name to the macro
61 result(%__i__) = extractMacro(l(%__i__), "function");
62 elseif isList(l(%__i__)) then
63 result(%__i__) = inspectList(l(%__i__));
64 elseif type(l(%__i__)) == 14 then //library, must not be save
67 result(%__i__) = l(%__i__);
71 fieldNb = size(getfield(1, l), "*");
72 for kField = 2:fieldNb // Do not inspect first field (field names)
73 fieldValue = getfield(kField, l);
74 if typeof(fieldValue) == "handle" then
75 fieldValue = extractMatrixHandle(fieldValue);
76 elseif isMacro(fieldValue) | isCompiledMacro(fieldValue) then
77 //build an arbitrary name to the macro
78 fieldValue = extractMacro(fieldValue, "function");
79 elseif isList(fieldValue) then
80 fieldValue = inspectList(fieldValue);
82 setfield(kField, fieldValue, l);
88 function matrixHandle = extractMatrixHandle(h)
90 if typeof(h) <> "handle" then
95 matrixHandle = tlist(["ScilabMatrixHandle", "dims", "values"]);
96 matrixHandle.dims = size(h);
97 matrixHandle.values = list();
98 for %__i__ = 1:size(h, "*")
99 matrixHandle.values($+1) = extractSingleHandle(h(%__i__));
100 if or(fieldnames(matrixHandle.values($))=="user_data") then
101 if isList(matrixHandle.values($).user_data) then
102 matrixHandle.values($).user_data = inspectList(matrixHandle.values($).user_data)
103 elseif typeof(matrixHandle.values($).user_data) == "handle" then
104 matrixHandle.values($).user_data = extractMatrixHandle(matrixHandle.values($).user_data)
111 function item = extractSingleHandle(h)
115 item = extractFigure(h);
117 item = extractAxes(h);
119 item = extractPolyline(h);
121 item = extractPlot3d(h);
123 item = extractFac3d(h);
125 item = extractCompound(h);
127 item = extractRectangle(h);
129 item = extractArc(h);
131 item = extractChamp(h);
133 item = extractSegs(h);
135 item = extractGrayplot(h);
137 item = extractMatplot(h);
139 item = extractFec(h);
141 item = extractLegend(h);
143 item = extractText(h);
145 item = extractAxis(h);
147 item = extractuimenu(h);
149 item = extractuicontextmenu(h);
151 item = extractuicontrol(h);
153 item = extractLight(h);
155 error("handle of type " + h.type + " unhandled");
163 function returnedFigure = extractFigure(h)
164 returnedFigure = tlist([
165 "ScilabSingleHandle", ...
168 "figure_position", ...
179 "pixel_drawing_mode", ...
181 "immediate_drawing", ...
183 "rotation_style", ...
185 "event_handler_enable", ...
187 "closerequestfcn", ...
191 fields = fieldnames(returnedFigure);
193 for %__i__ = 1:size(fields, "*")
194 if fields(%__i__) == "children" then
195 returnedFigure(fields(%__i__)) = extractMatrixHandle(h(fields(%__i__)));
197 returnedFigure(fields(%__i__)) = h(fields(%__i__));
205 function returnedLabel = extractLabel(h)
206 returnedLabel = tlist([
207 "ScilabSingleHandle", ...
211 "font_foreground", ...
217 "fractional_font", ...
223 fields = fieldnames(returnedLabel);
225 for %__i__ = 1:size(fields, "*")
226 returnedLabel(fields(%__i__)) = h(fields(%__i__));
233 function returnedTicks = extractTicks(ticks)
234 returnedTicks = tlist([
239 fields = fieldnames(returnedTicks);
241 for %__i__ = 1:size(fields, "*")
242 returnedTicks(fields(%__i__)) = ticks(fields(%__i__));
249 function returnedAxes = extractAxes(h)
250 returnedAxes = tlist([
251 "ScilabSingleHandle", ...
275 "fractional_font", ...
278 "rotation_angles", ...
287 "hidden_axis_color", ...
288 "arc_drawing_method", ...
296 "mark_size_unit", ...
297 "mark_foreground", ...
298 "mark_background", ...
306 fields = fieldnames(returnedAxes);
308 for %__i__ = 1:size(fields, "*")
309 if or(fields(%__i__) == ["title","x_label","y_label","z_label"]) then
310 returnedAxes(fields(%__i__)) = extractLabel(h(fields(%__i__)));
311 elseif or(fields(%__i__) == ["x_ticks", "y_ticks", "z_ticks"]) then
312 returnedAxes(fields(%__i__)) = extractTicks(h(fields(%__i__)));
313 elseif fields(%__i__) == "children" then
314 returnedAxes(fields(%__i__)) = extractMatrixHandle(h(fields(%__i__)));
316 returnedAxes(fields(%__i__)) = h(fields(%__i__));
324 function returnedPolyline = extractPolyline(h)
325 returnedPolyline = tlist([
326 "ScilabSingleHandle", ...
335 "arrow_size_factor", ...
336 "polyline_style", ...
337 "interp_color_vector", ...
338 "interp_color_mode", ...
342 "mark_size_unit", ...
345 "mark_foreground", ...
346 "mark_background", ...
355 fields = fieldnames(returnedPolyline);
357 for %__i__ = 1:size(fields, "*")
358 returnedPolyline(fields(%__i__)) = h(fields(%__i__));
365 function returnedPlot3d = extractPlot3d(h)
366 returnedPlot3d = extractSurface(h);
372 function returnedFac3d = extractFac3d(h)
373 returnedFac3d = extractSurface(h);
379 function returnedSurface = extractSurface(h)
380 returnedSurface = tlist([
381 "ScilabSingleHandle", ...
390 "mark_size_unit", ...
391 "mark_foreground", ...
392 "mark_background", ...
397 "use_color_material", ...
404 fields = fieldnames(returnedSurface);
406 for %__i__ = 1:size(fields, "*")
407 if fields(%__i__)=="cdata_mapping" then
408 if h.type=="Fac3d" then
409 returnedSurface(fields(%__i__)) = h(fields(%__i__));
412 returnedSurface(fields(%__i__)) = h(fields(%__i__));
420 function returnedCompound = extractCompound(h)
421 returnedCompound = tlist([
422 "ScilabSingleHandle", ...
428 fields = fieldnames(returnedCompound);
430 for %__i__ = 1:size(fields, "*")
431 if fields(%__i__) == "children" then
432 returnedCompound(fields(%__i__)) = extractMatrixHandle(h(fields(%__i__)));
434 returnedCompound(fields(%__i__)) = h(fields(%__i__));
442 function returnedRectangle = extractRectangle(h)
443 returnedRectangle = tlist([
444 "ScilabSingleHandle", ...
451 "mark_size_unit", ...
452 "mark_foreground", ...
453 "mark_background", ...
464 fields = fieldnames(returnedRectangle);
466 for %__i__ = 1:size(fields, "*")
467 returnedRectangle(fields(%__i__)) = h(fields(%__i__));
474 function returnedArc = extractArc(h)
475 returnedArc = tlist([
476 "ScilabSingleHandle", ...
486 "arc_drawing_method", ...
491 fields = fieldnames(returnedArc);
493 for %__i__ = 1:size(fields, "*")
494 returnedArc(fields(%__i__)) = h(fields(%__i__));
501 function returnedChamp = extractChamp(h)
502 returnedChamp = tlist([
503 "ScilabSingleHandle", ...
515 fields = fieldnames(returnedChamp);
517 for %__i__ = 1:size(fields, "*")
518 returnedChamp(fields(%__i__)) = h(fields(%__i__));
525 function returnedSeg = extractSegs(h)
526 returnedSeg = tlist([
527 "ScilabSingleHandle", ...
538 "mark_size_unit", ...
539 "mark_foreground", ...
540 "mark_background", ...
545 fields = fieldnames(returnedSeg);
547 for %__i__ = 1:size(fields, "*")
548 returnedSeg(fields(%__i__)) = h(fields(%__i__));
555 function returnedGrayplot = extractGrayplot(h)
556 returnedGrayplot = tlist([
557 "ScilabSingleHandle", ...
566 fields = fieldnames(returnedGrayplot);
568 for %__i__ = 1:size(fields, "*")
569 returnedGrayplot(fields(%__i__)) = h(fields(%__i__));
576 function returnedMatplot = extractMatplot(h)
577 returnedMatplot = tlist([
578 "ScilabSingleHandle", ...
587 fields = fieldnames(returnedMatplot);
589 for %__i__ = 1:size(fields, "*")
590 returnedMatplot(fields(%__i__)) = h(fields(%__i__));
597 function returnedFec = extractFec(h)
598 returnedFec = tlist([
599 "ScilabSingleHandle", ...
606 "outside_colors", ...
613 fields = fieldnames(returnedFec);
615 for %__i__ = 1:size(fields, "*")
616 returnedFec(fields(%__i__)) = h(fields(%__i__));
623 function returnedLegend = extractLegend(h)
624 returnedLegend = tlist([
625 "ScilabSingleHandle", ...
632 "fractional_font", ...
634 "legend_location", ...
646 fields = fieldnames(returnedLegend);
648 for %__i__ = 1:size(fields, "*")
649 if fields(%__i__) == "links" then
650 returnedLegend(fields(%__i__)) = extractMatrixHandle(h(fields(%__i__)));
651 elseif fields(%__i__) == "paths" then
653 for kl=1:size(h.links,"*");
654 p($+1) = get_entity_path(h.links(kl));
656 returnedLegend(fields(%__i__)) = p;
658 returnedLegend(fields(%__i__)) = h(fields(%__i__));
666 function returnedText = extractText(h)
667 returnedText = tlist([
668 "ScilabSingleHandle", ...
682 "font_foreground", ...
685 "fractional_font", ...
690 fields = fieldnames(returnedText);
692 for %__i__ = 1:size(fields, "*")
693 returnedText(fields(%__i__)) = h(fields(%__i__));
700 function returnedAxis = extractAxis(h)
701 returnedAxis = tlist([
702 "ScilabSingleHandle", ...
705 "tics_direction", ...
713 "labels_font_size", ...
714 "labels_font_color", ...
715 "fractional_font", ...
720 fields = fieldnames(returnedAxis);
722 for %__i__ = 1:size(fields, "*")
723 returnedAxis(fields(%__i__)) = h(fields(%__i__));
730 function returneduimenu = extractuimenu(h)
731 returneduimenu = tlist([
732 "ScilabSingleHandle", ...
735 "foregroundcolor", ...
745 fields = fieldnames(returneduimenu);
747 for %__i__ = 1:size(fields, "*")
748 if fields(%__i__) == "children" then
749 returneduimenu(fields(%__i__)) = extractMatrixHandle(h(fields(%__i__)));
751 returneduimenu(fields(%__i__)) = h(fields(%__i__));
759 function returneduicontextmenu = extractuicontextmenu(h)
760 returneduicontextmenu = tlist([
761 "ScilabSingleHandle", ...
765 fields = fieldnames(returneduicontextmenu);
767 for %__i__ = 1:size(fields, "*")
768 if fields(%__i__) == "children" then
769 returneduicontextmenu(fields(%__i__)) = extractMatrixHandle(h(fields(%__i__)));
771 returneduicontextmenu(fields(%__i__)) = h(fields(%__i__));
779 function returneduicontrol = extractuicontrol(h)
780 returneduicontrol = tlist([
781 "ScilabSingleHandle", ...
784 "backgroundcolor", ...
791 "foregroundcolor", ...
792 "horizontalalignment", ...
803 "verticalalignment", ...
811 fields = fieldnames(returneduicontrol);
813 for %__i__ = 1:size(fields, "*")
814 if fields(%__i__) == "children" then
815 returneduicontrol(fields(%__i__)) = extractMatrixHandle(h(fields(%__i__)));
817 returneduicontrol(fields(%__i__)) = h(fields(%__i__));
825 function returnedLight = extractLight(h)
826 returnedLight = tlist([
827 "ScilabSingleHandle", ...
837 fields = fieldnames(returnedLight);
839 for %__i__ = 1:size(fields, "*")
840 returnedLight(fields(%__i__)) = h(fields(%__i__));
844 // Utility function for legends, copy/paste from %h_load
845 function p=get_entity_path(e)
846 // given a handle e on an entity this function returns its path relative
847 // to its parent axes.
848 // the path is a vector of child index.
853 pos=find(parent.children==e,1)
855 error(msprintf(_("%s : Invalid entity %s\n"),"save","Legend"))
858 if parent.type=="Axes" then
866 function macro = extractMacro(macroPtr, macroName)
867 macroSt = fun2string(macroPtr, macroName);
868 oldMode = warning("query");
870 macro = tlist("ScilabMacro", isCompiledMacro(macroPtr), macroSt);
877 if size(varargin) == 0 then
884 "extractMatrixHandle"
885 "extractSingleHandle"
906 "extractuicontextmenu"
915 //get all user variables
916 %__varList__ = who_user(%f);
917 //remove exclude variables/functions
918 %__grepResult__ = grep(%__varList__, %__excludeList__);
919 %__varList__(%__grepResult__) = [];
920 for %__i__ = 1:size(%__varList__, "*")
921 //store them as input arguments
922 varargin(%__i__) = %__varList__(%__i__);
926 oldMode = warning("query");
929 if size(varargin) == 0 then
932 for %__i__ = size(varargin):-1:1
934 if varargin(%__i__) == "-append" then
938 temp = evstr(varargin(%__i__));
942 value = inspectList(temp);
944 execstr(varargin(%__i__) + " = value");
945 elseif typeof(temp) == "handle" then
946 if ~is_handle_valid(temp) then // Invalid handle ignored
948 warning(msprintf(gettext("%s: handle no more valid ignored.\n"), "save"));
950 varargin(%__i__) = null();
951 else //convert handle to tlist
952 value = extractMatrixHandle(temp);
954 execstr(varargin(%__i__) + " = value");
956 elseif isMacro(temp) | isCompiledMacro(temp) then
957 //convert macro to tlist
958 value = extractMacro(temp, varargin(%__i__));
960 execstr(varargin(%__i__) + " = value");
961 elseif type(temp) == 14 then //library, must not be save
962 varargin(%__i__) = null();
967 //if size(varargin) == 0, create an empty file
968 result = export_to_hdf5(%__filename__, varargin(:));