using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[
assembly: CommandClass(typeof(JigTest.LineJig))]
namespace JigTest{
/// <summary> /// 命令类 /// </summary> public class LineJig {
/// <summary> /// 命令方法 /// </summary> [
CommandMethod('linejig')]
public void DoIt() {
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions options =
new PromptPointOptions(
'\n 指定起点');
ed.WriteMessage(
'\n 准备接收用户输入的点');
PromptPointResult result = ed.GetPoint(options);
//阻塞方法 ed.WriteMessage(
'\n 已获取用户输入的点');
ed.WriteMessage(
$'\n 起点坐标:{result.Value.X},{result.Value.Y},{result.Value.Z}');
Database db = Application.DocumentManager.MdiActiveDocument.Database;
LineJigImpl jig =
new LineJigImpl(result.Value);
Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
as BlockTableRecord;
btr.AppendEntity(jig.GetEntity());
tr.AddNewlyCreatedDBObject(jig.GetEntity(),
true);
tr.Commit();
}
}
}
/// <summary> /// Jig实现类 /// </summary> class LineJigImpl :
EntityJig {
Point3d m_StartPoint, m_EndPoint;
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
/// <summary> /// 构造函数 /// </summary> /// <param name='start'>起点</param> public LineJigImpl(Point3d start) : base(new Line()) {
editor.WriteMessage(
'\n 执行构造函数');
m_StartPoint = start;
editor.WriteMessage(
$'\n m_StartPoint初始值:{m_StartPoint}');
editor.WriteMessage(
$'\n m_EndPoint默认值:{m_EndPoint}');
}
/// <summary> /// 动态/即时/实时采样器 /// </summary> /// <param name='prompts'>即时提示</param> /// <returns></returns> protected override SamplerStatus Sampler(JigPrompts prompts) {
editor.WriteMessage(
'\n 执行Sampler;');
JigPromptPointOptions options =
new JigPromptPointOptions(
'\n 指定终点');
options.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.NoZeroResponseAccepted |
UserInputControls.NoNegativeResponseAccepted;
PromptPointResult result = prompts.AcquirePoint(options);
Point3d tempPoint = result.Value;
if (tempPoint != m_EndPoint) m_EndPoint = tempPoint;
else return SamplerStatus.NoChange;
if (result.Status == PromptStatus.Cancel)
return SamplerStatus.Cancel;
else return SamplerStatus.OK;
}
/// <summary> /// (动态/即时/实时)更新显示/动态显示 /// </summary> /// <returns></returns> protected override bool Update() {
editor.WriteMessage(
'\n 执行Update;');
try {
((Line)Entity).StartPoint = m_StartPoint;
((Line)Entity).EndPoint = m_EndPoint;
editor.WriteMessage(
$'\n m_EndPoint当前值:{m_EndPoint}');
}
catch (System.Exception)
{
return false;
}
return true;
}
/// <summary> /// 获取实体,便于外部调用 /// </summary> /// <returns></returns> public Entity GetEntity() {
return Entity; }
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。