- 积分
- 11621
- 明经币
- 个
- 注册时间
- 2004-10-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
下列代码创建多重引线样式和绘制多重引线- 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;
- }
- }
- }
- }
效果如图
问题是
1:程序起始生成的多重引线文字为何右对齐,需拖动后才显示正确的效果。
2:样式里面的文字连接位置-右怎样才可以设置为在文字中间,指在代码里设置。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|