001/*
002 *   Entity106_12_3DLinearString  -- Entity that represents a consecutive series (string) of 3D 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 12 indicates that the points are 3-dimensional.
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>
043 * be written out to an IGES file.
044 * </p>
045 *
046 * <p> Modified by: Joseph A. Huwaldt </p>
047 * 
048 * @author Joseph A. Huwaldt, Date: March 10, 2013
049 * @version September 13, 2016
050 * @see Entity106_CopiousData
051 */
052public class Entity106_12_3DLinearString extends Entity106_CopiousData {
053
054    /**
055     * Default constructor.
056     *
057     * @param p  part to which this entity is contained
058     * @param de Directory Entry for this entity
059     */
060    public Entity106_12_3DLinearString(Part p, DirEntry de) {
061        super(p, de);
062
063        if (Constants.DEBUG) {
064            System.out.println("Entity106_12 constructor called");
065        }
066    }
067
068    /**
069     * Create this entity from the specified GeomSS geometry element.
070     *
071     * @param part  The Part in which this entity is contained.
072     * @param DEnum The line count from the start of the Directory Entry Section for this
073     *              entry (odd number).
074     * @param geom  The GeomSS PointString geometry to return an Entity for.
075     */
076    public Entity106_12_3DLinearString(Part part, int DEnum, PointString geom) {
077        super(part, new DirEntry(106, 12, DEnum, 0, geom.getName()));
078        ip = 2;
079        points.addAll(geom);
080    }
081
082    /**
083     * Returns <code>true</code> if the Entity can be written to an exchange file.
084     *
085     * @return true
086     */
087    @Override
088    public boolean canWrite() {
089        return true;
090    }
091
092    /**
093     * Write this entities parameter data to the specified PrintWriter.
094     *
095     * @param writer The PrintWriter to write the parameter data for this entity to.
096     * @param PDnum  The starting Parameter Data row index number.
097     * @return The Parameter Data row index number for the next row.
098     * @throws java.io.IOException
099     */
100    @Override
101    public int write(PrintWriter writer, int PDnum) throws IOException {
102
103        //  Build up the parameter data string.
104        GeomList<GeomPoint> points2 = (GeomList<GeomPoint>)points.to(Constants.unit);
105        StringBuilder buffer = new StringBuilder();
106        buffer.append(106);                 buffer.append(Constants.Delim);
107        buffer.append(ip);                  buffer.append(Constants.Delim);
108        int n = points2.size();
109        buffer.append(n);                   buffer.append(Constants.Delim);
110        for (GeomPoint p : points2) {
111            appendPoint3(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_12 - Copious Data - 3D Linear String";
151    }
152
153}