001/*
002 *   GeomSSEntity  -- Interface and implementation in common to all Entitys that contain GeomSS geometry.
003 *
004 *   Copyright (C) 2010-2016, Joseph A. Huwaldt. All rights reserved.
005 *   
006 *   This library is free software; you can redistribute it and/or
007 *   modify it under the terms of the GNU Lesser General Public
008 *   License as published by the Free Software Foundation; either
009 *   version 2.1 of the License, or (at your option) any later version.
010 *   
011 *   This library is distributed in the hope that it will be useful,
012 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *   Lesser General Public License for more details.
015 *
016 *   You should have received a copy of the GNU Lesser General Public License
017 *   along with this program; if not, write to the Free Software
018 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
020 */
021package geomss.geom.reader.iges;
022
023import geomss.geom.GTransform;
024import geomss.geom.GeomElement;
025import geomss.geom.Transformable;
026import java.io.IOException;
027
028/**
029 * The interface an implementation in common to all Entity objects that contain GeomSS
030 * geometry.
031 * 
032 * <p> Modified by: Joseph A. Huwaldt </p>
033 * 
034 * @author Joseph A. Huwaldt
035 * @version September 13, 2016
036 */
037public abstract class GeomSSEntity extends Entity {
038
039    protected static final Double ZERO = (double)0;
040    protected static final Double ONE = (double)1;
041
042    private boolean isUsedInList = false;
043
044    /**
045     * Default constructor.
046     *
047     * @param p  part to which this entity is contained
048     * @param de Directory Entry for this entity
049     */
050    public GeomSSEntity(Part p, DirEntry de) {
051        super(p, de);
052    }
053
054    /**
055     * The GeomSS geometry element is created from the IGES parameters when this method is
056     * called.
057     */
058    abstract void createGeometry() throws IOException;
059
060    /**
061     * Return a reference to the Transformable GeomElement element contained in this IGES
062     * Entity.
063     *
064     * @return A reference to the Transformable GeomElement element contained in this IGES
065     *         Entity.
066     */
067    protected abstract Transformable getGeomElement();
068
069    /**
070     * Return a reference to the GeomElement element contained in this IGES Entity.
071     *
072     * @param m the current transformation matrix to apply to the entity.
073     * @return A reference to the GeomElement element contained in this IGES Entity
074     */
075    public GeomElement getGeomElement(GTransform m) {
076        //  Get the geometry element from the sub-class.
077        Transformable transElement = getGeomElement();
078        GeomElement element = transElement;
079        if (element == null)
080            return null;
081
082        //  Apply any rotation matrix.
083        m = getMatrix(m);
084        if (!m.equals(GTransform.IDENTITY)) {
085            //  Apply transformation.
086            element = transElement.getTransformed(m);
087        }
088
089        // Attach some IGES meta-data.
090        applyMetaData(element);
091
092        return element;
093    }
094
095    /**
096     * Method used to apply IGES meta-data to GeomSS elements. This implementation applies
097     * the entity type, form, and header string as: IGESType, IGESForm and IGESHeader
098     * respectively.
099     *
100     * @param element The element to set the meta-data on.
101     */
102    protected void applyMetaData(GeomElement element) {
103
104        DirEntry DE = getDirectoryEntry();
105        String label = DE.getLabel();
106        if (!label.equals(""))
107            element.setName(label);
108        element.putUserData("IGESType", DE.getType());
109        element.putUserData("IGESForm", DE.getForm());
110        element.putUserData("IGESHeader", getHeader());
111
112    }
113
114    /**
115     * Returns true if this Entity is being used in another GeomSSEntity, otherwise
116     * returns false.
117     *
118     * @return true if this Entity is being used in another GeomSSEntity
119     */
120    public boolean isUsedInList() {
121        return isUsedInList;
122    }
123
124    /**
125     * Set if this GeomSSEntity is being used by another entity or not.
126     */
127    void setUsedInList(boolean used) {
128        isUsedInList = used;
129    }
130
131}