// Write all object, return the file pos of first index block. int WriteObjects() { int firstIndexBlock = 0; int offsetPrevBlock = 0; OcadIndexBlock currBlock = new OcadIndexBlock(); int index, count, total; ICollection<Symbol> symbols = map.AllSymbols; total = symbols.Count; index = 0; count = 0; foreach (Symbol sym in symbols) { if (version < 9 && (sym is GraphicsAreaSymbol || sym is GraphicsLineSymbol)) continue; // graphics objects not supported in OCAD 6-8. OcadIndex ocadIndex = new OcadIndex(); ocadIndex.Pos = (int) writer.Seek(0, SeekOrigin.Current); RectangleF bounds = sym.BoundingBox; ocadIndex.LowerLeft = OcadCoordFromPoint(new PointF(bounds.Left, bounds.Top)); ocadIndex.UpperRight = OcadCoordFromPoint(new PointF(bounds.Right, bounds.Bottom)); ocadIndex.Sym = ConvertSymdefId(sym.Definition.OcadID); OcadObject ocadObj; int nItems = WriteSymbol(sym, out ocadObj); if (version >= 9) { ocadIndex.ObjType = ocadObj.Otp; ocadIndex.Status = 1; ocadIndex.ViewType = 0; if (version >= 9 && ocadObj.Sym == -2) ocadIndex.Color = (short) ocadObj.Col; // graphics object. else if (version >= 9 && ocadObj.Sym == -3) ocadIndex.Color = 0; // image object. else ocadIndex.Color = mapSymdefToSingleColor[sym.Definition]; ocadIndex.ImpLayer = 0; } // UNDONE: throw exception if length of object is too long. if (version == 8) ocadIndex.Len = (short) nItems; else if (version >= 9) ocadIndex.Len = (short) (40 + 8 * nItems); else ocadIndex.Len = (short) (32 + 8 * nItems); currBlock.IndexArr[index] = ocadIndex; ++index; ++count; if (index == 256 || count == total) { index = 0; int current = (int) writer.Seek(0, SeekOrigin.Current); if (offsetPrevBlock == 0) firstIndexBlock = current; else { writer.Seek(offsetPrevBlock, SeekOrigin.Begin); writer.Write(current); writer.Seek(current, SeekOrigin.Begin); } currBlock.Write(writer, version); offsetPrevBlock = current; // reset for next block. currBlock.NextBlock = 0; for (int i = 0; i < currBlock.IndexArr.Length; ++i) currBlock.IndexArr[i] = new OcadIndex(); } } return firstIndexBlock; }
public void Read(BinaryReader reader, int firstBlock, int version) { List<OcadIndex> indexList = new List<OcadIndex>(); int nextBlock = firstBlock; while (nextBlock != 0) { reader.BaseStream.Seek(nextBlock, SeekOrigin.Begin); nextBlock = reader.ReadInt32(); for (int i = 0; i < 256; ++i) { OcadIndex index = new OcadIndex(); index.Read(reader, version); if (index.Sym != 0) indexList.Add(index); } } indexes = indexList.ToArray(); }