请教高手,在cad图面点击三点,程序自动绘制矩形框程序
本帖最后由 mycad 于 2019-6-12 16:48 编辑请教高手,在cad图面点击三点,程序自动绘制矩形框,矩形框可以使倾斜的,哪位大师有.net源代码能否贡献一下学习,谢谢!!!
请参考gile的程序片段:http://www.theswamp.org/index.php?topic=46284.msg513392#msg513392
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace RectangleDrawJig
{
public class CommandMethods
{
public void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointResult ppr = ed.GetPoint("\nSpecify the first corner: ");
if (ppr.Status != PromptStatus.OK) return;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
Point3d basePt = ppr.Value;
using (Transaction tr = db.TransactionManager.StartTransaction())
using(Line line = new Line(basePt, basePt))
using(Polyline pline = new Polyline(4))
{
Point2d basePt2d = new Point2d(basePt.X, basePt.Y);
pline.AddVertexAt(0, basePt2d, 0.0, 0.0, 0.0);
pline.AddVertexAt(1, basePt2d, 0.0, 0.0, 0.0);
pline.AddVertexAt(2, basePt2d, 0.0, 0.0, 0.0);
pline.AddVertexAt(3, basePt2d, 0.0, 0.0, 0.0);
pline.Closed = true;
RectangleJig jig = new RectangleJig(pline, line, ucs);
PromptResult pr = ed.Drag(jig);
if (pr.Status == PromptStatus.OK)
{
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
pline.TransformBy(ucs);
btr.AppendEntity(pline);
tr.AddNewlyCreatedDBObject(pline, true);
line.TransformBy(ucs);
btr.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
}
tr.Commit();
}
}
class RectangleJig : DrawJig
{
private Polyline pline;
private Line line;
private Matrix3d ucs2wcs, wcs2ucs;
private Point3d dragPt;
private Point2d basePt;
public RectangleJig(Polyline pline, Line line, Matrix3d ucs)
{
this.pline = pline;
this.line = line;
this.ucs2wcs = ucs;
this.wcs2ucs = ucs.Inverse();
this.dragPt = line.EndPoint;
this.basePt = pline.GetPoint2dAt(0);
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
Autodesk.AutoCAD.GraphicsInterface.WorldGeometry geo = draw.Geometry;
if (geo != null)
{
geo.PushModelTransform(ucs2wcs);
geo.Draw(line);
geo.Draw(pline);
geo.PopModelTransform();
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
PromptPointResult ppr = prompts.AcquirePoint("\nSpecify the opposite corner: ");
if (ppr.Value.IsEqualTo(dragPt))
return SamplerStatus.NoChange;
dragPt = ppr.Value;
Update();
return SamplerStatus.OK;
}
private void Update()
{
Point3d pt = dragPt.TransformBy(wcs2ucs);
line.EndPoint = pt;
pline.SetPointAt(1, new Point2d(pt.X, basePt.Y));
pline.SetPointAt(2, new Point2d(pt.X, pt.Y));
pline.SetPointAt(3, new Point2d(basePt.X, pt.Y));
}
}
}
}
关键字,jig,两点距离,向量,坐标转换 三点是任意三点吗?如果这三点是角点,不一定能构成矩形吧,如果不是角点,那能绘制的矩形不唯一 本帖最后由 Leo1980 于 2019-9-7 16:51 编辑
satan421 发表于 2019-9-5 10:21
三点是任意三点吗?如果这三点是角点,不一定能构成矩形吧,如果不是角点,那能绘制的矩形不唯一
点的先后顺序可以确定向量,下面是solidworks的
@Leo1980,是这个意思
是的 这个问题也有人回答,真狠惊奇。 楼主的代码实现了吗 mycad 发表于 2020-3-20 17:42
是的
能发上来,共享一下吗
页:
[1]
2