001/*
002 *   Entity106_13_3DVectorString  -- Entity that represents a string of of 3D points with associated vectors.
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.GeomPoint;
027import geomss.geom.GeomVector;
028import java.io.IOException;
029import java.io.PrintWriter;
030
031/**
032 * <b><i>COPIOUS DATA ENTITY - LINEAR STRING WITH VECTORS</i></b> - This entity defines an
033 * ordered set of 3D coordinate points with associated 3D vectors. Form 13 indicates that
034 * all the points are 3D, ordered, and have associated 3D vectors.
035 *
036 * <p>
037 * This entity, when read from an IGES file, is converted to a list of vectors
038 * (GeomList&lt;GeomVector&gt;). Each vector has the origin set to the appropriate point.
039 * </p>
040 *
041 * <p> Modified by: Joseph A. Huwaldt </p>
042 * 
043 * @author Joseph A. Huwaldt, Date: March 14, 2013
044 * @version February 22, 2025
045 * @see Entity106_CopiousData
046 */
047public class Entity106_13_3DVectorString extends Entity106_CopiousData {
048
049    /**
050     * Default constructor.
051     *
052     * @param p  part to which this entity is contained
053     * @param de Directory Entry for this entity
054     */
055    public Entity106_13_3DVectorString(Part p, DirEntry de) {
056        super(p, de);
057
058        if (Constants.DEBUG) {
059            System.out.println("Entity106_13 constructor called");
060        }
061    }
062
063    /**
064     * Create this entity from the specified GeomSS geometry element.
065     *
066     * @param part  The Part in which this entity is contained.
067     * @param DEnum The line count from the start of the Directory Entry Section for this
068     *              entry (odd number).
069     * @param geom  The GeomSS list of GeomVector geometry to return an Entity for.
070     */
071    public Entity106_13_3DVectorString(Part part, int DEnum, GeomList geom) {
072        super(part, new DirEntry(106, 13, DEnum, 0, geom.getName()));
073        ip = 2;
074        points.addAll(geom);
075    }
076
077    /**
078     * Returns <code>true</code> if the Entity can be written to an exchange file.
079     *
080     * @return true
081     */
082    @Override
083    public boolean canWrite() {
084        return true;
085    }
086
087    /**
088     * Write this entities parameter data to the specified PrintWriter.
089     *
090     * @param writer The PrintWriter to write the parameter data for this entity to.
091     * @param PDnum  The starting Parameter Data row index number.
092     * @return The Parameter Data row index number for the next row.
093     * @throws java.io.IOException if the parameter data could not be written out.
094     */
095    @Override
096    public int write(PrintWriter writer, int PDnum) throws IOException {
097
098        //  Build up the parameter data string.
099        GeomList<GeomVector> points2 = (GeomList<GeomVector>)points.to(Constants.unit);
100        StringBuilder buffer = new StringBuilder();
101        buffer.append(106);                 buffer.append(Constants.Delim);
102        buffer.append(ip);                  buffer.append(Constants.Delim);
103        int n = points2.size();
104        buffer.append(n);                   buffer.append(Constants.Delim);
105        for (GeomVector v : points2) {
106            GeomPoint p = v.getOrigin();
107            appendPoint3(buffer, p);
108            double i = v.getValue(0);
109            double j = v.getValue(1);
110            double k = v.getValue(2);
111            buffer.append(i);               buffer.append(Constants.Delim);
112            buffer.append(j);               buffer.append(Constants.Delim);
113            buffer.append(k);               buffer.append(Constants.Delim);
114        }
115        buffer.setLength(buffer.length() - 1);  //  Replace Delim with Term for last entry.
116        buffer.append(Constants.Term);
117
118        //  Write it out.
119        int oldPDnum = PDnum;
120        PDnum = Constants.writeSection(writer, PDnum, Constants.makeSequenceNumber(getDENum()),
121                'P', buffer);
122
123        //  Store the PD line number and line count in the directory entry.
124        getDirectoryEntry().setPDNumber(oldPDnum, PDnum - oldPDnum);
125
126        return PDnum;
127    }
128
129    /**
130     * Returns a short String describing this Entity object's type.
131     *
132     * @return A short String describing this Entity object's type.
133     */
134    @Override
135    public String getTypeString() {
136        return "Entity106_13 - Copious Data - String of 3D Vectors";
137    }
138
139}