001/*
002 *   Entity120_SurfaceOfRevolution  -- An entity representing a Surface of Revolution Entity.
003 *
004 *   Copyright (C) 2013-2025, Joseph A. Huwaldt. All rights reserved.
005 *   
006 *   part 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 *   part 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 part 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 */
021package geomss.geom.reader.iges;
022
023import geomss.geom.*;
024import geomss.geom.nurbs.NurbsCurve;
025import geomss.geom.nurbs.NurbsSurface;
026import geomss.geom.nurbs.SurfaceFactory;
027import jahuwaldt.js.param.Parameter;
028import java.io.IOException;
029import java.io.RandomAccessFile;
030import javax.measure.quantity.Angle;
031import javax.measure.quantity.Dimensionless;
032import javax.measure.quantity.Length;
033import javax.measure.unit.SI;
034
035/**
036 * <b><i>SURFACE OF REVOLUTION ENTITY</i></b> - This entity represents a surface of
037 * revolution. A surface of revolution is defined by an axis of rotation (which shall be a
038 * Line Entity), a generatrix, and start and terminate rotation angles. The surface is
039 * created by rotating the generatrix about the axis of rotation through the start and
040 * terminating angles.
041 *
042 * <p>
043 * This entity, when read from an IGES file, is converted to a NURBS curve. This entity
044 * type can not be written out to an IGES file. The surface parameters are stored in the
045 * user data with the prefix "IGES_120_" followed by the parameter name.
046 * </p>
047 *
048 * <p> Modified by: Joseph A. Huwaldt </p>
049 * 
050 * @author Joseph A. Huwaldt, Date: March 9, 2013
051 * @version February 22, 2025
052 */
053public class Entity120_SurfaceOfRevolution extends GeomSSEntity {
054
055    private int Lde;            //  Pointer to the DE of the Line Entity (axis of revolution)
056    private int Cde;            //  Pointer to the DE of the generatrix entity
057    private double SA;          //  Start angle in radians
058    private double TA;          //  Terminate angle in radians
059
060    private NurbsSurface srf;   //  The GeomSS surface this entity represents.
061
062    /**
063     * Default constructor.
064     *
065     * @param p  part to which this entity is contained
066     * @param de Directory Entry for this entity
067     */
068    public Entity120_SurfaceOfRevolution(Part p, DirEntry de) {
069        super(p, de);
070
071        if (Constants.DEBUG) {
072            System.out.println("Entity120 constructor called");
073        }
074
075    }
076
077    /**
078     * Checks to see if the entity is correct.
079     */
080    @Override
081    public void check() {
082    }
083
084    /**
085     * Read the Parameter Data from the String read in by the superclass.
086     *
087     * @param in input file
088     * @throws java.io.IOException if the parameter data could not be read in.
089     */
090    @Override
091    public void read(RandomAccessFile in) throws IOException {
092
093        if (Constants.DEBUG) {
094            System.out.println("Entity120.read() called");
095        }
096
097        super.read(in);
098        String s = getPDString();
099
100        if (Constants.DEBUG) {
101            System.out.println("PD String = \"" + s + "\"");
102        }
103
104        Lde = getInt(s);            //  Pointer to the DE of the Line Entity (axis of revolution)
105        Cde = getInt(s);            //  Pointer to the DE of the generatrix entity
106        SA = getReal(s);            //  Start angle in radians
107        TA = getReal(s);            //  Terminate angle in radians
108
109        super.read_additional();
110    }
111
112    /**
113     * The GeomSS geometry element is created from the IGES parameters when this method is
114     * called.
115     */
116    @Override
117    void createGeometry() throws IOException {
118        Part part = getPart();
119        Parameter<Length> tol = Parameter.valueOf(Constants.Grain, Constants.unit);
120
121        //  Create the surface of revolution from the specified axis, curve, and angles.
122        
123        //  Get the axis of rotation.
124        GeomVector<Dimensionless> axis;
125        Entity entity = part.getEntity(Lde);
126        if (entity instanceof GeomSSEntity) {
127            //  Found a GeomSS geometry Entity.
128            GeomSSEntity geomEntity = (GeomSSEntity)entity;
129            geomEntity.setUsedInList(true); //  Indicate that the entity is used by this association.
130            LineSegment L = (LineSegment)geomEntity.getGeomElement(GTransform.IDENTITY);
131            axis = L.getUnitVector();
132            axis.setOrigin(L.getStart().immutable());
133        } else
134            return;
135
136        //  Get the generatrix curve
137        NurbsCurve C;
138        entity = part.getEntity(Cde);
139        if (entity instanceof GeomSSEntity) {
140            //  Found a GeomSS geometry Entity.
141            GeomSSEntity geomEntity = (GeomSSEntity)entity;
142            geomEntity.setUsedInList(true); //  Indicate that the entity is used by this association.
143            Curve crv = (Curve)geomEntity.getGeomElement(GTransform.IDENTITY);
144            C = crv.toNurbs(tol);
145        } else
146            return;
147
148        //  Create the angle objects required.
149        Parameter<Angle> thetaS = Parameter.valueOf(SA, SI.RADIAN);
150        Parameter<Angle> thetaE = Parameter.valueOf(TA, SI.RADIAN);
151
152        //  Create the surface of revolution.
153        srf = SurfaceFactory.createRevolvedSurface(axis, C, thetaS, thetaE);
154
155        //  Apply meta-data.
156        srf.putUserData("IGES_120_L", axis);
157        srf.putUserData("IGES_120_C", C);
158        srf.putUserData("IGES_120_SA", thetaS);
159        srf.putUserData("IGES_120_TA", thetaE);
160
161        Double u0 = (Double)C.getUserData("IGES_U0");
162        if (u0 == null)
163            u0 = 0.0;
164        Double u1 = (Double)C.getUserData("IGES_U1");
165        if (u1 == null)
166            u1 = 1.0;
167        srf.putUserData("IGES_U0", u0);
168        srf.putUserData("IGES_U1", u1);
169        srf.putUserData("IGES_V0", SA);
170        srf.putUserData("IGES_V1", TA);
171
172    }
173
174    /**
175     * Return a reference to the Transformable GeomElement contained in this IGES Entity.
176     *
177     * @return A reference to the Transformable GeomElement contained in this IGES Entity.
178     */
179    @Override
180    protected Transformable getGeomElement() {
181        return srf;
182    }
183
184    /**
185     * Returns a short String describing this Entity object's type.
186     *
187     * @return A short String describing this Entity object's type.
188     */
189    @Override
190    public String getTypeString() {
191        return "Entity120 - Surface of Revolution";
192    }
193
194    /**
195     * Dump to String.
196     *
197     * @return String containing the resulting text.
198     */
199    @Override
200    public String toString() {
201        StringBuilder outStr = new StringBuilder(super.toString());
202        outStr.append("\n");
203
204        return outStr.toString();
205    }
206
207}