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