Chuyển đến nội dung
Diễn đàn CADViet
Đăng nhập để thực hiện theo  
viet_design

Giúp mình tạo dimension lines ( color...), kích thước arrowheads,text appearance bằng c#.

Các bài được khuyến nghị

Bác nào pro giúp mình hoàn thiện tạo một dimension style đầy đủ bằng c# với, mình chỉ làm được đến phần tạo name dimension thôi, ngoài ra tạo dimension lines ( color...), kích thước arrowheads,text appearance thì mình mù tịch. Mình xin cảm ơn pro trước nha.  code của mình create name dimension trong file dưới đây. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime;   namespace ClassLibrary1 {     public class Class1     {           [CommandMethod("ODS")]         public void OverrideDimensionStyle()         {             Document acdoc = Application.DocumentManager.MdiActiveDocument;             Database accurdb = acdoc.Database;             Transaction tr = accurdb.TransactionManager.StartTransaction();             using (tr)             {                 DimStyleTable dst = (DimStyleTable)tr.GetObject(accurdb.DimStyleTableId, OpenMode.ForWrite);                 DimStyleTableRecord dstr = new DimStyleTableRecord();                 dstr.Dimtad = 2;                 dstr.Dimgap = 0.3;                 dstr.Name = "MyStyle";                 ObjectId dsId = dst.Add(dstr);                 tr.AddNewlyCreatedDBObject(dstr, true);                                 AlignedDimension ad1 = new AlignedDimension(Point3d.Origin, new Point3d(5.0, 0.0, 0.0), new Point3d(2.5, 2.0, 0.0), "MyStyle", dsId);                 AlignedDimension ad2 = new AlignedDimension(new Point3d(5.0, 0.0, 0.0), new Point3d(10.0, 0.0, 0.0), new Point3d(7.5, 2.0, 0.0), "Overridden dimension", dsId);                 ad2.Dimtad = 4;                 ad2.Dimgap = 0.5;                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(accurdb.CurrentSpaceId, OpenMode.ForWrite);                 btr.AppendEntity(ad1);                 btr.AppendEntity(ad2);                 tr.AddNewlyCreatedDBObject(ad1, true);                 tr.AddNewlyCreatedDBObject(ad2, true);                 tr.Commit();             }  

Chỉnh sửa theo viet_design

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Bác nào pro giúp mình hoàn thiện tạo một dimension style đầy đủ bằng c# với, mình chỉ làm được đến phần tạo name dimension thôi, ngoài ra tạo dimension lines ( color...), kích thước arrowheads,text appearance thì mình mù tịch. Mình xin cảm ơn pro trước nha.  code của mình create name dimension trong file dưới đây. 

.............

viet_design tham khảo hàm GetOrSetDimensionStyleId trong code vẽ Text và k/thước : 

(thay đổi giá trị biến hệ thống cho phù hợp nhu cầu của mình)

#region Includes and aliases
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
#endregion

namespace GiaBach
{
    public class DimEx
    {
        [CommandMethod("AddAlignedDim")]
        public void AddAlignedDim()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                ObjectId txtStyId = GetOrSetTextStyleID(db, "Unicode", "Times.ttf");
                ObjectId VnTimeId = GetOrSetTextStyleID(db, "VnTime", "vntime.shx");
                Application.SetSystemVariable("TEXTSTYLE", "Unicode");
                ObjectId dimStyId = GetOrSetDimensionStyleId(db, "CadViet", txtStyId, ObjectId.Null);

                DBText txt = new DBText();
                txt.Position = new Point3d(25, 30, 0);
                txt.Height = 3.5;
                txt.TextString = "Chào mừng bạn đến với AutoCad.Net";
                txt.ColorIndex = 6;
                txt.Rotation = 0.261;

                AlignedDimension ad1 = new AlignedDimension(Point3d.Origin, new Point3d(50, 0, 0), new Point3d(25, 20, 0), "", dimStyId);
                AlignedDimension ad2 = new AlignedDimension(new Point3d(70, 0, 0), new Point3d(100, 30, 0), new Point3d(95, 30, 0), "", dimStyId);

                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
                btr.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);
                btr.AppendEntity(ad1);
                tr.AddNewlyCreatedDBObject(ad1, true);
                btr.AppendEntity(ad2);
                tr.AddNewlyCreatedDBObject(ad2, true);
                tr.Commit();
            }
        }

        private ObjectId GetOrSetDimensionStyleId(Database db, string styleName, ObjectId textStyleId, ObjectId arrowId)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DimStyleTable dimTbl = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                if (dimTbl.Has(styleName) == false)
                {
                    if (dimTbl.IsWriteEnabled == false) dimTbl.UpgradeOpen();
                    DimStyleTableRecord dimstyle = new DimStyleTableRecord();
                    dimstyle.Name = styleName;
                    dimstyle.Dimadec = 2;
                    dimstyle.Dimalt = false;
                    dimstyle.Dimaltd = 0;
                    dimstyle.Dimaltf = 25.4;
                    dimstyle.Dimaltrnd = 0.0;
                    dimstyle.Dimalttd = 0;
                    dimstyle.Dimalttz = 0;
                    dimstyle.Dimaltu = 2;
                    dimstyle.Dimaltz = 0;
                    dimstyle.Dimapost = "";
                    dimstyle.Dimarcsym = 0;
                    dimstyle.Dimasz = 3;
                    dimstyle.Dimatfit = 3;
                    dimstyle.Dimaunit = 1;
                    dimstyle.Dimazin = 0;
                    dimstyle.Dimblk = arrowId;
                    dimstyle.Dimblk1 = arrowId;
                    dimstyle.Dimblk2 = arrowId;
                    dimstyle.Dimcen = 0;
                    dimstyle.Dimclrd = Color.FromColorIndex(ColorMethod.ByBlock, 3);
                    dimstyle.Dimclre = Color.FromColorIndex(ColorMethod.ByBlock, 4);
                    dimstyle.Dimclrt = Color.FromColorIndex(ColorMethod.ByBlock, 5); ;
                    dimstyle.Dimdec = 0;
                    dimstyle.Dimdle = 0.0;
                    dimstyle.Dimdli = 7;
                    dimstyle.Dimdsep = char.Parse(".");
                    dimstyle.Dimexe = 1;
                    dimstyle.Dimexo = 1;
                    dimstyle.Dimfrac = 1;
                    dimstyle.Dimgap = 1;
                    dimstyle.Dimldrblk = arrowId;
                    dimstyle.Dimlfac = 1;
                    dimstyle.Dimlim = false;
                    dimstyle.Dimltex1 = db.ByBlockLinetype;
                    dimstyle.Dimltex2 = db.ByBlockLinetype;
                    dimstyle.Dimltype = db.ByBlockLinetype;
                    dimstyle.Dimlunit = 2;
                    dimstyle.Dimlwd = LineWeight.ByBlock;
                    dimstyle.Dimlwe = LineWeight.ByBlock;
                    dimstyle.Dimpost = "";
                    dimstyle.Dimrnd = 0.5;
                    dimstyle.Dimsah = true;
                    dimstyle.Dimscale = 1;
                    dimstyle.Dimsd1 = false;
                    dimstyle.Dimsd2 = false;
                    dimstyle.Dimse1 = false;
                    dimstyle.Dimse2 = false;
                    dimstyle.Dimsoxd = false;
                    dimstyle.Dimtad = 1;
                    dimstyle.Dimtdec = 1;
                    //dimstyle.Dimtxtdirection = false;
                    dimstyle.Dimtfac = 1.0;
                    dimstyle.Dimtih = false;
                    dimstyle.Dimtix = true;
                    dimstyle.Dimtm = 0.0;
                    dimstyle.Dimtmove = 2;
                    dimstyle.Dimtofl = true;
                    dimstyle.Dimtoh = false;
                    dimstyle.Dimtol = false;
                    dimstyle.Dimtolj = 0;
                    dimstyle.Dimtp = 0.0;
                    dimstyle.Dimtsz = 0.0;
                    dimstyle.Dimtxsty = textStyleId;
                    dimstyle.Dimtvp = 0.0;
                    dimstyle.Dimtxt = 3.5;
                    dimstyle.Dimtzin = 8;
                    dimstyle.Dimupt = false;
                    dimstyle.Dimzin = 8;
                    dimTbl.Add(dimstyle);
                    tr.AddNewlyCreatedDBObject(dimstyle, true);
                    tr.Commit();
                }
                return dimTbl[styleName];
            }
        }

        private ObjectId GetOrSetTextStyleID(Database db, string styleName, string fontFileName)
        {
            ObjectId result = new ObjectId();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                TextStyleTable tab = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite, false);
                if (!tab.Has(styleName))
                {
                    TextStyleTableRecord rec = new TextStyleTableRecord();
                    rec.Name = styleName;
                    rec.FileName = fontFileName;
                    tab.Add(rec);
                    trans.AddNewlyCreatedDBObject(rec, true);
                    result = rec.ObjectId;
                }
                else
                    result = tab[styleName];
                trans.Commit();
            }
            return result;
        }
    }
}
#region Includes and aliases
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
#endregion
 
namespace GiaBach
{
    public class DimEx
    {
        [CommandMethod("AddAlignedDim")]
        public void AddAlignedDim()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
 
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                ObjectId txtStyId = GetOrSetTextStyleID(db, "Unicode", "Times.ttf");
                ObjectId VnTimeId = GetOrSetTextStyleID(db, "VnTime", "vntime.shx");
                Application.SetSystemVariable("TEXTSTYLE", "Unicode");
                ObjectId dimStyId = GetOrSetDimensionStyleId(db, "CadViet", txtStyId, ObjectId.Null);
 
                DBText txt = new DBText();
                txt.Position = new Point3d(25, 30, 0);
                txt.Height = 3.5;
                txt.TextString = "Chao m?ng b?n ??n v?i AutoCad.Net";
                txt.ColorIndex = 6;
                txt.Rotation = 0.261;
 
                AlignedDimension ad1 = new AlignedDimension(Point3d.Origin, new Point3d(50, 0, 0), new Point3d(25, 20, 0), "", dimStyId);
                AlignedDimension ad2 = new AlignedDimension(new Point3d(70, 0, 0), new Point3d(100, 30, 0), new Point3d(95, 30, 0), "", dimStyId);
 
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
                btr.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);
                btr.AppendEntity(ad1);
                tr.AddNewlyCreatedDBObject(ad1, true);
                btr.AppendEntity(ad2);
                tr.AddNewlyCreatedDBObject(ad2, true);
                tr.Commit();
            }
        }
 
        private ObjectId GetOrSetDimensionStyleId(Database db, string styleName, ObjectId textStyleId, ObjectId arrowId)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DimStyleTable dimTbl = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                if (dimTbl.Has(styleName) == false)
                {
                    if (dimTbl.IsWriteEnabled == false) dimTbl.UpgradeOpen();
                    DimStyleTableRecord dimstyle = new DimStyleTableRecord();
                    dimstyle.Name = styleName;
                    dimstyle.Dimadec = 2;
                    dimstyle.Dimalt = false;
                    dimstyle.Dimaltd = 0;
                    dimstyle.Dimaltf = 25.4;
                    dimstyle.Dimaltrnd = 0.0;
                    dimstyle.Dimalttd = 0;
                    dimstyle.Dimalttz = 0;
                    dimstyle.Dimaltu = 2;
                    dimstyle.Dimaltz = 0;
                    dimstyle.Dimapost = "";
                    dimstyle.Dimarcsym = 0;
                    dimstyle.Dimasz = 3;
                    dimstyle.Dimatfit = 3;
                    dimstyle.Dimaunit = 1;
                    dimstyle.Dimazin = 0;
                    dimstyle.Dimblk = arrowId;
                    dimstyle.Dimblk1 = arrowId;
                    dimstyle.Dimblk2 = arrowId;
                    dimstyle.Dimcen = 0;
                    dimstyle.Dimclrd = Color.FromColorIndex(ColorMethod.ByBlock, 3);
                    dimstyle.Dimclre = Color.FromColorIndex(ColorMethod.ByBlock, 4);
                    dimstyle.Dimclrt = Color.FromColorIndex(ColorMethod.ByBlock, 5); ;
                    dimstyle.Dimdec = 0;
                    dimstyle.Dimdle = 0.0;
                    dimstyle.Dimdli = 7;
                    dimstyle.Dimdsep = char.Parse(".");
                    dimstyle.Dimexe = 1;
                    dimstyle.Dimexo = 1;
                    dimstyle.Dimfrac = 1;
                    dimstyle.Dimgap = 1;
                    dimstyle.Dimldrblk = arrowId;
                    dimstyle.Dimlfac = 1;
                    dimstyle.Dimlim = false;
                    dimstyle.Dimltex1 = db.ByBlockLinetype;
                    dimstyle.Dimltex2 = db.ByBlockLinetype;
                    dimstyle.Dimltype = db.ByBlockLinetype;
                    dimstyle.Dimlunit = 2;
                    dimstyle.Dimlwd = LineWeight.ByBlock;
                    dimstyle.Dimlwe = LineWeight.ByBlock;
                    dimstyle.Dimpost = "";
                    dimstyle.Dimrnd = 0.5;
                    dimstyle.Dimsah = true;
                    dimstyle.Dimscale = 1;
                    dimstyle.Dimsd1 = false;
                    dimstyle.Dimsd2 = false;
                    dimstyle.Dimse1 = false;
                    dimstyle.Dimse2 = false;
                    dimstyle.Dimsoxd = false;
                    dimstyle.Dimtad = 1;
                    dimstyle.Dimtdec = 1;
                    //dimstyle.Dimtxtdirection = false;
                    dimstyle.Dimtfac = 1.0;
                    dimstyle.Dimtih = false;
                    dimstyle.Dimtix = true;
                    dimstyle.Dimtm = 0.0;
                    dimstyle.Dimtmove = 2;
                    dimstyle.Dimtofl = true;
                    dimstyle.Dimtoh = false;
                    dimstyle.Dimtol = false;
                    dimstyle.Dimtolj = 0;
                    dimstyle.Dimtp = 0.0;
                    dimstyle.Dimtsz = 0.0;
                    dimstyle.Dimtxsty = textStyleId;
                    dimstyle.Dimtvp = 0.0;
                    dimstyle.Dimtxt = 3.5;
                    dimstyle.Dimtzin = 8;
                    dimstyle.Dimupt = false;
                    dimstyle.Dimzin = 8;
                    dimTbl.Add(dimstyle);
                    tr.AddNewlyCreatedDBObject(dimstyle, true);
                    tr.Commit();
                }
                return dimTbl[styleName];
            }
        }
 
        private ObjectId GetOrSetTextStyleID(Database db, string styleName, string fontFileName)
        {
            ObjectId result = new ObjectId();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                TextStyleTable tab = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite, false);
                if (!tab.Has(styleName))
                {
                    TextStyleTableRecord rec = new TextStyleTableRecord();
                    rec.Name = styleName;
                    rec.FileName = fontFileName;
                    tab.Add(rec);
                    trans.AddNewlyCreatedDBObject(rec, true);
                    result = rec.ObjectId;
                }
                else
                    result = tab[styleName];
                trans.Commit();
            }
            return result;
        }
    }
}
Chỉnh sửa theo gia_bach

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

anh gia_bach giúp em chỗ này với, em đổi thành font có đuôi .shx ( vd: vnsimli.shx) thì bị báo lỗi vậy anh.

viet_design nhầm lẫn giữa "Font Name" và "Font Filename".

Đã update code ở trên cho t/hợp "Font Filename".

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Tạo một tài khoản hoặc đăng nhập để nhận xét

Bạn cần phải là một thành viên để lại một bình luận

Tạo tài khoản

Đăng ký một tài khoản mới trong cộng đồng của chúng tôi. Điều đó dễ mà.

Đăng ký tài khoản mới

Đăng nhập

Bạn có sẵn sàng để tạo một tài khoản ? Đăng nhập tại đây.

Đăng nhập ngay
Đăng nhập để thực hiện theo  

×