使用 C# 创建 MleaderStyle Autocad

本文关键字:Autocad MleaderStyle 创建 使用 | 更新日期: 2023-09-27 18:35:22

我在C#中创建了MleaderStyle。没有错误,但是当我运行它时,我收到以下错误。这是我用于此目的的代码的一部分,也是 autocad 显示的错误。

MLeaderStyle dst = (MLeaderStyle)acTrans.GetObject( acCurDb.DimStyleTableId, OpenMode.ForWrite);

        MText mt = new MText();
        mt.Contents = text;
        dst.Name = " My LeaderStyle";
        dst.ArrowSymbolId = ObjectId.Null;
        dst.ArrowSize = 0.18 * scale;
        dst.ContentType = 0;
        dst.DefaultMText = mt;
        dst.LandingGap = gap;
        dst.EnableBlockRotation = true;
        dst.MaxLeaderSegmentsPoints = 2;

        //dst1.Add(dst);
        acTrans.AddNewlyCreatedDBObject(dst, true);
        MLeader lead = new MLeader();
      int i=  lead.AddLeader();
        lead.AddLeaderLine(i);
        lead.AddFirstVertex(i, start);
        lead.AddLastVertex(i, end);
        lead.MLeaderStyle = dst.ObjectId;

错误;

无法强制转换类型的对象'Autodesk.Autocad.DataBaseServices.DimStyleTable'to 'AutoDesk.AutoCad.DataBaseServices.MleaderStyle'

使用 C# 创建 MleaderStyle Autocad

MLeaderStyle dst = (MLeaderStyle)acTrans.GetObject( acCurDb.DimStyleTableId, OpenMode.ForWrite);

不能将 DimStyleTable 强制转换为 MLeaderStyle,必须使用其中一个 MLeaderStyle 构造函数来创建新的构造函数。

using (var tr = db.TransactionManager.StartTransaction())
{
    // the newly created MText have to be disposed after using
    using (MText mt = new MText())
    {
        mt.Contents = text;
         // check if the MLeaderStyle dictionary does not already contains a style named "MyLeaderStyle"
        DBDictionary mlStyles = (DBDictionary)tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForWrite);
        if (!mlStyles.Contains("MyLeaderStyle"))
        {
            // create a new instance of MLeaderStyle (you can use the overloaded ctor to copy an existing style)
            MLeaderStyle dst = new MLeaderStyle();
            dst.ArrowSymbolId = ObjectId.Null;
            dst.ArrowSize = 0.18 * scale;
            dst.ContentType = 0;
            dst.DefaultMText = mt;
            dst.LandingGap = gap;
            dst.EnableBlockRotation = true;
            dst.MaxLeaderSegmentsPoints = 2;
            // add the new MLeaderStyle to the database
            dst.PostMLeaderStyleToDb(db, "MyLeaderStyle");
            tr.AddNewlyCreatedDBObject(dst, true);
        }
    }
    tr.Commit();
}