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

📄 topgui.m

📁 用于连续体的结构拓扑优化设计的matalb代码
💻 M
📖 第 1 页 / 共 5 页
字号:

if ~if_nodes_selected(handles)
    return;
end

h_constrain_type = findobj('tag','popupmenu_constrain_type');
value = get( h_constrain_type,'value');

switch value
    case 1 % Ux
        dofs = [0 1];
    case 2 % Uy
        dofs = [1 0];
    case 3 % UxUy
        dofs = [0 0];
    otherwise
        dofs = [0 0];
end

% Update handles.fixeddofs and handles.fixeddofs_patch3 
%   and redraw constrain triangles
handles = apply_constrains(handles.selected_nodes, dofs, handles);

% Save updated handles
guidata(hObject,handles);% --- Executes on button press in pushbutton_constrain_clear.function pushbutton_constrain_clear_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_constrain_clear (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

if ~if_nodes_selected(handles)
    return;
end

button = questdlg('Are you sure to cancel the specified constrains on the selected nodes?','Confirm selection','Yes','No','No');
if strcmp(button,'No')
    return;
end

index_selected_nodes=IJ2index(handles.selected_nodes,handles);

h_constrain_type = findobj('tag','popupmenu_constrain_type');
value = get(h_constrain_type,'value'); % 1-Ux 2-Uy 3-UxUy

switch value
    case 1 % Ux
        dofs_free=2*index_selected_nodes-1;
    case 2 % Uy
        dofs_free=2*index_selected_nodes;
    case 3 % UxUy
        dofs_free=[2*index_selected_nodes-1 2*index_selected_nodes];
    otherwise
        dofs_free=[2*index_selected_nodes-1 2*index_selected_nodes];
end

n = length( handles.fixeddofs );

% To save uncanceled dofs
fixeddofs_temp=[]; 
fixeddofs_patch3_temp=[];

for i=1:n
    result = find( dofs_free == handles.fixeddofs(i));
    if length( result ) == 0 % Not canceled
        fixeddofs_temp=[fixeddofs_temp handles.fixeddofs(i)];
        fixeddofs_patch3_temp=[fixeddofs_patch3_temp handles.fixeddofs_patch3(i)];
    else % Constrain Canceled
        delete(handles.fixeddofs_patch3(i));
    end
end

handles.fixeddofs=fixeddofs_temp;
handles.fixeddofs_patch3=fixeddofs_patch3_temp;

% Update constrain number
num_dofs = length( handles.fixeddofs );
num_uy = length( find( mod( handles.fixeddofs,2 )==0 ) ); % number of Uy constrains
num_ux = num_dofs - num_uy; % number of Ux constrains


% Update constrain number
h_num_constrained_ux=findobj('tag','text_num_constrained_ux');
h_num_constrained_uy=findobj('tag','text_num_constrained_uy');

set(h_num_constrained_ux,'string',num2str(num_ux));
set(h_num_constrained_uy,'string',num2str(num_uy));


guidata(gcbo,handles);
% --- Executes during object creation, after setting all properties.function popupmenu_force_type_CreateFcn(hObject, eventdata, handles)% hObject    handle to popupmenu_force_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_force_apply.function pushbutton_force_apply_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_force_apply (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

if ~if_nodes_selected(handles)
    return;
end

% Get force type
h_force_type = findobj('tag','popupmenu_force_type');
value = get( h_force_type,'value');

dlgTitle='Input magnitude of force';
switch value
    case 1 % Fx
        dof=1;
        prompt={'Fx:'};
    case 2 % Fy
        dof=2;
        prompt={'Fy:'};
    otherwise
        return;
end

str_mag=inputdlg(prompt,dlgTitle); %magnitude of force
if length(str_mag)==0 % canceled
   return;
end
force=str2num(str_mag{1});

% Update handles.F and handles.F_arrow
%   and redraw force arrows
handles = apply_forces(handles.selected_nodes, dof, force, handles );

% Save updated handles
guidata(hObject,handles);% --- Executes on button press in pushbutton_force_clear.function pushbutton_force_clear_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_force_clear (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

if ~if_nodes_selected(handles)
    return;
end

button = questdlg('Are you sure to cancel the specified forces on the selected nodes?','Confirm selection','Yes','No','No');
if strcmp(button,'No')
    return;
end

index_selected_nodes=IJ2index(handles.selected_nodes,handles);

% Get force type
h_force_type = findobj('tag','popupmenu_force_type');
value = get( h_force_type,'value');

switch value
    case 1 % Fx
        dofs_free=2*index_selected_nodes-1;
    case 2 % Fy
        dofs_free=2*index_selected_nodes;
    otherwise
        dofs_free=[2*index_selected_nodes-1 2*index_selected_nodes];
end

i_nonzeros = find(handles.F);
n = length( i_nonzeros );

% To save uncanceled forces
F_arrow_temp=[];

for i=1:n
    dof = i_nonzeros(i);
    result = find( dofs_free == dof);
    if length( result ) == 0 % Not canceled
        F_arrow_temp=[F_arrow_temp handles.F_arrow(:,i)];
    else % Force Canceled
        handles.F(dof) = 0.0;
        delete(handles.F_arrow(:,i));
    end
end
handles.F_arrow=F_arrow_temp;



i_nonzeros = find(handles.F);
n = length( i_nonzeros );
num_fy = length( find( mod ( i_nonzeros, 2 ) == 0 ) );
num_fx = n - num_fy; 

% Update force number
h_num_force_fx=findobj('tag','text_num_force_fx');
h_num_force_fy=findobj('tag','text_num_force_fy');

set(h_num_force_fx,'string',num2str(num_fx));
set(h_num_force_fy,'string',num2str(num_fy));


guidata(gcbo,handles);
% --- Executes on button press in checkbox_save_files.function checkbox_save_files_Callback(hObject, eventdata, handles)% hObject    handle to checkbox_save_files (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of checkbox_save_files
h_edit=findobj('tag','edit_save_path');
h_browse=findobj('tag','pushbutton_browse');

value=get(hObject,'Value');
if value % Checked
    set(h_edit,'enable','on');    
    set(h_browse,'enable','on');
else
    set(h_edit,'string','');
    set(h_edit,'enable','off');    
    set(h_browse,'enable','off');
end% --- Executes on button press in pushbutton_OK.function pushbutton_OK_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_OK (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)
disp('Starting optimization ...');
uiresume;
    
% --- Set appearance of nodes
function set_nodes_appearance(h_nodes,handles,iflag)
% iflag: 0 - default apprarance
%        1 - selected appearance

switch iflag
    case 0 % unselected mode
        set(h_nodes,'Marker',handles.nodeMarker_default);
        set(h_nodes,'MarkerSize',handles.nodeMarkerSize_default);
        set(h_nodes,'Color',handles.nodeColor_default);
    case 1 % selected mode
        set(h_nodes,'Marker',handles.nodeMarker_selected);
        set(h_nodes,'MarkerSize',handles.nodeMarkerSize_selected);
        set(h_nodes,'Color',handles.nodeColor_selected);
    otherwise % set default to unselected mode
        set(h_nodes,'Marker',handles.nodeMarker_default);
        set(h_nodes,'MarkerSize',handles.nodeMarkerSize_default);
        set(h_nodes,'Color',handles.nodeColor_default);
end
    
% --- Select nodes
function select_nodes(nodes_index_array,handles,i_select_type)
% nodes_index_array=[x1 x2 ... xn
%                   y1 y2 ...yn ]
% handles : topgui handles
% i_select_type :  1 - New      2 - Add

if ~size(nodes_index_array,2)>0
    return;
end

switch i_select_type
    case 1 % New
        if (size(handles.selected_nodes,2)>0)
            ii=handles.selected_nodes(1,:);
            jj=handles.selected_nodes(2,:);
            
            % Set previous selected nodes appearance to unselected mode
            set_nodes_appearance(handles.h_nodes(ii,jj),handles,0); 
            
            % Clear selected nodes
            handles.selected_nodes=[];             
        end
        
        % Save newly selected nodes index(sorted and no repetites)
        handles.selected_nodes=[unique(nodes_index_array','rows')]'; 
        
        ii=handles.selected_nodes(1,:);
        jj=handles.selected_nodes(2,:);
        
        % Set nodes appearance to selected mode
        set_nodes_appearance(handles.h_nodes(ii,jj),handles,1); 
    case 2 % Add
        ii=nodes_index_array(1,:);
        jj=nodes_index_array(2,:);
        
        % Add newly selected nodes index(sorted and no repetites)
        handles.selected_nodes=[unique([handles.selected_nodes nodes_index_array]','rows')]';  
        
        % Set newly selected nodes appearance to selected mode        
        set_nodes_appearance(handles.h_nodes(ii,jj),handles,1); 
    otherwise        
end    

% Update num of selected nodes
num=size( handles.selected_nodes,2 );
str_num=num2str(num);
h_text_num_nodes_selected=findobj('tag','text_num_nodes_selected');
set(h_text_num_nodes_selected,'string',str_num);

% Save updated handles
    guidata(gcf,handles);
    
% --- Check/alert for a new selection
function if_return = checknew(hObject);

if_return = 0;
handles = guidata(hObject);

% Alert for clearing previous selection
if(size(handles.selected_nodes,2)>0) 
    button = questdlg('Are you sure to cancel the previous selection and start a new selestion?','Confirm selection','Yes','No','No');
    if strcmp(button,'No')
        if_return = 1;
        return;
    else % Unselect previously selected nodes
        % Set previously selected nodes to unselected mode
        ii=handles.selected_nodes(1,:);
        jj=handles.selected_nodes(2,:);
        set_nodes_appearance(handles.h_nodes(ii,jj),handles,0);
        
        % Clear handls.selected_nodes
        handles.selected_nodes=[];
        
        % Update num of selected nodes
        h_text_num_nodes_selected=findobj('tag','text_num_nodes_selected');
        set(h_text_num_nodes_selected,'string','0');
        
        guidata(hObject,handles);
    end
end

% --- apply_constrains
function handles = apply_constrains(nodes_index_array, dofs, handles)
% node_index_array=[x1 x2 ... xn  
%                   y1 y2 ... yn]
% dof = [0 1]-Ux     [1 0]-Uy    [0 0]-UxUy

% handles.fixeddofs=[u1 v1 u2 v2 ...];

% handles.fixeddofs_patch3=[ h_patch1 h_patch2 ... h_patchN]

index_selected_nodes=IJ2index(nodes_index_array, handles);

% Update fixed dofs
if(all(dofs==[0 1])) % Ux fixed
    dofs_fixed=2*index_selected_nodes-1;
    handles.fixeddofs=unique([handles.fixeddofs dofs_fixed]); % unique also sorts it in asc
elseif(all(dofs==[1 0]))
    dofs_fixed=2*index_selected_nodes;
    handles.fixeddofs=unique([handles.fixeddofs dofs_fixed]); % unique also sorts it in asc
elseif(all(dofs==[0 0]))
    dofs_fixed=[2*index_selected_nodes-1 2*index_selected_nodes];
    handles.fixeddofs=unique([handles.fixeddofs dofs_fixed]); % unique also sorts it in asc
end

% Redraw constrain symbols
if(length(handles.fixeddofs)>0)
    delete(handles.fixeddofs_patch3); % delete old patchs
end
handles.fixeddofs_patch3=[]; % initial patchs

num_dofs=length(handles.fixeddofs);
num_ux=0; % number of Ux constrains
num_uy=0; % number of Uy constrains
for i=1:num_dofs
    index_dof=handles.fixeddofs(i);
    if(mod(index_dof,2)==1) % Ux
        idof=1;
        num_ux=num_ux+1;
        inode=(index_dof+1)/2;
    else % Uy
        idof=2;
        num_uy=num_uy+1;
        inode=index_dof/2;
    end
    y=mod(inode,handles.nely+1);
    x=(inode-y)/(handles.nely+1)+1;
    if y==0
        y=handles.nely+1;
        x=x-1;
    end
    handles.fixeddofs_patch3=[handles.fixeddofs_patch3 patch3(x,y,idof,handles)];
end

% Update constrain number
h_num_constrained_ux=findobj('tag','text_num_constrained_ux');
h_num_constrained_uy=findobj('tag','text_num_constrained_uy');

set(h_num_constrained_ux,'string',num2str(num_ux));
set(h_num_constrained_uy,'string',num2str(num_uy));
    
% Save handles
guidata(gcbo,handles);

% End of function apply_constrains

% --- Get node index in FEA by (i,j)
function index=IJ2index(nodes_index_array, handles)
% nodes_index_array=[x1 x2 ... xn  
%                    y1 y2 ... yn]

index=zeros(1,size(nodes_index_array,2));

I=nodes_index_array(1,:);
J=nodes_index_array(2,:);

index=sort((handles.nely+1)*(I-1)+J);


⌨️ 快捷键说明

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