使用 C# 的 Autocad 中的 Mleader 样式

本文关键字:Mleader 样式 中的 Autocad 使用 | 更新日期: 2023-09-27 18:35:35

我使用 C# 在 autocad 中创建了一个新的 Mleader 样式。 我想在 CAD 中使用它;将"领导者"样式指定给领导者。我对此一无所知。我试过这段代码

public class test
    {
        public void drawMleaders(Transaction acTrans, Database acCurDb, Document acDoc, double scale, double gap, double[] pickPont)
        {
            BlockTable acBlkTbl;
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                            OpenMode.ForRead) as BlockTable;
            //Open the Block table record Model space for write
            BlockTableRecord acBlkTblRec;
            acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                           OpenMode.ForWrite) as BlockTableRecord;
         DBDictionary mlStyles = (DBDictionary)acTrans.GetObject(acCurDb.MLeaderStyleDictionaryId, OpenMode.ForWrite);
        // mlStyles.UpgradeOpen();
          if (!mlStyles.Contains("MyLeaderStyle"))
           {

                MLeaderStyle dst = new MLeaderStyle();
                // MText mt = new MText();
                // mt.Contents = text;
            //dst.Name="MyLeaderStyle";
                dst.ArrowSymbolId = ObjectId.Null;
                dst.ArrowSize = 0.5 * scale;
                //dst.ContentType = 0;
                //dst.DefaultMText = "";
                dst.LandingGap = 0;
                dst.EnableBlockRotation = true;
                dst.MaxLeaderSegmentsPoints = 2;
                dst.EnableLanding = true;

                dst.PostMLeaderStyleToDb(acCurDb, "MyLeaderStyle");
                acCurDb.MLeaderstyle = dst.ObjectId;

                //dst1.Add(dst);
                acTrans.AddNewlyCreatedDBObject(dst, true);
            }

            MLeader lead = new MLeader();
                int i = lead.AddLeader();
                lead.AddLeaderLine(i);
                lead.AddFirstVertex(i, new Point3d(pickPont[0], pickPont[1], 0));
                lead.AddLastVertex(i, new Point3d(pickPont[0] + 5, pickPont[1] + 5, 0));
                //lead.MLeaderStyle = acCurDb.MLeaderstyle;
                acBlkTblRec.AppendEntity(lead);
                acTrans.AddNewlyCreatedDBObject(lead, true);

            }
    }

任何帮助将不胜感激。

使用 C# 的 Autocad 中的 Mleader 样式

你只需要设置领导者。MLeaderStyle 属性到 MLeader 样式 ObjectId。您可以从 MLeaderStyle 字典(如果已存在)或从 PostMLeaderStyleToDb 返回值(如果创建它)中获取它

        ObjectId mlStyleId;
        DBDictionary mlStyles = (DBDictionary)acTrans.GetObject(acCurDb.MLeaderStyleDictionaryId, OpenMode.ForRead);
        if (mlStyles.Contains("MyLeaderStyle"))
        {
            mlStyleId = mlStyles.GetAt("MyLeaderStyle");
        }
        else
        {
            MLeaderStyle dst = new MLeaderStyle();
            dst.ArrowSymbolId = ObjectId.Null;
            dst.ArrowSize = 0.5 * scale;
            dst.LandingGap = 0;
            dst.EnableBlockRotation = true;
            dst.MaxLeaderSegmentsPoints = 2;
            dst.EnableLanding = true;
            mlStyleId = dst.PostMLeaderStyleToDb(acCurDb, "MyLeaderStyle");
            acTrans.AddNewlyCreatedDBObject(dst, true);
        }
        MLeader lead = new MLeader();
        int i = lead.AddLeader();
        lead.AddLeaderLine(i);
        lead.AddFirstVertex(i, new Point3d(pickPont[0], pickPont[1], 0));
        lead.AddLastVertex(i, new Point3d(pickPont[0] + 5, pickPont[1] + 5, 0));
        lead.MLeaderStyle = mlStyleId;
        acBlkTblRec.AppendEntity(lead);
        acTrans.AddNewlyCreatedDBObject(lead, true);
        acTrans.Commit();