001/*
002 *   Entity308_Subfigure  -- Entity that represents a subfigure defined as a collection of entities.
003 *
004 *   Copyright (C) 2012-2016, Joseph A. Huwaldt.
005 *   All rights reserved.
006 *   
007 *   This library is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *   
012 *   This library is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 *   Lesser General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public License
018 *   along with this program; if not, write to the Free Software
019 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
021 */
022package geomss.geom.reader.iges;
023
024import geomss.geom.*;
025import java.io.IOException;
026import java.io.RandomAccessFile;
027import java.util.ArrayList;
028import java.util.List;
029
030/**
031 * <b><i>SUBFIGURE DEFINITION ENTITY</i></b> - This entity supports multiple instantiation
032 * of a defined collection of entities.
033 *
034 * <p>
035 * This entity, when read from an IGES file, is converted to a list of geometry objects.
036 * This entity type can not be written out to an IGES file.
037 * </p>
038 *
039 * <p> Modified by: Joseph A. Huwaldt </p>
040 *
041 * @author Joseph A. Huwaldt, Date: January 31, 2012
042 * @version September 13, 2016
043 */
044public class Entity308_Subfigure extends GeomSSEntity {
045
046    protected List<Integer> pointers = new ArrayList();     //  List of entity DE numbers.
047
048    protected GeometryList geom = GeomList.newInstance();   //  List of GeomSS geometry objects.
049
050    /**
051     * Default constructor.
052     *
053     * @param p  part to which this entity is contained
054     * @param de Directory Entry for this entity
055     */
056    public Entity308_Subfigure(Part p, DirEntry de) {
057        super(p, de);
058
059        if (Constants.DEBUG) {
060            System.out.println("Entity308 constructor called");
061        }
062    }
063
064    /**
065     * Checks to see if the entity is correct. No restrictions are imposed.
066     */
067    @Override
068    public void check() {
069    }
070
071    /**
072     * Read the Parameter Data from the String read in by the superclass.
073     *
074     * @param in input file
075     * @throws java.io.IOException
076     */
077    @Override
078    public void read(RandomAccessFile in) throws IOException {
079        super.read(in);
080        String s = getPDString();
081
082        if (Constants.DEBUG) {
083            System.out.println("PD String = \"" + s + "\"");
084        }
085
086        int depth = getInt(s);      //  Ignoring depth for now.
087        String name = getString(s); //  Set the output geometry name to the specified string.
088        geom.setName(name);
089        int n = getInt(s);
090        for (int i = 0; i < n; ++i)
091            pointers.add(getInt(s));
092
093        super.read_additional();
094    }
095
096    /**
097     * The GeomSS geometry element is created from the IGES parameters when this method is
098     * called.
099     */
100    @Override
101    void createGeometry() {
102        Part part = getPart();
103
104        //  Loop over all the entities in this association.
105        int n = pointers.size();
106        for (int i = 0; i < n; ++i) {
107            int deNum = pointers.get(i);
108            Entity entity = part.getEntity(deNum);
109
110            if (entity instanceof GeomSSEntity) {
111                //  Found a GeomSS geometry Entity.
112                GeomSSEntity geomEntity = (GeomSSEntity)entity;
113                geomEntity.setUsedInList(true); //  Indicate that the entity is used by this association.
114                GeomElement element = geomEntity.getGeomElement(GTransform.IDENTITY);
115                geom.add(element);
116            }
117        }
118    }
119
120    /**
121     * Return a reference to the Transformable GeomElement contained in this IGES Entity.
122     *
123     * @return A reference to the Transformable GeomElement contained in this IGES Entity.
124     */
125    @Override
126    protected Transformable getGeomElement() {
127        return geom;
128    }
129
130    /**
131     * Dump to String.
132     *
133     * @return String containing the resulting text.
134     */
135    @Override
136    public String toString() {
137        StringBuilder outStr = new StringBuilder(super.toString());
138        outStr.append("\n");
139
140        int n = pointers.size();
141        outStr.append("n  = "); outStr.append(n);   outStr.append("\n");
142
143        for (int i = 0; i < n; i++) {
144            outStr.append("subfigure(");
145            outStr.append(i);
146            outStr.append(") = ");
147            outStr.append(pointers.get(i));
148            outStr.append("\n");
149        }
150
151        return outStr.toString();
152    }
153
154    /**
155     * Returns a short String describing this Entity object's type.
156     *
157     * @return A short String describing this Entity object's type.
158     */
159    @Override
160    public String getTypeString() {
161        return "Entity308 - Subfigure Definition";
162    }
163
164}