001/*
002 *   Entity106_CopiousData  -- Entity that represents a list of data points, 2D, 3D, closed or open.
003 *
004 *   Copyright (C) 2010-2025, 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 *
021 *   Based on, but heavily modified from, IGESView ( http://ts.nist.gov/Standards/IGES/igesTools.cfm )
022 */
023package geomss.geom.reader.iges;
024
025import geomss.geom.GeomList;
026import geomss.geom.Point;
027import geomss.geom.Transformable;
028import geomss.geom.Vector;
029import java.io.IOException;
030import java.io.RandomAccessFile;
031import java.text.MessageFormat;
032import javax.measure.quantity.Length;
033
034/**
035 * <b><i>COPIOUS DATA ENTITY</i></b> - This entity is a list of data points, either 2D or
036 * 3D, closed or open.
037 *
038 * <p>
039 * All supported forms of this entity, when read from an IGES file, are converted to a
040 * list of points or vectors as appropriate.
041 * </p>
042 *
043 * <p> Modified by: Joseph A. Huwaldt </p>
044 *
045 * @author JDN, AED, Version 1.0
046 * @version February 22, 2025
047 */
048public abstract class Entity106_CopiousData extends GeomSSEntity {
049
050    protected int ip = 2;                 // Interpretation flag
051    protected GeomList points = GeomList.newInstance(); //  Map all forms to a list of 3D points.
052
053    /**
054     * Default constructor.
055     *
056     * @param p  part to which this entity is contained
057     * @param de Directory Entry for this entity
058     */
059    public Entity106_CopiousData(Part p, DirEntry de) {
060        super(p, de);
061    }
062
063    /**
064     * Checks to see if the entity is correct. The following restrictions are imposed:
065     *
066     * - The Label Display Pointer shall be 0
067     */
068    @Override
069    public void check() {
070        DirEntry DE = getDirectoryEntry();
071
072        // DE Label Display shall be 0
073        if (DE.getLblDsp() != 0) {
074            String msg = MessageFormat.format(RESOURCES.getString("labelDisplay"), DE.getLblDsp());
075            addErrorMessage(getWarningString(msg));
076        }
077    }
078
079    /**
080     * Read the Parameter Data from the String read in by the superclass.
081     *
082     * @param in input file
083     * @throws java.io.IOException if the parameter data could not be read in.
084     */
085    @Override
086    public void read(RandomAccessFile in) throws IOException {
087
088        super.read(in);
089        String s = getPDString();
090
091        if (Constants.DEBUG) {
092            System.out.println("PD String = \"" + s + "\"");
093        }
094
095        ip = getInt(s);
096        int n = getInt(s);
097
098        switch (ip) {
099            case 1: // x,y pairs, common z
100                double z = getReal(s);
101                //if (Math.abs(z) < Constants.Grain)    z = 0;
102
103                for (int idx = 0; idx < n; idx++) {
104                    Point p = getPoint2(s);
105                    points.add(p);
106                }
107                break;
108
109            case 2: // x,y,z coordinates
110                for (int idx = 0; idx < n; idx++) {
111                    Point p = getPoint3(s);
112                    points.add(p);
113                }
114                break;
115
116            case 3: // x,y,z coordinates and i,j,k vectors
117                for (int idx = 0; idx < n; idx++) {
118                    Point p = getPoint3(s);
119
120                    double i = getReal(s);
121                    double j = getReal(s);
122                    double k = getReal(s);
123
124                    Vector<Length> v = Vector.valueOf(Constants.unit, i, j, k);
125                    v.setOrigin(p);
126                    points.add(v);
127                }
128                break;
129
130            default:
131                throw new IOException(MessageFormat.format(RESOURCES.getString("unknownDataTypeCode"),
132                        ip, getTypeString()));
133        }
134
135        super.read_additional();
136    }
137
138    /**
139     * The GeomSS geometry element is created from the IGES parameters when this method is
140     * called.
141     */
142    @Override
143    void createGeometry() throws IOException {
144        //  Already done in read().
145    }
146
147    /**
148     * Return a reference to the Transformable GeomElement contained in this IGES Entity.
149     *
150     * @return A reference to the Transformable GeomElement contained in this IGES Entity.
151     */
152    @Override
153    protected Transformable getGeomElement() {
154        return points;
155    }
156
157    /**
158     * Dump to String.
159     *
160     * @return String containing the resulting text.
161     */
162    @Override
163    public String toString() {
164        StringBuilder outStr = new StringBuilder(super.toString());
165        outStr.append("\n");
166
167        int n = points.size();
168        outStr.append("ip = "); outStr.append(ip);  outStr.append("\n");
169        outStr.append("n  = "); outStr.append(n);   outStr.append("\n");
170
171        for (int i = 0; i < n; i++) {
172            outStr.append(i);
173            outStr.append(": ");
174            outStr.append(points.get(i).toString());
175            outStr.append("\n");
176        }
177
178        return outStr.toString();
179    }
180
181}