这个是关于AutoCAD二次开发模拟步骤效果,将块图形更新到与鼠标最为接近的点。注意该代码中已经将创建线段的两个点坐标已经写死了,大家可以尝试着写入动态的点,即鼠标选择的点,然后将图形更新到数据库图形空间中。这里涉及到块的名字预先是固定了的,如果是换成其他名字,那么,对于该程序将是行不通的,当然也可以按照需要输入块的名字,然后选择图形。
实现代码如下:
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;
namespace JigBetweenTwoPoint
{
public class Class1
{
[CommandMethod("TEST")]
public static void Test() {
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has("chkj"))
{
return;
}
using (BlockReference br = new BlockReference(Point3d.Origin, bt["chkj"]))
{
br.TransformBy(ed.CurrentUserCoordinateSystem);
var jig = new BlockJig(br, Point3d.Origin, new Point3d(10.0, 0.0, 0.0));
var pr = ed.Drag(jig);
if (pr.Status == PromptStatus.OK)
{
var curSpace = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
curSpace.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
}
}
trans.Commit();
}
}
}
class BlockJig : EntityJig
{
BlockReference br;
LineSegment3d line;
Point3d dragPt;
public BlockJig(BlockReference br, Point3d pt1, Point3d pt2)
: base(br)
{
this.br = br;
line = new LineSegment3d(pt1, pt2);
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions options = new JigPromptPointOptions("\nInsertion point: ");
options.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.UseBasePointElevation;
var result = prompts.AcquirePoint(options);
if (result.Value.IsEqualTo(dragPt))
{
return SamplerStatus.NoChange;
}
dragPt = result.Value;
return SamplerStatus.OK;
}
protected override bool Update()
{
br.Position = line.GetClosestPointTo(dragPt).Point;
return true;
}
}
}
更多内容,请关注公众号