动态标注jig示例
本帖最后由 Dani1988 于 2023-10-25 20:42 编辑先看效果,再发源码。。。。。
http://bbs.mjtd.com/data/attachment/album/202302/23/121948jo30p0tn3f55m3pc.gif
public static void CS5()
{
// 创建文字
var db = Env.Database;
DBText upText = new DBText();
DBText downText = new DBText();
upText.SetDatabaseDefaults(db);
downText.SetDatabaseDefaults(db);
upText.TextString = "我是升哥2018";
downText.TextString = "QQ:674971560";
double textHeight = 300;
upText.Height = downText.Height = textHeight;
// 设置对齐
upText.HorizontalMode = downText.HorizontalMode = TextHorizontalMode.TextLeft;
upText.VerticalMode = downText.VerticalMode = TextVerticalMode.TextVerticalMid;
upText.AdjustAlignment(db);
downText.AdjustAlignment(db);
// 调整位置
upText.Move(upText.Position, Point3d.Origin + new Vector3d(0, textHeight*0.2, 0));
downText.Move(downText.Position, Point3d.Origin + new Vector3d(0, -textHeight*1.2, 0));
// 计算高度
var textWidth = Math.Max(upText.GeometricExtents.MaxPoint.X - upText.GeometricExtents.MinPoint.X, downText.GeometricExtents.MaxPoint.X - downText.GeometricExtents.MinPoint.X);
// 创建文字线
Line textLine = new Line(Point3d.Origin, new Point3d(textWidth, 0, 0));
//textLine.ColorIndex = 3;
textLine.SetDatabaseDefaults(db);
// 获取标注位置
var r1 = Env.Editor.GetPoint("\n选择点1");
if (r1.Status != PromptStatus.OK)
return;
var pt1 = r1.Value.Ucs2Wcs();
// 创建引线
Line leaderLine = new Line(pt1, Point3d.Origin);
//leaderLine.ColorIndex = 3;
leaderLine.SetDatabaseDefaults(db);
// 创建拖拽类
using var j1 = new JigEx((mpw, _) =>
{
var pt2 = mpw;
leaderLine.EndPoint = pt2;
leaderLine.ColorIndex = 3;
textLine.ColorIndex = 3;
Matrix3d mt = Matrix3d.Displacement(pt2 - (pt2.X >= pt1.X ? textLine.StartPoint : textLine.EndPoint));
textLine.TransformBy(mt);
upText.TransformBy(mt);
downText.TransformBy(mt);
});
j1.DatabaseEntityDraw(wd =>
{
wd.Geometry.Draw(textLine);
wd.Geometry.Draw(leaderLine);
wd.Geometry.Draw(upText);
wd.Geometry.Draw(downText);
});
j1.SetOptions(pt1, CursorType.Crosshair, "\n选择下一点");
// 开始拖拽
var r2 = Env.Editor.Drag(j1);
if (r2.Status != PromptStatus.OK)
return;
// 加入数据库
using var tr = new DBTrans();
tr.CurrentSpace.AddEntity(leaderLine, textLine, upText, downText);
}
资瓷一下.. 真不错:handshake 本帖最后由 yupeng_dyp 于 2023-3-12 21:59 编辑
// 同样的功能不同的写法
[CommandMethod("TestCmd")]
public static void TestCmd()
{
using var tr = new DBTrans();
var resPt = Env.Editor.GetPoint("\n指定第一点");
if (resPt.Status != PromptStatus.OK) return;
var basePt = resPt.Value.Ucs2Wcs();
var textHeight = 300;
var textOffset = textHeight * 0.2;
var textStyle = tr.TextStyleTable[(string)Acap.GetSystemVariable("TEXTSTYLE")].GetObject(OpenMode.ForRead).ObjectId;
var upString = "上面:IFoxCad学习";
var downString = "下面:Jig模拟引线标注";
using var jigEx = new JigEx((mousePt, entities) => {
var upText = new DBText() { TextString = upString, Height = textHeight };
var downText = new DBText() { TextString = downString, Height = textHeight };
var leaderLine = new Line(basePt, mousePt);
var textLine = new Line(mousePt, mousePt);
#if NET35
upText.TextStyle = textStyle;
downText.TextStyle = textStyle;
#else
upText.TextStyleId = textStyle;
downText.TextStyleId = textStyle;
#endif
var upSize = upText.GeometricExtents.GetSize();
var downSize = downText.GeometricExtents.GetSize();
if (mousePt.X >= basePt.X) {
upText.HorizontalMode = downText.HorizontalMode = TextHorizontalMode.TextLeft;
upText.Move(upText.GeometricExtents.MinPoint, mousePt + new Vector3d(textOffset, textOffset, 0));
downText.Move(downText.GeometricExtents.MinPoint,
mousePt + new Vector3d(textOffset, -downSize.Height - textOffset, 0));
textLine.EndPoint = mousePt + new Vector3d(Math.Max(upSize.Width, downSize.Width), 0, 0);
}
else {
upText.HorizontalMode = downText.HorizontalMode = TextHorizontalMode.TextRight;
upText.Move(upText.GeometricExtents.MinPoint, mousePt + new Vector3d(-textOffset, textOffset, 0));
downText.Move(downText.GeometricExtents.MinPoint,
mousePt + new Vector3d(-textOffset, -downSize.Height - textOffset, 0));
textLine.EndPoint = mousePt + new Vector3d(-Math.Max(upSize.Width, downSize.Width), 0, 0);
}
upText.SetDatabaseDefaults(Env.Database);
upText.AdjustAlignment(Env.Database);
downText.SetDatabaseDefaults(Env.Database);
downText.AdjustAlignment(Env.Database);
entities.Enqueue(upText);
entities.Enqueue(downText);
entities.Enqueue(leaderLine);
entities.Enqueue(textLine);
});
jigEx.SetOptions(basePt, msg: "\n指定第二点");
if (jigEx.Drag().Status != PromptStatus.OK) return;
tr.CurrentSpace.AddEntity(jigEx.Entitys);
}
如何高亮呢?
页:
[1]