- 积分
- 35945
- 明经币
- 个
- 注册时间
- 2006-12-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 qjchen 于 2011-12-20 11:53 编辑
这段代码主要是为了弥补LISP中grread较难实现捕捉的缺点
各位见笑了 :)
撰写此段代码的时候,学习了ahlzl兄和飞狐兄的相关代码,谢谢两位~
平台 : VS2008+CAD2007
命令 : JPE
效果 :如下
代码 :
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.GraphicsInterface;
- using Autodesk.AutoCAD.Runtime;
- namespace JigPerToCurve
- {
- public class Class0 : IExtensionApplication
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- // 加载程序时的初始化操作
- void IExtensionApplication.Initialize()
- {
- ed.WriteMessage("**Write by [url=mailto:qjchen@gmail.com]qjchen@gmail.com[/url], To draw a line per to a curve, Command:JPE");
- }
- // 卸载程序时的清除操作
- void IExtensionApplication.Terminate()
- {
- }
- }
- public class Class1 : DrawJig
- {
- private Line ent;
- private Curve curve;
- [CommandMethod("jpe")]
- public void Createjigjdx()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- PromptEntityOptions optEnt = new PromptEntityOptions("\n选择曲线:");
- PromptEntityResult resEnt = ed.GetEntity(optEnt);
- if (resEnt.Status == PromptStatus.OK)
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- curve = (Curve)(trans.GetObject(resEnt.ObjectId, OpenMode.ForWrite));
- ent = new Line(Point3d.Origin, Point3d.Origin);
- PromptResult resJig = ed.Drag(this);
- if (resJig.Status == PromptStatus.OK)
- {
- btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- }
- trans.Commit();
- }
- }
- }
- // Sampler函数用于检测用户的输入.
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- JigPromptPointOptions optJigDis = new JigPromptPointOptions("\n请指定一个新的点");
- optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
- PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
- //curPt就是现在屏幕上鼠标位置点
- Point3d curPt = resJigDis.Value;
- //下面这句话很重要,不然会出错,必须保证这个点是在线上
- Point3d point = curve.GetClosestPointTo(curPt, false);
- if (resJigDis.Status == PromptStatus.Cancel)
- {
- return SamplerStatus.Cancel;
- }
- Vector3d vtan = curve.GetFirstDerivative(point);
- Vector3d vnor0 = vtan.RotateBy(Math.PI / 2, Vector3d.ZAxis).GetNormal();
- Vector3d vnor1 = vtan.RotateBy(-Math.PI / 2, Vector3d.ZAxis).GetNormal();
- double vsize = (double)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("VIEWSIZE");
- Point3d point_per0 = point + vnor0.MultiplyBy(vsize / 5.0);
- Point3d point_per1 = point + vnor1.MultiplyBy(vsize / 5.0);
- //判断当前点在曲线的哪边,若相等的时候取默认的
- if (point_per1.DistanceTo(curPt) < point_per0.DistanceTo(curPt))
- {
- point_per0 = point_per1;
- }
- ent.StartPoint = point;
- ent.EndPoint = point_per0;
- return SamplerStatus.OK;
- }
- // WorldDraw函数用于刷新屏幕上显示的图形.
- protected override bool WorldDraw(WorldDraw draw)
- {
- // 刷新画面.
- draw.Geometry.Draw(ent);
- return true;
- }
- }
- }
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
查看全部评分
|