001/*
002 *   Entity106_11_2DLinearString  -- Entity that represents a consecutive series (string) of 2D points.
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.PointString;
028import geomss.geom.Transformable;
029import java.io.IOException;
030import java.io.PrintWriter;
031
032/**
033 * <b><i>COPIOUS DATA ENTITY - LINEAR STRING</i></b> - This entity defines a series of
034 * linear segments along the consecutive points of the path which may be isolated or used
035 * as a component of a Subfigure Entity. The segments may cross, or be coincident with,
036 * each other. Paths may close; i.e., the first path point may be coincident with the
037 * last. Form 11 indicates that all segments share a constant depth (zt) value (are 2D).
038 * 
039 * <p>
040 * This entity, when read from an IGES file, is converted to a list of points (a
041 * PointString object). This entity type <b>can</b> be written out to an IGES file.
042 * </p>
043 *
044 * <p> Modified by: Joseph A. Huwaldt </p>
045 * 
046 * @author Joseph A. Huwladt, Date: March 10, 2013
047 * @version February 22, 2025
048 * @see Entity106_CopiousData
049 */
050public class Entity106_11_2DLinearString extends Entity106_CopiousData {
051
052    /**
053     * Default constructor.
054     *
055     * @param p  part to which this entity is contained
056     * @param de Directory Entry for this entity
057     */
058    public Entity106_11_2DLinearString(Part p, DirEntry de) {
059        super(p, de);
060
061        if (Constants.DEBUG) {
062            System.out.println("Entity106_11 constructor called");
063        }
064    }
065
066    /**
067     * Create this entity from the specified GeomSS geometry element.
068     *
069     * @param part  The Part in which this entity is contained.
070     * @param DEnum The line count from the start of the Directory Entry Section for this
071     *              entry (odd number).
072     * @param geom  The GeomSS PointString geometry to return an Entity for.
073     */
074    public Entity106_11_2DLinearString(Part part, int DEnum, PointString geom) {
075        super(part, new DirEntry(106, 11, DEnum, 0, geom.getName()));
076        ip = 1;
077        points.addAll(geom);
078    }
079
080    /**
081     * Returns <code>true</code> if the Entity can be written to an exchange file.
082     *
083     * @return true
084     */
085    @Override
086    public boolean canWrite() {
087        return true;
088    }
089
090    /**
091     * Write this entities parameter data to the specified PrintWriter.
092     *
093     * @param writer The PrintWriter to write the parameter data for this entity to.
094     * @param PDnum  The starting Parameter Data row index number.
095     * @return The Parameter Data row index number for the next row.
096     * @throws java.io.IOException if the parameter data could not be written out.
097     */
098    @Override
099    public int write(PrintWriter writer, int PDnum) throws IOException {
100
101        //  Build up the parameter data string.
102        GeomList<GeomPoint> points2 = (GeomList<GeomPoint>)points.to(Constants.unit);
103        StringBuilder buffer = new StringBuilder();
104        buffer.append(106);                 buffer.append(Constants.Delim);
105        buffer.append(ip);                  buffer.append(Constants.Delim);
106        int n = points2.size();
107        buffer.append(n);                   buffer.append(Constants.Delim);
108        for (GeomPoint p : points2) {
109            buffer.append(0);               buffer.append(Constants.Delim);
110            appendPoint2(buffer, p);
111        }
112        buffer.setLength(buffer.length() - 1);  //  Replace Delim with Term for last entry.
113        buffer.append(Constants.Term);
114
115        //  Write it out.
116        int oldPDnum = PDnum;
117        PDnum = Constants.writeSection(writer, PDnum, Constants.makeSequenceNumber(getDENum()),
118                'P', buffer);
119
120        //  Store the PD line number and line count in the directory entry.
121        getDirectoryEntry().setPDNumber(oldPDnum, PDnum - oldPDnum);
122
123        return PDnum;
124    }
125
126    /**
127     * Return a reference to the Transformable GeomElement contained in this IGES Entity.
128     *
129     * @return A reference to the Transformable GeomElement contained in this IGES Entity.
130     */
131    @Override
132    protected Transformable getGeomElement() {
133        PointString<GeomPoint> str = PointString.newInstance();
134        int size = points.size();
135        for (int i = 0; i < size; ++i) {
136            GeomPoint p = (GeomPoint)points.get(i);
137            str.add(p);
138        }
139        return str;
140    }
141
142    /**
143     * Returns a short String describing this Entity object's type.
144     *
145     * @return A short String describing this Entity object's type.
146     */
147    @Override
148    public String getTypeString() {
149        return "Entity106_11 - Copious Data - 2D Linear String";
150    }
151
152}