001/*
002 *   Entity402_Associativity  -- Entity that represents an associativity relationship between entities.
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.*;
026import java.io.IOException;
027import java.io.RandomAccessFile;
028import java.util.ArrayList;
029import java.util.List;
030
031/**
032 * <b><i>ASSOCIATIVITY ENTITY</i></b> - This entity defines an associativity relationship
033 * between other entities.
034 * 
035 * <p>
036 * This entity, when read from an IGES file, is converted to a list of geometry objects.
037 * This entity type can not be written out to an IGES file.
038 * </p>
039 *
040 * <p> Modified by: Joseph A. Huwaldt </p>
041 * 
042 * @author JDN, AED, Version 1.0
043 * @version February 22, 2025
044 */
045public abstract class Entity402_Associativity extends GeomSSEntity {
046
047    protected List<Integer> pointers = new ArrayList(); //  List of entity DE numbers.
048
049    protected GeometryList geom = GeomList.newInstance();   //  List of GeomSS geometry objects.
050
051    /**
052     * Default constructor.
053     *
054     * @param p  part to which this entity is contained
055     * @param de Directory Entry for this entity
056     */
057    public Entity402_Associativity(Part p, DirEntry de) {
058        super(p, de);
059    }
060
061    /**
062     * Checks to see if the entity is correct. No restrictions are imposed.
063     */
064    @Override
065    public void check() {
066    }
067
068    /**
069     * Read the Parameter Data from the String read in by the superclass.
070     *
071     * @param in input file
072     * @throws java.io.IOException if the parameter data could not be read in.
073     */
074    @Override
075    public void read(RandomAccessFile in) throws IOException {
076        super.read(in);
077        String s = getPDString();
078
079        if (Constants.DEBUG) {
080            System.out.println("PD String = \"" + s + "\"");
081        }
082
083        int n = getInt(s);
084        for (int i = 0; i < n; ++i)
085            pointers.add(getInt(s));
086
087        super.read_additional();
088    }
089
090    /**
091     * The GeomSS geometry element is created from the IGES parameters when this method is
092     * called.
093     */
094    @Override
095    void createGeometry() {
096        Part part = getPart();
097
098        //  Loop over all the entities in this association.
099        int n = pointers.size();
100        for (int i = 0; i < n; ++i) {
101            int deNum = pointers.get(i);
102            Entity entity = part.getEntity(deNum);
103
104            if (entity instanceof GeomSSEntity) {
105                //  Found a GeomSS geometry Entity.
106                GeomSSEntity geomEntity = (GeomSSEntity)entity;
107                geomEntity.setUsedInList(true); //  Indicate that the entity is used by this association.
108                GeomElement element = geomEntity.getGeomElement(GTransform.IDENTITY);
109                geom.add(element);
110            }
111        }
112    }
113
114    /**
115     * Return a reference to the Transformable GeomElement contained in this IGES Entity.
116     *
117     * @return A reference to the Transformable GeomElement contained in this IGES Entity.
118     */
119    @Override
120    protected Transformable getGeomElement() {
121        return geom;
122    }
123
124    /**
125     * Dump to String.
126     *
127     * @return String containing the resulting text.
128     */
129    @Override
130    public String toString() {
131        StringBuilder outStr = new StringBuilder(super.toString());
132        outStr.append("\n");
133
134        int n = pointers.size();
135        outStr.append("n  = ");
136        outStr.append(n);
137        outStr.append("\n");
138
139        for (int i = 0; i < n; i++) {
140            outStr.append("entity(");
141            outStr.append(i);
142            outStr.append(") = ");
143            outStr.append(pointers.get(i));
144            outStr.append("\n");
145        }
146
147        return outStr.toString();
148    }
149
150}