private static void DimensionNestedBlock() { DxfDocument dxf = new DxfDocument(); Vector3 p1 = new Vector3(0, 0, 0); Vector3 p2 = new Vector3(5, 5, 0); Line line = new Line(p1, p2); DimensionStyle myStyle = new DimensionStyle("MyStyle"); myStyle.DIMPOST = "<>mm"; myStyle.DIMDEC = 2; LinearDimension dim = new LinearDimension(line, 7, 0.0, myStyle); Block nestedBlock = new Block("NestedBlock"); nestedBlock.Entities.Add(line); Insert nestedIns = new Insert(nestedBlock); Block block = new Block("MyBlock"); block.Entities.Add(dim); block.Entities.Add(nestedIns); Insert ins = new Insert(block); ins.Position = new Vector3(10, 10, 0); dxf.AddEntity(ins); Circle circle = new Circle(p2, 5); Block block2 = new Block("MyBlock2"); block2.Entities.Add(circle); Insert ins2 = new Insert(block2); ins2.Position = new Vector3(-10, -10, 0); dxf.AddEntity(ins2); Block block3 = new Block("MyBlock3"); block3.Entities.Add((EntityObject)ins.Clone()); block3.Entities.Add((EntityObject)ins2.Clone()); Insert ins3 = new Insert(block3); ins3.Position = new Vector3(-10, 10, 0); dxf.AddEntity(ins3); dxf.Save("nested blocks.dxf"); dxf = DxfDocument.Load("nested blocks.dxf"); dxf.Save("nested blocks.dxf"); }
/// <summary> /// Creates a new LinearDimension that is a copy of the current instance. /// </summary> /// <returns>A new LinearDimension that is a copy of this instance.</returns> public override object Clone() { LinearDimension entity = new LinearDimension { //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, //Dimension properties Style = (DimensionStyle)this.style.Clone(), AttachmentPoint = this.attachmentPoint, LineSpacingStyle = this.lineSpacingStyle, LineSpacingFactor = this.lineSpacing, //LinearDimension properties FirstReferencePoint = this.start, SecondReferencePoint = this.end, Rotation = this.rotation, Offset = this.offset }; foreach (XData data in this.XData.Values) { entity.XData.Add((XData)data.Clone()); } return(entity); }
/// <summary> /// Creates a new LinearDimension that is a copy of the current instance. /// </summary> /// <returns>A new LinearDimension that is a copy of this instance.</returns> public override object Clone() { LinearDimension entity = new LinearDimension { //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, //Dimension properties Style = (DimensionStyle)this.Style.Clone(), AttachmentPoint = this.AttachmentPoint, LineSpacingStyle = this.LineSpacingStyle, LineSpacingFactor = this.LineSpacingFactor, //LinearDimension properties FirstReferencePoint = this.firstRefPoint, SecondReferencePoint = this.secondRefPoint, Rotation = this.rotation, Offset = this.offset, Elevation = this.Elevation }; foreach (XData data in this.XData.Values) { entity.XData.Add((XData)data.Clone()); } return(entity); }
/// <summary> /// Creates a new LinearDimension that is a copy of the current instance. /// </summary> /// <returns>A new LinearDimension that is a copy of this instance.</returns> public override object Clone() { LinearDimension entity = new LinearDimension { //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, //Dimension properties Style = (DimensionStyle)this.Style.Clone(), DefinitionPoint = this.DefinitionPoint, TextReferencePoint = this.TextReferencePoint, TextPositionManuallySet = this.TextPositionManuallySet, TextRotation = this.TextRotation, AttachmentPoint = this.AttachmentPoint, LineSpacingStyle = this.LineSpacingStyle, LineSpacingFactor = this.LineSpacingFactor, UserText = this.UserText, //LinearDimension properties FirstReferencePoint = this.firstRefPoint, SecondReferencePoint = this.secondRefPoint, Rotation = this.rotation, Offset = this.offset, Elevation = this.Elevation }; 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); }
public static void DimensionUserText() { DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010); Vector3 p2 = new Vector3(0, 0, 0); Vector3 p1 = new Vector3(5, 0, 0); Line line1 = new Line(p1, p2) { Layer = new Layer("Reference line") { Color = AciColor.Green } }; dxf.AddEntity(line1); DimensionStyle style = new DimensionStyle("MyStyle"); double offset = 0.75; LinearDimension dim = new LinearDimension(line1, offset, 0, style); dim.UserText = null; // 5.00 (this is the default behavior) dxf.AddEntity(dim); dim = new LinearDimension(line1, 2 * offset, 0, style); dim.UserText = string.Empty; // 5.00 (same behavior as null) dxf.AddEntity(dim); dim = new LinearDimension(line1, 3 * offset, 0, style); dim.UserText = " "; // No dimension text will be drawn (one blank space) dxf.AddEntity(dim); dim = new LinearDimension(line1, 4 * offset, 0, style); dim.UserText = "<>"; // 5.00 (the characters <> will be substituted with the style.DIMPOST property) dxf.AddEntity(dim); dim = new LinearDimension(line1, 5 * offset, 0, style); dim.UserText = "Length: <> mm"; // Length: 5.00 mm (the characters <> will be substituted with the style.DIMPOST property) dxf.AddEntity(dim); dim = new LinearDimension(line1, 6 * offset, 0, style); dim.UserText = "User text"; // User text dxf.AddEntity(dim); dxf.Save("DimensionUserText.dxf"); }
public static Block Build(LinearDimension dim, string name) { double measure = dim.Measurement; bool reversed = false; DimensionStyle style = BuildDimensionStyleOverride(dim); List<EntityObject> entities = new List<EntityObject>(); Vector2 ref1 = dim.FirstReferencePoint; Vector2 ref2 = dim.SecondReferencePoint; Vector2 midRef = Vector2.MidPoint(ref1, ref2); double dimRotation = dim.Rotation*MathHelper.DegToRad; Vector2 dimRef1tmp = new Vector2(-measure*0.5, dim.Offset); Vector2 dimRef2tmp = new Vector2(measure*0.5, dim.Offset); Vector2 dimRef1 = MathHelper.Transform(dimRef1tmp, dimRotation, CoordinateSystem.Object, CoordinateSystem.World) + midRef; Vector2 dimRef2 = MathHelper.Transform(dimRef2tmp, dimRotation, CoordinateSystem.Object, CoordinateSystem.World) + midRef; Vector2 midDim = Vector2.MidPoint(dimRef1, dimRef2); double relativeAngle = Vector2.AngleBetween(ref2 - ref1, dimRef2 - dimRef1); if (relativeAngle > MathHelper.HalfPI && relativeAngle <= MathHelper.ThreeHalfPI) { Vector2 tmp = ref1; ref1 = ref2; ref2 = tmp; reversed = true; } // reference points Layer defPointLayer = new Layer("Defpoints") {Plot = false}; entities.Add(new Point(ref1) {Layer = defPointLayer}); entities.Add(new Point(ref2) {Layer = defPointLayer}); entities.Add(reversed ? new Point(dimRef1) {Layer = defPointLayer} : new Point(dimRef2) {Layer = defPointLayer}); // dimension line entities.Add(DimensionLine(dimRef1, dimRef2, dimRotation, style)); // extension lines Vector2 dirDimRef = Vector2.Normalize(dimRef2 - dimRef1); Vector2 perpDimRef = Vector2.Perpendicular(dirDimRef); Vector2 v; v = dimRef1 - ref1; Vector2 dirRef1 = MathHelper.IsZero(Vector2.DotProduct(v, v)) ? perpDimRef : Vector2.Normalize(dimRef1 - ref1); v = dimRef2 - ref2; Vector2 dirRef2 = MathHelper.IsZero(Vector2.DotProduct(v, v)) ? -perpDimRef : Vector2.Normalize(dimRef2 - ref2); double dimexo = style.ExtLineOffset*style.DimScaleOverall; double dimexe = style.ExtLineExtend*style.DimScaleOverall; if (!style.ExtLine1) entities.Add(ExtensionLine(ref1 + dimexo*dirRef1, dimRef1 + dimexe*dirRef1, style, style.ExtLine1Linetype)); if (!style.ExtLine2) entities.Add(ExtensionLine(ref2 + dimexo*dirRef2, dimRef2 + dimexe*dirRef2, style, style.ExtLine2Linetype)); // dimension arrowheads if (reversed) { entities.Add(StartArrowHead(dimRef2, dimRotation, style)); entities.Add(EndArrowHead(dimRef1, dimRotation + MathHelper.PI, style)); } else { entities.Add(StartArrowHead(dimRef1, dimRotation + MathHelper.PI, style)); entities.Add(EndArrowHead(dimRef2, dimRotation, style)); } // dimension text string text = FormatDimensionText(measure, false, dim.UserText, style, dim.Owner.Record.Layout); double textRot = dimRotation; if (textRot > MathHelper.HalfPI && textRot <= MathHelper.ThreeHalfPI) textRot += MathHelper.PI; MText mText = DimensionText(Vector2.Polar(midDim, style.TextOffset*style.DimScaleOverall, textRot + MathHelper.HalfPI), textRot, text, style); if (mText != null) entities.Add(mText); Vector3 defPoint = reversed ? new Vector3(dimRef1.X, dimRef1.Y, dim.Elevation) : new Vector3(dimRef2.X, dimRef2.Y, dim.Elevation); dim.DefinitionPoint = MathHelper.Transform(defPoint, dim.Normal, CoordinateSystem.Object, CoordinateSystem.World); dim.MidTextPoint = new Vector3(midDim.X, midDim.Y, dim.Elevation); // this value is in OCS // drawing block return new Block(name, false, entities, null) {Flags = BlockTypeFlags.AnonymousBlock}; }
public static Block Build(LinearDimension dim, string name) { Vector2 ref1; Vector2 ref2; Vector2 dimRef1; Vector2 dimRef2; short reversed; double measure = dim.Value; DimensionStyle style = dim.Style; // we will build the dimension block in object coordinates with normal the dimension normal Vector3 refPoint; refPoint = MathHelper.Transform(dim.FirstReferencePoint, dim.Normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object); ref1 = new Vector2(refPoint.X, refPoint.Y); double elevation = refPoint.Z; refPoint = MathHelper.Transform(dim.SecondReferencePoint, dim.Normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object); ref2 = new Vector2(refPoint.X, refPoint.Y); double dimRotation = dim.Rotation * MathHelper.DegToRad; double refAngle = Vector2.Angle(ref1, ref2); reversed = ReverseEnds(ref1, ref2, dimRotation, refAngle); Vector2 midRef = Vector2.MidPoint(ref1, ref2); Vector2 midDim = Vector2.Polar(midRef, dim.Offset, dimRotation + MathHelper.HalfPI); dimRef1 = Vector2.Polar(midDim, -reversed * measure * 0.5, dimRotation); dimRef2 = Vector2.Polar(midDim, reversed * measure * 0.5, dimRotation); // reference points Layer defPointLayer = new Layer("Defpoints") { Plot = false }; Point ref1Point = new Point(ref1) { Layer = defPointLayer }; Point ref2Point = new Point(ref2) { Layer = defPointLayer }; Point defPoint = new Point(dimRef2) { Layer = defPointLayer }; // dimension line Line dimLine = DimensionLine(dimRef1, dimRef2, dimRotation, reversed, style); // extension lines double dimexo = Math.Sign(dim.Offset) * style.DIMEXO * style.DIMSCALE; double dimexe = Math.Sign(dim.Offset) * style.DIMEXE * style.DIMSCALE; double extRot = dimRotation + MathHelper.HalfPI; Line ext1Line = null; if (!style.DIMSE1) { ext1Line = ExtensionLine(Vector2.Polar(ref1, dimexo, extRot), Vector2.Polar(dimRef1, dimexe, extRot), style, style.DIMLTEX1); } Line ext2Line = null; if (!style.DIMSE2) { ext2Line = ExtensionLine(Vector2.Polar(ref2, dimexo, extRot), Vector2.Polar(dimRef2, dimexe, extRot), style, style.DIMLTEX2); } EntityObject arrow1 = StartArrowHead(dimRef1, Vector2.Angle(dimRef2, dimRef1), style); EntityObject arrow2 = EndArrowHead(dimRef2, Vector2.Angle(dimRef1, dimRef2), style); // dimension text string text = FormatDimensionText(measure, false, style); MText mText = DimensionText(Vector2.Polar(midDim, style.DIMGAP * style.DIMSCALE, extRot), dimRotation, text, style); dim.DefinitionPoint = MathHelper.Transform(new Vector3(dimRef2.X, dimRef2.Y, elevation), dim.Normal, MathHelper.CoordinateSystem.Object, MathHelper.CoordinateSystem.World); dim.MidTextPoint = new Vector3(midDim.X, midDim.Y, elevation); // this value is in OCS // drawing block Block block = new Block(name, false) { Flags = BlockTypeFlags.AnonymousBlock }; block.Entities.Add(ref1Point); block.Entities.Add(ref2Point); block.Entities.Add(defPoint); block.Entities.Add(ext1Line); block.Entities.Add(ext2Line); block.Entities.Add(dimLine); block.Entities.Add(arrow1); block.Entities.Add(arrow2); block.Entities.Add(mText); return(block); }
private void WriteLinearDimension(LinearDimension dim) { this.chunk.Write(100, SubclassMarker.AlignedDimension); IList<Vector3> wcsPoints = MathHelper.Transform( new[] { new Vector3(dim.FirstReferencePoint.X, dim.FirstReferencePoint.Y, dim.Elevation), new Vector3(dim.SecondReferencePoint.X, dim.SecondReferencePoint.Y, dim.Elevation) }, dim.Normal, CoordinateSystem.Object, CoordinateSystem.World); this.chunk.Write(13, wcsPoints[0].X); this.chunk.Write(23, wcsPoints[0].Y); this.chunk.Write(33, wcsPoints[0].Z); this.chunk.Write(14, wcsPoints[1].X); this.chunk.Write(24, wcsPoints[1].Y); this.chunk.Write(34, wcsPoints[1].Z); this.chunk.Write(50, dim.Rotation); // AutoCAD is unable to recognized code 52 for oblique dimension line even though it appears as valid in the dxf documentation // this.chunk.Write(52, dim.ObliqueAngle); this.chunk.Write(100, SubclassMarker.LinearDimension); this.WriteXData(dim.XData); }
private LinearDimension ReadLinearDimension(Vector3 defPoint, Vector3 normal) { Vector3 firstRef = Vector3.Zero; Vector3 secondRef = Vector3.Zero; double rot = 0.0; List<XData> xData = new List<XData>(); while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 13: firstRef.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 23: firstRef.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 33: firstRef.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 14: secondRef.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 24: secondRef.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 34: secondRef.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 50: rot = this.chunk.ReadDouble(); this.chunk.Next(); break; case 52: // AutoCAD is unable to recognized code 52 for oblique dimension line even though it appears as valid in the dxf documentation 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; } } IList<Vector3> ocsPoints = MathHelper.Transform( new List<Vector3> { firstRef, secondRef, defPoint }, normal, CoordinateSystem.World, CoordinateSystem.Object); LinearDimension entity = new LinearDimension { FirstReferencePoint = new Vector2(ocsPoints[0].X, ocsPoints[0].Y), SecondReferencePoint = new Vector2(ocsPoints[1].X, ocsPoints[1].Y), Rotation = rot, Elevation = ocsPoints[2].Z, Normal = normal }; Vector2 point = new Vector2(ocsPoints[2].X, ocsPoints[2].Y); entity.SetDimensionLinePosition(point); entity.XData.AddRange(xData); return entity; }
private LinearDimension ReadLinearDimension(Vector3 defPoint) { Vector3 firtRef = Vector3.Zero; Vector3 secondRef = Vector3.Zero; double rot = 0.0; List<XData> xData = new List<XData>(); while (this.chunk.Code != 0) { switch (this.chunk.Code) { case 13: firtRef.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 23: firtRef.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 33: firtRef.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 14: secondRef.X = this.chunk.ReadDouble(); this.chunk.Next(); break; case 24: secondRef.Y = this.chunk.ReadDouble(); this.chunk.Next(); break; case 34: secondRef.Z = this.chunk.ReadDouble(); this.chunk.Next(); break; case 50: rot = this.chunk.ReadDouble(); this.chunk.Next(); break; case 52: // AutoCAD is unable to recognized code 52 for oblique dimension line even though it appears as valid in the dxf documentation 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; } } Vector3 midPoint = Vector3.MidPoint(firtRef, secondRef); Vector3 origin = defPoint; Vector3 dir = new Vector3(Math.Cos(rot*MathHelper.DegToRad), Math.Sin(rot*MathHelper.DegToRad), 0.0); dir.Normalize(); double offset = MathHelper.PointLineDistance(midPoint, origin, dir); LinearDimension entity = new LinearDimension { FirstReferencePoint = firtRef, SecondReferencePoint = secondRef, Offset = offset, Rotation = rot }; entity.XData.AddRange(xData); return entity; }
public static Block Build(LinearDimension dim, string name) { Vector2 ref1; Vector2 ref2; Vector2 dimRef1; Vector2 dimRef2; short reversed; double measure = dim.Measurement; DimensionStyle style = dim.Style; List<EntityObject> entities = new List<EntityObject>(); // we will build the dimension block in object coordinates with normal the dimension normal Vector3 refPoint; refPoint = MathHelper.Transform(dim.FirstReferencePoint, dim.Normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object); ref1 = new Vector2(refPoint.X, refPoint.Y); double elevation = refPoint.Z; refPoint = MathHelper.Transform(dim.SecondReferencePoint, dim.Normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object); ref2 = new Vector2(refPoint.X, refPoint.Y); double dimRotation = dim.Rotation*MathHelper.DegToRad; double refAngle = Vector2.Angle(ref1, ref2); reversed = ReverseEnds(ref1, ref2, dimRotation, refAngle); Vector2 midRef = Vector2.MidPoint(ref1, ref2); Vector2 midDim = Vector2.Polar(midRef, dim.Offset, dimRotation + MathHelper.HalfPI); dimRef1 = Vector2.Polar(midDim, -reversed*measure*0.5, dimRotation); dimRef2 = Vector2.Polar(midDim, reversed*measure*0.5, dimRotation); // reference points Layer defPointLayer = new Layer("Defpoints") {Plot = false}; entities.Add(new Point(ref1) {Layer = defPointLayer}); entities.Add(new Point(ref2) {Layer = defPointLayer}); entities.Add(new Point(dimRef2) {Layer = defPointLayer}); // dimension line entities.Add(DimensionLine(dimRef1, dimRef2, dimRotation, reversed, style)); // extension lines double dimexo = Math.Sign(dim.Offset)*style.DIMEXO*style.DIMSCALE; double dimexe = Math.Sign(dim.Offset)*style.DIMEXE*style.DIMSCALE; double extRot = dimRotation + MathHelper.HalfPI; if (!style.DIMSE1) entities.Add(ExtensionLine(Vector2.Polar(ref1, dimexo, extRot), Vector2.Polar(dimRef1, dimexe, extRot), style, style.DIMLTEX1)); if (!style.DIMSE2) entities.Add(ExtensionLine(Vector2.Polar(ref2, dimexo, extRot), Vector2.Polar(dimRef2, dimexe, extRot), style, style.DIMLTEX2)); entities.Add(StartArrowHead(dimRef1, Vector2.Angle(dimRef2, dimRef1), style)); entities.Add(EndArrowHead(dimRef2, Vector2.Angle(dimRef1, dimRef2), style)); // dimension text string text = FormatDimensionText(measure, false, dim.UserText, style, dim.Owner.Record.Layout); MText mText = DimensionText(Vector2.Polar(midDim, style.DIMGAP * style.DIMSCALE, extRot), dimRotation, text, style); if (mText != null) entities.Add(mText); dim.DefinitionPoint = MathHelper.Transform(new Vector3(dimRef2.X, dimRef2.Y, elevation), dim.Normal, MathHelper.CoordinateSystem.Object, MathHelper.CoordinateSystem.World); dim.MidTextPoint = new Vector3(midDim.X, midDim.Y, elevation); // this value is in OCS // drawing block return new Block(name, false, entities, null) {Flags = BlockTypeFlags.AnonymousBlock}; }
private void WriteLinearDimension(LinearDimension dim) { this.chunk.Write(100, SubclassMarker.AlignedDimension); this.chunk.Write(13, dim.FirstReferencePoint.X); this.chunk.Write(23, dim.FirstReferencePoint.Y); this.chunk.Write(33, dim.FirstReferencePoint.Z); this.chunk.Write(14, dim.SecondReferencePoint.X); this.chunk.Write(24, dim.SecondReferencePoint.Y); this.chunk.Write(34, dim.SecondReferencePoint.Z); this.chunk.Write(50, dim.Rotation); // AutoCAD is unable to recognized code 52 for oblique dimension line even though it appears as valid in the dxf documentation // this.chunk.Write(52, dim.ObliqueAngle); this.chunk.Write(100, SubclassMarker.LinearDimension); this.WriteXData(dim.XData); }
private static void LinearDimension() { DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010); DimensionStyle myStyle = CreateDimStyle(); Vector3 p1 = new Vector3(0, -5, 0); Vector3 p2 = new Vector3(-5, 0, 0); Line line1 = new Line(p1, p2) { Layer = new Layer("Reference line") { Color = AciColor.Green } }; dxf.AddEntity(line1); double offset = 4; LinearDimension dimX1 = new LinearDimension(line1, offset, 0, myStyle); LinearDimension dimY1 = new LinearDimension(line1, offset, 90, myStyle); LinearDimension dim5 = new LinearDimension(line1, offset, -30, myStyle); LinearDimension dim6 = new LinearDimension(line1, offset, -60, myStyle); Vector3 p3 = new Vector3(6, -5, 0); Vector3 p4 = new Vector3(11, 0, 0); Line line2 = new Line(p3, p4) { Layer = new Layer("Reference line") { Color = AciColor.Green } }; dxf.AddEntity(line2); LinearDimension dimX2 = new LinearDimension(line2, offset, -30.0, myStyle); LinearDimension dimY2 = new LinearDimension(line2, offset, -60.0, myStyle); LinearDimension dim3 = new LinearDimension(line2, offset, 30.0, myStyle); LinearDimension dim4 = new LinearDimension(line2, offset, 60.0, myStyle); dxf.AddEntity(dimX1); dxf.AddEntity(dimY1); dxf.AddEntity(dimX2); dxf.AddEntity(dimY2); dxf.AddEntity(dim3); dxf.AddEntity(dim4); dxf.AddEntity(dim5); dxf.AddEntity(dim6); dxf.Save("dimension drawing.dxf"); //DxfDocument dwg = DxfDocument.Load("dimension drawing.dxf"); //dwg.Save("dimension drawing saved.dxf"); //Console.WriteLine("Press a key..."); //Console.ReadKey(); }
public static void DimensionsLinearAndAngularUnits() { DimensionStyle style = new DimensionStyle("MyStyle") { // DIMDEC defines the number of decimal places. // For Architectural and Fractional units the minimum fraction is defined by 1/2^DIMDEC. DIMDEC = 4, DIMFRAC = FractionFormatType.Horizontal, DIMLUNIT = LinearUnitType.Engineering, SuppressLinearTrailingZeros = true, SuppressZeroFeet = false, SuppressZeroInches = false, DIMLFAC = 10.0, // the round off to nearest DIMRND is applied to the linear dimension measurement after applying the scale DIMLFAC DIMRND = 0.025, DIMADEC = 2, DIMAUNIT = AngleUnitType.DegreesMinutesSeconds }; Layer layer = new Layer("Layer1") { Color = AciColor.Blue }; Vector3 p1 = new Vector3(0, 0, 0); Vector3 p2 = new Vector3(21.2548, 0, 0); LinearDimension dim = new LinearDimension(p1, p2, 4, 0, style); Vector2 s1 = new Vector2(-2, 2); Vector2 s2 = new Vector2(2, -2); Vector2 e1 = new Vector2(-1, -3); Vector2 e2 = new Vector2(1, 3); Line line1 = new Line(s1, s2) { Layer = layer }; Line line2 = new Line(e1, e2) { Layer = layer }; Angular2LineDimension dim1 = new Angular2LineDimension(line2, line1, 4, style); DxfDocument doc = new DxfDocument(); doc.AddEntity(dim); doc.AddEntity(dim1); doc.Save("DimensionsLinearAndAngularUnits.dxf"); DxfDocument dxf = DxfDocument.Load("DimensionsLinearAndAngularUnits.dxf"); }
public static void ModifyingDimensionGeometryAndStyle() { DimensionStyle style = new DimensionStyle("MyStyle"); Vector3 p1 = new Vector3(-2.5, 0, 0); Vector3 p2 = new Vector3(2.5, 0, 0); LinearDimension dim = new LinearDimension(p1, p2, 4, 0, style); // This is illegal. Trying to rebuild the dimension block before it has been added to a document will throw an exception //dim.RebuildBlock(); DxfDocument doc = new DxfDocument(); doc.AddEntity(dim); // modifying the dimension style dim.Style.DIMBLK = DimensionArrowhead.ArchitecturalTick; // if we make any change to the dimension style, we need to manually call the RebuildBlock method to reflect the new changes // since we will also modify the geometry of the dimension we will rebuild the block latter //dim.RebuildBlock(); // the same kind of procedure needs to be done when modifying the geometry of a dimension dim.FirstReferencePoint = new Vector3(-5.0, 0, 0); dim.SecondReferencePoint = new Vector3(5.0, 0, 0); // now that all necessary changes has been made, we will rebuild the block. // this is an expensive operation, use it only when need it. dim.Style.DIMBLK = DimensionArrowhead.Box; dim.Style.DIMBLK = DimensionArrowhead.ArchitecturalTick; Debug.Assert(ReferenceEquals(dim.Style.DIMBLK, doc.Blocks[dim.Style.DIMBLK.Name]), "References are not equal."); Debug.Assert(ReferenceEquals(style.DIMBLK, doc.Blocks[style.DIMBLK.Name]), "References are not equal."); //dim.Style.DIMBLK = null; // VERY IMPORTANT: If any change is made to the dimension geometry and/or its style, we need to rebuild the drawing representation // so the dimension block will reflect the new changes. This is only necessary for dimension that already belongs to a document. // This process is automatically called when a new dimension is added to a document. dim.Update(); Debug.Assert(ReferenceEquals(dim.Block, doc.Blocks[dim.Block.Name])); doc.Save("dimension.dxf"); Test("dimension.dxf"); }
private static void LinearDimensionDrawing() { DxfDocument dxf = new DxfDocument(); Vector3 p1 = new Vector3(0, 0, 0); Vector3 p2 = new Vector3(5, 5, 0); Line line = new Line(p1, p2); dxf.AddEntity(line); DimensionStyle myStyle = new DimensionStyle("MyStyle"); myStyle.DIMPOST = "<>mm"; myStyle.DIMDEC = 2; double offset = 7; LinearDimension dimX = new LinearDimension(line, offset,0.0, myStyle); dimX.Rotation += 30.0; LinearDimension dimY = new LinearDimension(line, offset, 90.0, myStyle); dimY.Rotation += 30.0; XData xdata = new XData(new ApplicationRegistry("other application")); xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf")); xdata.XDataRecord.Add(XDataRecord.OpenControlString); xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "Linear Dimension")); xdata.XDataRecord.Add(new XDataRecord(XDataCode.Real, 15.5)); xdata.XDataRecord.Add(new XDataRecord(XDataCode.Int32, 350)); xdata.XDataRecord.Add(XDataRecord.CloseControlString); dimX.XData.Add(xdata); dimY.XData.Add(xdata); dxf.AddEntity(dimX); dxf.AddEntity(dimY); dxf.Save("linear dimension.dxf"); // dxf = DxfDocument.Load("linear dimension.dxf"); }
private static void TextAndDimensionStyleUsesAndRemove() { DxfDocument dxf = new DxfDocument(); Layer layer1 = new Layer("Layer1"); layer1.Color = AciColor.Blue; layer1.LineType = LineType.Center; Layer layer2 = new Layer("Layer2"); layer2.Color = AciColor.Red; // blocks needs an special attention Layer layer3 = new Layer("Layer3"); layer3.Color = AciColor.Yellow; Circle circle = new Circle(Vector3.Zero, 15); // it is always recommended that all block entities will be located in layer 0, but this is up to the user. circle.Layer = new Layer("circle"); circle.Layer.Color = AciColor.Green; Block block = new Block("MyBlock"); block.Entities.Add(circle); AttributeDefinition attdef = new AttributeDefinition("NewAttribute"); block.AttributeDefinitions.Add(attdef); Insert insert = new Insert(block, new Vector2(5, 5)); insert.Attributes[0].Style = new TextStyle("Arial.ttf"); dxf.AddEntity(insert); dxf.Save("style.dxf"); DxfDocument dxf2; dxf2 = DxfDocument.Load("style.dxf"); dxf.RemoveEntity(circle); Vector3 p1 = new Vector3(0, 0, 0); Vector3 p2 = new Vector3(5, 5, 0); Line line = new Line(p1, p2); dxf.AddEntity(line); DimensionStyle myStyle = new DimensionStyle("MyStyle"); myStyle.DIMTXSTY = new TextStyle("Tahoma.ttf"); myStyle.DIMPOST = "<>mm"; myStyle.DIMDEC = 2; double offset = 7; LinearDimension dimX = new LinearDimension(line, offset, 0.0, myStyle); dimX.Rotation += 30.0; LinearDimension dimY = new LinearDimension(line, offset, 90.0, myStyle); dimY.Rotation += 30.0; dxf.AddEntity(dimX); dxf.AddEntity(dimY); dxf.Save("style2.dxf"); dxf2 = DxfDocument.Load("style2.dxf"); dxf.RemoveEntity(dimX); dxf.RemoveEntity(dimY); bool ok; // we can remove myStyle it was only referenced by dimX and dimY ok = dxf.DimensionStyles.Remove(myStyle.Name); // we cannot remove myStyle.TextStyle since it is in use by the internal blocks created by the dimension entities ok = dxf.Blocks.Remove(dimX.Block.Name); ok = dxf.Blocks.Remove(dimY.Block.Name); // no we can remove the unreferenced textStyle ok = dxf.TextStyles.Remove(myStyle.DIMTXSTY.Name); dxf.Save("style3.dxf"); dxf2 = DxfDocument.Load("style3.dxf"); }
//private static void ExplodeInsert() //{ // DxfDocument dxf = DxfDocument.Load("explode\\ExplodeInsertUniformScale.dxf"); // List<DxfObject> refs = dxf.Blocks.References["ExplodeBlock"]; // Insert insert = (Insert)refs[0]; // dxf.RemoveEntity(insert); // insert.Layer = new Layer("Original block"); // insert.Layer.Color = AciColor.DarkGrey; // dxf.AddEntity(insert); // List<Entity> explodedEntities = insert.Explode(); // dxf.AddEntity(explodedEntities); // dxf.Save("ExplodeInsert.dxf"); //} private static void LinearDimensionTests() { DxfDocument dxf1 = new DxfDocument(); Vector2 pt1 = new Vector2(15, -5); Vector2 pt2 = new Vector2(5, 5); double offset = 10; LinearDimension ld1z = new LinearDimension(pt1, pt2, offset, 30); LinearDimension ld2z = new LinearDimension(pt1, pt2, offset, 45); LinearDimension ld3z = new LinearDimension(pt1, pt2, offset, 90); LinearDimension ld4z = new LinearDimension(pt1, pt2, offset, 135); LinearDimension ld5z = new LinearDimension(pt1, pt2, offset, 180); LinearDimension ld6z = new LinearDimension(pt1, pt2, offset, 220); LinearDimension ld7z = new LinearDimension(pt2, pt1, offset, 270); dxf1.AddEntity(ld1z); dxf1.AddEntity(ld2z); dxf1.AddEntity(ld3z); dxf1.AddEntity(ld4z); dxf1.AddEntity(ld5z); dxf1.AddEntity(ld6z); dxf1.AddEntity(ld7z); Line line = new Line(pt1, pt2); line.Color = AciColor.Yellow; dxf1.AddEntity(line); dxf1.Save("test2.dxf"); DxfDocument dxf2 = new DxfDocument(); LinearDimension ld1 = new LinearDimension(new Vector2(0, 0), new Vector2(0, 15), 1, 90); LinearDimension ld1b = new LinearDimension(new Vector2(0, 0), new Vector2(0, 15), 1, 100); LinearDimension ld1c = new LinearDimension(new Vector2(0, 0), new Vector2(0, 15), 1, 80); LinearDimension ld2 = new LinearDimension(new Vector2(5, 15), new Vector2(5, 0), 1, 90); LinearDimension ld3 = new LinearDimension(new Vector2(10, 0), new Vector2(10, 15), -1, 270); LinearDimension ld4 = new LinearDimension(new Vector2(15, 0), new Vector2(15, 15), 1, 270); LinearDimension ld4b = new LinearDimension(new Vector2(15, 0), new Vector2(15, 15), 1, 300); LinearDimension ld4c = new LinearDimension(new Vector2(15, 0), new Vector2(15, 15), 1, 240); LinearDimension ld5 = new LinearDimension(new Vector2(15, 0), new Vector2(0, 0), 1, 0); LinearDimension ld6 = new LinearDimension(new Vector2(0, 0), new Vector2(15, 0), -1, 0); AlignedDimension ld1a = new AlignedDimension(new Vector2(0, 0), new Vector2(0, 15), 1); ld1a.Color = AciColor.Yellow; AlignedDimension ld2a = new AlignedDimension(new Vector2(5, 15), new Vector2(5, 0), 1); ld2a.Color = AciColor.Yellow; AlignedDimension ld3a = new AlignedDimension(new Vector2(10, 0), new Vector2(10, 15), -1); ld3a.Color = AciColor.Yellow; AlignedDimension ld4a = new AlignedDimension(new Vector2(15, 0), new Vector2(15, 15), 1); ld4a.Color = AciColor.Yellow; AlignedDimension ld5a = new AlignedDimension(new Vector2(15, 0), new Vector2(0, 0), 1); ld5a.Color = AciColor.Yellow; AlignedDimension ld6a = new AlignedDimension(new Vector2(0, 0), new Vector2(15, 0), -1); ld6a.Color = AciColor.Yellow; dxf2.AddEntity(ld1); dxf2.AddEntity(ld1b); dxf2.AddEntity(ld1c); dxf2.AddEntity(ld2); dxf2.AddEntity(ld3); dxf2.AddEntity(ld4); dxf2.AddEntity(ld4b); dxf2.AddEntity(ld4c); dxf2.AddEntity(ld5); dxf2.AddEntity(ld6); dxf2.AddEntity(ld1a); dxf2.AddEntity(ld2a); dxf2.AddEntity(ld3a); dxf2.AddEntity(ld4a); dxf2.AddEntity(ld5a); dxf2.AddEntity(ld6a); dxf2.Save("test1.dxf"); }
public static Block Build(LinearDimension dim, string name) { Vector2 ref1; Vector2 ref2; Vector2 dimRef1; Vector2 dimRef2; double measure = dim.Measurement; bool reversed = false; DimensionStyle style = dim.Style; List <EntityObject> entities = new List <EntityObject>(); // we will build the dimension block in object coordinates with normal the dimension normal Vector3 refPoint; refPoint = MathHelper.Transform(dim.FirstReferencePoint, dim.Normal, CoordinateSystem.World, CoordinateSystem.Object); ref1 = new Vector2(refPoint.X, refPoint.Y); double elevation = refPoint.Z; refPoint = MathHelper.Transform(dim.SecondReferencePoint, dim.Normal, CoordinateSystem.World, CoordinateSystem.Object); ref2 = new Vector2(refPoint.X, refPoint.Y); Vector2 midRef = Vector2.MidPoint(ref1, ref2); //double refAngle = Vector2.Angle(ref1, ref2); //if (refAngle > MathHelper.HalfPI && refAngle <= MathHelper.ThreeHalfPI) //{ // Vector2 tmp = ref1; // ref1 = ref2; // ref2 = tmp; //} double dimRotation = dim.Rotation * MathHelper.DegToRad; Vector2 dimRef1tmp = new Vector2(-measure * 0.5, dim.Offset); Vector2 dimRef2tmp = new Vector2(measure * 0.5, dim.Offset); dimRef1 = MathHelper.Transform(dimRef1tmp, dimRotation, CoordinateSystem.Object, CoordinateSystem.World) + midRef; dimRef2 = MathHelper.Transform(dimRef2tmp, dimRotation, CoordinateSystem.Object, CoordinateSystem.World) + midRef; Vector2 midDim = Vector2.MidPoint(dimRef1, dimRef2); double relativeAngle = Vector2.AngleBetween(ref2 - ref1, dimRef2 - dimRef1); if (relativeAngle > MathHelper.HalfPI && relativeAngle <= MathHelper.ThreeHalfPI) { Vector2 tmp = ref1; ref1 = ref2; ref2 = tmp; reversed = true; } // reference points Layer defPointLayer = new Layer("Defpoints") { Plot = false }; entities.Add(new Point(ref1) { Layer = defPointLayer }); entities.Add(new Point(ref2) { Layer = defPointLayer }); entities.Add(reversed ? new Point(dimRef1) { Layer = defPointLayer } : new Point(dimRef2) { Layer = defPointLayer }); // dimension line entities.Add(DimensionLine(dimRef1, dimRef2, dimRotation, style)); // extension lines Vector2 dirRef1 = Vector2.Normalize(dimRef1 - ref1); Vector2 dirRef2 = Vector2.Normalize(dimRef2 - ref2); double dimexo = style.DIMEXO * style.DIMSCALE; double dimexe = style.DIMEXE * style.DIMSCALE; if (!style.DIMSE1) { entities.Add(ExtensionLine(ref1 + dimexo * dirRef1, dimRef1 + dimexe * dirRef1, style, style.DIMLTEX1)); } if (!style.DIMSE2) { entities.Add(ExtensionLine(ref2 + dimexo * dirRef2, dimRef2 + dimexe * dirRef2, style, style.DIMLTEX2)); } // dimension arrowheads if (reversed) { entities.Add(StartArrowHead(dimRef2, dimRotation, style)); entities.Add(EndArrowHead(dimRef1, dimRotation + MathHelper.PI, style)); } else { entities.Add(StartArrowHead(dimRef1, dimRotation + MathHelper.PI, style)); entities.Add(EndArrowHead(dimRef2, dimRotation, style)); } // dimension text string text = FormatDimensionText(measure, false, dim.UserText, style, dim.Owner.Record.Layout); double textRot = dimRotation; if (textRot > MathHelper.HalfPI && textRot <= MathHelper.ThreeHalfPI) { textRot += MathHelper.PI; } MText mText = DimensionText(Vector2.Polar(midDim, style.DIMGAP * style.DIMSCALE, textRot + MathHelper.HalfPI), textRot, text, style); if (mText != null) { entities.Add(mText); } Vector3 defPoint = reversed ? new Vector3(dimRef1.X, dimRef1.Y, elevation) : new Vector3(dimRef2.X, dimRef2.Y, elevation); dim.DefinitionPoint = MathHelper.Transform(defPoint, dim.Normal, CoordinateSystem.Object, CoordinateSystem.World); dim.MidTextPoint = new Vector3(midDim.X, midDim.Y, elevation); // this value is in OCS // drawing block return(new Block(name, false, entities, null) { Flags = BlockTypeFlags.AnonymousBlock }); }
public static Block Build(LinearDimension dim, string name) { Vector2 ref1; Vector2 ref2; Vector2 dimRef1; Vector2 dimRef2; double measure = dim.Measurement; bool reversed = false; DimensionStyle style = dim.Style; List<EntityObject> entities = new List<EntityObject>(); // we will build the dimension block in object coordinates with normal the dimension normal Vector3 refPoint; refPoint = MathHelper.Transform(dim.FirstReferencePoint, dim.Normal, CoordinateSystem.World, CoordinateSystem.Object); ref1 = new Vector2(refPoint.X, refPoint.Y); double elevation = refPoint.Z; refPoint = MathHelper.Transform(dim.SecondReferencePoint, dim.Normal, CoordinateSystem.World, CoordinateSystem.Object); ref2 = new Vector2(refPoint.X, refPoint.Y); Vector2 midRef = Vector2.MidPoint(ref1, ref2); //double refAngle = Vector2.Angle(ref1, ref2); //if (refAngle > MathHelper.HalfPI && refAngle <= MathHelper.ThreeHalfPI) //{ // Vector2 tmp = ref1; // ref1 = ref2; // ref2 = tmp; //} double dimRotation = dim.Rotation * MathHelper.DegToRad; Vector2 dimRef1tmp = new Vector2(-measure * 0.5, dim.Offset); Vector2 dimRef2tmp = new Vector2(measure * 0.5, dim.Offset); dimRef1 = MathHelper.Transform(dimRef1tmp, dimRotation, CoordinateSystem.Object, CoordinateSystem.World) + midRef; dimRef2 = MathHelper.Transform(dimRef2tmp, dimRotation, CoordinateSystem.Object, CoordinateSystem.World) + midRef; Vector2 midDim = Vector2.MidPoint(dimRef1, dimRef2); double relativeAngle = Vector2.AngleBetween(ref2 - ref1, dimRef2 - dimRef1); if (relativeAngle > MathHelper.HalfPI && relativeAngle <= MathHelper.ThreeHalfPI) { Vector2 tmp = ref1; ref1 = ref2; ref2 = tmp; reversed = true; } // reference points Layer defPointLayer = new Layer("Defpoints") { Plot = false }; entities.Add(new Point(ref1) { Layer = defPointLayer }); entities.Add(new Point(ref2) { Layer = defPointLayer }); entities.Add(reversed ? new Point(dimRef1) {Layer = defPointLayer} : new Point(dimRef2) {Layer = defPointLayer}); // dimension line entities.Add(DimensionLine(dimRef1, dimRef2, dimRotation, style)); // extension lines Vector2 dirRef1 = Vector2.Normalize(dimRef1 - ref1); Vector2 dirRef2 = Vector2.Normalize(dimRef2 - ref2); double dimexo = style.DIMEXO * style.DIMSCALE; double dimexe = style.DIMEXE * style.DIMSCALE; if (!style.DIMSE1) entities.Add(ExtensionLine(ref1 + dimexo * dirRef1, dimRef1 + dimexe * dirRef1, style, style.DIMLTEX1)); if (!style.DIMSE2) entities.Add(ExtensionLine(ref2 + dimexo * dirRef2, dimRef2 + dimexe * dirRef2, style, style.DIMLTEX2)); // dimension arrowheads if (reversed) { entities.Add(StartArrowHead(dimRef2, dimRotation, style)); entities.Add(EndArrowHead(dimRef1, dimRotation + MathHelper.PI, style)); } else { entities.Add(StartArrowHead(dimRef1, dimRotation + MathHelper.PI, style)); entities.Add(EndArrowHead(dimRef2, dimRotation, style)); } // dimension text string text = FormatDimensionText(measure, false, dim.UserText, style, dim.Owner.Record.Layout); double textRot = dimRotation; if (textRot > MathHelper.HalfPI && textRot <= MathHelper.ThreeHalfPI) textRot += MathHelper.PI; MText mText = DimensionText(Vector2.Polar(midDim, style.DIMGAP * style.DIMSCALE, textRot + MathHelper.HalfPI), textRot, text, style); if (mText != null) entities.Add(mText); Vector3 defPoint = reversed ? new Vector3(dimRef1.X, dimRef1.Y, elevation) : new Vector3(dimRef2.X, dimRef2.Y, elevation); dim.DefinitionPoint = MathHelper.Transform(defPoint, dim.Normal, CoordinateSystem.Object, CoordinateSystem.World); dim.MidTextPoint = new Vector3(midDim.X, midDim.Y, elevation); // this value is in OCS // drawing block return new Block(name, false, entities, null) { Flags = BlockTypeFlags.AnonymousBlock }; }
/// <summary> /// Creates a new LinearDimension that is a copy of the current instance. /// </summary> /// <returns>A new LinearDimension that is a copy of this instance.</returns> public override object Clone() { LinearDimension entity = new LinearDimension { //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, //Dimension properties Style = (DimensionStyle)this.style.Clone(), AttachmentPoint = this.attachmentPoint, LineSpacingStyle = this.lineSpacingStyle, LineSpacingFactor = this.lineSpacing, //LinearDimension properties FirstReferencePoint = this.start, SecondReferencePoint = this.end, Rotation = this.rotation, Offset = this.offset }; foreach (XData data in this.XData.Values) entity.XData.Add((XData)data.Clone()); return entity; }
/// <summary> /// Creates a new LinearDimension that is a copy of the current instance. /// </summary> /// <returns>A new LinearDimension that is a copy of this instance.</returns> public override object Clone() { LinearDimension entity = new LinearDimension { //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, //Dimension properties Style = (DimensionStyle) this.Style.Clone(), AttachmentPoint = this.AttachmentPoint, LineSpacingStyle = this.LineSpacingStyle, LineSpacingFactor = this.LineSpacingFactor, //LinearDimension properties FirstReferencePoint = this.firstRefPoint, SecondReferencePoint = this.secondRefPoint, Rotation = this.rotation, Offset = this.offset, Elevation = this.Elevation }; foreach (XData data in this.XData.Values) entity.XData.Add((XData) data.Clone()); return entity; }