package com.ximple.io.dgn7; //~--- JDK imports ------------------------------------------------------------ import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ShortBuffer; import java.util.ArrayList; import java.util.List; import com.vividsolutions.jts.geom.Envelope; /** * Record * * @author Ulysses * @version 0.1 * @since 2006/5/18 上午 11:14:50 */ public class Element { public static final int CONSTRUCTION_CLASS = 0; public static final int CONSTRUCTION_RULE_CLASS = 0; public static final int DIMENSION_CLASS = 0; public static final int LINEAR_PATTERNED_CLASS = 0; public static final int MAX_ELEMENT_SIZE = 0; public static final int MAX_VERTICES = 100; public static final int PATTERN_AREA = 0; public static final int PATTERN_COMPONENT_CLASS = 0; public static final int PATTERN_CROSSHATCH = 0; public static final int PATTERN_HATCH = 0; public static final int PRIMARY_CLASS = 0; public static final int PRIMARY_RULE_CLASS = 0; short[] raw; byte attrOffset = 0; public Element(short[] raw) { this.raw = raw; } public int getLineStyle() { return 0; } public Envelope getRange() { int lowCoorX = (int) ((raw[3] << 16) & 0xffff0000) + (raw[2] & 0x0000ffff); lowCoorX = Utility.ConvertFromDGN(lowCoorX); int lowCoorY = (int) ((raw[5] << 16) & 0xffff0000) + (raw[4] & 0x0000ffff); lowCoorY = Utility.ConvertFromDGN(lowCoorY); int highCoorX = (int) ((raw[9] << 16) & 0xffff0000) + (raw[8] & 0x0000ffff); highCoorX = Utility.ConvertFromDGN(highCoorX); int highCoorY = (int) ((raw[11] << 16) & 0xffff0000) + (raw[10] & 0x0000ffff); highCoorY = Utility.ConvertFromDGN(highCoorY); return new Envelope(Utility.ConverUnitToCoord(lowCoorX), Utility.ConverUnitToCoord(highCoorX), Utility.ConverUnitToCoord(lowCoorY), Utility.ConverUnitToCoord(highCoorY)); } public boolean isComponentElement() { if ((short) ((raw[0] >>> 7) & 0x0001) == 1) { return true; } else { return false; } } public boolean removeUserAttributeData(int iLinkageId) { return true; } public boolean removeUserAttributeData(int iLinkageId, int iLinkageIndex) { return true; } public boolean isDeleted() { if ((short) ((raw[0] >>> 15) & 0x0001) == 1) { return true; } else { return false; } } public int getColorIndex() { return ((raw[17] >>> 8) & 0x00ff); } public int getType() { return ((raw[0] >>> 8) & 0x007f); } public ElementType getElementType() { return ElementType.forID(getType()); } public int getLevelIndex() { return (raw[0] & 0x003f); } public int getWeight() { return ((raw[17] >>> 3) & 0x001f); } public void addUserAttributeData(byte[] pDataBlock, Class dataClass, int iLinkageId) throws Element.Exception { } public void addUserAttributeData(byte[] pDataBlock, int iLinkageId, Object oDataDef) throws Element.Exception { } public boolean hasUserAttributeData() { if (raw[15] <= 0) { return false; } short index = (short) (raw[15] + 16); if (index == -1) { return false; } return true; } public List getUserAttributeData() { short[] data; short length, nextAttribute; if (raw[15] <= 0) { return new ArrayList(); } short index = (short) (raw[15] + 16 + attrOffset); if (index == -1) { return null; } ArrayList aLinkageSet = new ArrayList(); while (index < raw.length) { length = (short) (raw[index] & (short) 0x00ff); if (length == 0) { break; } nextAttribute = (short) (index + length + 1); data = new short[length]; System.arraycopy(raw, index + 1, data, 0, length); if (data[0] == (short) 0x0020) { aLinkageSet.add(new FrammeAttributeData(data)); } else { aLinkageSet.add(new UserAttributeData(data)); } index = nextAttribute; } return aLinkageSet; } public void getUserAttributeData(byte[] pDataBlock, Class dataClass, int iLinkageId, int iLinkageIndex) { } public void getUserAttributeData(byte[] pDataBlock, int iLinkageId, Object oDataDef) { } public static class Exception extends java.lang.Exception { public Exception() { } // Constructs an Record.Exception with no detail message. public Exception(String oStrMessage) { super(oStrMessage); } } public static class ElementHandler implements IElementHandler { ElementType elementType; public ElementHandler(ElementType elementType) { this.elementType = elementType; } public ElementType getElementType() { return elementType; } public Object read(ByteBuffer buffer, short signature, int length) { byte[] dst = new byte[length - 4]; try { buffer.get(dst, 0, dst.length); } catch (BufferUnderflowException exception) { throw exception; } ByteBuffer tmpBuffer = ByteBuffer.wrap(dst); tmpBuffer.order(ByteOrder.LITTLE_ENDIAN); ShortBuffer sbuffer = tmpBuffer.asShortBuffer(); short[] rawMem = new short[(length / 2)]; sbuffer.get(rawMem, 2, rawMem.length - 2); rawMem[0] = signature; rawMem[1] = (short) ((length / 2) - 2); Element elm = createElement(rawMem); return elm; } public void write(ByteBuffer buffer, Object element) { } public int getLength(Object element) { return ((Element) element).raw.length; } protected Element createElement(short[] raw) { return new Element(raw); } } }