示例#1
0
文件: Viewport.cs 项目: Core2D/netdxf
        /// <summary>
        /// Creates a new viewport that is a copy of the current instance.
        /// </summary>
        /// <returns>A new viewport that is a copy of this instance.</returns>
        public override object Clone()
        {
            Viewport viewport = new Viewport
            {
                //EntityObject properties
                Layer = (Layer) this.Layer.Clone(),
                Linetype = (Linetype) this.Linetype.Clone(),
                Color = (AciColor) this.Color.Clone(),
                Lineweight = this.Lineweight,
                Transparency = (Transparency) this.Transparency.Clone(),
                LinetypeScale = this.LinetypeScale,
                Normal = this.Normal,
                IsVisible = this.IsVisible,
                //viewport properties
                Center = this.center,
                Width = this.width,
                Height = this.height,
                Stacking = this.stacking,
                Id = this.id,
                ViewCenter = this.viewCenter,
                SnapBase = this.snapBase,
                SnapSpacing = this.snapSpacing,
                GridSpacing = this.gridSpacing,
                ViewDirection = this.viewDirection,
                ViewTarget = this.viewTarget,
                LensLength = this.lensLength,
                FrontClipPlane = this.frontClipPlane,
                BackClipPlane = this.backClipPlane,
                ViewHeight = this.viewHeight,
                SnapAngle = this.snapAngle,
                TwistAngle = this.twistAngle,
                CircleZoomPercent = this.circleZoomPercent,
                Status = this.status,
                UcsOrigin = this.ucsOrigin,
                UcsXAxis = this.ucsXAxis,
                UcsYAxis = this.ucsYAxis,
                Elevation = this.elevation,
            };

            foreach (XData data in this.XData.Values)
                viewport.XData.Add((XData) data.Clone());

            if (this.boundary != null)
                viewport.ClippingBoundary = (EntityObject) this.boundary.Clone();

            viewport.FrozenLayers.AddRange(this.frozenLayers.ToArray());
            return viewport;
        }
示例#2
0
        private Viewport ReadViewport()
        {
            Debug.Assert(this.chunk.ReadString() == SubclassMarker.Viewport);

            Viewport viewport = new Viewport();
            Vector3 center = viewport.Center;
            Vector2 viewCenter = viewport.ViewCenter;
            Vector2 snapBase = viewport.SnapBase;
            Vector2 snapSpacing = viewport.SnapSpacing;
            Vector2 gridSpacing = viewport.GridSpacing;
            Vector3 viewDirection = viewport.ViewDirection;
            Vector3 viewTarget = viewport.ViewTarget;
            Vector3 ucsOrigin = Vector3.Zero;
            Vector3 ucsXAxis = Vector3.UnitX;
            Vector3 ucsYAxis = Vector3.UnitY;

            List<XData> xData = new List<XData>();

            this.chunk.Next();
            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 10:
                        center.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 20:
                        center.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 30:
                        center.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 40:
                        viewport.Width = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 41:
                        viewport.Height = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 68:
                        viewport.Stacking = this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 69:
                        viewport.Id = this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 12:
                        viewCenter.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 22:
                        viewCenter.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 13:
                        snapBase.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 23:
                        snapBase.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 14:
                        snapSpacing.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 24:
                        snapSpacing.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 15:
                        gridSpacing.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 25:
                        gridSpacing.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 16:
                        viewDirection.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 26:
                        viewDirection.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 36:
                        viewDirection.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 17:
                        viewTarget.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 27:
                        viewTarget.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 37:
                        viewTarget.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 42:
                        viewport.LensLength = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 43:
                        viewport.FrontClipPlane = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 44:
                        viewport.BackClipPlane = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 45:
                        viewport.ViewHeight = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 50:
                        viewport.SnapAngle = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 51:
                        viewport.TwistAngle = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 72:
                        viewport.CircleZoomPercent = this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 331:
                        Layer layer = (Layer) this.doc.GetObjectByHandle(this.chunk.ReadString());
                        viewport.FrozenLayers.Add(layer);
                        this.chunk.Next();
                        break;
                    case 90:
                        viewport.Status = (ViewportStatusFlags) this.chunk.ReadInt();
                        this.chunk.Next();
                        break;
                    case 340:
                        // we will post process the clipping boundary in case it has been defined before the viewport
                        this.viewports.Add(viewport, this.chunk.ReadHex());
                        this.chunk.Next();
                        break;
                    case 110:
                        ucsOrigin.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 120:
                        ucsOrigin.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 130:
                        ucsOrigin.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 111:
                        ucsXAxis.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 121:
                        ucsXAxis.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 131:
                        ucsXAxis.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 112:
                        ucsYAxis.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 122:
                        ucsYAxis.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 132:
                        ucsYAxis.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(this.GetApplicationRegistry(appId));
                        xData.Add(data);
                        break;
                    default:
                        if (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
                            throw new Exception("The extended data of an entity must start with the application registry code.");
                        this.chunk.Next();
                        break;
                }
            }

            viewport.Center = center;
            viewport.ViewCenter = viewCenter;
            viewport.SnapBase = snapBase;
            viewport.SnapSpacing = snapSpacing;
            viewport.GridSpacing = gridSpacing;
            viewport.ViewDirection = viewDirection;
            viewport.ViewTarget = viewTarget;
            viewport.UcsOrigin = ucsOrigin;
            viewport.UcsXAxis = ucsXAxis;
            viewport.UcsYAxis = ucsYAxis;

            viewport.XData.AddRange(xData);

            return viewport;
        }
        private void WriteViewport(Viewport vp)
        {
            this.chunk.Write(100, SubclassMarker.Viewport);

            this.chunk.Write(10, vp.Center.X);
            this.chunk.Write(20, vp.Center.Y);
            this.chunk.Write(30, vp.Center.Z);

            this.chunk.Write(40, vp.Width);
            this.chunk.Write(41, vp.Height);
            this.chunk.Write(68, vp.Stacking);
            this.chunk.Write(69, vp.Id);

            this.chunk.Write(12, vp.ViewCenter.X);
            this.chunk.Write(22, vp.ViewCenter.Y);

            this.chunk.Write(13, vp.SnapBase.X);
            this.chunk.Write(23, vp.SnapBase.Y);

            this.chunk.Write(14, vp.SnapSpacing.X);
            this.chunk.Write(24, vp.SnapSpacing.Y);

            this.chunk.Write(15, vp.GridSpacing.X);
            this.chunk.Write(25, vp.GridSpacing.Y);

            this.chunk.Write(16, vp.ViewDirection.X);
            this.chunk.Write(26, vp.ViewDirection.Y);
            this.chunk.Write(36, vp.ViewDirection.Z);

            this.chunk.Write(17, vp.ViewTarget.X);
            this.chunk.Write(27, vp.ViewTarget.Y);
            this.chunk.Write(37, vp.ViewTarget.Z);

            this.chunk.Write(42, vp.LensLength);

            this.chunk.Write(43, vp.FrontClipPlane);
            this.chunk.Write(44, vp.BackClipPlane);
            this.chunk.Write(45, vp.ViewHeight);

            this.chunk.Write(50, vp.SnapAngle);
            this.chunk.Write(51, vp.TwistAngle);
            this.chunk.Write(72, vp.CircleZoomPercent);

            foreach (Layer layer in vp.FrozenLayers)
                this.chunk.Write(331, layer.Handle);

            this.chunk.Write(90, (int) vp.Status);

            if (vp.ClippingBoundary != null) this.chunk.Write(340, vp.ClippingBoundary.Handle);

            this.chunk.Write(110, vp.UcsOrigin.X);
            this.chunk.Write(120, vp.UcsOrigin.Y);
            this.chunk.Write(130, vp.UcsOrigin.Z);

            this.chunk.Write(111, vp.UcsXAxis.X);
            this.chunk.Write(121, vp.UcsXAxis.Y);
            this.chunk.Write(131, vp.UcsXAxis.Z);

            this.chunk.Write(112, vp.UcsYAxis.X);
            this.chunk.Write(122, vp.UcsYAxis.Y);
            this.chunk.Write(132, vp.UcsYAxis.Z);

            this.WriteXData(vp.XData);
        }
示例#4
0
        private static void PaperSpace()
        {
            // Sample on how to work with Layouts
            DxfDocument dxf = new DxfDocument();
            // A new DxfDocument will create the default "Model" layout that is associated with the ModelSpace block. This layout cannot be erased or renamed.
            Line line = new Line(new Vector2(0), new Vector2(100));
            // The line will be added to the "Model" layout since this is the active one by default.
            dxf.AddEntity(line);

            // Create a new Layout, all new layouts will be associated with different PaperSpace blocks,
            // while there can be only one ModelSpace multiple PaperSpace blocks might exist in the document
            Layout layout1 = new Layout("Layout1");

            // When the layout is added to the list, a new PaperSpace block will be created automatically
            dxf.Layouts.Add(layout1);
            // Set this new Layout as the active one. All entities will now be added to this layout.
            dxf.ActiveLayout = layout1.Name;

            // Create a viewport, this is the window to the ModelSpace
            Viewport viewport1 = new Viewport
                {
                    Width = 100,
                    Height = 100,
                    Center = new Vector3(50, 50, 0),
                };

            // Add it to the "Layout1" since this is the active one
            dxf.AddEntity(viewport1);
            // Also add a circle
            Circle circle = new Circle(new Vector2(150), 25);
            dxf.AddEntity(circle);

            // Create a second Layout, add it to the list, and set it as the active one.
            Layout layout2 = new Layout("Layout2");
            dxf.Layouts.Add(layout2);
            dxf.ActiveLayout = layout2.Name;

            // viewports might have a non rectangular boundary, in this case we will use an ellipse.
            Ellipse ellipse = new Ellipse(new Vector2(100), 200, 150);
            Viewport viewport2 = new Viewport
            {
                ClippingBoundary = ellipse,
            };

            // Add the viewport to the document. This will also add the ellipse to the document.
            dxf.AddEntity(viewport2);

            Layout layout3 = new Layout("AnyName");
            dxf.Layouts.Add(layout3);
            //layout can also be renamed
            layout3.Name = "Layout3";
            
            //dxf.Layouts.Remove(layout2.Name);

            ShowDxfDocumentInformation(dxf);

            // Save the document as always.
            dxf.Save("PaperSpace.dxf");

#region CAUTION - This is subject to change in the future, use it with care

            // You cannot directly remove the ellipse from the document since it has been attached to a viewport
            bool ok = dxf.RemoveEntity(ellipse); // OK = false

            // If an entity has been attached to another, its reactor will point to its owner
            // This information is subject to change in the future to become a list, an entity can be attached to multiple objects;
            // but at the moment only the viewport clipping boundary make use of this.
            // This is the way AutoCad also handles hatch and dimension associativity, that I might implement in the future
            DxfObject reactor = ellipse.Reactors[0]; // in this case reactor points to viewport2

            // You need to delete the viewport instead. This deletes the viewport and the ellipse
            //dxf.RemoveEntity(viewport2);

            // another way of deleting the ellipse, is first to assign another clipping boundary to the viewport or just set it to null
            viewport2.ClippingBoundary = null;
            // now it will be possible to delete the ellipse. This will not delete the viewport.
            ok = dxf.RemoveEntity(ellipse); // OK = true

            // Save the document if you want to test the changes
            dxf.Save("PaperSpace.dxf");

#endregion

            DxfDocument dxfLoad = DxfDocument.Load("PaperSpace.dxf");

            // For every entity you can check its layout
            // The entity Owner will return the block to which it belongs, it can be a *Model_Space, *Paper_Space, ... or a common block if the entity is part of its geometry.
            // The block record stores information about the block and one of them is the layout, this mimics the way the dxf stores this information.
            // Remember only the internal blocks *Model_Space, *Paper_Space, *Paper_Space0, *Paper_Space1, ... have an associated layout,
            // all other blocks will return null is asked for block.Record.Layout
            Layout associatedLayout = dxfLoad.Lines[0].Owner.Record.Layout;

            // or you can get the complete list of entities of a layout
            foreach (Layout layout in dxfLoad.Layouts)
            {
                List<DxfObject> entities = dxfLoad.Layouts.GetReferences(layout.Name); 
            }

            // You can also remove any layout from the list, except the "Model".
            // Remember all entities that has been added to this layout will also be removed.
            // This mimics the behavior in AutoCad, when a layout is deleted all entities in it will also be deleted.
            dxfLoad.Layouts.Remove(layout1.Name);

            Layout layout4 = (Layout) layout2.Clone("Layout4");
            dxfLoad.Layouts.Add(layout4);

            ShowDxfDocumentInformation(dxfLoad);

            dxfLoad.Save("PaperSpace removed.dxf");

        }