001/*
002 *   Entity110_Line  -- Entity that defines a line segment.
003 *
004 *   Copyright (C) 2010-2016, 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.LineSeg;
026import geomss.geom.LineSegment;
027import geomss.geom.Point;
028import geomss.geom.Transformable;
029import java.io.IOException;
030import java.io.PrintWriter;
031import java.io.RandomAccessFile;
032import java.text.MessageFormat;
033
034/**
035 * <b><i>LINE ENTITY</i></b> - This entity defines a line segment which may be isolated or
036 * used as a component of a Composite Curve Entity or a subfigure. A line is a bounded,
037 * connected portion of a straight line which has distinct start and terminate points.
038 *
039 * <p>
040 * This entity, when read from an IGES file, is converted to a LineSegment curve. This
041 * 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 JDN, Version 1.0
047 * @version September 13, 2016
048 */
049public class Entity110_Line extends GeomSSEntity {
050
051    private Point p1; // Start point P1
052    private Point p2; // Terminate point P2
053    private LineSegment line;
054
055    /**
056     * Default constructor.
057     *
058     * @param p  part to which this entity is contained
059     * @param de Directory Entry for this entity
060     */
061    public Entity110_Line(Part p, DirEntry de) {
062        super(p, de);
063
064        if (Constants.DEBUG) {
065            System.out.println("Entity110 constructor called");
066        }
067
068    }
069
070    /**
071     * Create this entity from the specified GeomSS geometry element.
072     *
073     * @param part  The Part in which this entity is contained.
074     * @param DEnum The line count from the start of the Directory Entry Section for this
075     *              entry (odd number).
076     * @param geom  The GeomSS LineSegment geometry to return an Entity for.
077     */
078    public Entity110_Line(Part part, int DEnum, LineSegment geom) {
079        super(part, new DirEntry(110, 0, DEnum, 0, geom.getName()));
080        line = geom;
081        p1 = geom.getStart().immutable();
082        p2 = geom.getEnd().immutable();
083    }
084
085    /**
086     * Returns <code>true</code> if the Entity can be written to an exchange file.
087     *
088     * @return true
089     */
090    @Override
091    public boolean canWrite() {
092        return true;
093    }
094
095    /**
096     * Checks to see if the entity is correct. The following restrictions are imposed:
097     *
098     * - The Label Display Pointer shall be 0
099     */
100    @Override
101    public void check() {
102        DirEntry DE = getDirectoryEntry();
103
104        // DE Label Display Pointer shall be 0
105        if (DE.getLblDsp() != 0) {
106            String msg = MessageFormat.format(RESOURCES.getString("labelDisplay"), DE.getLblDsp());
107            addErrorMessage(getWarningString(msg));
108        }
109
110    }
111
112    /**
113     * Read the Parameter Data from the String read in by the superclass.
114     *
115     * @param in input file
116     * @throws java.io.IOException
117     */
118    @Override
119    public void read(RandomAccessFile in) throws IOException {
120        if (Constants.DEBUG) {
121            System.out.println("Entity110.read() called");
122        }
123
124        super.read(in);
125        String s = getPDString();
126
127        if (Constants.DEBUG) {
128            System.out.println("PD String = \"" + s + "\"");
129        }
130
131        p1 = getPoint3(s);
132        p2 = getPoint3(s);
133        line = LineSeg.valueOf(p1, p2);
134        line.putUserData("IGES_U0", ZERO);
135        line.putUserData("IGES_U1", ONE);
136
137        super.read_additional();
138    }
139
140    /**
141     * The GeomSS geometry element is created from the IGES parameters when this method is
142     * called.
143     */
144    @Override
145    void createGeometry() throws IOException {
146        //  Already done in read().
147    }
148
149    /**
150     * Return a reference to the Transformable GeomElement contained in this IGES Entity.
151     *
152     * @return A reference to the Transformable GeomElement contained in this IGES Entity.
153     */
154    @Override
155    protected Transformable getGeomElement() {
156        return line;
157    }
158
159    /**
160     * Write this entities parameter data to the specified PrintWriter.
161     *
162     * @param writer The PrintWriter to write the parameter data for this entity to.
163     * @param PDnum  The starting Parameter Data row index number.
164     * @return The Parameter Data row index number for the next row.
165     * @throws java.io.IOException
166     */
167    @Override
168    public int write(PrintWriter writer, int PDnum) throws IOException {
169
170        //  Build up the parameter data string.
171        LineSegment line2 = line.to(Constants.unit);
172        p1 = line2.getStart().immutable();
173        p2 = line2.getEnd().immutable();
174        StringBuilder buffer = new StringBuilder();
175        buffer.append(110);     buffer.append(Constants.Delim);
176        appendPoint3(buffer, p1);               //  X1,Y1,Z1
177        appendPoint3(buffer, p2);               //  X2,Y2,Z2
178        buffer.setLength(buffer.length() - 1);  //  Replace Delim with Term for last entry.
179        buffer.append(Constants.Term);
180
181        //  Write it out.
182        int oldPDnum = PDnum;
183        PDnum = Constants.writeSection(writer, PDnum, Constants.makeSequenceNumber(getDENum()),
184                'P', buffer);
185
186        //  Store the PD line number and line count in the directory entry.
187        getDirectoryEntry().setPDNumber(oldPDnum, PDnum - oldPDnum);
188
189        return PDnum;
190    }
191
192    /**
193     * Returns a short String describing this Entity object's type.
194     *
195     * @return A short String describing this Entity object's type.
196     */
197    @Override
198    public String getTypeString() {
199        return "Entity110 - Line";
200    }
201
202    /**
203     * Dump to String.
204     *
205     * @return String containing the resulting text.
206     */
207    @Override
208    public String toString() {
209        if (p1 == null || p2 == null)
210            return super.toString();
211
212        StringBuilder outStr = new StringBuilder(super.toString());
213        outStr.append("\n");
214
215        outStr.append("p1 = ");
216        outStr.append(p1.toString());
217        outStr.append("\n");
218        outStr.append("p2 = ");
219        outStr.append(p2.toString());
220
221        return outStr.toString();
222    }
223
224}