代码创建多重引线样式和绘制多重引线
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using 创建多重引线;
[assembly: CommandClass(typeof (Class1))]
namespace 创建多重引线
{
public class Class1
{
[CommandMethod("AddML")]
public static void DrawMleader()
{
var MLStyleId = CreateMleaderStyle("xuguang");
//获取当前数据库
var acDoc = Application.DocumentManager.MdiActiveDocument;
var acCurDb = acDoc.Database;
//启动事务
using (var acTrans = acCurDb.TransactionManager.StartTransaction())
{
/
/以读模式打开块表
var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
//以写模式打开块表记录模型空间
var acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
var pMLeader = new MLeader();
pMLeader.SetDatabaseDefaults();
pMLeader.MLeaderStyle = MLStyleId;
var pos = new Point3d(200, 200, 200);
var posTmp = new Point3d(220, 220, 0);
var sn = pMLeader.AddLeaderLine(pos);
pMLeader.AddFirstVertex(sn, pos);
pMLeader.SetFirstVertex(sn, pos);
pMLeader.SetLastVertex(sn, posTmp);
//创建多⾏⽂字(MText)注释
var pMtext = new MText();
pMtext.Contents = "创建多重引线";
pMtext.TextHeight = 3;
pMtext.TextStyleId = acCurDb.Textstyle;
pMtext.Location = posTmp;
pMLeader.MText = pMtext;
//添加新对象到模型空间,记录事务
acBlkTblRec.AppendEntity(pMLeader);
acTrans.AddNewlyCreatedDBObject(pMLeader, true);
//提交修改,回收内存
acTrans.Commit();
}
acDoc.Editor.Regen();
}
private static ObjectId CreateMleaderStyle(string MlName)
{
var acDoc = Application.DocumentManager.MdiActiveDocument;
var acDoc = Application.DocumentManager.MdiActiveDocument;
var db = HostApplicationServices.WorkingDatabase;
using (var acTrans = acDoc.TransactionManager.StartTransaction())
{
var DbDictionary = acTrans.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForWrite) as DBDictionary; //获取所有样式
if (DbDictionary.Contains(MlName))
{
return DbDictionary.GetAt(MlName);
}
var newMleadStyle = new MLeaderStyle();
newMleadStyle.TextAlignAlwaysLeft = true;
newMleadStyle.Annotative = AnnotativeStates.True;
newMleadStyle.DrawMLeaderOrderType = DrawMLeaderOrderType.DrawLeaderFirst;
newMleadStyle.TextAttachmentType = TextAttachmentType.AttachmentMiddle; //Text连接⽅式
newMleadStyle.TextAttachmentDirection = TextAttachmentDirection.AttachmentHorizontal; //⽂字⽔平排列 newMleadStyle.TextAlignmentType = TextAlignmentType.CenterAlignment; //Text排列⽅式
newMleadStyle.LeaderLineColor =
Color.FromColorIndex(ColorMethod.ByLayer, 256);
newMleadStyle.LeaderLineType = LeaderType.StraightLeader;
newMleadStyle.ContentType = ContentType.MTextContent;
newMleadStyle.ArrowSize = 3.0; //箭头⼤⼩
newMleadStyle.BreakSize = 0.6; //基线打断⼤⼩
textstylenewMleadStyle.DoglegLength = 1.25; //基线距离
newMleadStyle.EnableLanding = true;
newMleadStyle.EnableDogleg = true; //显⽰基线
newMleadStyle.EnableFrameText = true; //显⽰⽂本框
newMleadStyle.LandingGap = 1; //基线间隙
newMleadStyle.MaxLeaderSegmentsPoints = 2; //最⼤引线点数
newMleadStyle.TextStyleId = acDoc.Database.Textstyle; //⽂字样式
newMleadStyle.TextHeight = 3.0;
newMleadStyle.PostMLeaderStyleToDb(db, MlName);
newMleadStyle.DowngradeOpen();
acTrans.Commit();
return newMleadStyle.ObjectId;
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论