/
startImaris.m
153 lines (134 loc) · 4.55 KB
/
startImaris.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function success = startImaris(this, userControl)
% IceImarisConnector: startImaris (public method)
%
% DESCRIPTION
%
% This method starts an Imaris instance and stores the ImarisApplication
% ICE object.
%
%
% SYNOPSIS
%
% success = conn.startImaris(userControl)
%
% INPUT
%
% userControl : (optional, default = 0)
% The optional parameter userControl sets the fate of
% Imaris when the client is closed: if userControl is
% true (1), Imaris terminates when the IceImarisConnector
% object (conn) is deleted. If is it set to false (0),
% Imaris stays open after the client is closed.
%
% OUTPUT
%
% success : 1 if starting Imaris was successful, 0 otherwise
% AUTHORS
%
% Author: Aaron Ponti
% LICENSE
%
% ImarisConnector is a simple commodity class that eases communication between
% Imaris and MATLAB using the Imaris XT interface.
% Copyright (C) 2011 Aaron Ponti
%
% 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.
% Imaris only runs on Windows and Mac OS X
success = 0;
if (~ispc() && ~ismac())
disp('IceImarisConnector can only work on Windows and Mac OS X.');
return
end
% Check and if needed set the optional parameter userControl
if nargin == 1
userControl = false;
end
if ~ismember(userControl, [0 1])
error('userControl must be either 0 or 1.');
end
% Store the userControl
this.mUserControl = userControl;
% On Mac OS X, make sure that the Imaris Frameworks folder is at the
% beginning of the dynamic library path to prevent any conflicts with
% the Qt libraries loaded by MATLAB
if ismac()
dylPath = getenv('DYLD_LIBRARY_PATH');
ImarisFrameworksPath = [this.mImarisPath, filesep, ...
'Contents', filesep, 'Frameworks'];
indx = strfind(dylPath, ImarisFrameworksPath);
if isempty(indx) || indx ~= 1
% Prepend
dylPath = [ImarisFrameworksPath, ':', dylPath];
setenv('DYLD_LIBRARY_PATH', dylPath)
end
end
% If an Imaris instance is open, we close it -- no questions asked
if this.isAlive() == 1
this.closeImaris();
end
% Now we open a new one
try
% Start ImarisServer
[success, errorMessage] = this.startImarisServer();
if success == 0
disp(errorMessage);
success = 0;
return
end
% Launch Imaris
[status, result] = system(...
['"', this.mImarisExePath, '" id', ...
num2str(this.mImarisObjectID), ' &']);
if status == 1
disp(result);
success = 0;
return
end
% Try getting the application over a certain time period in case it
% takes to long for Imaris to be registered.
for trial = 1 : 200
try
% A too quick call to mImarisLib.GetApplication() could potentially
% throw an exception and leave the mImarisLib object in an unusable
% state. As a workaround, we reinstantiate ImarisLib() at
% every iteration. This will make sure that sooner or later
% we will get the application. The exception is not
% automatically casted to a MATLAB exception, se we cannot
% really catch it...
this.mImarisLib = ImarisLib();
vImaris = this.mImarisLib.GetApplication(this.mImarisObjectID);
catch ex
% We can't catch the exception...
end
if ~isempty(vImaris)
break
end
pause(0.1)
end
% At this point we should have the application
if isempty(vImaris)
disp('Could not link to the Imaris application.');
success = 0;
return
end
% We can store the application
this.mImarisApplication = vImaris;
% We set success to 1
success = 1;
catch ex
disp(ex.message);
success = 0;
end
end