www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/@cgexpr/buildSLSystem.m
function msg = buildSLSystem(E,sys,ExportMode,SOPTS) %BUILDSLSYSTEM build Simulink system for expression % % [predecessor,pos,msg] = buildSLSystem(E,sys,ExportMode,SOPTS) % E : expression % sys : Simulink diagram % ExportMode 1= editmode % SOPTS structure with export options % SOPTS.WriteCalibration : boolean to write table and constant values to SL % SOPTS.Export : Export Calibration parameters to a file % SOPTS.ExportParameterFile : Parameter file used when Export is true % SOPTS.SLType : Strategy Type % 1 = development (native simulink blocks) % 5 = Prelookup/LookupTables % % See also cgexp2sl % Copyright 2007-2012 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin<3 ExportMode = 1; end if nargin < 4 SOPTS.WriteCalibration = true; SOPTS.Export = true; SOPTS.ExportParameterFile = ''; SOPTS.SLType = 1; end msg =''; % In this case we are constructing the model from scratch if isa(E,'cgfeature') eq= get(E,'equation'); elseif isa(E,'cgmodexpr') eq = address(E); else eq = mbcpointer(0,1); end % Create the diagram % Normal or EXPRTable Version switch ExportMode case 1 % Top level of an EXPRTable version - Create subsystem add_block('built-in/SubSystem',sys,'position',[60 100 140 200],'tag','nosplit'); case 2 % Lower level of an EXPRTable version - Create subsystem add_block('built-in/SubSystem',sys,'position',[40 100 120 200]); end % Get a list of equation variables [Vars,Normalisers,pAllPtrs] = getEqPointers(eq,SOPTS); vpos=45; if isempty(Vars) || ExportMode == 2 add_block('built-in/outport',[sys,'/',getname(E)],... 'position',[20 100 40 120]); else for i=1:length(Vars) vP = Vars(i); if vP.isa('cgfeature') col = 'green'; else col = 'black'; end % add inport and goto on lhs [C,vpos] = buildSLSource(vP.info,sys,vpos,col,SOPTS,setdiff(pAllPtrs,Normalisers{i})); for j=1:length(Normalisers{i}) % add any normaliser required nP = Normalisers{i}(j); buildSLNormaliser(nP.info,sys,SOPTS,vpos,C); vpos=vpos+40; end end % Add divider and the outport add_block('built-in/SubSystem',[sys,'/divider'],... 'ShowName','off',... 'position',[255 24 260 24+vpos],... 'backgroundcolor','black',... 'mask','on','maskvariables','b=@1;','b','1',... 'masktype','Graphical Divider',... 'maskdescription','This is purely a graphical block. It has no affect on the evaluation of the diagram and can be deleted. The parameter below is for internal use only.'); if ~SOPTS.Export add_block('cgeqlibprivate/label',[sys,'/label'],... 'ShowName','off',... 'position',[35 15 550 44]); end add_block('built-in/outport',[sys,'/',getname(E)],... 'position',[280 140 300 160]); end h = get_param([sys,'/',getname(E)],'handle'); hpos = get(h,'Position'); if isempty(eq) % no equation yet place ooutport at right of diagram syspos = get_param(sys,'Location'); hpos(1) = syspos(3)-40; hpos(3) = syspos(3)-20; set(h,'Position',hpos); else % move outport to the right based on depth of expression depth = expressionDepth(info(eq)); del = 110*max(1,depth) + 20; hpos([1 3]) = hpos([1 3]) + del; set(h,'Position',hpos); % position for next block pos = hpos; pos([1 3]) = pos([1 3]) - 110; % Construct the main part of the model portstruct = get_param(h,'Porthandles'); predecessor=portstruct.Inport; msg = addSLrecursive(eq.info,sys,SOPTS,predecessor,pos); end function d = expressionDepth(e,d) %expressionDepth calculate expression depth for Simulink diagram if nargin==1 d = 0; end if isa(e,'cglookupone') % skip one level - no normaliser inp = info(get(e,'x')); d = expressionDepth(inp,d+1); elseif isa(e,'cgfeature') || (isa(e,'cgnormaliser') && isa( info(getinputs(e)),'cgvariable')) % normalisers fed by variables are in LHS % cgfeatures are either on the LHS or a subsystem d=d+1; elseif isa(e,'cgvariable') d = d+1; else % recurse into inputs to find depth pInp = getinputs(e); if ~isempty(pInp) Inputs = infoarray(pInp); inputDepth = ones(size(Inputs)); for i=1:length(pInp) inputDepth(i) = expressionDepth(Inputs{i},d+1); end d = max(inputDepth); end end function [Vars,Normalisers,pAllPtrs] = getEqPointers(eq,SOPTS) EqPtrs=mbcpointer(0,1); pAllPtrs = mbcpointer(0,1); % Get a unique list of pointers in the equation if ~isempty(eq) if SOPTS.Export pAllPtrs = [eq eq.getAllInputs]'; else pAllPtrs = [eq;eq.getptrsnosf]; end for i=1:length(pAllPtrs) if pAllPtrs(i).isa('cgvariable') EqPtrs=[EqPtrs;pAllPtrs(i)]; end end EqPtrs = removeDuplicates(EqPtrs); end Vars = EqPtrs; Normalisers(1:length(Vars)) = {mbcpointer(1,0)}; if ~isempty(eq) for i=1:length(pAllPtrs) pAllPtrs(i).info = setaddress(pAllPtrs(i).info,pAllPtrs(i)); [Vars,Normalisers] = MakeInputList(pAllPtrs(i).info,Vars,Normalisers,SOPTS.Export); end end