1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2012 - DIGITEO - Antoine ELIAS
3 // Copyright (C) 2012 - DIGITEO - Vincent COUVERT
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11 function %_sodload(%__filename__, varargin)
13 function v = getScilabFileVersion(%__filename__)
14 verStr = h5readattr(%__filename__, "/", "SCILAB_scilab_version")
15 [a,b,c,d] = regexp(verStr, "/scilab-.*(\d)\.(\d)\.(\d)/");
16 if size(d, "*") == 3 then
17 v = evstr(d(1)) * 100 + evstr(d(2)) * 10 + evstr(d(3));
19 error("unable to find file version: %s", %__filename__);
23 function [varValues] = %__convertVariable__(varValues, varNames)
24 for i = 1:size(varValues)
25 if typeof(varValues(i)) == "ScilabMatrixHandle" then
26 //convert tlist to handle
27 varValues(i) = createMatrixHandle(varValues(i));
28 elseif typeof(varValues(i)) == "ScilabMacro" then
29 //convert tlist to macro
30 varValues(i) = createMacro(varValues(i), varNames(i));
31 elseif isList(varValues(i)) then
33 varValues(i) = parseList(varValues(i));
38 function result = isList(var)
42 if or(type(var) == [15, 16, 17]) then
49 function varValue = parseList(varValue)
50 if typeof(varValue)=="list" then
51 for i = definedfields(varValue)
52 if typeof(varValue(i)) == "ScilabMatrixHandle" then
53 varValue(i) = createMatrixHandle(varValue(i));
54 elseif typeof(varValue(i)) == "ScilabMacro" then
55 //convert tlist to macro
56 varValue(i) = createMacro(varValue(i), "function");
57 elseif isList(varValue(i)) then
58 varValue(i) = parseList(varValue(i));
60 varValue(i) = varValue(i);
64 fieldNb = size(getfield(1, varValue), "*");
65 for kField = 2:fieldNb // Do not inspect first field (field names)
66 fieldValue = getfield(kField, varValue);
67 if typeof(fieldValue) == "ScilabMatrixHandle" then
68 fieldValue = createMatrixHandle(fieldValue);
69 elseif typeof(fieldValue) == "ScilabMacro" then
70 //convert tlist to macro
71 fieldValue = createMacro(fieldValue, "function");
72 elseif isList(fieldValue) then
73 fieldValue = parseList(fieldValue);
75 setfield(kField, fieldValue, varValue);
80 function h = createMatrixHandle(matrixHandle)
82 if typeof(matrixHandle) <> "ScilabMatrixHandle" then
86 for i = prod(matrixHandle.dims):-1:1
88 newItem = createSingleHandle(matrixHandle.values(i));
94 if or(fieldnames(matrixHandle.values(i))=="user_data") then // TODO Remove after graphic branch merge
95 if isList(matrixHandle.values(i).user_data) then
96 set(h($), "user_data", parseList(matrixHandle.values(i).user_data));
97 elseif typeof(matrixHandle.values(i).user_data) == "ScilabMatrixHandle" then
98 set(h($), "user_data", createMatrixHandle(matrixHandle.values(i).user_data));
104 function h = createSingleHandle(item)
107 h = createFigure(item);
109 h = createAxes(item);
111 h = createPolyline(item);
113 h = createPlot3d(item);
115 h = createFac3d(item);
117 h = createCompound(item);
119 h = createRectangle(item);
123 h = createChamp(item);
125 h = createSegs(item);
127 h = createGrayplot(item);
129 h = createMatplot(item);
133 h = createLegend(item);
135 h = createText(item);
137 h = createAxis(item);
139 h = createuimenu(item);
141 h = createuicontextmenu(item);
143 h = createuicontrol(item);
145 h = createDatatip(item);
147 h = createLight(item);
149 error("handle of type " + item.type + " unhandled");
157 function h = createFigure(figureProperties)
158 fields = fieldnames(figureProperties);
162 isVisible = h.visible;
167 fields(fields=="figure_id") = [];
169 h.figure_position=figureProperties.figure_position;
170 fields(fields=="figure_position") = [];
171 // set auto_resize first otherwise viewport modification may not have any effect.
172 h.auto_resize = figureProperties.auto_resize;
173 fields(fields=="auto_resize") = [];
174 h.figure_size = figureProperties.figure_size;
175 fields(fields=="figure_size") = [];
176 // set axes_size last because it's more important than figure_size
177 h.axes_size = figureProperties.axes_size;
178 fields(fields=="axes_size") = [];
180 for i = 1:size(fields, "*")
181 if fields(i) == "children" then
182 c = figureProperties(fields(i));
184 createSingleHandle(c.values(s));
186 xsetech(wrect=[0 0 .1 .1])
187 createSingleHandle(c.values(i));
189 elseif fields(i) == "event_handler" then
190 event_handler = figureProperties(fields(i));
191 elseif fields(i) == "resizefcn" then
192 resizefcn = figureProperties(fields(i));
193 elseif fields(i) == "visible" then
194 isVisible = figureProperties(fields(i));// do not set visible = "true" before the end of load.
196 set(h, fields(i), figureProperties(fields(i)));
200 h.visible = isVisible;
201 h.resizefcn = resizefcn;
202 h.event_handler = event_handler;
208 function h = createLabel(labelProperties, h)
209 fields = fieldnames(labelProperties);
211 for i = 1:size(fields, "*")
212 set(h, fields(i), labelProperties(fields(i)));
219 function h = createTicks(ticksProperties)
220 h = tlist(["ticks","locations","labels"], [], []);
221 fields = fieldnames(ticksProperties);
222 for i = 1:size(fields, "*")
223 h(fields(i)) = ticksProperties(fields(i));
230 function h = createAxes(axesProperties)
231 // Hack to determine whether %h_load has been called by the %h_copy macro
232 // in which case a new Axes object is created
234 [lnums, fnames] = where();
235 ind = grep(fnames, "%h_copy");
236 if ~isempty(ind) then
241 fields = fieldnames(axesProperties);
244 // Get log_flags to be sure to set them after data_bounds
245 log_flags = axesProperties.log_flags;
246 fields(fields=="log_flags") = [];
248 // Get mark_mode to be sure to set it after mark_style
249 mark_mode = axesProperties.mark_mode;
250 fields(fields=="mark_mode") = [];
252 // Get auto_ticks to be sure to set it after ticks labels
253 auto_ticks = axesProperties.auto_ticks;
254 fields(fields=="auto_ticks") = [];
256 for i = 1:size(fields, "*")
257 if or(fields(i) == ["title","x_label","y_label","z_label"]) then
258 createLabel(axesProperties(fields(i)), h(fields(i)));
259 elseif or(fields(i) == ["x_ticks", "y_ticks", "z_ticks"]) then
260 set(h, fields(i), createTicks(axesProperties(fields(i))));
261 elseif fields(i) == "children" then
262 createMatrixHandle(axesProperties(fields(i)));
263 elseif fields(i) == "clip_state" then
264 if axesProperties.clip_state=="on" then
265 set(h,"clip_box",axesProperties.clip_box);
267 set(h,"clip_state", axesProperties.clip_state);
268 elseif fields(i) == "clip_box" then
269 // managed with 'clip_state'
270 elseif fields(i) == "data_bounds" then
271 set(h, "data_bounds", axesProperties.data_bounds);
272 set(h, "log_flags", log_flags);
273 elseif fields(i) == "mark_style" then
274 set(h, "mark_style", axesProperties.mark_style);
275 set(h, "mark_mode", mark_mode);
277 set(h, fields(i), axesProperties(fields(i)));;
281 set(h, "auto_ticks", auto_ticks);
285 if ~isempty(%LEG) then
286 // Get handles from paths
287 links=getlinksfrompath(h, %LEG.paths)
288 if ~isempty(links) then
289 L = captions(links, %LEG.text)
290 L.visible = %LEG.visible
291 L.font_style = %LEG.font_style
292 L.font_size = %LEG.font_size
293 L.font_color = %LEG.font_color
294 L.fractional_font = %LEG.fractional_font
296 L.legend_location = %LEG.legend_location
297 L.position = %LEG.position
298 L.line_mode = %LEG.line_mode
299 L.thickness = %LEG.thickness
300 L.foreground = %LEG.foreground
301 L.fill_mode = %LEG.fill_mode
302 L.background = %LEG.background
303 if %LEG.clip_state=="on" then
304 L.clip_box = %LEG.clip_box
306 L.clip_state = %LEG.clip_state
307 L.user_data = %LEG.user_data
309 warning(msprintf(_("%s: Legend does not fit with the current context. Skipped\n"), "load"));
319 function h = createPolyline(polylineProperties)
320 fields = fieldnames(polylineProperties);
323 xpoly(polylineProperties.data(:,1), polylineProperties.data(:,2))
327 if polylineProperties.clip_state=="on" then
328 set(h, "clip_box", polylineProperties.clip_box)
330 set(h, "clip_state", polylineProperties.clip_state);
331 fields(fields=="clip_box") = [];
332 fields(fields=="clip_state") = [];
334 if polylineProperties.interp_color_mode=="on" & ~isempty(polylineProperties.interp_color_vector) then
335 set(h, "interp_color_vector", polylineProperties.interp_color_vector);
336 set(h, "interp_color_mode", polylineProperties.interp_color_mode);
338 if ~isempty(polylineProperties.interp_color_vector) then
339 h.interp_color_vector = polylineProperties.interp_color_vector;
341 h.interp_color_mode = polylineProperties.interp_color_mode;
343 fields(fields=="interp_color_vector") = [];
344 fields(fields=="interp_color_mode") = [];
346 // Get mark_mode to be sure to set it after mark_style
347 mark_mode = polylineProperties.mark_mode;
348 fields(fields=="mark_mode") = [];
353 for i = 1:size(fields, "*")
354 if fields(i) == "mark_style" then
355 set(h, "mark_style", polylineProperties.mark_style);
356 set(h, "mark_mode", mark_mode);
357 elseif fields(i) == "children" then
358 createMatrixHandle(polylineProperties(fields(i)));
360 h(fields(i)) = polylineProperties(fields(i));
364 clearglobal %POLYLINE
371 function h = createPlot3d(plot3dProperties)
372 h = createSurface(plot3dProperties);
378 function h = createFac3d(fac3dProperties)
379 h = createSurface(fac3dProperties);
385 function h = createSurface(surfaceProperties)
386 fields = fieldnames(surfaceProperties);
388 // plot3d modify the axes properties
393 rotation_angles = a.rotation_angles;
394 axes_visible = a.axes_visible;
397 x_label_visible = a.x_label.visible;
398 y_label_visible = a.y_label.visible;
399 z_label_visible = a.z_label.visible;
400 x_label_text = a.x_label.text;
401 y_label_text = a.y_label.text;
402 z_label_text = a.z_label.text;
403 axes_isoview = a.isoview;
405 if (or(surfaceProperties.color_flag==[2 5]) & ~or(fields=="cdata_mapping")) | ..
406 ((surfaceProperties.color_flag>=2) & or(fields=="cdata_mapping")) then
407 plot3d1(surfaceProperties.data.x, surfaceProperties.data.y, list(surfaceProperties.data.z, surfaceProperties.data.color))
409 plot3d(surfaceProperties.data.x,surfaceProperties.data.y,surfaceProperties.data.z)
411 fields(fields=="data") = [];
413 // Restore this properties after plot3d.
414 a.rotation_angles = rotation_angles;
415 a.axes_visible = axes_visible;
418 a.x_label.visible = x_label_visible;
419 a.y_label.visible = y_label_visible;
420 a.z_label.visible = z_label_visible;
421 a.x_label.text = x_label_text;
422 a.y_label.text = y_label_text;
423 a.z_label.text = z_label_text;
424 a.isoview = axes_isoview;
426 // Get mark_mode to be sure to set it after mark_style
427 mark_mode = surfaceProperties.mark_mode;
428 fields(fields=="mark_mode") = [];
432 if or(fields=="cdata_mapping") then // Fac3d specific
433 if surfaceProperties.color_flag >= 2 then
434 set(h, "cdata_mapping", surfaceProperties.cdata_mapping);
436 fields(fields=="cdata_mapping") = [];
439 if surfaceProperties.clip_state == "on" then
440 set(h,"clip_box", surfaceProperties.clip_box);
442 set(h,"clip_state",surfaceProperties.clip_state);
443 fields(fields=="clip_box") = [];
444 fields(fields=="clip_state") = [];
446 for i = 1:size(fields, "*")
447 if fields(i) == "mark_style" then
448 set(h, "mark_style", surfaceProperties.mark_style);
449 set(h, "mark_mode", mark_mode);
451 h(fields(i)) = surfaceProperties(fields(i));
459 function h = createCompound(compoundProperties)
460 fields = fieldnames(compoundProperties);
463 h = glue(createMatrixHandle(compoundProperties.children));
464 fields(fields=="children") = [];
466 for i = 1:size(fields, "*")
467 set(h, fields(i), compoundProperties(fields(i)));
474 function h = createRectangle(rectangleProperties)
475 fields = fieldnames(rectangleProperties);
478 xrect(0,1,1,1); // create the rectangle with dummy values
481 if rectangleProperties.clip_state == "on" then
482 set(h,"clip_box", rectangleProperties.clip_box);
484 set(h,"clip_state",rectangleProperties.clip_state);
485 fields(fields=="clip_box") = [];
486 fields(fields=="clip_state") = [];
488 // Get mark_mode to be sure to set it after mark_style
489 mark_mode = rectangleProperties.mark_mode;
490 fields(fields=="mark_mode") = [];
492 for i = 1:size(fields, "*")
493 if fields(i) == "mark_style" then
494 set(h, "mark_style", rectangleProperties.mark_style);
495 set(h, "mark_mode", mark_mode);
497 h(fields(i)) = rectangleProperties(fields(i));
505 function h = createArc(arcProperties)
506 fields = fieldnames(arcProperties);
509 xarc(0,1,1,1,0,360); // create the arc with dummy values
512 if arcProperties.clip_state == "on" then
513 set(h,"clip_box", arcProperties.clip_box);
515 set(h,"clip_state",arcProperties.clip_state);
516 fields(fields=="clip_box") = [];
517 fields(fields=="clip_state") = [];
519 for i = 1:size(fields, "*")
520 set(h, fields(i), arcProperties(fields(i)));
527 function h = createChamp(champProperties)
528 fields = fieldnames(champProperties);
531 champ(champProperties.data.x, champProperties.data.y, champProperties.data.fx, champProperties.data.fy);
532 fields(fields=="data") = [];
536 if champProperties.clip_state == "on" then
537 set(h,"clip_box", champProperties.clip_box);
539 set(h,"clip_state",champProperties.clip_state);
540 fields(fields=="clip_box") = [];
541 fields(fields=="clip_state") = [];
543 for i = 1:size(fields, "*")
544 set(h, fields(i), champProperties(fields(i)));
551 function h = createSegs(segsProperties)
552 fields = fieldnames(segsProperties);
555 xsegs(segsProperties.data(:,1), segsProperties.data(:,2))
559 if segsProperties.clip_state == "on" then
560 set(h,"clip_box", segsProperties.clip_box);
562 set(h,"clip_state",segsProperties.clip_state);
563 fields(fields=="clip_box") = [];
564 fields(fields=="clip_state") = [];
566 // Get mark_mode to be sure to set it after mark_style
567 mark_mode = segsProperties.mark_mode;
568 fields(fields=="mark_mode") = [];
570 for i = 1:size(fields, "*")
571 if fields(i) == "mark_style" then
572 set(h, "mark_style", segsProperties.mark_style);
573 set(h, "mark_mode", mark_mode);
575 h(fields(i)) = segsProperties(fields(i));
583 function h = createGrayplot(grayplotProperties)
584 fields = fieldnames(grayplotProperties);
587 grayplot(grayplotProperties.data.x, grayplotProperties.data.y, grayplotProperties.data.z);
588 fields(fields=="data") = [];
592 if grayplotProperties.clip_state=="on" then
593 set(h, "clip_box", grayplotProperties.clip_box)
595 set(h, "clip_state", grayplotProperties.clip_state);
596 fields(fields=="clip_box") = [];
597 fields(fields=="clip_state") = [];
599 for i = 1:size(fields, "*")
600 set(h, fields(i), grayplotProperties(fields(i)));
607 function h = createMatplot(matplotProperties)
608 fields = fieldnames(matplotProperties);
611 Matplot(matplotProperties.data);
612 fields(fields=="data") = [];
616 if matplotProperties.clip_state=="on" then
617 set(h, "clip_box", matplotProperties.clip_box)
619 set(h, "clip_state", matplotProperties.clip_state);
620 fields(fields=="clip_box") = [];
621 fields(fields=="clip_state") = [];
623 for i = 1:size(fields, "*")
624 set(h, fields(i), matplotProperties(fields(i)));
631 function h = createFec(fecProperties)
632 fields = fieldnames(fecProperties);
635 fec(fecProperties.data(:,1), fecProperties.data(:,2), fecProperties.triangles, fecProperties.data(:,3));
636 fields(fields=="data") = [];
637 fields(fields=="triangles") = [];
641 if fecProperties.clip_state=="on" then
642 set(h, "clip_box", fecProperties.clip_box)
644 set(h, "clip_state", fecProperties.clip_state);
645 fields(fields=="clip_box") = [];
646 fields(fields=="clip_state") = [];
648 for i = 1:size(fields, "*")
649 set(h, fields(i), fecProperties(fields(i)));
656 function h = createLegend(legendProperties)
658 %LEG = legendProperties;
665 function h = createText(textProperties)
666 fields = fieldnames(textProperties);
669 if textProperties.text_box_mode == "off" then
670 xstring(textProperties.data(1), textProperties.data(2), textProperties.text)
672 xstringb(textProperties.data(1), textProperties.data(2), textProperties.text, textProperties.text_box(1), textProperties.text_box(2))
677 if textProperties.clip_state=="on" then
678 set(h, "clip_box", textProperties.clip_box)
680 set(h, "clip_state", textProperties.clip_state);
681 fields(fields=="clip_box") = [];
682 fields(fields=="clip_state") = [];
684 for i = 1:size(fields, "*")
685 set(h, fields(i), textProperties(fields(i)));
692 function h = createDatatip(datatipProperties)
694 fields = fieldnames(datatipProperties);
697 h = datatipCreate(%POLYLINE, 0);
699 if datatipProperties.clip_state=="on" then
700 set(h, "clip_box", datatipProperties.clip_box)
702 set(h, "clip_state", datatipProperties.clip_state);
703 fields(fields=="clip_box") = [];
704 fields(fields=="clip_state") = [];
706 for i = 1:size(fields, "*")
707 set(h, fields(i), datatipProperties(fields(i)));
714 function h = createAxis(axisProperties)
715 fields = fieldnames(axisProperties);
718 if axisProperties.tics_direction == "bottom" then
720 elseif axisProperties.tics_direction == "top" then
722 elseif axisProperties.tics_direction == "left" then
724 elseif axisProperties.tics_direction == "right" then
726 elseif size(axisProperties.xtics_coord, "*") > 1 then
731 fields(fields=="tics_direction") = [];
733 drawaxis(x=axisProperties.xtics_coord,y=axisProperties.ytics_coord,dir=axisdir);
734 fields(fields=="xtics_coord") = [];
735 fields(fields=="ytics_coord") = [];
739 if axisProperties.clip_state=="on" then
740 set(h, "clip_box", axisProperties.clip_box)
742 set(h, "clip_state", axisProperties.clip_state);
743 fields(fields=="clip_box") = [];
744 fields(fields=="clip_state") = [];
746 for i = 1:size(fields, "*")
747 set(h, fields(i), axisProperties(fields(i)));
754 function h = createuimenu(uimenuProperties)
755 fields = fieldnames(uimenuProperties);
760 for i = 1:size(fields, "*")
761 if fields(i) == "children" then
762 children = createMatrixHandle(uimenuProperties(fields(i)));
763 for k=1:size(children, "*")
764 set(children(k), "parent", h);
767 set(h, fields(i), uimenuProperties(fields(i)));
775 function h = createuicontextmenu(uicontextmenuProperties)
776 fields = fieldnames(uicontextmenuProperties);
781 for i = 1:size(fields, "*")
782 if fields(i) == "children" then
783 children = createMatrixHandle(uicontextmenuProperties(fields(i)));
784 for k=1:size(children, "*")
785 set(children(k), "parent", h);
788 set(h, fields(i), uicontextmenuProperties(fields(i)));
796 function h = createuicontrol(uicontrolProperties)
797 fields = fieldnames(uicontrolProperties);
800 h = uicontrol("Style", uicontrolProperties.style);
801 fields(fields=="style") = [];
803 for i = 1:size(fields, "*")
804 if fields(i) == "children" then
805 children = createMatrixHandle(uicontrolProperties(fields(i)));
806 for k=1:size(children, "*")
807 set(children(k), "parent", h);
810 set(h, fields(i), uicontrolProperties(fields(i)));
818 function h = createLight(lightProperties)
819 fields = fieldnames(lightProperties);
823 fields(fields=="children") = [];
825 for i = 1:size(fields, "*")
826 set(h, fields(i), lightProperties(fields(i)));
830 // Utility function for legends, copy/paste from %h_load
831 function links=getlinksfrompath(ax,paths)
832 // ax is a handle on an axes entity
833 // paths a list or row vector which gives the set of paths relative to
839 p(1)=p(1)-1// the caption does not exists yet
840 for kp=1:size(p,"*"),
841 if or(e.type==["Axes","Compound"])&p(kp)<=size(e.children,"*") then
858 function macro = createMacro(macroStr, macroName)
860 macroSt = macroStr(3);
861 if macroStr(2) == %t then
866 header = strsubst(macroSt(1), "function ", "");
867 body = macroSt(2:$-1);
871 deff(header, body, flag);
872 execstr("macro = " + macroName);
875 [%__lhs__, %__rhs__] = argn();
876 %__resumeList__ = list();
877 %__resumeVarlist__ = [];
879 error(999, msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"), "load", 1));
882 if %__rhs__ >= 1 then
883 if typeof(%__filename__) <> "string" | size(%__filename__, "*") <> 1 then
884 error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "load", 1));
888 if isfile(%__filename__) & is_hdf5_file(%__filename__) then
889 %__loadFunction__ = import_from_hdf5;
890 //fileVersion = getScilabFileVersion(%__filename__); // Not needed for the moment
892 %__loadFunction__ = %_load;
895 //multiple output variables to prevent listinfile prints
896 [%__variableList__, %__varB__, %__varC__, %__varD__] = listvarinfile(%__filename__);
898 if size(varargin) <> 0 then
899 for i = 1:size(varargin)
900 %__variableName__ = varargin(i);
901 if typeof(%__variableName__) <> "string" | size(%__variableName__, "*") <> 1 then
902 error(999, msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"), "load", i));
905 if or(%__variableList__ == %__variableName__) then
906 %__loadFunction__(%__filename__, %__variableName__);
907 %__resumeList__($+1) = evstr(%__variableName__);
908 %__resumeVarlist__($+1) = %__variableName__;
909 clear(%__variableName__);
911 error(999, msprintf(gettext("%s: variable ''%s'' does not exist in ''%s''.\n"), "load", %__variableName__, %__filename__));
915 for i = 1:size(%__variableList__, "*")
916 %__variableName__ = %__variableList__(i);
917 %__loadFunction__(%__filename__, %__variableName__);
918 %__resumeList__($+1) = evstr(%__variableName__);
919 %__resumeVarlist__($+1) = %__variableName__;
920 clear(%__variableName__);
924 if isfile(%__filename__) & is_hdf5_file(%__filename__) then
925 %__resumeList__ = %__convertVariable__(%__resumeList__, %__resumeVarlist__);
928 execstr("[" + strcat(%__resumeVarlist__, ",") + "] = resume(%__resumeList__(:))");