Package bs.bs2d.gui.views

Source Code of bs.bs2d.gui.views.MeshView

package bs.bs2d.gui.views;

import bs.bs2d.fea.TriMesh2D;
import bs.bs2d.gui.BSConstants;
import bs.bs2d.gui.BusyStateListener;
import bs.bs2d.gui.UserMessage;
import bs.bs2d.gui.plot.MeshPlotter;
import bs.bs2d.gui.plot.PlotPanel;
import bs.bs2d.io.Gmsh;
import bs.bs2d.io.MeshReader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.io.File;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
*
* @author Djen
*/
public class MeshView implements BSGUIView{
   
    private static final String NAME = "Mesh";
   
    // gui
    private final PlotPanel content;
    private final MeshViewControls controls;
    private final JMenu[] menus;
   
    private BusyStateListener bsl;
    private JCheckBoxMenuItem chkSolid;
    private JCheckBoxMenuItem chkWire;
    private JCheckBoxMenuItem chkNodes;
    private JCheckBoxMenuItem chkEdges;
   
    // data
    private File inputFile;
    private TriMesh2D mesh;
    private Rectangle2D meshBounds;
    private MeshPlotter meshPlotter;
   
   
    public MeshView(){
        content = new PlotPanel();
        controls = new MeshViewControls(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(bsl != null)
                    bsl.setBusy(true);
               
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            generateMesh();
                            showMesh();
                        } catch (Exception ex){
                            UserMessage.showError("Error generating mesh!",
                                                            ex.getMessage());
                        }
                       
                        if(bsl != null)
                            bsl.setBusy(false);
                    }
                }).start();
               
            }
        });
        menus = buildMenus();
    }
   
    private JMenu[] buildMenus(){
        JMenu[] ms = new JMenu[1];
       
        JMenu m = new JMenu("View");
       
        chkSolid  = new JCheckBoxMenuItem("Solid Elements");
        chkSolid.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                meshPlotter.setShowSolidElements(chkSolid.isSelected());
            }
        });
        chkSolid.setSelected(true);
        m.add(chkSolid);
        chkWire = new JCheckBoxMenuItem("Wireframe");
        chkWire.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                meshPlotter.setShowWireframe(chkWire.isSelected());
            }
        });
        chkWire.setSelected(true);
        m.add(chkWire);
       
        m.addSeparator();
       
        chkNodes  = new JCheckBoxMenuItem("Show Nodes");
        chkNodes.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                meshPlotter.setShowNodes(chkNodes.isSelected());
            }
        });
        chkNodes.setSelected(false);
        m.add(chkNodes);
        chkEdges = new JCheckBoxMenuItem("Edge Nodes Only");
        chkEdges.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                meshPlotter.setShowEdgeNodesOnly(chkEdges.isSelected());
            }
        });
        chkEdges.setSelected(false);
        m.add(chkEdges);
       
        m.addSeparator();
       
        JMenuItem mitResetView = new JMenuItem("Reset View");
        mitResetView.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                content.resetView();
            }
        });
        m.add(mitResetView);
       
        ms[0] = m;
        return ms;
    }
   
    @Override
    public JComponent getContent() {
        return content;
    }

    @Override
    public JComponent getControls() {
        return controls;
    }

    @Override
    public JMenu[] getMenus() {
        return menus;
    }

    @Override
    public void init(Object data) {
        if(data instanceof File){
            controls.init((File) data);
        } else if (data instanceof TriMesh2D) {
            controls.init(null);
            mesh = (TriMesh2D) data;
            showMesh();
        } else {
            throw new IllegalArgumentException("Invalid input data! "
                    + "Must be a file or a mesh!");
        }
           
    }

    @Override
    public Object getData() {
        return mesh;
    }

    @Override
    public String getName() {
        return NAME;
    }
   
   
    public void generateMesh() throws Exception{
       
        inputFile = controls.getFile();
        boolean newFile = controls.isNewFile();
        double clmin, clmax;
       
        if(newFile){
            clmin = 1;
            clmax = 100;
            mesh = getMesh(inputFile, clmin, clmax);
            meshBounds = mesh.getBounds();
            System.out.println("MeshBounds: " + meshBounds);
        }
       
        int fineness = controls.getFineness();
        double relSize = 2.0/fineness;
        double meshSize = (meshBounds.getWidth() + meshBounds.getHeight()) / 2;
        clmax = meshSize * relSize;
        clmin = clmax / 2;
       
        mesh = getMesh(inputFile, clmin, clmax);
    }
   
    public void showMesh(){
        meshPlotter = new MeshPlotter(mesh);
        applyViewSettings();
        content.setPlotObject(meshPlotter, true);
    }
   
    private void applyViewSettings(){
        meshPlotter.setShowSolidElements(chkSolid.isSelected());
        meshPlotter.setShowWireframe(chkWire.isSelected());
        meshPlotter.setShowNodes(chkNodes.isSelected());
        meshPlotter.setShowEdgeNodesOnly(chkEdges.isSelected());
    }
   
    private TriMesh2D getMesh(File geom, double clmin, double clmax)
                                                               throws Exception{
        File out = BSConstants.getTempFile("msh");
       
        Gmsh.generate2DMesh(geom, out, clmin, clmax);
        return MeshReader.readMesh(out);
    }

    @Override
    public void setBusyStateListener(BusyStateListener bsl) {
        this.bsl = bsl;
    }
   
}
TOP

Related Classes of bs.bs2d.gui.views.MeshView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.