forked from geodmms/xdgnjobs

?? ?
2010-04-20 b326756ddd7eee426d7d0e93a485ec30b6fbb5fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package com.ximple.io.dgn7;
 
//~--- JDK imports ------------------------------------------------------------
 
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
 
import com.vividsolutions.jts.geom.Envelope;
 
import com.ximple.util.DgnUtility;
 
/**
 * FileRecord
 *
 * @author Ulysses
 * @version 0.1
 * @since 2006/5/18 �W�� 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;
 
    protected short[] raw;
    protected byte attrOffset = 0;
    protected ByteBuffer rawBuffer;
    protected boolean newElement = false;
 
    Element(byte[] raw) {
        // this.raw = raw;
        this.raw = new short[raw.length / 2];
        rawBuffer = ByteBuffer.wrap(raw);
        rawBuffer.order(ByteOrder.LITTLE_ENDIAN);
        rawBuffer.asShortBuffer().get(this.raw);
    }
 
    public int getLineStyle() {
        return (raw[17] & 0x0007);
    }
 
    protected void setLineStyle(int value) {
        if (value > -1 && value < 8)
            raw[17] = (short) ((raw[17] & 0xfff8) | (value & 0x0007));
        else
            new IllegalArgumentException("Out of Range!");
    }
 
    public Envelope getRange() {
        int lowCoorX = ((raw[3] << 16) & 0xffff0000) + (raw[2] & 0x0000ffff);
        lowCoorX = DgnUtility.convertFromDGN(lowCoorX);
 
        int lowCoorY = ((raw[5] << 16) & 0xffff0000) + (raw[4] & 0x0000ffff);
        lowCoorY = DgnUtility.convertFromDGN(lowCoorY);
 
        int highCoorX = ((raw[9] << 16) & 0xffff0000) + (raw[8] & 0x0000ffff);
        highCoorX = DgnUtility.convertFromDGN(highCoorX);
 
        int highCoorY = ((raw[11] << 16) & 0xffff0000) + (raw[10] & 0x0000ffff);
        highCoorY = DgnUtility.convertFromDGN(highCoorY);
 
        return new Envelope(DgnUtility.converUnitToCoord(lowCoorX), DgnUtility.converUnitToCoord(highCoorX),
                            DgnUtility.converUnitToCoord(lowCoorY), DgnUtility.converUnitToCoord(highCoorY));
    }
 
    public void setRange(Envelope bbox) {
        int lowCoordX = DgnUtility.converCoordToUnit(bbox.getMinX());
        int temp = DgnUtility.converToDGN(lowCoordX);
        raw[3] = (short) (temp >> 16 & 0x0000ffff);
        raw[2] = (short) (temp & 0x0000ffff);
 
        int lowCoordY = DgnUtility.converCoordToUnit(bbox.getMinY());
        temp = DgnUtility.converToDGN(lowCoordY);
        raw[5] = (short) (temp >> 16 & 0x0000ffff);
        raw[4] = (short) (temp & 0x0000ffff);
 
        int highCoorX = DgnUtility.converCoordToUnit(bbox.getMaxX());
        temp = DgnUtility.converToDGN(highCoorX);
        raw[9] = (short) (temp >> 16 & 0x0000ffff);
        raw[8] = (short) (temp & 0x0000ffff);
 
        int highCoorY = DgnUtility.converCoordToUnit(bbox.getMaxY());
        temp = DgnUtility.converToDGN(highCoorY);
        raw[11] = (short) (temp >> 16 & 0x0000ffff);
        raw[10] = (short) (temp & 0x0000ffff);
    }
 
    public boolean isComponentElement() {
        return (short) ((raw[0] >>> 7) & 0x0001) == 1;
    }
 
    protected void setComponentElement(boolean value) {
        raw[0] = (short) ((raw[0] & 0xff7f) | (value ? 0x0080 : 0x0));
    }
 
    public boolean removeUserAttributeData(int iLinkageId) {
        return true;
    }
 
    public boolean removeUserAttributeData(int iLinkageId, int iLinkageIndex) {
        return true;
    }
 
    public boolean isDeleted() {
        return (short) ((raw[0] >>> 15) & 0x0001) == 1;
    }
 
    protected void setDeleted(boolean value) {
            raw[0] = (short) ((raw[0] & 0x7fff) | ((((value) ? 1 : 0) << 15) & 0x8000));
    }
 
    public int getColorIndex() {
        return ((raw[17] >>> 8) & 0x00ff);
    }
 
    protected void setColorIndex(int value) {
        if (value > -1 && value < 256)
        {
            raw[17] = (short) ((raw[17] & 0x00ff) | (value << 8 & 0xff00));
        } else  new IllegalArgumentException("Out of Range!");
    }
 
    public int getType() {
        return ((raw[0] >>> 8) & 0x007f);
    }
 
    protected void setType(int value) {
        raw[0] = (short) ((raw[0] & 0x80ff) | (value << 8) & 0x3f00);
    }
 
    public ElementType getElementType() {
        return ElementType.forID(getType());
    }
 
    public int getLevelIndex() {
        return (raw[0] & 0x003f);
    }
 
    public void setLevelIndex(int value) {
        raw[0] = (short) ((raw[0] & 0xffc0) | (value & 0x003f));
    }
 
    public int getWeight() {
        return ((raw[17] >>> 3) & 0x001f);
    }
 
    public void setWeight(int value) {
        if (value > -1 && value < 31) {
            raw[17] = (short) ((raw[17] & 0xff07) | (value << 3 & 0x00f8));
        } else {
            throw new RuntimeException("Out of Range!");
        }
    }
 
    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<UserAttributeData> getUserAttributeData() {
        short[] data;
        short length, nextAttribute;
 
        if (raw[15] <= 0) {
            return new ArrayList<UserAttributeData>();
        }
 
        short index = (short) (raw[15] + 16 + attrOffset);
 
        if (index == -1) {
            return null;
        }
 
        ArrayList<UserAttributeData> aLinkageSet = new ArrayList<UserAttributeData>();
 
        while (index < raw.length) {
 
 
            if (raw[index] == 0 || ((short) raw[index]) == 0x8000)
              {
                    index += 4;
                    aLinkageSet.add(null);
                    continue;
              }
 
            length = (short) (raw[index] & (short) 0x00ff);
 
            if (length == 0) {
                break;
            }
 
            if (length > raw.length - index - 1)
            {
                    return null;
            }
 
            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 ByteBuffer getRawBuffer() {
        return rawBuffer.asReadOnlyBuffer();
    }
 
    public short[] getRawArray() {
        if (raw == null) return null;
        short[] result = new short[raw.length];
        System.arraycopy(raw, 0, result, 0, raw.length);
        return result;
    }
 
    public static class Exception extends java.lang.Exception {
        public Exception() {
        }
 
        // Constructs an ElementRecord.Exception with no detail message.
        public Exception(String oStrMessage) {
            super(oStrMessage);
        }
    }
 
    protected static int getOffsetPosition(int offset) {
        return offset * 2;
    }
 
    public void resyncBuffer() {
        byte[] tempRaw = new byte[this.raw.length * 2];
        ByteBuffer tempBuffer = ByteBuffer.wrap(tempRaw);
        tempBuffer.order(ByteOrder.LITTLE_ENDIAN);
        tempBuffer.asShortBuffer().put(this.raw);
 
        int pos = rawBuffer.position();
        rawBuffer = tempBuffer;
        rawBuffer.position(pos);
    }
 
    public static class ElementHandler implements IElementHandler {
        ElementType elementType;
 
        public ElementHandler(ElementType elementType) {
            this.elementType = elementType;
        }
 
        public ElementType getElementType() {
            return elementType;
        }
 
        public Element read(ByteBuffer buffer, short signature, int length) {
            byte[] dst = new byte[length];
            try {
                buffer.get(dst, 4, dst.length - 4);
            } catch (BufferUnderflowException exception) {
                throw exception;
            }
 
            ByteBuffer tmpBuffer = ByteBuffer.wrap(dst);
            tmpBuffer.order(ByteOrder.LITTLE_ENDIAN);
            tmpBuffer.position(0);
            tmpBuffer.putShort(signature);
            tmpBuffer.putShort((short) ((length / 2) - 2));
 
            /*
            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(dst);
 
            return elm;
        }
 
        public void write(ByteBuffer buffer, Object element) {
            buffer.put(((Element) element).rawBuffer);
        }
 
        public int getLength(Object element) {
            return ((Element) element).raw.length;
        }
 
        public int getBufferLength(Object element) {
            return ((Element) element).rawBuffer.limit();
        }
 
        protected Element createElement(byte[] raw) {
            return new Element(raw);
        }
    }
 
    public static final class FileRecord {
        int length;
        int number = 0;
        int offset;           // Relative to the whole file
        int start = 0;    // Relative to the current loaded buffer
        short signature = 0;
 
        /**
         * The minimum X value.
         */
        public double minX;
 
        /**
         * The minimum Y value.
         */
        public double minY;
 
        /**
         * The minimum Z value.
         */
        public double minZ;
 
        /**
         * The maximum X value.
         */
        public double maxX;
 
        /**
         * The maximum Y value.
         */
        public double maxY;
 
        /**
         * The maximum Z value.
         */
        public double maxZ;
 
        // ElementType type;
        int end = 0;    // Relative to the whole file
        Object element = null;
        IElementHandler handler;
        ByteBuffer buffer;
 
        public Object element() {
            if (element == null) {
                buffer.position(start);
                buffer.order(ByteOrder.LITTLE_ENDIAN);
 
                if (handler == null) {
                    return null;
                }
 
                element = handler.read(buffer, signature, length);
            }
 
            return element;
        }
 
        public int offset() {
            return offset;
        }
 
        /**
         * A summary of the record.
         */
        public String toString() {
            return "FileRecord " + number + " length " + length + " bounds " + minX + "," + minY + " " + maxX + "," + maxY;
        }
    }
}