예제 #1
0
        public static uint AssignHandles(DxfFile file)
        {
            var visitedItems = new HashSet <IDxfItemInternal>();

            foreach (var item in file.GetFileItems())
            {
                ClearPointers(item, visitedItems);
            }

            visitedItems.Clear();

            uint nextPointer = 1u;

            foreach (var item in file.GetFileItems().Where(i => i != null))
            {
                nextPointer = AssignHandles(item, nextPointer, 0u, visitedItems);
                foreach (var child in item.GetChildItems().Where(c => c != null))
                {
                    nextPointer = AssignHandles(child, nextPointer, item.Handle, visitedItems);
                    child.SetOwner(item);
                }
            }

            return(nextPointer);
        }
예제 #2
0
파일: DxfPointer.cs 프로젝트: zakb120/dxf
        public static DxfHandle AssignHandles(DxfFile file)
        {
            var visitedItems = new HashSet <IDxfItemInternal>();

            foreach (var item in file.GetFileItems())
            {
                ClearPointers(item, visitedItems);
            }

            visitedItems.Clear();

            var nextPointer = new DxfHandle(1);

            foreach (var item in file.GetFileItems().Where(i => i != null))
            {
                nextPointer = AssignHandles(item, nextPointer, default(DxfHandle), visitedItems);
                foreach (var child in item.GetChildItems().Where(c => c != null))
                {
                    var parentHandle = GetParentHandle(item, child);
                    nextPointer = AssignHandles(child, nextPointer, parentHandle, visitedItems);
                    SetOwner(child, item, isWriting: true);
                }
            }

            return(nextPointer);
        }
예제 #3
0
        public void Save(DxfFile file)
        {
            // write sentinel "AutoCAD DXB 1.0 CR LF ^Z NUL"
            foreach (var c in DxbReader.BinarySentinel)
            {
                _writer.Write((byte)c);
            }

            _writer.Write((byte)'\r'); // CR
            _writer.Write((byte)'\n'); // LF
            _writer.Write((byte)0x1A); // ^Z
            _writer.Write((byte)0x00);

            var writingBlock = file.Entities.Count == 0 && file.Blocks.Count == 1;

            if (writingBlock)
            {
                // write block header
                WriteItemType(DxbItemType.BlockBase);
                var block = file.Blocks.Single();
                WriteN((float)block.BasePoint.X);
                WriteN((float)block.BasePoint.Y);
            }

            // force all numbers to be floats
            WriteItemType(DxbItemType.NumberMode);
            WriteW(1);

            if (writingBlock)
            {
                WriteEntities(file.Blocks.Single().Entities);
            }
            else
            {
                var layerGroups = file.Entities.GroupBy(e => e.Layer).OrderBy(g => g.Key);
                foreach (var group in layerGroups)
                {
                    var layerName = group.Key;
                    WriteItemType(DxbItemType.NewLayer);
                    foreach (var c in layerName)
                    {
                        _writer.Write((byte)c);
                    }

                    _writer.Write((byte)0.00); // null terminator for string
                    WriteEntities(group);
                }
            }

            // write null terminator
            _writer.Write((byte)0x00);
            _writer.Flush();
        }
예제 #4
0
파일: DxbWriter.cs 프로젝트: nakijun/Dxf
        public void Save(DxfFile file)
        {
            // write sentinel "AutoCAD DXB 1.0 CR LF ^Z NUL"
            foreach (var c in DxbReader.BinarySentinel)
            {
                _writer.Write((byte)c);
            }

            _writer.Write((byte)'\r'); // CR
            _writer.Write((byte)'\n'); // LF
            _writer.Write((byte)0x1A); // ^Z
            _writer.Write((byte)0x00);

            var writingBlock = file.Entities.Count == 0 && file.Blocks.Count == 1;
            if (writingBlock)
            {
                // write block header
                WriteItemType(DxbItemType.BlockBase);
                var block = file.Blocks.Single();
                WriteN((float)block.BasePoint.X);
                WriteN((float)block.BasePoint.Y);
            }

            // force all numbers to be floats
            WriteItemType(DxbItemType.NumberMode);
            WriteW(1);

            if (writingBlock)
            {
                WriteEntities(file.Blocks.Single().Entities);
            }
            else
            {
                var layerGroups = file.Entities.GroupBy(e => e.Layer).OrderBy(g => g.Key);
                foreach (var group in layerGroups)
                {
                    var layerName = group.Key;
                    WriteItemType(DxbItemType.NewLayer);
                    foreach (var c in layerName)
                    {
                        _writer.Write((byte)c);
                    }

                    _writer.Write((byte)0.00); // null terminator for string
                    WriteEntities(group);
                }
            }

            // write null terminator
            _writer.Write((byte)0x00);
            _writer.Flush();
        }
예제 #5
0
        public static void BindPointers(DxfFile file)
        {
            // gather all items by handle
            var handleMap    = new Dictionary <uint, IDxfItemInternal>();
            var visitedItems = new HashSet <IDxfItemInternal>();

            foreach (var item in file.GetFileItems())
            {
                GatherPointers(item, handleMap, visitedItems);
            }

            visitedItems.Clear();
            var visitedChildren = new HashSet <IDxfItemInternal>();

            // bind all pointers
            foreach (var item in file.GetFileItems())
            {
                BindPointers(item, handleMap, visitedItems, visitedChildren);
            }
        }
예제 #6
0
        internal static DxfFile LoadFromReader(IDxfCodePairReader reader)
        {
            var file = new DxfFile();

            file.Clear();
            var buffer  = new DxfCodePairBufferReader(reader);
            var version = DxfAcadVersion.R14;

            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                if (DxfCodePair.IsSectionStart(pair))
                {
                    buffer.Advance(); // swallow (0, SECTION) pair
                    var section = DxfSection.FromBuffer(buffer, file, version);
                    if (section != null)
                    {
                        switch (section.Type)
                        {
                        case DxfSectionType.Blocks:
                            file.BlocksSection = (DxfBlocksSection)section;
                            break;

                        case DxfSectionType.Entities:
                            file.EntitiesSection = (DxfEntitiesSection)section;
                            break;

                        case DxfSectionType.Classes:
                            file.ClassSection = (DxfClassesSection)section;
                            break;

                        case DxfSectionType.Header:
                            file.HeaderSection = (DxfHeaderSection)section;
                            version            = file.Header.Version;
                            break;

                        case DxfSectionType.Objects:
                            file.ObjectsSection = (DxfObjectsSection)section;
                            break;

                        case DxfSectionType.Tables:
                            file.TablesSection = (DxfTablesSection)section;
                            break;

                        case DxfSectionType.Thumbnail:
                            file.ThumbnailImageSection = (DxfThumbnailImageSection)section;
                            break;
                        }
                    }
                }
                else if (DxfCodePair.IsEof(pair))
                {
                    // swallow and quit
                    buffer.Advance();
                    break;
                }
                else if (DxfCodePair.IsComment(pair))
                {
                    // swallow comments
                    buffer.Advance();
                }
                else
                {
                    // swallow unexpected code pair
                    buffer.Advance();
                }
            }

            Debug.Assert(!buffer.ItemsRemain);
            DxfPointer.BindPointers(file);

            return(file);
        }
예제 #7
0
        public DxfFile ReadFile(BinaryReader reader)
        {
            // swallow next two characters
            var sub = reader.ReadChar();

            Debug.Assert(sub == 0x1A);
            var nul = reader.ReadChar();

            Debug.Assert(nul == 0x00);

            DxfPoint?blockBase    = null;
            var      entities     = new List <DxfEntity>();
            var      stillReading = true;
            Action <Func <BinaryReader, DxfEntity> > addEntity = (entityReader) =>
            {
                var entity = entityReader(reader);
                AssignCommonValues(entity);
                entities.Add(entity);
            };
            Func <DxfVertex> getLastVertex = () => entities.LastOrDefault() as DxfVertex;

            while (stillReading)
            {
                var itemType = (DxbItemType)reader.ReadByte();
                switch (itemType)
                {
                case DxbItemType.Line:
                    addEntity(ReadLine);
                    break;

                case DxbItemType.Point:
                    addEntity(ReadPoint);
                    break;

                case DxbItemType.Circle:
                    addEntity(ReadCircle);
                    break;

                case DxbItemType.Arc:
                    addEntity(ReadArc);
                    break;

                case DxbItemType.Trace:
                    addEntity(ReadTrace);
                    break;

                case DxbItemType.Solid:
                    addEntity(ReadSolid);
                    break;

                case DxbItemType.Seqend:
                    addEntity(ReadSeqend);
                    break;

                case DxbItemType.Polyline:
                    addEntity(ReadPolyline);
                    break;

                case DxbItemType.Vertex:
                    addEntity(ReadVertex);
                    break;

                case DxbItemType.Face:
                    addEntity(ReadFace);
                    break;

                case DxbItemType.ScaleFactor:
                    _scaleFactor = ReadF(reader);
                    break;

                case DxbItemType.NewLayer:
                    var sb = new StringBuilder();
                    for (int b = reader.ReadByte(); b != 0; b = reader.ReadByte())
                    {
                        sb.Append((char)b);
                    }
                    _layerName = sb.ToString();
                    break;

                case DxbItemType.LineExtension:
                    addEntity(ReadLineExtension);
                    break;

                case DxbItemType.TraceExtension:
                    addEntity(ReadTraceExtension);
                    break;

                case DxbItemType.BlockBase:
                    var x = ReadN(reader);
                    var y = ReadN(reader);
                    if (blockBase == null && entities.Count == 0)
                    {
                        // only if this is the first item encountered
                        blockBase = new DxfPoint(x, y, 0.0);
                    }
                    break;

                case DxbItemType.Bulge:
                {
                    var bulge      = ReadU(reader);
                    var lastVertex = getLastVertex();
                    if (lastVertex != null)
                    {
                        lastVertex.Bulge = bulge;
                    }
                }
                break;

                case DxbItemType.Width:
                {
                    var startWidth = ReadN(reader);
                    var endWidth   = ReadN(reader);
                    var lastVertex = getLastVertex();
                    if (lastVertex != null)
                    {
                        lastVertex.StartingWidth = startWidth;
                        lastVertex.EndingWidth   = endWidth;
                    }
                }
                break;

                case DxbItemType.NumberMode:
                    _isIntegerMode = ReadW(reader) == 0;
                    break;

                case DxbItemType.NewColor:
                    _color = DxfColor.FromRawValue((short)ReadW(reader));
                    break;

                case DxbItemType.LineExtension3D:
                    addEntity(ReadLineExtension3D);
                    break;

                case 0:
                    stillReading = false;
                    break;
                }
            }

            var file = new DxfFile();

            foreach (var section in file.Sections)
            {
                section.Clear();
            }

            // collect the entities (e.g., polylines, etc.)
            entities = DxfEntitiesSection.GatherEntities(entities);

            if (blockBase != null)
            {
                // entities are all contained in a block
                var block = new DxfBlock();
                block.BasePoint = blockBase.GetValueOrDefault();
                foreach (var entity in entities)
                {
                    block.Entities.Add(entity);
                }

                file.Blocks.Add(block);
            }
            else
            {
                // just a normal collection of entities
                foreach (var entity in entities)
                {
                    file.Entities.Add(entity);
                }
            }

            return(file);
        }
예제 #8
0
파일: DxfFile.cs 프로젝트: yangkf1985/Dxf
        private static DxfFile LoadFromReader(IDxfCodePairReader reader)
        {
            var file = new DxfFile();
            file.Clear();
            var buffer = new DxfCodePairBufferReader(reader.GetCodePairs());
            var version = DxfAcadVersion.R14;
            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                if (DxfCodePair.IsSectionStart(pair))
                {
                    buffer.Advance(); // swallow (0, SECTION) pair
                    var section = DxfSection.FromBuffer(buffer, version);
                    if (section != null)
                    {
                        switch (section.Type)
                        {
                            case DxfSectionType.Blocks:
                                file.BlocksSection = (DxfBlocksSection)section;
                                break;
                            case DxfSectionType.Entities:
                                file.EntitiesSection = (DxfEntitiesSection)section;
                                break;
                            case DxfSectionType.Classes:
                                file.ClassSection = (DxfClassesSection)section;
                                break;
                            case DxfSectionType.Header:
                                file.HeaderSection = (DxfHeaderSection)section;
                                version = file.Header.Version;
                                break;
                            case DxfSectionType.Objects:
                                file.ObjectsSection = (DxfObjectsSection)section;
                                break;
                            case DxfSectionType.Tables:
                                file.TablesSection = (DxfTablesSection)section;
                                break;
                            case DxfSectionType.Thumbnail:
                                file.ThumbnailImageSection = (DxfThumbnailImageSection)section;
                                break;
                        }
                    }
                }
                else if (DxfCodePair.IsEof(pair))
                {
                    // swallow and quit
                    buffer.Advance();
                    break;
                }
                else if (DxfCodePair.IsComment(pair))
                {
                    // swallow comments
                    buffer.Advance();
                }
                else
                {
                    // swallow unexpected code pair
                    buffer.Advance();
                }
            }

            Debug.Assert(!buffer.ItemsRemain);
            DxfPointer.BindPointers(file);

            return file;
        }
예제 #9
0
파일: DxbReader.cs 프로젝트: nakijun/Dxf
        public DxfFile ReadFile(BinaryReader reader)
        {
            // swallow next two characters
            var sub = reader.ReadChar();
            Debug.Assert(sub == 0x1A);
            var nul = reader.ReadChar();
            Debug.Assert(nul == 0x00);

            DxfPoint blockBase = null;
            var entities = new List<DxfEntity>();
            var stillReading = true;
            Action<Func<BinaryReader, DxfEntity>> addEntity = (entityReader) =>
            {
                var entity = entityReader(reader);
                AssignCommonValues(entity);
                entities.Add(entity);
            };
            Func<DxfVertex> getLastVertex = () => entities.LastOrDefault() as DxfVertex;
            while (stillReading)
            {
                var itemType = (DxbItemType)reader.ReadByte();
                switch (itemType)
                {
                    case DxbItemType.Line:
                        addEntity(ReadLine);
                        break;
                    case DxbItemType.Point:
                        addEntity(ReadPoint);
                        break;
                    case DxbItemType.Circle:
                        addEntity(ReadCircle);
                        break;
                    case DxbItemType.Arc:
                        addEntity(ReadArc);
                        break;
                    case DxbItemType.Trace:
                        addEntity(ReadTrace);
                        break;
                    case DxbItemType.Solid:
                        addEntity(ReadSolid);
                        break;
                    case DxbItemType.Seqend:
                        addEntity(ReadSeqend);
                        break;
                    case DxbItemType.Polyline:
                        addEntity(ReadPolyline);
                        break;
                    case DxbItemType.Vertex:
                        addEntity(ReadVertex);
                        break;
                    case DxbItemType.Face:
                        addEntity(ReadFace);
                        break;
                    case DxbItemType.ScaleFactor:
                        _scaleFactor = ReadF(reader);
                        break;
                    case DxbItemType.NewLayer:
                        var sb = new StringBuilder();
                        for (int b = reader.ReadByte(); b != 0; b = reader.ReadByte())
                            sb.Append((char)b);
                        _layerName = sb.ToString();
                        break;
                    case DxbItemType.LineExtension:
                        addEntity(ReadLineExtension);
                        break;
                    case DxbItemType.TraceExtension:
                        addEntity(ReadTraceExtension);
                        break;
                    case DxbItemType.BlockBase:
                        var x = ReadN(reader);
                        var y = ReadN(reader);
                        if (blockBase == null && entities.Count == 0)
                        {
                            // only if this is the first item encountered
                            blockBase = new DxfPoint(x, y, 0.0);
                        }
                        break;
                    case DxbItemType.Bulge:
                        {
                            var bulge = ReadU(reader);
                            var lastVertex = getLastVertex();
                            if (lastVertex != null)
                            {
                                lastVertex.Bulge = bulge;
                            }
                        }
                        break;
                    case DxbItemType.Width:
                        {
                            var startWidth = ReadN(reader);
                            var endWidth = ReadN(reader);
                            var lastVertex = getLastVertex();
                            if (lastVertex != null)
                            {
                                lastVertex.StartingWidth = startWidth;
                                lastVertex.EndingWidth = endWidth;
                            }
                        }
                        break;
                    case DxbItemType.NumberMode:
                        _isIntegerMode = ReadW(reader) == 0;
                        break;
                    case DxbItemType.NewColor:
                        _color = DxfColor.FromRawValue((short)ReadW(reader));
                        break;
                    case DxbItemType.LineExtension3D:
                        addEntity(ReadLineExtension3D);
                        break;
                    case 0:
                        stillReading = false;
                        break;
                }
            }

            var file = new DxfFile();

            // collect the entities (e.g., polylines, etc.)
            entities = DxfEntitiesSection.GatherEntities(entities);

            if (blockBase != null)
            {
                // entities are all contained in a block
                var block = new DxfBlock();
                block.BasePoint = blockBase;
                block.Entities.AddRange(entities);
                file.Blocks.Add(block);
            }
            else
            {
                // just a normal collection of entities
                file.Entities.AddRange(entities);
            }

            return file;
        }