知方号

知方号

代码创建多重引线样式和绘制多重引线

代码创建多重引线样式和绘制多重引线

代码创建多重引线样式和绘制多重引线 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 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; //基线打断大小 newMleadStyle.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; } } }}

posted on 2014-10-14 09:41 xuguangren 阅读(1308) 评论(0) 编辑 收藏 举报

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。