예제 #1
0
        /// <summary>
        /// Creates a new Leader that is a copy of the current instance.
        /// </summary>
        /// <returns>A new Leader that is a copy of this instance.</returns>
        public override object Clone()
        {
            Leader entity = new Leader(this.vertexes)
            {
                //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,
                //Leader properties
                Elevation     = this.elevation,
                Style         = (DimensionStyle)this.style.Clone(),
                ShowArrowhead = this.showArrowhead,
                PathType      = this.pathType,
                Offset        = this.offset,
                LineColor     = this.lineColor,
                Annotation    = (EntityObject)this.annotation.Clone(),
                hasHookLine   = this.hasHookLine
            };

            foreach (XData data in this.XData.Values)
            {
                entity.XData.Add((XData)data.Clone());
            }

            //entity.Update();
            return(entity);
        }
예제 #2
0
        /// <summary>
        /// Creates a new Leader that is a copy of the current instance.
        /// </summary>
        /// <returns>A new Leader that is a copy of this instance.</returns>
        public override object Clone()
        {
            Leader entity = new Leader(this.vertexes)
            {
                //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,
                //Leader properties
                Elevation     = this.elevation,
                Style         = (DimensionStyle)this.style.Clone(),
                ShowArrowhead = this.showArrowhead,
                PathType      = this.pathType,
                Offset        = this.offset,
                LineColor     = this.lineColor,
                Annotation    = (EntityObject)this.annotation?.Clone(),
                hasHookline   = this.hasHookline // do not call directly the property, the vertexes list already includes it if it has a hook line
            };

            foreach (DimensionStyleOverride styleOverride in this.StyleOverrides.Values)
            {
                object     copy;
                ICloneable value = styleOverride.Value as ICloneable;
                copy = value != null?value.Clone() : styleOverride.Value;

                entity.StyleOverrides.Add(new DimensionStyleOverride(styleOverride.Type, copy));
            }

            foreach (XData data in this.XData.Values)
            {
                entity.XData.Add((XData)data.Clone());
            }

            return(entity);
        }
예제 #3
0
 private void AddLeaderTextPositionXData(Leader leader)
 {
      MText mText = leader.Annotation as MText;
     if (mText != null)
     {
         XData xdataEntry;
         if (leader.XData.ContainsAppId(ApplicationRegistry.DefaultName))
         {
             xdataEntry = leader.XData[ApplicationRegistry.DefaultName];
             xdataEntry.XDataRecord.Clear();
         }
         else
         {
             xdataEntry = new XData(new ApplicationRegistry(ApplicationRegistry.DefaultName));
             leader.XData.Add(xdataEntry);
         }
         xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.String, "DSTYLE"));
         xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.ControlString, "{"));
         xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short)70));
         xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short)mText.AttachmentPoint));
         xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short)77));
         xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.Int16, (short)leader.TextVerticalPosition));
         xdataEntry.XDataRecord.Add(new XDataRecord(XDataCode.ControlString, "}"));
     }
 }
예제 #4
0
        private void WriteLeader(Leader leader)
        {
            this.chunk.Write(100, SubclassMarker.Leader);

            this.chunk.Write(3, leader.Style.Name);

            if(leader.ShowArrowhead)
                this.chunk.Write(71, (short)1);
            else
                this.chunk.Write(71, (short)0);

            this.chunk.Write(72, (short)leader.PathType);

            if (leader.Annotation != null)
            {
                switch (leader.Annotation.Type)
                {
                    case EntityType.MText:
                        this.chunk.Write(73, (short) 0);
                        break;
                    case EntityType.Insert:
                        this.chunk.Write(73, (short) 2);
                        break;
                    default:
                        this.chunk.Write(73, (short) 3);
                        break;
                }
            }
            else
            {
                this.chunk.Write(73, (short)3);                
            }

            Vector2 v = leader.Vertexes[leader.Vertexes.Count - 1] - leader.Vertexes[leader.Vertexes.Count - 2];
            if (v.Equals(Vector2.Zero)) throw new Exception(string.Format("The last and previous vertex of the leader with handle {0} cannot be the same", leader.Handle));
            int side = v.X < 0 ? -1 : 1;

            if (side < 0)
                this.chunk.Write(74, (short)1);
            else
                this.chunk.Write(74, (short)0);

            if (leader.HasHookline)
                this.chunk.Write(75, (short)1);
            else
                this.chunk.Write(75, (short)0);

            //this.chunk.Write(40, 0.0);
            //this.chunk.Write(41, 0.0);

            // in the dxf the leader vertexes list is in WCS
            Vector2 dir = side * Vector2.UnitX;
            List<Vector3> ocsVertexes = new List<Vector3>();
            if (leader.HasHookline)
            {
                Vector2 hook = leader.Vertexes[leader.Vertexes.Count - 1];
                MText text = leader.Annotation as MText;
                if (text != null)
                {
                    double rotation = text.Rotation*MathHelper.DegToRad;
                    double sin = Math.Sin(rotation);
                    double cos = Math.Cos(rotation);
                    dir = new Vector2(cos, sin);
                    Vector2 position = hook + new Vector2(side * leader.Style.DIMGAP * leader.Style.DIMSCALE, leader.Style.DIMGAP * leader.Style.DIMSCALE);
                    Vector2 a = hook - position;
                    a = MathHelper.Transform(a, rotation, CoordinateSystem.Object, CoordinateSystem.World);
                    hook = a + position;
                }

                Vector2 vertex = hook + dir * leader.Style.DIMASZ * leader.Style.DIMSCALE;
                for (int i = 0; i < leader.Vertexes.Count - 1; i++)
                    ocsVertexes.Add(new Vector3(leader.Vertexes[i].X, leader.Vertexes[i].Y, leader.Elevation));

                ocsVertexes.Add(new Vector3(vertex.X, vertex.Y, leader.Elevation));
                ocsVertexes.Add(new Vector3(hook.X, hook.Y, leader.Elevation));
            }
            else
            {
                for (int i = 0; i < leader.Vertexes.Count; i++)
                    ocsVertexes.Add(new Vector3(leader.Vertexes[i].X, leader.Vertexes[i].Y, leader.Elevation));
            }

            IList<Vector3> wcsVertexes = MathHelper.Transform(ocsVertexes, leader.Normal, CoordinateSystem.Object, CoordinateSystem.World);
            this.chunk.Write(76, (short)wcsVertexes.Count);
            foreach (Vector3 vertex in wcsVertexes)
            {
                this.chunk.Write(10, vertex.X);
                this.chunk.Write(20, vertex.Y);
                this.chunk.Write(30, vertex.Z);
            }

            this.chunk.Write(77, leader.LineColor.Index);

            if (leader.Annotation != null) this.chunk.Write(340, leader.Annotation.Handle);

            this.chunk.Write(210, leader.Normal.X);
            this.chunk.Write(220, leader.Normal.Y);
            this.chunk.Write(230, leader.Normal.Z);

            Vector3 xDir = MathHelper.Transform(new Vector3(dir.X, dir.Y, 0.0), leader.Normal, CoordinateSystem.Object, CoordinateSystem.World);
            xDir.Normalize();
            this.chunk.Write(211, xDir.X);
            this.chunk.Write(221, xDir.Y);
            this.chunk.Write(231, xDir.Z);

            Vector3 wcsOffset = MathHelper.Transform(new Vector3(leader.Offset.X, leader.Offset.Y, leader.Elevation), leader.Normal, CoordinateSystem.Object, CoordinateSystem.World);
            this.chunk.Write(213, wcsOffset.X);
            this.chunk.Write(223, wcsOffset.Y);
            this.chunk.Write(233, wcsOffset.Z);

            // leader text vertical position info
            this.AddLeaderTextPositionXData(leader);

            this.WriteXData(leader.XData);
        }
예제 #5
0
 private void Leader_DimStyleOverrideRemoved(Leader sender, DimensionStyleOverrideChangeEventArgs e)
 {
     switch (e.Item.Type)
     {
         case DimensionStyleOverrideType.DimLineLinetype:
         case DimensionStyleOverrideType.ExtLine1Linetype:
         case DimensionStyleOverrideType.ExtLine2Linetype:
             Linetype linetype = (Linetype) e.Item.Value;
             this.linetypes.References[linetype.Name].Remove(sender);
             break;
         case DimensionStyleOverrideType.LeaderArrow:
         case DimensionStyleOverrideType.DimArrow1:
         case DimensionStyleOverrideType.DimArrow2:
             Block block = (Block) e.Item.Value;
             if (block == null)
                 return; // the block might be defined as null to indicate that the default arrowhead will be used
             this.blocks.References[block.Name].Remove(sender);
             break;
         case DimensionStyleOverrideType.TextStyle:
             TextStyle style = (TextStyle) e.Item.Value;
             this.textStyles.References[style.Name].Remove(sender);
             break;
     }
 }
예제 #6
0
        private void Leader_DimStyleChanged(Leader sender, TableObjectChangedEventArgs<DimensionStyle> e)
        {
            this.dimStyles.References[e.OldValue.Name].Remove(sender);

            e.NewValue = this.dimStyles.Add(e.NewValue);
            this.dimStyles.References[e.NewValue.Name].Add(sender);
        }
예제 #7
0
파일: DxfReader.cs 프로젝트: Core2D/netdxf
        private Leader ReadLeader()
        {
            DimensionStyle style = DimensionStyle.Default;
            bool showArrowhead = true;
            LeaderPathType path = LeaderPathType.StraightLineSegements;
            bool hasHookline = false;
            List<Vector3> wcsVertexes = null;
            AciColor lineColor = AciColor.ByLayer;
            string annotation = string.Empty;
            Vector3 normal = Vector3.UnitZ;
            double elevation = 0.0;
            Vector3 offset = Vector3.Zero;

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

            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 3:
                        string styleName = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        if (string.IsNullOrEmpty(styleName))
                            styleName = this.doc.DrawingVariables.DimStyle;
                        style = this.GetDimensionStyle(styleName);
                        this.chunk.Next();
                        break;
                    case 71:
                        showArrowhead = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 72:
                        path = (LeaderPathType) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 73:
                        this.chunk.Next();
                        break;
                    case 74:
                        this.chunk.Next();
                        break;
                    case 75:
                        hasHookline = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 76:
                        this.chunk.Next();
                        break;
                    case 77:
                        lineColor = AciColor.FromCadIndex(this.chunk.ReadShort());
                        this.chunk.Next();
                        break;
                    case 10:
                        wcsVertexes = this.ReadLeaderVertexes();
                        break;
                    case 340:
                        annotation = this.chunk.ReadHex();
                        this.chunk.Next();
                        break;
                    case 210:
                        normal.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 220:
                        normal.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 230:
                        normal.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 213:
                        offset.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 223:
                        offset.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 233:
                        offset.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;
                }
            }

            if (wcsVertexes == null)
                return null;

            if (hasHookline && wcsVertexes.Count >= 3)
            {
                wcsVertexes.RemoveAt(wcsVertexes.Count - 2);
            }
            IList<Vector3> ocsVertexes = MathHelper.Transform(wcsVertexes, normal, CoordinateSystem.World, CoordinateSystem.Object);
            List<Vector2> vertexes = new List<Vector2>();
            foreach (Vector3 v in ocsVertexes)
            {
                vertexes.Add(new Vector2(v.X, v.Y));
                elevation = v.Z;
            }

            // The text vertical position is stored in the Leader extended data
            Vector3 ocsOffset = MathHelper.Transform(offset, normal, CoordinateSystem.World, CoordinateSystem.Object);
            Leader leader = new Leader(vertexes)
            {
                Style = style,
                ShowArrowhead = showArrowhead,
                PathType = path,
                LineColor = lineColor,
                Elevation = elevation,
                Normal = normal,
                Offset = new Vector2(ocsOffset.X, ocsOffset.Y),
                TextVerticalPosition = LeaderTextVerticalPosition.Above
            };

            leader.XData.AddRange(xData);

            // this is for post-processing, the annotation entity might appear after the leader
            this.leaderAnnotation.Add(leader, annotation);

            return leader;
        }
예제 #8
0
        private static void LeaderEntity()
        {
            // a basic text annotation
            List<Vector2> vertexes1 = new List<Vector2>();
            vertexes1.Add(new Vector2(0, 0));
            vertexes1.Add(new Vector2(-5, 5));
            Leader leader1 = new Leader("Sample annotation", vertexes1);
            leader1.Offset = new Vector2(0, -0.5);
            // We need to call manually the method Update if the annotation position is modified,
            // or the Leader properties like Style, Normal, Elevation, Annotation, TextVerticalPosition, and/or Offset.
            leader1.Update();

            // leader not in the XY plane
            Leader cloned = (Leader) leader1.Clone();
            cloned.Normal = new Vector3(1);
            cloned.Elevation = 5;

            // a text annotation with style
            DimensionStyle style = new DimensionStyle("MyStyle");
            style.DIMCLRD = AciColor.Green;
            style.DIMCLRT = AciColor.Blue;
            style.DIMLDRBLK = DimensionArrowhead.DotBlank;
            style.DIMSCALE = 2.0;

            List<Vector2> vertexes2 = new List<Vector2>();
            vertexes2.Add(new Vector2(0, 0));
            vertexes2.Add(new Vector2(5, 5));
            Leader leader2 = new Leader("Sample annotation", vertexes2, style);
            ((MText) leader2.Annotation).AttachmentPoint = MTextAttachmentPoint.MiddleLeft;
            leader2.TextVerticalPosition = LeaderTextVerticalPosition.Centered;
            leader2.Update();

            // a tolerance annotation,
            // its hook point will always appear at the left since I have no way of measuring the real length of a tolerance,
            // and it has no alignment options as the MText does.
            List<Vector2> vertexes3 = new List<Vector2>();
            vertexes3.Add(new Vector2(0));
            vertexes3.Add(new Vector2(5, -5));
            vertexes3.Add(new Vector2(7.5, -5));
            ToleranceEntry entry = new ToleranceEntry
            {
                GeometricSymbol = ToleranceGeometricSymbol.Symmetry,
                Tolerance1 = new ToleranceValue(true, "12.5", ToleranceMaterialCondition.Maximum)
            };
            Leader leader3 = new Leader(entry, vertexes3);

            // a block annotation
            Block block = new Block("BlockAnnotation");
            block.Entities.Add(new Line(new Vector2(-1, -1), new Vector2(1, 1)));
            block.Entities.Add(new Line(new Vector2(-1, 1), new Vector2(1, -1)));
            block.Entities.Add(new Circle(Vector2.Zero, 0.5));

            List<Vector2> vertexes4 = new List<Vector2>();
            vertexes4.Add(new Vector2(0));
            vertexes4.Add(new Vector2(-5, -5));
            vertexes4.Add(new Vector2(-7.5, -5));
            Leader leader4 = new Leader(block, vertexes4);
            // change the leader offset to move  the leader hook (the last vertex of the leader vertexes list) in relation to the annotation position.
            leader4.Offset = new Vector2(1, 1);
            leader4.Update();

            // add entities to the document
            DxfDocument doc = new DxfDocument();
            doc.AddEntity(cloned);
            doc.AddEntity(leader1);
            doc.AddEntity(leader2);
            doc.AddEntity(leader3);
            doc.AddEntity(leader4);
            doc.Save("Leader.dxf");

            DxfDocument test = DxfDocument.Load("Leader.dxf");
            test.Save("test.dxf");
        }
예제 #9
0
        private Leader ReadLeader()
        {
            DimensionStyle style = DimensionStyle.Default;
            bool showArrowhead = true;
            LeaderPathType path = LeaderPathType.StraightLineSegements;
            bool hasHookline = false;
            List<Vector3> wcsVertexes = null;
            AciColor lineColor = AciColor.ByLayer;
            string annotation = string.Empty;
            Vector3 normal = Vector3.UnitZ;
            double elevation = 0.0;
            Vector3 offset = Vector3.Zero;

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

            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 3:
                        string styleName = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        if (string.IsNullOrEmpty(styleName))
                            styleName = this.doc.DrawingVariables.DimStyle;
                        style = this.GetDimensionStyle(styleName);
                        this.chunk.Next();
                        break;
                    case 71:
                        showArrowhead = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 72:
                        path = (LeaderPathType) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 73:
                        this.chunk.Next();
                        break;
                    case 74:
                        this.chunk.Next();
                        break;
                    case 75:
                        hasHookline = this.chunk.ReadShort() != 0;
                        this.chunk.Next();
                        break;
                    case 76:
                        this.chunk.Next();
                        break;
                    case 77:
                        lineColor = AciColor.FromCadIndex(this.chunk.ReadShort());
                        this.chunk.Next();
                        break;
                    case 10:
                        wcsVertexes = this.ReadLeaderVertexes();
                        break;
                    case 340:
                        annotation = this.chunk.ReadString();
                        this.chunk.Next();
                        break;
                    case 210:
                        normal.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 220:
                        normal.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 230:
                        normal.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 213:
                        offset.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 223:
                        offset.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 233:
                        offset.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(appId);
                        xData.Add(data);
                        break;
                    default:
                        if (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
                            throw new DxfInvalidCodeValueEntityException(this.chunk.Code, this.chunk.ReadString(),
                                "The extended data of an entity must start with the application registry code.");
                        this.chunk.Next();
                        break;
                }
            }

            if (hasHookline) wcsVertexes.RemoveAt(wcsVertexes.Count - 2);
            IList<Vector3> ocsVertexes = MathHelper.Transform(wcsVertexes, normal, CoordinateSystem.World, CoordinateSystem.Object);
            List<Vector2> vertexes = new List<Vector2>();
            foreach (Vector3 v in ocsVertexes)
            {
                vertexes.Add(new Vector2(v.X, v.Y));
                elevation = v.Z;
            }

            // The text vertical position is stored in the Leader extended data
            Vector3 ocsOffset = MathHelper.Transform(offset, normal, CoordinateSystem.World, CoordinateSystem.Object);
            Leader leader = new Leader(vertexes)
            {
                Style = style,
                ShowArrowhead = showArrowhead,
                PathType = path,
                LineColor = lineColor,
                Elevation = elevation,
                Normal = normal,
                Offset = new Vector2(ocsOffset.X, ocsOffset.Y)
            };

            leader.XData.AddRange(xData);

            // this is for post-processing, the annotation entity might appear after the leader
            this.leaderAnnotation.Add(leader, annotation);

            LeaderTextVerticalPosition txtPosition = LeaderTextVerticalPosition.Above;
            XData txtPosData;
            if (leader.XData.TryGetValue(ApplicationRegistry.Default.Name, out txtPosData))
            {
                bool dstyleInfo = false;
                List<XDataRecord>.Enumerator entries = txtPosData.XDataRecord.GetEnumerator();
                while (entries.MoveNext())
                {
                    XDataRecord data = entries.Current;
                    // in this XDataRecord is where the leader text vertical position is stored
                    if (data.Code == XDataCode.String)
                    {
                        if (string.Equals((string)data.Value, "DSTYLE", StringComparison.OrdinalIgnoreCase)) dstyleInfo = true;
                    }

                    if (data.Code == XDataCode.Int16)
                    {
                        // the leader text vertical position is stored in value 77
                        if ((short)data.Value == 77 && dstyleInfo)
                        {
                            if (entries.MoveNext())
                                    data = entries.Current;
                                else // unexpected end of the DSTYLE information         
                                    break;
                            txtPosition = (LeaderTextVerticalPosition)(short)data.Value;
                            break;
                        }
                    }
                }
            }

            leader.TextVerticalPosition = txtPosition;

            return leader;
        }
예제 #10
0
        /// <summary>
        /// Creates a new Leader that is a copy of the current instance.
        /// </summary>
        /// <returns>A new Leader that is a copy of this instance.</returns>
        public override object Clone()
        {
            Leader entity = new Leader(this.vertexes)
            {
                //EntityObject properties
                Layer = (Layer)this.layer.Clone(),
                LineType = (LineType)this.lineType.Clone(),
                Color = (AciColor)this.color.Clone(),
                Lineweight = (Lineweight)this.lineweight.Clone(),
                Transparency = (Transparency)this.transparency.Clone(),
                LineTypeScale = this.lineTypeScale,
                Normal = this.normal,
                //Leader properties
                Elevation = this.elevation,
                Style = (DimensionStyle) this.style.Clone(),
                ShowArrowhead = this.showArrowhead,
                PathType = this.pathType,
                hasHookline = this.hasHookline,
                Offset = this.offset,
                LineColor = this.lineColor,
                Annotation = (EntityObject) this.annotation.Clone()
            };

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

            entity.Update();
            return entity;
        }