1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation; either version 2 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOUSE. See the GNU
10  * General Public License for more details.
11  *
12  * You should have recieved a copy of the GNU General Public License
13  * along with this program; if not write to the Free Software
14  * Foundation, inc., 59 Temple Place, Suite 330, Boston MA 02111-1307
15  * USA
16  */
17 package j3d;
18 
19 
20 import java.awt.*;
21 import java.awt.event.*;
22 import java.awt.geom.*;
23 import java.awt.image.*;
24 import java.io.*;
25 import java.util.*;
26 
27 import javax.swing.*;
28 import javax.swing.tree.*;
29 
30 import util.QSortZ;
31 
32 import gui.PreProcessor;
33 import util.PngEncoderB;
34 /**
35  * Insert the type's description here.
36  *
37  * @author: Yuriy Mikhaylovskiy, Jonas Forssell
38  */
39 
40 public class Canvas3DSW extends JPanel implements Canvas3D, Serializable {
41   private DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode();
42   public DefaultTreeModel tree3d = new DefaultTreeModel(treeNode);
43   private BufferedImage offscreen;
44   private Graphics2D  offgraphics;
45   boolean painted = false;
46   private Vector obj3d = new Vector();
47   private Vector shp3d = new Vector();
48   private int id_obj3d=0;
49   public Matrix3D VMatrix3D = new Matrix3D();
50   public Vector3D center_of_rotation = new Vector3D();
51   public JLabel coodinates = new JLabel();
52   private QSortZ qsort = new QSortZ();
53   private int x,y,x0,y0;
54   private PreProcessor pre;
55   private boolean show = true;
56 //
57   public boolean DRAFTMODE=false;
58   public Color BGCOLOR = Color.white;
59   public byte GRAPHICSMODE=2;
60   public Color GRIDCOLOR=Color.black;
61   public float GRIDLEVEL=0;
62   public byte GRIDPLANE=0;
63   public boolean GRIDMODE=true;
64   public float GRIDSIZE=10;
65   public float[] LIMITS={0,0,210,297};
66   public byte RENDERMODE=2;
67   public boolean SHOW_ID_NODE=false;
68   public boolean SHOW_ID_ELEMENT=false;
69   public boolean SHOW_ID_CONSTRAINTS=true;
70   public boolean SHOW_ID_LOADS=true;
71   public boolean SHOW_ID_TRACKERS=true;
72   public boolean SHOW_ID_MATERIALS=true;
73   public int POINTSIZE=8;
74   public int NODESIZE=6;
75   public Color STLCOLOR=new Color(0.7f,0.7f,0.7f);
76   private boolean DRAG = false;
77   // Codes for handling of geometry
78   public float GeometricTolerance = 0.01f; // Tolerance for calculation of geometry
79   public float NODE_MERGE_TOLERANCE = 1e-3F;
80 //
81   private _Group nodes, elements, geometry;
82 
83 
Canvas3DSW(PreProcessor pre)84   public Canvas3DSW(PreProcessor pre) {
85     this.pre = pre;
86 
87     createGroups();
88 
89     addComponentListener(new ComponentAdapter() {
90       public void componentResized(ComponentEvent e) {
91         offscreen=null;
92       }
93     });
94 
95     addMouseListener(new MouseAdapter() {
96 
97         public void mousePressed(MouseEvent e) {
98         int mod = e.getModifiers();
99         x=e.getX(); y=e.getY();
100         x0 = x; y0 = y;
101       }
102 
103         public void mouseReleased(MouseEvent e) {
104             int mod = e.getModifiers();
105             if (((mod & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) && DRAG == true) {
106                 boxSelect(Math.min(x0,x),Math.min(y0,y),Math.max(x0,x),Math.max(y0,y));
107 
108                 view_reset();
109             }
110 
111         DRAG = false;
112         DRAFTMODE = false;
113       }
114 
115         public void mouseClicked(MouseEvent e) {
116             int mod = e.getModifiers();
117             if((((mod & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) && ((mod & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK)) || ((mod & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK)){
118                 if (!e.isControlDown()) {
119                     clearSelectOnAllObjects3D();
120                     clearTree();
121                 }
122 
123                 setPickedObject(e.getX(), e.getY());
124 
125                 view_reset();
126 
127                 if (e.getClickCount() == 2)
128                     showEditPanel(e);
129 
130                 requestAction();
131             }
132 
133             if(((mod & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK)){
134                  view_all();
135             }
136         }
137     });
138 
139     addMouseMotionListener(new MouseMotionAdapter() {
140 
141         public void mouseMoved(MouseEvent e) {
142             coodinates.setText(e.getX()+","+e.getY());
143         }
144 
145         public void mouseDragged(MouseEvent e) {
146             if(e.getModifiers() == MouseEvent.BUTTON1_MASK)
147                 DRAG = true;
148             else
149                 DRAFTMODE = true;
150             this_mouseDragged(e);
151         }
152     });
153 
154     VMatrix3D.translate(400,-300,0);
155     VMatrix3D.scale(1,-1,1);
156     view_grid();
157   }
158 
159 
160 
createGroups()161 private void createGroups() {
162     nodes = new _Group(false, true);
163     nodes.setName("Node");
164     elements = new _Group(false, true);
165     elements.setName("Elem");
166     geometry = new _Group(false, true);
167     geometry.setName("Geo");
168 
169     this.add3D(nodes);
170     this.add3D(elements);
171     this.add3D(geometry);
172 }
173 
174 
175 
setPainted()176   private synchronized void setPainted(){
177     painted = true;
178     notifyAll();
179   }
180 
paint(Graphics g)181   public synchronized void paint(Graphics g){
182       shp tmp;
183 
184     if (offscreen == null){
185       if (offgraphics != null) offgraphics.dispose();
186       if (offscreen != null) offscreen.flush();
187       offscreen = (BufferedImage)createImage(getWidth(), getHeight());
188       offgraphics = (Graphics2D)offscreen.getGraphics();
189     }
190 
191 
192     // Create background
193     offgraphics.setColor(BGCOLOR);
194     offgraphics.fillRect(0, 0, getWidth(), getHeight());
195 
196     // Draw each element
197     for(int i=0; i<shp3d.size(); i++){
198         tmp = (shp)shp3d.elementAt(i);
199         if (tmp.isShow() == show)
200             tmp.paint(offgraphics,this);
201     }
202 
203     // Draw bounding box if used
204     if (DRAG == true) {
205         offgraphics.setColor(Color.RED);
206         offgraphics.drawRect(Math.min(x0,x),Math.min(y0,y),Math.abs(x-x0),Math.abs(y-y0));
207     }
208 
209     g.drawImage(offscreen, 0, 0,this);
210     setPainted();
211   }
212 
update(Graphics g)213   public void update(Graphics g){
214     paint(g);
215   }
216 
view_transform()217   private synchronized void view_transform(){
218     shp tmp;
219     int count = shp3d.size();
220     for(int i=0; i<count; i++) {
221         tmp = (shp)shp3d.elementAt(i);
222         if (tmp.isShow() == show)
223             tmp.transform2D(VMatrix3D, center_of_rotation, this);
224     }
225 
226     if (!DRAFTMODE)
227         qsort.sort(shp3d);
228   }
229 
add3D(Object obj)230   public int add3D(Object obj){
231     id_obj3d++;
232     ((_Object)obj).set_Id(id_obj3d+"");
233 
234     try {
235     ((_Object)obj).reset(true);
236     } catch(Exception e1) {
237         error(e1);
238         return -1;
239     }
240 
241     obj3d.addElement(obj);
242 
243     _Object [] o = new _Object[1];
244     o[0] = (_Object)obj;
245 
246     if (obj instanceof _Node)
247         nodes.addToGroup(o);
248     else
249         if (obj instanceof _Element)
250             elements.addToGroup(o);
251         else if (obj instanceof _Geometry)
252             geometry.addToGroup(o);
253         else {
254             treeNode.add(((_Object)obj).get_TreeNode());
255         }
256 
257     Object[] arr = ((_Object)obj).get_Array(this);
258     for(int i=0; i<arr.length; i++){
259       ((shp)arr[i]).transform2D(VMatrix3D, center_of_rotation, this);
260       shp3d.addElement(arr[i]);
261     }
262     return id_obj3d;
263   }
264 
clearTree()265   private void clearTree() {
266       if (pre != null) pre.clearTree();
267   }
268 
showEditPanel(MouseEvent e)269   private void showEditPanel(MouseEvent e) {
270       if (pre != null) pre.showEditPanel(e);
271   }
272 
setPickedObject(int x, int y)273   private void setPickedObject(int x, int y) {
274       int count = obj3d.size();
275       _Object o;
276 
277       for (int i=0; i<count; i++) {
278           o = ((_Object)obj3d.elementAt(i));
279           if (o.isPickPoint(x,y,show,false))
280               o.setSelected(true);
281       }
282 
283       if (pre != null) pre.rescanTree();
284   }
285 
286 
boxSelect(int sx, int sy, int ex, int ey)287   private void boxSelect(int sx, int sy, int ex, int ey) {
288       int count = obj3d.size();
289       _Object o;
290 
291       Rectangle2D r = new Rectangle(sx,sy,ex-sx,ey-sy);
292 
293       for (int i=0; i<count; i++) {
294           o = ((_Object)obj3d.elementAt(i));
295           if (o.isPickPoint(r,show,false))
296               o.setSelected(true);
297       }
298 
299       if (pre != null) pre.rescanTree();
300   }
301 
302 
getSelectedObject3D()303   public _Object getSelectedObject3D(){
304       _Object[] obj = getSelectedObjects3D();
305 
306       if(obj.length > 0) return obj[0];
307 
308       return null;
309   }
310 
getSelectedObjects3D()311   public _Object[] getSelectedObjects3D() {
312       Vector v = new Vector();
313       _Object o;
314       _Object[] p;
315 
316       for (int i=0; i<obj3d.size(); i++) {
317           o = ((_Object)obj3d.elementAt(i));
318           if (o.isSelected())
319               v.add(o);
320       }
321 
322       p = new _Object[v.size()];
323       for (int i=0; i<v.size(); i++)
324           p[i] = (_Object)v.elementAt(i);
325 
326       return p;
327   }
328 
getAllObjects3D()329   public _Object[] getAllObjects3D() {
330     _Object[] arr = new _Object[obj3d.size()];
331     for(int i=0; i<obj3d.size(); i++) arr[i] = ((_Object)obj3d.elementAt(i));
332     return arr;
333   }
334 
getAllNodes3D()335   public _Node[] getAllNodes3D() {
336       Vector arr = new Vector();
337       _Object o;
338 
339       for(int i=0; i<obj3d.size(); i++) {
340           o = ((_Object)obj3d.elementAt(i));
341           if (o instanceof _Node)
342               arr.add(o);
343       }
344 
345       // Generate array
346       _Node[] out = new _Node[arr.size()];
347       for (int i=0; i<out.length; i++)
348           out[i] = (_Node)arr.elementAt(i);
349 
350       return out;
351   }
352 
getAllElements3D()353   public _Object[] getAllElements3D() {
354       Vector arr = new Vector();
355       _Object o;
356 
357       for(int i=0; i<obj3d.size(); i++) {
358           o = ((_Object)obj3d.elementAt(i));
359           if (o instanceof _Element)
360               arr.add(o);
361       }
362 
363       // Generate array
364       _Object[] out = new _Object[arr.size()];
365       for (int i=0; i<out.length; i++)
366           out[i] = (_Element)arr.elementAt(i);
367 
368       return out;
369   }
370 
getAllGeometry3D()371   public _Geometry[] getAllGeometry3D() {
372       Vector arr = new Vector();
373       _Object o;
374 
375       for(int i=0; i<obj3d.size(); i++) {
376           o = ((_Object)obj3d.elementAt(i));
377           if (o instanceof _Geometry)
378               arr.add(o);
379       }
380 
381       // Generate array
382       _Geometry[] out = new _Geometry[arr.size()];
383       for (int i=0; i<out.length; i++)
384           out[i] = (_Geometry)arr.elementAt(i);
385 
386       return out;
387   }
388 
getAllGroups3D()389   public _Group[] getAllGroups3D() {
390       Vector arr = new Vector();
391       _Object o;
392 
393       for(int i=0; i<obj3d.size(); i++) {
394           o = ((_Object)obj3d.elementAt(i));
395           if (o instanceof _Group)
396               arr.add(o);
397       }
398 
399       _Group[] out = new _Group[arr.size()];
400       for (int i=0; i<out.length; i++)
401           out[i] = (_Group)arr.elementAt(i);
402 
403       return out;
404   }
405 
406 
remove_all()407   public void remove_all(){
408     id_obj3d=0;
409     obj3d.removeAllElements();
410     shp3d.removeAllElements();
411     treeNode.removeAllChildren();
412     this.createGroups();
413     tree3d.reload();
414     view_grid();
415     repaint();
416     painted=true;
417   }
clearSelectOnAllObjects3D()418   public void clearSelectOnAllObjects3D(){
419     int count = obj3d.size();
420     for(int i=0; i<count; i++) ((_Object)obj3d.elementAt(i)).setSelected(false);
421   }
422 
removeSelectedObjects3D()423   public void removeSelectedObjects3D(){
424       _Object o;
425       _Object[] s;
426       /*
427       treeNode.removeAllChildren();
428       for(int i=0; i<obj3d.size(); i++) {
429           o = (_Object)obj3d.elementAt(i);
430           if(o.isSelected()){
431               for (int j=0; j<obj3d.size(); j++)  // Remove from all groups
432                   if (obj3d.elementAt(j) instanceof _Group)
433                       ((_Group)obj3d.elementAt(j)).removeFromGroup(o);
434               obj3d.removeElementAt(i);
435               i--;
436           } else
437               treeNode.add(((_Object)obj3d.elementAt(i)).get_TreeNode());
438       }
439       tree3d.reload();
440       view_reset();
441 
442       */
443 
444       // Deselect required elements
445       for (int i=0; i<obj3d.size(); i++)
446         ((_Object)obj3d.elementAt(i)).deselectRequiredObjects();
447 
448       // Remove selected objects from all groups
449       s = this.getSelectedObjects3D();
450 
451       for (int i=0; i<obj3d.size(); i++) {
452           o = (_Object)obj3d.elementAt(i);
453           if (o instanceof _Group)
454               ((_Group)o).removeFromGroup(s);
455       }
456 
457       // Remove selected objects from j3d
458       for (int i=0; i< obj3d.size(); i++) {
459           o = (_Object)obj3d.elementAt(i);
460           if (o.isSelected()) {
461               obj3d.remove(i);
462               i--;
463           }
464       }
465 
466       this.tree_reset();
467       this.view_reset();
468 
469   }
470 
duplicate()471   public void duplicate(){
472       int count = obj3d.size();
473       _Object o;
474       Vector tmp = new Vector();
475 
476       for(int i=0; i<count; i++) {
477           o = (_Object)obj3d.elementAt(i);
478           if(o.isSelected()){
479               tmp.add(o);
480           }
481       }
482 
483       for (int i=0; i<tmp.size(); i++)
484           ((_Object)tmp.elementAt(i)).duplicate(this,true);
485 
486       tree3d.reload();
487       view_reset();
488     }
489 
transform3D(Matrix3D m)490   public void transform3D(Matrix3D m){
491     int count = obj3d.size();
492 
493     _Object[] p = getSelectedObjects3D();
494 
495     for (int i=0; i<p.length; i++)
496         if (!p[i].isProcessed()) {
497             p[i].transform3D(m);
498             p[i].setProcessed(true);
499         }
500 
501 
502     for(int i=0; i<count; i++)
503         ((_Object)obj3d.elementAt(i)).setProcessed(false);
504 
505     view_reset();
506     view_repaint();
507     tree_reset();
508 
509   }
510 
tree_reset()511   public void tree_reset(){
512       _Object o;
513 
514       treeNode.removeAllChildren();
515       int count = obj3d.size();
516       for(int i=0; i<count; i++) {
517           o = (_Object)obj3d.elementAt(i);
518           if (o instanceof _Group)
519               if (((_Group)o).isTopgroup())
520                   treeNode.add(o.get_TreeNode());
521       }
522 
523       tree3d.reload();
524   }
525 
view_reset()526   public synchronized void view_reset(){
527     int count = obj3d.size();
528     shp3d.removeAllElements();
529     for(int i=0; i<count; i++){
530       Object[] arr = ((_Object)obj3d.elementAt(i)).get_Array(this);
531       for(int j=0; j<arr.length; j++){
532         ((shp)arr[j]).transform2D(VMatrix3D, center_of_rotation, this);
533         shp3d.addElement(arr[j]);
534       }
535     }
536     view_grid();
537   }
538 
view_repaint()539   public synchronized void view_repaint(){
540     if(painted){
541       painted = false;
542       view_transform();
543       repaint();
544     }
545   }
546 
view_translate(float dx, float dy, float dz)547   public synchronized void view_translate(float dx, float dy, float dz){
548     VMatrix3D.translate(dx,-dy,dz);
549     view_repaint();
550   }
551 
view_xrot(double theta)552   public synchronized void view_xrot(double theta){
553     float dx = VMatrix3D.xo;
554     float dy = VMatrix3D.yo;
555     float dz = VMatrix3D.zo;
556     VMatrix3D.translate(-dx,-dy,-dz);
557     VMatrix3D.xrot(theta);
558     VMatrix3D.translate(dx,dy,dz);
559     view_repaint();
560   }
561 
view_yrot(double theta)562   public synchronized void view_yrot(double theta){
563     float dx = VMatrix3D.xo;
564     float dy = VMatrix3D.yo;
565     float dz = VMatrix3D.zo;
566     VMatrix3D.translate(-dx,-dy,-dz);
567     VMatrix3D.yrot(theta);
568     VMatrix3D.translate(dx,dy,dz);
569     view_repaint();
570   }
571 
view_zrot(double theta)572   public synchronized void view_zrot(double theta){
573     float dx = VMatrix3D.xo;
574     float dy = VMatrix3D.yo;
575     float dz = VMatrix3D.zo;
576     VMatrix3D.translate(-dx,-dy,-dz);
577     VMatrix3D.zrot(theta);
578     VMatrix3D.translate(dx,dy,dz);
579     view_repaint();
580   }
581 
view_top()582   public synchronized void view_top(){
583     VMatrix3D.view_top();
584     view_repaint();
585   }
586 
view_bottom()587   public synchronized void view_bottom(){
588     VMatrix3D.view_bottom();
589     view_repaint();
590   }
591 
view_ne()592   public synchronized void view_ne(){
593     VMatrix3D.view_ne();
594     view_repaint();
595   }
596 
view_sw()597   public synchronized void view_sw(){
598     VMatrix3D.view_sw();
599     view_repaint();
600   }
601 
view_se()602   public synchronized void view_se(){
603     VMatrix3D.view_se();
604     view_repaint();
605   }
606 
view_nw()607   public synchronized void view_nw(){
608     VMatrix3D.view_nw();
609     view_repaint();
610   }
611 
view_left()612   public synchronized void view_left(){
613     VMatrix3D.view_left();
614     view_repaint();
615   }
616 
view_right()617   public synchronized void view_right(){
618     VMatrix3D.view_right();
619     view_repaint();
620   }
621 
view_front()622   public synchronized void view_front(){
623     VMatrix3D.view_front();
624     view_repaint();
625   }
626 
view_back()627   public synchronized void view_back(){
628     VMatrix3D.view_back();
629     view_repaint();
630   }
631 
view_all()632   public synchronized void view_all(){
633 
634       int h = this.getHeight();
635       int w = this.getWidth();
636       shp shp;
637       float[] bound;
638       boolean zoomeable = false;
639 
640       float xmin = java.lang.Float.MAX_VALUE;
641       float ymin = java.lang.Float.MAX_VALUE;
642       float xmax = java.lang.Float.MIN_VALUE;
643       float ymax = java.lang.Float.MIN_VALUE;
644 
645       for(int i=0; i<shp3d.size(); i++){
646           try{
647               shp = (shp)shp3d.elementAt(i);
648 
649               if (!(shp instanceof shpPointGrid) && !(shp instanceof shpOrientedText)) {
650 
651                   bound = shp.get2DBoundaries();
652                   xmax = Math.max(xmax,bound[0]);
653                   xmin = Math.min(xmin,bound[1]);
654                   ymax = Math.max(ymax,bound[2]);
655                   ymin = Math.min(ymin,bound[3]);
656 
657                   zoomeable = true;
658               }
659           }catch(Exception e){}
660       }
661 
662       // Check if there is something to zoom around?
663       if (zoomeable == false)
664           return;
665 
666       double sw = w / (xmax-xmin);
667       double sh = h / (ymax-ymin);
668       double s = 0.8 * Math.min(sh,sw);
669 
670       view_scale((float)s);
671 
672       xmin = java.lang.Float.MAX_VALUE;
673       ymin = java.lang.Float.MAX_VALUE;
674       xmax = java.lang.Float.MIN_VALUE;
675       ymax = java.lang.Float.MIN_VALUE;
676 
677       for(int i=0; i<shp3d.size(); i++){
678           try{
679               shp = (shp)shp3d.elementAt(i);
680 
681               if (!(shp instanceof shpPointGrid) && !(shp instanceof shpOrientedText)) {
682                   bound = shp.get2DBoundaries();
683                   xmax = Math.max(xmax,bound[0]);
684                   xmin = Math.min(xmin,bound[1]);
685                   ymax = Math.max(ymax,bound[2]);
686                   ymin = Math.min(ymin,bound[3]);
687               }
688           }catch(Exception e){}
689       }
690 
691       int mx = (int)((w / 2.0f) - (xmax+xmin) / 2.0f);
692       int my = (int)((h / 2.0f) - (ymax+ymin) / 2.0f);
693 
694       this.view_translate(mx,-my,0);
695 
696       painted = true;
697       view_repaint();
698 
699   }
700 
showhide()701   public synchronized boolean showhide() {
702 
703       _Object[] obj = this.getSelectedObjects3D();
704       if (obj.length != 0)
705           for (int i=0; i<obj.length; i++)
706               obj[i].setShow(!show);
707       else
708           show = !show;
709 
710       clearSelectOnAllObjects3D();
711       if (pre != null) pre.clearTree();
712       view_reset();
713       view_repaint();
714 
715       return show;
716   }
717 
718 
view_grid()719   public synchronized void view_grid(){
720     for(int i=0; i<shp3d.size(); i++){
721       if(shp3d.elementAt(i) instanceof shpPointGrid){
722         shp3d.removeElementAt(i);
723         i--;
724       }
725     }
726     if(GRIDMODE)for(float i=LIMITS[0]; i<=LIMITS[2]; i+=GRIDSIZE){
727       for(float j=LIMITS[1]; j<=LIMITS[3]; j+=GRIDSIZE){
728         float x,y,z;
729         if(GRIDPLANE==GRIDPLANE_XY){
730           x=i;
731           y=j;
732           z=GRIDLEVEL;
733         }else if(GRIDPLANE==GRIDPLANE_YZ){
734           x=GRIDLEVEL;
735           y=j;
736           z=i;
737         }else{
738           x=i;
739           y=GRIDLEVEL;
740           z=j;
741         }
742         shpPointGrid gp = new shpPointGrid(x,y,z,GRIDCOLOR);
743         gp.setShow(show);
744         gp.transform2D(VMatrix3D, center_of_rotation, this);
745         shp3d.addElement(gp);
746       }
747     }
748     shpXYZ xyz = new shpXYZ();
749     xyz.transform2D(VMatrix3D, center_of_rotation, this);
750     shp3d.addElement(xyz);
751     view_repaint();
752   }
753 
view_scale(float s)754   public synchronized void view_scale(float s){
755 
756     float dx = VMatrix3D.xo;
757     float dy = VMatrix3D.yo;
758     float dz = VMatrix3D.zo;
759     VMatrix3D.translate(-dx,-dy,-dz);
760     VMatrix3D.scale(s);
761     VMatrix3D.translate(dx,dy,dz);
762     view_repaint();
763   }
764 
this_mouseDragged(MouseEvent e)765   void this_mouseDragged(MouseEvent e) {
766       int mod = e.getModifiers();
767 
768       if(((mod & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) && ((mod & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) && ((mod & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK)) {
769           this.view_zrot(e.getY()-y);
770       } else
771           if (((mod & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) && ((mod & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK)) {
772               this.view_yrot(e.getX()-x);
773               this.view_xrot(e.getY()-y);
774           }else
775               if(((mod & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) && ((mod & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK)){
776                   if(y-e.getY()>0) this.view_scale(0.9f);
777                   if(y-e.getY()<0) this.view_scale(1.1f);
778               }else
779                   if(((mod & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)) {
780                       this.view_translate(e.getX()-x,y-e.getY(),0);
781                   } else
782                       if (((mod & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) && DRAG == true)
783                           view_repaint();
784 
785       x=e.getX();
786       y=e.getY();
787   }
788 
save(String file)789   public void save(String file) {
790     try {
791       FileOutputStream ostream = new FileOutputStream(file);
792       ObjectOutputStream p = new ObjectOutputStream(ostream);
793       p.writeObject(VMatrix3D);
794       for(int i=0; i<obj3d.size(); i++) p.writeObject(obj3d.elementAt(i));
795       p.flush();
796       ostream.close();
797     }catch(FileNotFoundException fnfe) {
798       System.err.println("File not found: " + file);
799     }catch(IOException ioe){ System.err.println("Could not write file: " + file); ioe.printStackTrace();}
800   }
801 
save(ObjectOutputStream oos)802   public void save(ObjectOutputStream oos) {
803     try {
804       oos.writeObject(VMatrix3D);
805       for(int i=0; i<obj3d.size(); i++) oos.writeObject(obj3d.elementAt(i));
806     }catch(Exception e) { e.printStackTrace();}
807   }
808 
open(String file)809   public void open(String file) {
810     try {
811       FileInputStream istream = new FileInputStream(file);
812       ObjectInputStream p = new ObjectInputStream(istream);
813       remove_all();
814       VMatrix3D=(Matrix3D)p.readObject();
815       Object obj;
816       try { while((obj = p.readObject())!=null) add3D(obj); }catch(EOFException eof){}
817       view_all();
818       p.close();
819       istream.close();
820     }catch(Exception ioe){ System.err.println("Could not open file: " + file+"\n"+ioe); }
821   }
822 
open(ObjectInputStream ois)823   public void open(ObjectInputStream ois) {
824     try {
825       //remove_all();
826       VMatrix3D=(Matrix3D)ois.readObject();
827       Object obj;
828       try { while((obj = ois.readObject())!=null) add3D(obj); }catch(EOFException eof){}
829       view_all();
830     }catch(Exception e) { e.printStackTrace();}
831   }
832 
save_Image(String file)833   public void save_Image(String file) {
834       byte[] pngbytes;
835       PngEncoderB png;
836 
837     try {
838       if(file.toLowerCase().indexOf(".png")==-1) file+=".png";
839       FileOutputStream out = new FileOutputStream(file);
840       BufferedImage image = (BufferedImage)createImage(getWidth(), getHeight());
841       Graphics g = image.getGraphics();
842       paint(g);
843 
844       png = new PngEncoderB(image,false,0,5); // Encode at level 5 compression
845       pngbytes = png.pngEncode();
846       out.write(pngbytes);
847 
848       g.dispose();
849       out.flush();
850       out.close();
851     }catch(FileNotFoundException fnfe) {
852       System.err.println("File not found: " + file);
853     }catch(IOException ioe){ System.err.println("Could not write file: " + file); }
854   }
855 
open_stl(String file)856   public void open_stl(String file){
857     this.add3D(new _Stl(file));
858     view_repaint();
859   }
860 
error(Object st)861   public void error(Object st){
862     JOptionPane.showMessageDialog(null,"Error: "+st,"Error!",JOptionPane.ERROR_MESSAGE);
863     if(st instanceof Exception) ((Exception)st).printStackTrace(); else System.out.println("Error: "+st);
864   }
865 
rebuild()866   public void rebuild() {
867       int count = obj3d.size();
868       for (int i=0; i<4; i++)
869           for(int j=0; j<count; j++){
870               _Object obj = ((_Object)obj3d.elementAt(j));
871               if (obj.getGeometryType() == i)
872                   obj.reset(true);
873           }
874       view_reset();
875       view_repaint();
876       tree_reset();
877   }
878 
setCenterOfRotation()879   public void setCenterOfRotation() {
880       _Object obj = this.getSelectedObject3D();
881       if (obj != null) {
882 
883          center_of_rotation = obj.getCenter();
884          center_of_rotation.sub(new Vector3D(0.0f,0.0f,0.0f),center_of_rotation);
885 
886       }
887       view_reset();
888       view_repaint();
889   }
890 
addBorderObjects()891   public void addBorderObjects() {
892       _Object[] obj = this.getSelectedObjects3D();
893       _Object[] add = null;
894       if (obj != null)
895           for (int i=0; i<obj.length; i++) {
896               add = obj[i].getBorderObjects();
897                   if (add != null)
898                       for (int j=0; j<add.length; j++)
899                           this.add3D(add[j]);
900           }
901       view_reset();
902       view_repaint();
903   }
904 
intersectObjects()905   public void intersectObjects() throws IllegalStateException {
906       _Object[] obj = this.getSelectedObjects3D();
907       _NurbCurve[] o = new _NurbCurve[obj.length];
908       _NurbSurface[] s = new _NurbSurface[obj.length];
909       _Point[] p = null;
910       _NurbCurve[] c = null;
911       int j = 0, k = 0;
912 
913       if (obj != null)
914           if (obj.length > 1)
915           for (int i=0; i<obj.length; i++) {
916               if (obj[i] instanceof _NurbCurve && !lib3D.contains(o,obj[i]))
917                   o[j++] = (_NurbCurve)obj[i];
918               if (obj[i] instanceof _NurbSurface && !lib3D.contains(s,obj[i]))
919                   s[k++] = (_NurbSurface)obj[i];
920           }
921 
922       if (j == 2) {
923           try {
924               p = lib3D.intersectCurves(o[0], o[1], GeometricTolerance);
925           } catch (IllegalStateException e) {
926               throw new IllegalStateException("No intersection found");
927           }
928       }
929       else
930           if (j == 1 && k == 1) {
931               try {
932                   p = lib3D.intersectCurveWithSurface(o[0], s[0], GeometricTolerance);
933               } catch (IllegalStateException e) {
934                   throw new IllegalStateException("No intersection found");
935               }
936           }
937           else
938               if (k == 2) {
939                   try {
940                       c = lib3D.intersectSurfaces(s[0], s[1]);
941                   } catch (IllegalStateException e) {
942                       throw new IllegalStateException("No intersection found");
943                   }
944               }
945 
946       if (p != null)
947           if (p.length > 0)
948               for (int i=0; i<p.length; i++)
949                   this.add3D(p[i]);
950 
951       if (c != null)
952           if (c.length > 0)
953               for (int i=0; i<c.length; i++)
954                   this.add3D(c[i]);
955 
956       view_reset();
957       view_repaint();
958   }
959 
960 
961 
projectObjects()962 public void projectObjects() throws IllegalStateException {
963       _Object[] obj = this.getSelectedObjects3D();
964       _Point[] ps = new _Point[obj.length];
965       _NurbCurve[] o = new _NurbCurve[obj.length];
966       _NurbSurface[] s = new _NurbSurface[obj.length];
967       _Point[] p = null;
968       _NurbCurve[] c = null;
969       int j = 0, k = 0, l = 0;
970 
971       if (obj != null)
972           if (obj.length > 1)
973           for (int i=0; i<obj.length; i++) {
974               if (obj[i] instanceof _Point && !lib3D.contains(ps,obj[i]))
975                   ps[l++] = (_Point)obj[i];
976               if (obj[i] instanceof _NurbCurve && !lib3D.contains(o,obj[i]))
977                   o[j++] = (_NurbCurve)obj[i];
978               if (obj[i] instanceof _NurbSurface && !lib3D.contains(s,obj[i]))
979                   s[k++] = (_NurbSurface)obj[i];
980           }
981 
982       if (l == 1 && j == 1) {
983           try {
984               p = lib3D.projectPointOntoCurve(ps[0], o[0], GeometricTolerance);
985           } catch (IllegalStateException e) {
986               throw new IllegalStateException("No projection found");
987           }
988       }
989       else
990           if (l == 1 && k == 1) {
991               try {
992                   p = lib3D.projectPointOntoSurface(ps[0], s[0], GeometricTolerance);
993               } catch (IllegalStateException e) {
994                   throw new IllegalStateException("No projection found");
995               }
996           }
997           else
998           if (j == 1 && k == 1) {
999               try {
1000                   // c = lib3D.projectCurveOntoSurface(o[0], s[0]);
1001               } catch (IllegalStateException e) {
1002                   throw new IllegalStateException("No projection found");
1003               }
1004           }
1005 
1006       if (p != null)
1007           if (p.length > 0)
1008               for (int i=0; i<p.length; i++)
1009                   this.add3D(p[i]);
1010 
1011       if (c != null)
1012           if (c.length > 0)
1013               for (int i=0; i<c.length; i++)
1014                   this.add3D(c[i]);
1015 
1016       view_reset();
1017       view_repaint();
1018   }
1019 
1020 
1021 
breakObjects()1022 public void breakObjects() throws IllegalStateException {
1023       _Object[] obj = this.getSelectedObjects3D();
1024       _Point[] ps = new _Point[obj.length];
1025       _NurbCurve[] o = new _NurbCurve[obj.length];
1026       _NurbSurface[] s = new _NurbSurface[obj.length];
1027       _Point[] p = null;
1028       _NurbCurve[] c = null;
1029       int j = 0, k = 0, l = 0;
1030 
1031       if (obj != null)
1032           if (obj.length > 1)
1033           for (int i=0; i<obj.length; i++) {
1034               if (obj[i] instanceof _Point && !lib3D.contains(ps,obj[i]))
1035                   ps[l++] = (_Point)obj[i];
1036               if (obj[i] instanceof _NurbCurve && !lib3D.contains(o,obj[i]))
1037                   o[j++] = (_NurbCurve)obj[i];
1038               if (obj[i] instanceof _NurbSurface && !lib3D.contains(s,obj[i]))
1039                   s[k++] = (_NurbSurface)obj[i];
1040           }
1041 
1042       if (l == 1 && j == 1) {
1043           try {
1044               c = lib3D.breakCurve(o[0], ps[0], GeometricTolerance);
1045           } catch (IllegalStateException e) {
1046               throw new IllegalStateException("No break possible");
1047           }
1048       }
1049       else
1050           if (l == 1 && k == 1)
1051               throw new IllegalStateException("Surface break not supported yet");
1052 
1053           else
1054               if (j == 1 && k == 1)
1055                   throw new IllegalStateException("Surface break using a curve is not supported yet");
1056 
1057 
1058       if (p != null)
1059           if (p.length > 0)
1060               for (int i=0; i<p.length; i++)
1061                   this.add3D(p[i]);
1062 
1063       if (c != null)
1064           if (c.length > 0)
1065               for (int i=0; i<c.length; i++)
1066                   this.add3D(c[i]);
1067 
1068       view_reset();
1069       view_repaint();
1070   }
1071 
requestAction()1072   private void requestAction() {
1073 
1074       if (pre != null)
1075           pre.requestAction();
1076   }
1077 
1078 
1079 
getSHOW_ID_NODE()1080 public boolean getSHOW_ID_NODE() {
1081     return SHOW_ID_NODE;
1082 }
1083 
setSHOW_ID_NODE(boolean show_id_node)1084 public void setSHOW_ID_NODE(boolean show_id_node) {
1085     SHOW_ID_NODE = show_id_node;
1086 }
1087 
getBGCOLOR()1088 public Color getBGCOLOR() {
1089     return BGCOLOR;
1090 }
1091 
setBGCOLOR(Color bgcolor)1092 public void setBGCOLOR(Color bgcolor) {
1093     BGCOLOR = bgcolor;
1094 }
1095 
getDRAFTMODE()1096 public boolean getDRAFTMODE() {
1097     return DRAFTMODE;
1098 }
1099 
setDRAFTMODE(boolean draftmode)1100 public void setDRAFTMODE(boolean draftmode) {
1101     DRAFTMODE = draftmode;
1102 }
1103 
getGRAPHICSMODE()1104 public byte getGRAPHICSMODE() {
1105     return GRAPHICSMODE;
1106 }
1107 
setGRAPHICSMODE(byte graphicsmode)1108 public void setGRAPHICSMODE(byte graphicsmode) {
1109     GRAPHICSMODE = graphicsmode;
1110 }
1111 
getGRIDCOLOR()1112 public Color getGRIDCOLOR() {
1113     return GRIDCOLOR;
1114 }
1115 
setGRIDCOLOR(Color gridcolor)1116 public void setGRIDCOLOR(Color gridcolor) {
1117     GRIDCOLOR = gridcolor;
1118 }
1119 
getGRIDLEVEL()1120 public float getGRIDLEVEL() {
1121     return GRIDLEVEL;
1122 }
1123 
setGRIDLEVEL(float gridlevel)1124 public void setGRIDLEVEL(float gridlevel) {
1125     GRIDLEVEL = gridlevel;
1126 }
1127 
getGRIDMODE()1128 public boolean getGRIDMODE() {
1129     return GRIDMODE;
1130 }
1131 
setGRIDMODE(boolean gridmode)1132 public void setGRIDMODE(boolean gridmode) {
1133     GRIDMODE = gridmode;
1134 }
1135 
getGRIDPLANE()1136 public byte getGRIDPLANE() {
1137     return GRIDPLANE;
1138 }
1139 
setGRIDPLANE(byte gridplane)1140 public void setGRIDPLANE(byte gridplane) {
1141     GRIDPLANE = gridplane;
1142 }
1143 
getGRIDSIZE()1144 public float getGRIDSIZE() {
1145     return GRIDSIZE;
1146 }
1147 
setGRIDSIZE(float gridsize)1148 public void setGRIDSIZE(float gridsize) {
1149     GRIDSIZE = gridsize;
1150 }
1151 
getLIMITS()1152 public float[] getLIMITS() {
1153     return LIMITS;
1154 }
1155 
setLIMITS(float[] limits)1156 public void setLIMITS(float[] limits) {
1157     LIMITS = limits;
1158 }
1159 
getNODE_MERGE_TOLERANCE()1160 public float getNODE_MERGE_TOLERANCE() {
1161     return NODE_MERGE_TOLERANCE;
1162 }
1163 
setNODE_MERGE_TOLERANCE(float node_merge_tolerance)1164 public void setNODE_MERGE_TOLERANCE(float node_merge_tolerance) {
1165     NODE_MERGE_TOLERANCE = node_merge_tolerance;
1166 }
1167 
getNODESIZE()1168 public int getNODESIZE() {
1169     return NODESIZE;
1170 }
1171 
setNODESIZE(int nodesize)1172 public void setNODESIZE(int nodesize) {
1173     NODESIZE = nodesize;
1174 }
1175 
getPOINTSIZE()1176 public int getPOINTSIZE() {
1177     return POINTSIZE;
1178 }
1179 
setPOINTSIZE(int pointsize)1180 public void setPOINTSIZE(int pointsize) {
1181     POINTSIZE = pointsize;
1182 }
1183 
getRENDERMODE()1184 public byte getRENDERMODE() {
1185     return RENDERMODE;
1186 }
1187 
setRENDERMODE(byte rendermode)1188 public void setRENDERMODE(byte rendermode) {
1189     RENDERMODE = rendermode;
1190 }
1191 
getSHOW_ID_CONSTRAINTS()1192 public boolean getSHOW_ID_CONSTRAINTS() {
1193     return SHOW_ID_CONSTRAINTS;
1194 }
1195 
setSHOW_ID_CONSTRAINTS(boolean show_id_constraints)1196 public void setSHOW_ID_CONSTRAINTS(boolean show_id_constraints) {
1197     SHOW_ID_CONSTRAINTS = show_id_constraints;
1198 }
1199 
getSHOW_ID_ELEMENT()1200 public boolean getSHOW_ID_ELEMENT() {
1201     return SHOW_ID_ELEMENT;
1202 }
1203 
setSHOW_ID_ELEMENT(boolean show_id_element)1204 public void setSHOW_ID_ELEMENT(boolean show_id_element) {
1205     SHOW_ID_ELEMENT = show_id_element;
1206 }
1207 
getSHOW_ID_LOADS()1208 public boolean getSHOW_ID_LOADS() {
1209     return SHOW_ID_LOADS;
1210 }
1211 
setSHOW_ID_LOADS(boolean show_id_loads)1212 public void setSHOW_ID_LOADS(boolean show_id_loads) {
1213     SHOW_ID_LOADS = show_id_loads;
1214 }
1215 
getSHOW_ID_MATERIALS()1216 public boolean getSHOW_ID_MATERIALS() {
1217     return SHOW_ID_MATERIALS;
1218 }
1219 
setSHOW_ID_MATERIALS(boolean show_id_materials)1220 public void setSHOW_ID_MATERIALS(boolean show_id_materials) {
1221     SHOW_ID_MATERIALS = show_id_materials;
1222 }
1223 
getSHOW_ID_TRACKERS()1224 public boolean getSHOW_ID_TRACKERS() {
1225     return SHOW_ID_TRACKERS;
1226 }
1227 
setSHOW_ID_TRACKERS(boolean show_id_trackers)1228 public void setSHOW_ID_TRACKERS(boolean show_id_trackers) {
1229     SHOW_ID_TRACKERS = show_id_trackers;
1230 }
1231 
getSTLCOLOR()1232 public Color getSTLCOLOR() {
1233     return STLCOLOR;
1234 }
1235 
setSTLCOLOR(Color stlcolor)1236 public void setSTLCOLOR(Color stlcolor) {
1237     STLCOLOR = stlcolor;
1238 }
1239 
getVMatrix3D()1240 public Matrix3D getVMatrix3D() {
1241     return VMatrix3D;
1242 }
1243 
setVMatrix3D(Matrix3D matrix3D)1244 public void setVMatrix3D(Matrix3D matrix3D) {
1245     VMatrix3D = matrix3D;
1246 }
1247 
getTree3d()1248 public DefaultTreeModel getTree3d() {
1249     return tree3d;
1250 }
1251 
getGeometricTolerance()1252 public float getGeometricTolerance() {
1253     return GeometricTolerance;
1254 }
1255 
setGeometricTolerance(float geometricTolerance)1256 public void setGeometricTolerance(float geometricTolerance) {
1257     GeometricTolerance = geometricTolerance;
1258 }
1259 
1260 
1261 
replaceAllInstancesOf(_Object o, _Object replacement)1262 public void replaceAllInstancesOf(_Object o, _Object replacement) {
1263 
1264     // Replace all instances of object o with replacement
1265     for (int i=0; i<obj3d.size(); i++)
1266       ((_Object)obj3d.elementAt(i)).replaceObjectWith(o,replacement);
1267 
1268     // Remove object o from database
1269     obj3d.remove(o);
1270 
1271 }
1272 
1273 
1274 }