% % % Object Manager Function for Imaris % % Copyright Bitplane AG 2006 % % % Installation: % % - Copy this file into the XTensions folder in the Imaris installation directory % - You will find this function in the Image Processing menu % % % % % Matlab::BPObjectManager(%i) % % % % % Matlab::BPObjectManager(%i) % % % % % % Description: % % Save/load/clear single imaris objects in .mat files. % Apply to Spots, Surface, Track, Filament and Groups. % Groups are saved with all their content. % Only first-level objects (mSurpassScene direct childs) can be saved. % % function BPObjectManager(aImarisApplicationID) global vImarisApplication vEditFileName; % button callbacks if aImarisApplicationID==-1 clear global vImarisApplication vEditFileName; return; elseif isa(aImarisApplicationID, 'numeric') && aImarisApplicationID==-5; Browse; return; elseif isa(aImarisApplicationID, 'numeric') && aImarisApplicationID<-1; Execute(-aImarisApplicationID-1); return; end % connect to Imaris Com interface if ~isa(aImarisApplicationID, 'COM.Imaris_Application') vImarisServer = actxserver('ImarisServer.Server'); vImarisApplication = vImarisServer.GetObject(aImarisApplicationID); else vImarisApplication = aImarisApplicationID; end % create the window uicontrol('Style', 'text', 'String', 'Imaris Object Manager', ... 'FontName', 'Times New Roman', 'FontSize', 10, ... 'Position', [10,70,170,20], 'DeleteFcn', 'BPObjectManager(-1)'); uicontrol('Style', 'pushbutton', 'String', 'Browse...', ... 'Position', [210,70,80,20], 'Callback', 'BPObjectManager(-5)'); uicontrol('Style', 'text', 'String', 'File Name:', ... 'FontName', 'Times New Roman', 'FontSize', 10, ... 'Position', [10,50,70,20]); vEditFileName = uicontrol('Style', 'edit', 'String', 'ImarisObjects.mat', ... 'FontName', 'Times New Roman', 'FontSize', 10, ... 'Position', [80,50,210,20]); uicontrol('Style', 'pushbutton', 'String', 'Save', ... 'Position', [10,10,80,30], 'Callback', 'BPObjectManager(-2)'); uicontrol('Style', 'pushbutton', 'String', 'Load', ... 'Position', [110,10,80,30], 'Callback', 'BPObjectManager(-3)'); uicontrol('Style', 'pushbutton', 'String', 'Clear', ... 'Position', [210,10,80,30], 'Callback', 'BPObjectManager(-4)'); vWindow = get(vEditFileName, 'Parent'); vWindowPosition = get(vWindow, 'Position'); vWindowPosition(3:4) = [300, 100]; set(vWindow, 'Position', vWindowPosition); %----------------------------------------------% function Browse() global vEditFileName; vFileName = get(vEditFileName,'String'); [vFileName, vPathName] = uigetfile({'*.mat','MAT-files (*.mat)'}, ... 'Select the file', vFileName); if ~isequal(vFileName,0) set(vEditFileName,'String',fullfile(vPathName,vFileName)); end %----------------------------------------------% function Execute(aOperation, aParent, aParentName) % aOperation = 1 save, 2 load, 3 clear global vImarisApplication vEditFileName; vStringOperations = {'save', 'load', 'clear'}; vStringTypes = {'Spots', 'Surface', 'Track', 'Filament', 'Group'}; vFileName = get(vEditFileName,'String'); vFactory = vImarisApplication.mFactory; vDataSet = vImarisApplication.mDataSet; if nargin==1 vParent = vImarisApplication.mSurpassScene; vParentName = ''; else vParent = aParent; vParentName = aParentName; end vObjectNameString = [vParentName,'vObjectsName']; vObjectTypeString = [vParentName,'vObjectsType']; vObjectsName = {}; vObjectsType = []; vObjectsIndex = []; vNumberOfObjects = 0; if aOperation==1 % search the scene objects - Spots, Surface, Track and Filament for vChildIndex = 1:vParent.GetNumberOfChildren vDataItem = vParent.GetChild(vChildIndex - 1); vFound = false; if vFactory.IsSpots(vDataItem) vObjectsType(vNumberOfObjects+1) = 1; vFound = true; elseif vFactory.IsSurface(vDataItem) vObjectsType(vNumberOfObjects+1) = 2; vFound = true; elseif vFactory.IsTrack(vDataItem) vObjectsType(vNumberOfObjects+1) = 3; vFound = true; elseif vFactory.IsFilament(vDataItem) vObjectsType(vNumberOfObjects+1) = 4; vFound = true; elseif vFactory.IsDataContainer(vDataItem) vObjectsType(vNumberOfObjects+1) = 5; vFound = true; end if vFound vNumberOfObjects = vNumberOfObjects + 1; vObjectsName(vNumberOfObjects) = cellstr(vDataItem.mName); vObjectsIndex(vNumberOfObjects) = vChildIndex; end end else % load the list of the saved objects if exist(vFileName, 'file') load(vFileName,vObjectNameString,vObjectTypeString); vObjectsName = eval(vObjectNameString); vObjectsType = eval(vObjectTypeString); vNumberOfObjects = length(vObjectsName); else vNumberOfObjects = 0; end end if vNumberOfObjects==0 if nargin==1 msgbox(['There is no object to ', char(vStringOperations(aOperation)), '!']); end return; end vObjectsCaption = vObjectsName; for vObject = 1:vNumberOfObjects % Name [Type] vObjectsCaption(vObject) = {[char(vObjectsName(vObject)), ' [', ... char(vStringTypes(vObjectsType(vObject))), ']']}; end if nargin==1 [vObjectList, vOk] = listdlg('ListString', vObjectsCaption, ... 'SelectionMode','single',... 'ListSize',[250 150],... 'Name','Imaris Object Manager',... 'PromptString',{['Please select the object to ', ... char(vStringOperations(aOperation)), ':']}); if vOk<1, return; end; else vObjectList = 1:vNumberOfObjects; end vObjectsName_ = vObjectsName; vObjectsType_ = vObjectsType; for vSelectedObject = vObjectList vName = char(vObjectsName_(vSelectedObject)); vType = vObjectsType_(vSelectedObject); % remove invalid characters vName_ = vName; for vLetter = 1:length(vName) vChar = vName(vLetter); if (vChar<'0' || vChar>'9') && (vChar<'a' || vChar>'z') && (vChar<'A' || vChar>'Z') vName_(vLetter) = '_'; end end % names (that should be unique) of the variables in the file.mat vPrefix = [vParentName,'v',vName_,'_','0'+vType,'_']; vNameColorVisible = [vPrefix,'ColorVisible']; vNamePosition = [vPrefix,'Position']; vNameTime = [vPrefix,'Time']; vNameRadius = [vPrefix,'Radius']; vNameNormals = [vPrefix,'Normals']; vNameTriangles = [vPrefix,'Triangles']; vNameVertices = [vPrefix,'Vertices']; vNameEdges = [vPrefix,'Edges']; if aOperation==1 % save vObject = vParent.GetChild(vObjectsIndex(vSelectedObject) - 1); if exist(vFileName, 'file') load(vFileName, vObjectNameString, vObjectTypeString); vObjectsName = eval(vObjectNameString); vObjectsType = eval(vObjectTypeString); % if the variable is already saved, their values are overwritten % but no new object is added vFound = false; vNumberOfObjects = length(vObjectsName); for vIndex = 1:vNumberOfObjects if strcmp(vName,char(vObjectsName(vIndex))) && vType==vObjectsType(vIndex) vFound = true; break; end end if vFound vAnswer = questdlg(['Overwrite ',char(vObjectsCaption(vSelectedObject)),'?'], ... 'Imaris Object Manager', 'Yes', 'No', 'Yes'); if ~strcmp(vAnswer,'Yes') return; elseif vType==5 % clear all the children of the group Execute(3, vObject, vPrefix); end else vObjectsName(vNumberOfObjects+1) = {vName}; vObjectsType(vNumberOfObjects+1) = vType; end eval([vObjectNameString,' = vObjectsName;']); eval([vObjectTypeString,' = vObjectsType;']); save(vFileName, vObjectNameString, vObjectTypeString, '-append'); else vObjectsName = {vName}; vObjectsType = vType; save(vFileName, 'vObjectsName', 'vObjectsType'); end [vR, vG, vB, vA] = vObject.GetColor; eval([vNameColorVisible, ' = [vR, vG, vB, vA, vObject.mVisible];']); save(vFileName, vNameColorVisible, '-append'); if vType==1 % spots eval(['[',vNamePosition,',',vNameTime,',',vNameRadius,'] = vObject.Get;']); save(vFileName, vNamePosition, vNameRadius, '-append'); elseif vType==2 % surface eval([vNameVertices,' = vObject.GetVertices;']); eval([vNameTriangles,' = vObject.GetTriangles;']); eval([vNameNormals,' = vObject.GetNormals;']); eval([vNameTime,' = vObject.GetTimeIndex;']); save(vFileName, vNameVertices, vNameTriangles, vNameNormals, '-append'); elseif vType==3 % track vSpots = vObject.GetSpots; eval([vNameEdges,' = vObject.GetEdges;']); eval(['[',vNamePosition,',',vNameTime,',',vNameRadius,'] = vSpots.Get;']); save(vFileName, vNameEdges, vNamePosition, vNameRadius, '-append'); elseif vType==4 % filament eval(['[',vNamePosition,',',vNameRadius,',',vNameEdges,'] = vObject.Get;']); eval([vNameTime,' = vObject.mIndexT;']); save(vFileName, vNamePosition, vNameRadius, vNameEdges, '-append'); elseif vType==5 % data container eval([vNameTime,' = 0;']); % save all the childrens eval([vPrefix,'vObjectsName',' = {};']); eval([vPrefix,'vObjectsType',' = [];']); save(vFileName,[vPrefix,'vObjectsName'],'-append'); save(vFileName,[vPrefix,'vObjectsType'],'-append'); Execute(1, vObject, vPrefix) end % time is required for all oject types (please set dummy 0 if not available) save(vFileName, vNameTime, '-append'); elseif aOperation==2 % load load(vFileName, vNameTime); vTime = max(eval(vNameTime))+1; if vTime>vDataSet.mSizeT vAnswer = questdlg(sprintf(['%s is composited by %i frames. ', ... 'Resizing the DataSet could cause memory problems. Procede anyway?'], ... char(vObjectsCaption(vSelectedObject)),vTime), ... 'Imaris Object Manager', 'Yes', 'No', 'Yes'); if ~strcmp(vAnswer,'Yes') return; end vDataSet.mSizeT = vTime; end if vType==1 % spots load(vFileName, vNamePosition, vNameRadius); vObject = vFactory.CreateSpots; eval(['vObject.Set(',vNamePosition,',',vNameTime,',',vNameRadius,');']); elseif vType==2 % surface load(vFileName, vNameVertices, vNameTriangles, vNameNormals); vObject = vFactory.CreateSurface; eval(['vObject.SetSurface(',vNameVertices,',',vNameTriangles,',', ... vNameNormals,',',vNameTime,');']); elseif vType==3 % track load(vFileName, vNameEdges, vNamePosition, vNameRadius); vObject = vFactory.CreateTrack; vSpots = vFactory.CreateSpots; eval(['vSpots.Set(',vNamePosition,',',vNameTime,',',vNameRadius,');']); vObject.SetSpots(vSpots); eval(['vObject.SetEdges(',vNameEdges,');']); elseif vType==4 % filament load(vFileName, vNamePosition, vNameRadius, vNameEdges); vObject = vFactory.CreateFilament; eval(['vObject.Set(',vNamePosition,',',vNameRadius,',',vNameEdges,');']); eval(['vObject.mIndexT = ', vNameTime,';']); elseif vType==5 % data container vObject = vFactory.CreateDataContainer; Execute(2, vObject, vPrefix); end load(vFileName, vNameColorVisible); eval(['vObject.SetColor(',vNameColorVisible,'(1),',vNameColorVisible,'(2),', ... vNameColorVisible,'(3),',vNameColorVisible,'(4));']); eval(['vObject.mVisible = ',vNameColorVisible,'(5);']); vObject.mName = vName; vParent.AddChild(vObject); else % clear % copy the data into a struct % deletes the original file % rebuild the right file if nargin==1 vAnswer = questdlg(['Remove ',char(vObjectsCaption(vSelectedObject)),' from the memory?'], ... 'Imaris Object Manager', 'Yes', 'No', 'Yes'); if ~strcmp(vAnswer,'Yes') return; end end vFile = load(vFileName); vFields = fieldnames(vFile); if nargin==1 % remove the name and the type from the lists vRemaining = [1:vSelectedObject-1,vSelectedObject+1:vNumberOfObjects]; vObjectsName = vObjectsName(vRemaining); vObjectsType = vObjectsType(vRemaining); end save(vFileName, 'vObjectsName', 'vObjectsType'); vNumberOfLetters = length(vPrefix); for vVariable = 1:length(vFields) vString = char(vFields(vVariable)); if ~strcmp(vString,'vObjectsName') && ~strcmp(vString,'vObjectsType') && ... (length(vString)