/*
* Project Info: http://jcae.sourceforge.net
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* (C) Copyright 2009, by EADS France
*/
package org.jcae.netbeans.cad.modeler;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.jcae.netbeans.cad.GeomUtils;
import org.jcae.netbeans.cad.NbShape;
import org.jcae.netbeans.cad.ShapeChildren;
import org.jcae.netbeans.viewer3d.ViewManager;
import org.jcae.opencascade.jni.BRepBuilderAPI_MakeEdge;
import org.jcae.opencascade.jni.BRepBuilderAPI_MakeVertex;
import org.jcae.opencascade.jni.BRepBuilderAPI_MakeWire;
import org.jcae.opencascade.jni.TopAbs_ShapeEnum;
import org.jcae.opencascade.jni.TopoDS_Edge;
import org.jcae.opencascade.jni.TopoDS_Vertex;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
/**
*
* @author Jerome Robert
*/
public class WirePanel extends JPanel {
private VTKWire vtkWire;
/** Creates new form WirePanel */
public WirePanel() {
initComponents();
//TODO implement closed wire
closeBox.setVisible(false);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
GridBagConstraints gridBagConstraints;
JScrollPane scrollPane = new JScrollPane();
textArea = new JTextArea();
JButton previewButton = new JButton();
JButton createButton = new JButton();
closeBox = new JCheckBox();
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new GridBagLayout());
scrollPane.setBorder(BorderFactory.createTitledBorder("Coordinates"));
scrollPane.setViewportView(textArea);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new Insets(0, 0, 5, 0);
add(scrollPane, gridBagConstraints);
previewButton.setText("Preview");
previewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
previewButtonActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridy = 2;
gridBagConstraints.weightx = 1.0;
add(previewButton, gridBagConstraints);
createButton.setText("Create");
createButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
createButtonActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridy = 2;
gridBagConstraints.weightx = 1.0;
add(createButton, gridBagConstraints);
closeBox.setText("Close");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
add(closeBox, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void previewButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_previewButtonActionPerformed
if(vtkWire == null)
vtkWire = new VTKWire(ViewManager.getDefault().getCurrentView());
vtkWire.showWire(getCoordinates());
}//GEN-LAST:event_previewButtonActionPerformed
private void createButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
NbShape shape = null;
Node node = null;
for(Node n:TopComponent.getRegistry().getActivatedNodes())
{
shape = n.getLookup().lookup(NbShape.class);
if(shape != null && shape.getType() == TopAbs_ShapeEnum.COMPOUND)
{
node = n;
break;
}
}
if(node != null)
{
NbShape newShape = createWire(getCoordinates());
shape.add(newShape);
newShape.setName(newShape.getName());
ShapeChildren sc=node.getCookie(ShapeChildren.class);
sc.addShapes(Collections.singleton(newShape));
GeomUtils.getParentBrep(node).getDataObject().setModified(true);
}
if(vtkWire != null)
vtkWire.hide();
}//GEN-LAST:event_createButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private JCheckBox closeBox;
private JTextArea textArea;
// End of variables declaration//GEN-END:variables
public double[] getCoordinates()
{
String[] ss = textArea.getText().split("[\r\n]");
ArrayList<double[]> r = new ArrayList<double[]>(ss.length);
for(String line:ss)
{
String[] ws = line.trim().split("[\\s]+");
if(ws.length == 3)
{
try
{
double[] v = new double[3];
for(int i = 0; i<3; i++)
v[i] = Double.parseDouble(ws[i]);
r.add(v);
}
catch(NumberFormatException ex)
{
}
}
}
double[] rr = new double[r.size()*3];
int p = 0;
for(double[] v:r)
{
System.arraycopy(v, 0, rr, p, 3);
p += 3;
}
return rr;
}
@Override
public void removeNotify() {
super.removeNotify();
if(vtkWire != null)
vtkWire.hide();
}
private NbShape createWire(double[] coordinates) {
BRepBuilderAPI_MakeWire mw=new BRepBuilderAPI_MakeWire();
double[] tmp = new double[3];
System.arraycopy(coordinates, 0, tmp, 0, 3);
TopoDS_Vertex previous = (TopoDS_Vertex) new BRepBuilderAPI_MakeVertex(tmp).shape();
for(int i = 1; i<coordinates.length/3; i++)
{
System.arraycopy(coordinates, 3*i, tmp, 0, 3);
TopoDS_Vertex current = (TopoDS_Vertex) new BRepBuilderAPI_MakeVertex(tmp).shape();
TopoDS_Edge e = (TopoDS_Edge) new BRepBuilderAPI_MakeEdge(previous, current).shape();
mw.add(e);
previous = current;
}
return new NbShape(mw.shape());
}
}