001/*
002 *   Entity402_15_OrderedGroupNoBackPointers  -- Entity that represents an ordered group without back pointers.
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 java.io.IOException;
026import java.io.PrintWriter;
027import java.util.List;
028
029/**
030 * <b><i>ASSOCIATIVITY ENTITY - ORDERED GROUP WITHOUT BACK POINTERS</i></b> - This entity
031 * allows an ordered group of entities without back pointers to be maintained as a single,
032 * logical entity.
033 * 
034 * <p> Modified by: Joseph A. Huwaldt </p>
035 * 
036 * @author JDN, Version 1.0
037 * @version February 22, 2025
038 * @see Entity402_Associativity
039 */
040public class Entity402_15_OrderedGroupNoBackPointers extends Entity402_Associativity {
041
042    /**
043     * Default constructor.
044     *
045     * @param p  part to which this entity is contained
046     * @param de Directory Entry for this entity
047     */
048    public Entity402_15_OrderedGroupNoBackPointers(Part p, DirEntry de) {
049        super(p, de);
050
051        if (Constants.DEBUG) {
052            System.out.println("Entity402_15 constructor called");
053        }
054    }
055
056    /**
057     * Create this entity from the specified GeomSS geometry element.
058     *
059     * @param part     The Part in which this entity is contained.
060     * @param DEnum    The line count from the start of the Directory Entry Section for
061     *                 this entry (odd number).
062     * @param children A list of the entities contained in this list.
063     * @param name     The name to apply to the entity or null for no name.
064     */
065    public Entity402_15_OrderedGroupNoBackPointers(Part part, int DEnum, List<Entity> children, String name) {
066        super(part, new DirEntry(402, 15, DEnum, 0, name));
067        for (Entity entity : children) {
068            pointers.add(entity.getDENum());
069        }
070    }
071
072    /**
073     * Returns <code>true</code> if the Entity can be written to an exchange file.
074     *
075     * @return true
076     */
077    @Override
078    public boolean canWrite() {
079        return true;
080    }
081
082    /**
083     * Write this entities parameter data to the specified PrintWriter.
084     *
085     * @param writer The PrintWriter to write the parameter data for this entity to.
086     * @param PDnum  The starting Parameter Data row index number.
087     * @return The Parameter Data row index number for the next row.
088     * @throws java.io.IOException if the parameter data could not be written out.
089     */
090    @Override
091    public int write(PrintWriter writer, int PDnum) throws IOException {
092
093        //  Build up the parameter data string.
094        int n = pointers.size();
095        int nm1 = n - 1;
096        StringBuilder buffer = new StringBuilder();
097        buffer.append(402);                 buffer.append(Constants.Delim);
098        buffer.append(n);                   buffer.append(Constants.Delim);
099        for (int i = 0; i < n; ++i) {
100            buffer.append(pointers.get(i));
101            if (i != nm1)
102                buffer.append(Constants.Delim);
103            else
104                buffer.append(Constants.Term);
105        }
106
107        //  Write it out.
108        int oldPDnum = PDnum;
109        PDnum = Constants.writeSection(writer, PDnum, Constants.makeSequenceNumber(getDENum()),
110                'P', buffer);
111
112        //  Store the PD line number and line count in the directory entry.
113        getDirectoryEntry().setPDNumber(oldPDnum, PDnum - oldPDnum);
114
115        return PDnum;
116    }
117
118    /**
119     * Returns a short String describing this Entity object's type.
120     *
121     * @return A short String describing this Entity object's type.
122     */
123    @Override
124    public String getTypeString() {
125        return "Entity402_15 - Ordered Group Without Back Pointers";
126    }
127
128}