www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@xregdesign/importconstraints.m
function [d,ok]=importconstraints(d,action,varargin) %IMPORTCONSTRAINTS GUI for importing constraints into a design % % [D,OK]=IMPORTCONSTRAINTS(D,DES_LIST) % % OK==0 if cancel is pressed. % OK==1 if OK pressed but no changes made. % OK==2 if OK pressed and changes made to design. % OK==3 if OK pressed and constraints were immediately applied to the % design. % Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin<2 action={}; % empty list of optional constraints end if ~ischar(action) varargin=[{action} varargin]; action='figure'; end switch lower(action) case 'figure' [d,ok]=i_createfig(d,varargin{:}); end end function [dout,ok]=i_createfig(d,des_list) dlgH = mbcgui.container.Dialog(... 'Name','Import Constraints',... 'Resize','off',... 'Buttons', 'OK_CANCEL_HELP',... 'HelpCode', 'xreg_desConImport',... 'Size', [500 300]); figh = dlgH.Figure; ptr=xregGui.RunTimePointer(d); ptr.LinkToObject(figh); lyt=i_createlyt(figh,ptr,des_list); dlgH.Content = lyt; closeMode = dlgH.showDialog(); if strcmp(closeMode,'OK') changes=i_finalise(lyt); if changes>0 dout=ptr.info; ok=1+changes; else % This handles both "OK but no constraints to add" and "Cancelled" dout=d; ok=1+changes; end else ok=0; dout=d; end delete(dlgH); end function lyt=i_createlyt(figh,p,des_list) udptr=xregGui.RunTimePointer; udptr.LinkToObject(figh); ud.desptr=p; ud.deslist=des_list; ud.constraints={}; ud.sources={}; ud.nullstring=''; txt=uicontrol('Parent',figh,... 'Style','text',... 'String','Import from:',... 'HorizontalAlignment','left',... 'HitTest','off',... 'Enable','inactive'); ud.sourcetype=uicontrol('Parent',figh,... 'Style','popupmenu',... 'String',{'Current designs','Design Editor file (*.mvd)',... 'Boundary Constraints (current project)', 'Boundary Constraints (*.mat file)'},... 'BackgroundColor',[1 1 1],... 'Callback',{@i_setsource,udptr}); ud.filetxt=uicontrol('Parent',figh,... 'Style','text',... 'String','Source file:',... 'HorizontalAlignment','left',... 'HitTest','off',... 'Enable','off'); ud.filename=uicontrol('Parent',figh,... 'Style','edit',... 'String','',... 'HorizontalAlignment','left',... 'Enable','off',... 'Callback',{@i_updateconlist,udptr}); ud.fileopen=uicontrol('Parent',figh,... 'Style','pushbutton',... 'String','...',... 'Enable','off',... 'Callback',{@i_openfile,udptr}); conframe = mbcgui.container.layoutpanel(... 'Parent', figh, ... 'Title','Available constraints',... 'BorderType', 'etchedin', ... 'LayoutBorder',[10 10 10 5]); descriptxt=uicontrol('Parent',conframe,... 'Style','text',... 'String','Select one or more constraints to import:',... 'HorizontalAlignment','left',... 'HitTest','off',... 'Enable','inactive'); list = mbcwidgets.Table1D( 'list',... 'Parent', conframe,... 'SelectionMode', 'MultiRow' ); list.Peer.setColumnData( {'Description', 'Source', 'Type'} ); list.Peer.setColumnWidths( [200, 120, 120] ); ud.conlist=list; grd=xreggridbaglayout(conframe,... 'packstatus','off',... 'dimension',[2 1],... 'gapy',5,... 'rowsizes',[15 -1],... 'elements',{descriptxt,ud.conlist}); set(conframe, 'LayoutComponent', {grd}); lyt=xreggridbaglayout(figh,'dimension',[9 4],... 'rowsizes',[4 15 3 7 3 15 2 10 -1],... 'colsizes',[70 190 20 -1],... 'gapx',5,... 'mergeblock',{[1 3],[2 3]},... 'mergeblock',{[5 7],[2 2]},... 'mergeblock',{[5 7],[3 3]},... 'mergeblock',{[9 9],[1 4]},... 'elements',{[],txt,[],[],[],ud.filetxt,[],[],conframe,... ud.sourcetype,[],[],[],ud.filename,[],[],[],[],... [],[],[],[],ud.fileopen},... 'userdata',udptr); udptr.info=ud; % populate list with initial settings i_gen_con_list(udptr); i_populate_list(udptr); end function i_setsource(src,evt,udptr) % enable/disable file edit box ud=udptr.info; switch get(src,'Value') case 1, % Current designs set([ud.filename;ud.filetxt;ud.fileopen],'Enable','off'); sc=xregGui.SystemColorsDbl; set(ud.filename,'BackgroundColor',sc.CTRL_BACK); case 2, % Design Editor file (*.mvd) set([ud.filename;ud.filetxt;ud.fileopen],'Enable','on'); set(ud.filename,'BackgroundColor',[1 1 1]); case 3, % Boundary Constraints (current project) set([ud.filename;ud.filetxt;ud.fileopen],'Enable','off'); sc=xregGui.SystemColorsDbl; set(ud.filename,'BackgroundColor',sc.CTRL_BACK); case 4, % Boundary Constraints (*.mat file) set([ud.filename;ud.filetxt;ud.fileopen],'Enable','on'); set(ud.filename,'BackgroundColor',[1 1 1]); otherwise warning(message('mbc:xregdesign:InvalidState2')); end % update list box i_gen_con_list(udptr); i_populate_list(udptr); end function i_updateconlist(src,evt,udptr) % update list box after filename change i_gen_con_list(udptr); i_populate_list(udptr); end function i_openfile(src,evt,udptr) % callback from file choosing button ud=udptr.info; switch get(ud.sourcetype,'Value') case 2, % design file defp = mbcGetPath( 'mbcmodel', 'Designs', '*.mvd' ); case 4, % Boundary Constraints (*.mat file) defp = mbcGetPath( 'mbcmodel', 'Projects', '*.mat' ); otherwise warning(message('mbc:xregdesign:InvalidState3')); return end [f,p]=uigetfile(defp); if f~=0 set(ud.filename,'String',fullfile(p,f)); i_gen_con_list(udptr); i_populate_list(udptr); end end function i_gen_con_list(udptr) ud=udptr.info; % Number of factors at current node mdl = model( ud.desptr.info ); nf = nfactors( mdl ); switch get(ud.sourcetype,'Value') case 1, % design list n_ParseDesignList case 2, % design file n_ParseDesignFile case 3, % Boundary Constraints (current project) n_ParseCurrentProject; case 4, % Boundary Constraints (*.mat file) n_ParseProjectFile; otherwise warning(message('mbc:xregdesign:InvalidState4')); end udptr.info=ud; function n_ParseDesignList cons={}; src={}; if ~isempty(ud.deslist) % Need to check the other designs for suitable constraints % There has to be sufficient inuts factors on the current constraint % to use the potential inputs % % Parse design list looking for constraints for n = 1:length( ud.deslist ), C = constraints( ud.deslist{n} ); if ~isempty( C ), newCons = constraints( C ); keep = false( size( newCons ) ); for i = 1:length( newCons ), keep(i) = nf >= nActiveFactors( newCons{i} ); end cons = [cons, newCons(keep)]; src = [src, repmat( {name( ud.deslist{n} )}, 1, nnz( keep ) )]; end end end if isempty(cons) ud.nullstring='No suitable constraints to import'; else ud.nullstring=''; end ud.constraints=cons; ud.sources=src; end function n_ParseDesignFile filename=get(ud.filename,'String'); if ~isempty(filename) [d,ok]=importfromfile(ud.desptr.info,filename); if ok mdl=model(d); if all(abs(gettarget(mdl)-gettarget(model(ud.desptr.info)))<10*eps) C=constraints(d); if ~isempty(C) ud.constraints=constraints(C); ud.sources=repmat({'File'},1,length(ud.constraints)); if isempty(ud.constraints) ud.nullstring='No constraints in file'; else ud.nullstring=''; end else ud.constraints={}; ud.nullstring='No constraints in file'; end else ud.nullstring='Incorrect design ranges in file'; ud.constraints={}; end else ud.constraints={}; ud.nullstring='Unable to load design from file'; end else ud.constraints={}; ud.nullstring='No file specified'; end end function n_ParseCurrentProject mbh = MBrowser; if mbh.GUIExists, n_ParseProject( mbh.RootNode.info ); else ud.constraints = {}; ud.nullstring = 'Model Browser is not open.'; end end function n_ParseProjectFile % Load a MBC Model Project file and parse it for boundary % constraints filename = get( ud.filename, 'String' ); if isempty( filename ), ud.constraints = {}; ud.nullstring = 'No file specified.'; elseif isProjectFile( mdevproject, filename ), try loaded = load( filename, '-mat' ); n_ParseProject( loaded.MP ); delete( loaded.MP ); catch unused % This is here to catch file load errors. ud.constraints = {}; ud.nullstring = 'Unable to load project from file'; end else ud.constraints = {}; ud.nullstring = 'File not found or invalid format.'; end end function n_ParseProject( project ) [ud.constraints, ud.sources] = getAllBoundaries( project ); if isempty( ud.constraints ), ud.nullstring = 'No constraints in file.'; else % Need to check the other designs for suitable constraints % There has to be sufficient inuts factors on the current constraint % to use the potential inputs keep = false( size( ud.constraints ) ); for i = 1:length( ud.constraints ), keep(i) = nf >= nActiveFactors( ud.constraints{i} ); end ud.constraints = ud.constraints(keep); ud.sources = ud.sources( keep); end end end function i_populate_list(udptr) ud=udptr.info; cons=ud.constraints; src=ud.sources; if isempty(cons) ud.conlist.Enable = 'off'; listData = {ud.nullstring, '', ''}; selectedIndex = 0; else ud.conlist.Enable = 'on'; listData = cell(length(cons), 3); for n=1:length(cons) listData(n,:) = {tostring(cons{n}), src{n}, typename(cons{n})}; end selectedIndex = 1; end ud.conlist.Peer.setData( listData ); ud.conlist.selectRows( selectedIndex ); end function changes=i_finalise(lyt) udptr=get(lyt,'UserData'); ud=udptr.info; changes=0; if ~isempty(ud.constraints) sel=ud.conlist.getSelectedRows(); if ~isempty(sel) % Need to ensure that the inputs for the constraints are matched to the % design mdl = model( ud.desptr.info ); cif = coninputfactor( mdl ); for i = sel(:).', [ud.constraints{i}, ok] = guiMatchInputs( ud.constraints{i}, cif ); if ~ok, sel(sel==i) = []; end end end if ~isempty( sel ) % add new constraints to design D = addConstraint(ud.desptr.info,ud.constraints(sel)); [ud.desptr.info, mode] = applyconstraints(D, 'interactive'); switch mode case 'none' % Design didn't need to be altered changes = 1; case '' % Operation cancelled changes = -1; otherwise % Design altered in some way changes = 2; end end end end