|
GetPoint 方法提示用户在 AutoCAD 命令提示下给出点的定义。PromptPointOptions 对象允许用户控制输入和如何显示提示信息。PromptPointOptions 对象的 UseBasePoint 和 BasePoint 属性控制是否从基点绘制一条橡皮线。PromptPointOptions 对象的 Keywords 属性允许用户定义用户可以在命令提示中输入除指定点外的关键字。
下面的样例提示用户输入两个点,然后以这两个点为起点和端点绘制一条直线(该例子根据用户点击鼠标,获取用户选定的点) - namespace ClassLibrary3;
- interface
- uses
- Autodesk.AutoCAD.Runtime, Autodesk.AutoCAD.ApplicationServices,Autodesk.AutoCAD.EditorInput,
- Autodesk.AutoCAD.DatabaseServices,Autodesk.AutoCAD.Geometry;
- type
- Class1 =public class
- private
- public
- [CommandMethod('GetPointsFromUser')]
- class method GetPointsFromUser;
- end;
- implementation
- class method Class1.GetPointsFromUser;
- var
- acDococument;
- acCurDbatabase;
- pPtResromptPointResult;
- pPtOptsromptPointOptions;
- ptStartoint3d;
- ptEndoint3d;
- acTrans:Transaction;
- acBlkTbl:BlockTable;
- acBlkTblRec:BlockTableRecord;
- acLineine;
- begin
- //获得当前数据库并启动事务管理器 Get the current database and start the Transaction Manager
- acDoc:= Application.DocumentManager.MdiActiveDocument;
- acCurDb:= acDoc.Database;
- pPtOpts:= New PromptPointOptions('');
- //提示开始点 Prompt for the start point
- pPtOpts.Message := #13+ 'Enter the start point of the line: ';
- pPtRes := acDoc.Editor.GetPoint(pPtOpts);
- ptStart:= pPtRes.Value;
- //如果用户按了 ESC 键或取消了命令就退出 Exit if the user presses ESC or cancels the command
- If pPtRes.Status = PromptStatus.Cancel Then Exit;
- //提示结束点
- pPtOpts.Message :=#13+'Enter the end point of the line:';
- pPtOpts.UseBasePoint:=True;
- pPtOpts.BasePoint:=ptStart;
- pPtRes := acDoc.Editor.GetPoint(pPtOpts);
- ptEnd:= pPtRes.Value;
- If pPtRes.Status = PromptStatus.Cancel Then Exit;
- //启动一个事务
- try
- acTrans:= acCurDb.TransactionManager.StartTransaction;
- //以写的方式打开模型空间
- acBlkTbl := acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;
- acBlkTblRec := acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- //定义新的直线
- acLine:= New Line(ptStart, ptEnd);
- acLine.SetDatabaseDefaults;
- //添加直线到图形中去
- acBlkTblRec.AppendEntity(acLine);
- acTrans.AddNewlyCreatedDBObject(acLine, True);
- //缩放到图形的范围或界限
- acDoc.SendStringToExecute('._zoom _all ', True, False, False);
- finally
- //提交修改并销毁事务
- acTrans.Commit;
- end;
- end;
- end.
复制代码 后记:在翻译这一段代码的过程中,出现了一个非委托的错误,原因是我在翻译的时候将下两行代码 - acBlkTbl := acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;
- acBlkTblRec := acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
- 中的as BlockTable和as BlockTableRecord两个运算去掉,加上这两个以后就正常了。
复制代码 |
|