⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topgui.m

📁 用于连续体的结构拓扑优化设计的matalb代码
💻 M
📖 第 1 页 / 共 5 页
字号:
function varargout = topgui(varargin)
%TOPGUI Apply constrains and forces to FEA model. It can be used as an assitant
%       tool for Ole Sigmund's 99 line topology optimization code.
% Usage : [fixeddofs F]=topgui(nelx,nely);
% More infomation :    http://www.geocities.com/chinakkong/research/topgui/
%                      http://www.topopt.dtu.dk/cgi-bin/matlab/matlab_counter.cgi
%
% ---------------------------------------------------------------------- %
%                    TopGUI V1.0                                         %
%                                                                        %
%  Release date : 2003-11-21                                             %
%  Written by   : Kang Zhao,Dalian University of Technology,Dalian,China.%
%  E-mail       : kangzhao@student.dlut.edu.cn                           %
%  More info    : http://www.geocities.com/chinakkong/research/topgui/      %
% ---------------------------------------------------------------------- %

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @topgui_OpeningFcn, ...
                   'gui_OutputFcn',  @topgui_OutputFcn, ...
                   'gui_LayoutFcn',  @topgui_LayoutFcn, ...                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before topgui is made visible.
function topgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to topgui (see VARARGIN)

% Choose default command line output for topgui

% To avoid callback executed before axes is ready
% "topgui_export" below has to be modified to exported .m file name.
set(hObject,'WindowButtonMotionFcn','topgui(''figure1_WindowButtonMotionFcn'',gcbo,[],guidata(gcbo))',...
            'WindowButtonUpFcn','topgui(''figure1_WindowButtonUpFcn'',gcbo,[],guidata(gcbo))');
        
h_text_email = findobj('tag','text_email');
backcolor = get(h_text_email,'BackgroundColor');
set(hObject,'Color',backcolor);

% Get input variables
handles.nelx=varargin{1};
handles.nely=varargin{2};

nelx = handles.nelx;
nely = handles.nely;

    % Re-set axes properties
    
    % Get handle of the axes by axes handle in handles
    h_axes=gca;
    set(h_axes,'XTick',[1 (5:5:nelx-1) nelx+1]);
    set(h_axes,'YTick',[1 (5:5:nely-1) nely+1]);
    set(h_axes,'XLim',[0 nelx+2]);
    set(h_axes,'YLim',[0 nely+2]);
    
    % Set limits of x,y,i,j
    h_lim_x = findobj('tag','text_lim_x');
    h_lim_y = findobj('tag','text_lim_y');
    h_lim_i = findobj('tag','text_lim_i');
    h_lim_j = findobj('tag','text_lim_j');
    
    set(h_lim_x,'string',['(' num2str(1) ':' num2str(nelx+1) ')']);
    set(h_lim_y,'string',['(' num2str(1) ':' num2str(nely+1) ')']);
    set(h_lim_i,'string',['(' num2str(1) ':' num2str(nelx) ')']);
    set(h_lim_j,'string',['(' num2str(1) ':' num2str(nely) ')']);
    

    x=(1:nelx+1);
    y=(1:nely+1);
    
    
    % =======  Add variables to handles   ======= %
    
    % Nodes selection
        handles.h_nodes=zeros(nelx+1,nely+1); % Handles of nodes               
        handles.selection_rect=zeros(1,4); % Select field when picking nodes.
        
        handles.pt1=zeros(1,2); % [x1 y1] Starting corner of picking rectangle
        handles.pt3=zeros(1,2); % [x2 y2] Ending corner of picking rectangle
        
       handles.h_rect=0; % Handle of selection rectangle
        
       % handles.h_rect=line([x1 x2 x3 x4],[y1 y2 y3 y4])
       % 1------4
       % |      |
       % 2------3
        handles.if_picking=0; % in Picking mode and press is down
        
    
    % Constrains
        handles.fixeddofs=[];
        % patch handles relative to handles.fixeddofs
        % handles.fixeddofs_patch3=[h_patch1 h_patch1 ... h_patchN]
        handles.fixeddofs_patch3=[]; 
    
    % Forces
        handles.F=sparse(2*(nely+1)*(nelx+1),1);
        handles.F_arrow=[]; % arrow handles relative to handles.F
        
    % ======= Configurate plot ======= %
    % Unselected nodes
        handles.nodeMarker_default = ['.'];
        handles.nodeMarkerSize_default = 8;
        handles.nodeColor_default = [0 0 1];
    
    % Selected nodes
		handles.nodeMarker_selected = ['o'];
        handles.nodeMarkerSize_selected = 5;
        handles.nodeColor_selected = 'm';
    
    % Constrain triangle
        handles.constrain_triangle_height = 0.5;
        handles.constrain_triangle_width = 0.7;
        handles.constrain_triangle_FaceColor = 'none';
        handles.constrain_triangle_LineWidth = 1.0;
    
   % Force arrow
        handles.force_arrow_line_length=0.55;
        handles.force_arrow_line_color=[1 0 0];
        handles.force_arrow_line_width=2.0;
        
        handles.force_arrow_patch_width=0.3;
        handles.force_arrow_patch_height=0.3;
        handles.force_arrow_patch_facecolor='r';
        handles.force_arrow_patch_edgecolor='r';
    
	% handles.selected_nodes= 
	% [i1 i2 ...in
	%  j1 j2 ...jn]
    handles.selected_nodes=[];     
    
    % =======   Plot meshes   ======= %
    % Horizontal Grid Lines
    for(j=1:nely+1)
        x1=x(1);
        x2=x(nelx+1);
        y1=y(j);
        y2=y(j);
        plot([x1 x2],[y1 y2]);
        hold on;
    end
    
    % Vertical Grid Lines
    for(i=1:nelx+1)
        x1=x(i);
        x2=x(i);
        y1=y(1);
        y2=y(nely+1);
        plot([x1 x2],[y1 y2]);
        hold on;
    end
	
	% Nodes
	for i=1:nelx+1
		for j=1:nely+1
			handles.h_nodes(i,j)=plot(x(i),y(j));
		end	
	end        
    
    % Set default appearance to nodes
    set_nodes_appearance(handles.h_nodes,handles,0);
    
    % Save to handles
    guidata(hObject,handles);
       
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes topgui wait for user response (see UIRESUME)
%uiwait(handles.figure1);
uiwait(hObject);


% --- Outputs from this function are returned to the command line.
function varargout = topgui_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1}=handles.fixeddofs;
varargout{2}=handles.F;


% Delete TOPGUI
delete(hObject);% --- Executes on button press in pushbutton_selNodes_inp.function pushbutton_selNodes_inp_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_selNodes_inp (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

% handles.selected_nodes=[2,:]; 
% [i1 i2 ...in
%  j1 j2 ...jn]


% Get select type
h_selNodes_type = findobj('tag','popupmenu_select_type');
value = get(h_selNodes_type,'value');

if value==1
    handles = guidata(hObject);
    if_return = checknew(hObject);% if select No in dialog if_return=1, and then quit this function
    if if_return
        return;
    end
end

% Prompt dialog for input x,y
prompt = {' x :',' y:'};
dlg_title = 'Input index vectors of nodes to be selected (In matlab syntax)';
num_lines = 1;
def       = {'',''};
answer    = inputdlg(prompt,dlg_title,num_lines,def);


% Validity check for input vector
valid = 0;
while ( length(answer) > 0 & valid == 0 )
    str_x=answer{1};
    str_y=answer{2};
    
    if ( length(str_x) > 0 & length(str_y) > 0 )
        % Valid input for eval
        eval(['arr_x=(' str_x ');']);
        eval(['arr_y=(' str_y ');']);       
        if ( size(arr_x,1) ~= 1 ) % Row vector
            err_msg='Please input x as a row vector.';
        elseif ( size(arr_x,2) == 0 ) % Length>0
            err_msg='Length of x can not be zero. ';
        elseif ( size(arr_y,1) ~= 1 ) % Row vector
            err_msg='Please input y as a row vector.';
        elseif ( size(arr_y,2) == 0 ) % Length>0
            err_msg='Length of y can not be zero. ';
        elseif ~all( arr_x >= 1 & arr_x <= handles.nelx + 1) % Bound check
            err_msg=['All elements in x must between 1 and ' num2str(handles.nelx+1)];
        elseif ~all( arr_y >= 1 & arr_y <= handles.nely + 1) % Bound check
            err_msg=['All elements in y must between 1 and ' num2str(handles.nely+1)];
        else
            valid=1;
        end
        
        if ~valid
            % Alert error information
            h = msgbox(err_msg,'Invalid input!','error','modal');
            waitfor(h); % Wait until dialog is deleted
            
            % Input again
            prompt = {'x(eg. [1:20 30 40]) :','y(eg. [1:20 30 40]) :'};
            dlg_title = 'Input index vectors of nodes to be selected';
            num_lines = 1;
            def       = {str_x,str_y}; % Use last input as default
            answer    = inputdlg(prompt,dlg_title,num_lines,def);
        end
    end
end % End while (length(answer)>0 & valid==0)

if ( length(answer)==0 ) % Canceled
    return;
end

% Below:Dialog not canceled and input valid

% Get index array of selected nodes
len_x=length(arr_x);
len_y=length(arr_y);
len=len_x*len_y;                
nodes_index_array=zeros(2,len);
k=0;
for i=1:len_x
    for j=1:len_y
        k=k+1;
        nodes_index_array(1,k)=arr_x(i);
        nodes_index_array(2,k)=arr_y(j);
    end
end

% Select nodes. value: 1-New  2-Add
select_nodes(nodes_index_array,handles,value); 

% End of function pushbutton_selNodes_inp_Callback% --- Executes on button press in pushbutton_SelNodes_pick.function pushbutton_SelNodes_pick_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_SelNodes_pick (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

handles.if_picking = 1;
guidata(gcbo,handles);

% Get select type
h_selNodes_type = findobj('tag','popupmenu_select_type');
value = get(h_selNodes_type,'value');

if value==1
    handles = guidata(hObject);
     if_return = checknew(hObject);% if select No in dialog if_return=1, and then quit this function
    if if_return
        return;
    end
end

nelx=handles.nelx;
nely=handles.nely;

% Pick by mouse
set(gcf,'Pointer','crosshair');

% Get starting corner of selecting rectangle
waitfor(gcf,'userdata','ButtonDown'); % axes1.WindowButtonDownFcn  -  get handles.if_picking
handles = guidata(gcbo); % Get updated handles. ( handles.pt1 is got in callback WindowButtonDownFcn) 

if ~handles.if_picking % Canceled by right click
    return;
end

% Initialize line of rectangle
xdata=[0 0 0 0 0];
ydata=[0 0 0 0 0];
handles.h_rect=line('XData', xdata,'YData', ydata,'Visible', 'off','linestyle','--');

% handles.if_picking=1;

guidata(gcbo,handles); % Save handles


% Get end corner of selecting rectangle
waitfor(gcf,'userdata','ButtonUp'); % 0-Press up and rectangle finished    
handles = guidata(gcbo); % Get updated handles

set(gcf,'userdata',1);

x1=handles.pt1(1);
y1=handles.pt1(2);
x3=handles.pt3(1);
y3=handles.pt3(2);

xmin=ceil(min(x1,x3));
xmax=floor(max(x1,x3));
ymin=ceil(min(y1,y3));
ymax=floor(max(y1,y3));

% Validity check for selection field
if ( ~(xmin<=nelx+1 & xmax>=1 & ymin<=nely+1 & ymax>=1 ))
    errordlg('No nodes selected!');
    return;
end

% Get nodes index array
len_x=xmax-xmin+1;
len_y=ymax-ymin+1;
len=len_x*len_y;                
nodes_index_array=zeros(2,len);
k=0;
for x=xmin:xmax
    for y=ymin:ymax
        k=k+1;
        nodes_index_array(1,k)=x;
        nodes_index_array(2,k)=y; 
    end	
end
% Select nodes. value: 1-New  2-Add
select_nodes(nodes_index_array,handles,value); 
% --- Executes during object creation, after setting all properties.function popupmenu_select_type_CreateFcn(hObject, eventdata, handles)% hObject    handle to popupmenu_select_type (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes during object creation, after setting all properties.function popupmenu_constrain_type_CreateFcn(hObject, eventdata, handles)% hObject    handle to popupmenu_constrain_type (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes on button press in pushbutton_constrain_apply.function pushbutton_constrain_apply_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_constrain_apply (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

% Get constrain type

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -