展示
红色(1号色)是三维的,黄色(2号色)投影下来的,它是曲线,因为曲线是直线的父类.
命令
[CommandMethod("test_ty", CommandFlags.Modal | CommandFlags.Session | CommandFlags.Redraw)]
public static void JJ_testty()
{
Database db = HostApplicationServices.WorkingDatabase;//当前的数据库
Editor ed = Acap.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\n****{惊惊连盒}测试,投影三维图元到某个平面上");
using (Acap.DocumentManager.MdiActiveDocument.LockDocument())//锁文档 CommandFlags.Session
{
db.Action(tr =>
{
//三维的线
var entity = new Line(Point3d.Origin, new Point3d(1, 1, 1));//var entity = EntityAdd.AddLineToEntity(Point3d.Origin, new Point3d(1, 1, 1));
entity.ColorIndex = 1;
var lineId = tr.AddEntityToMsPs(db, entity);
var ids = new List<ObjectId>() { lineId };
var cus = tr.Entitys2Plane(ids);
foreach (var item in cus)
{
item.ColorIndex = 2;
tr.AddEntityToMsPs(db, item);
}
});
}
}
投影平面
/// <summary>
/// 投影平面
/// </summary>
/// <param name="tr">事务</param>
/// <param name="ids">图元</param>
/// <param name="normal">投影方向</param>
/// <returns>投影到某个平面的图元</returns>
public static List<Curve> Entitys2Plane(this Transaction tr,
IEnumerable<ObjectId> ids, Vector3d? normal = null)
{
var ls = new List<Curve>();
if (normal == null)
{
normal = Vector3d.ZAxis;//绕Z轴,也就是XY平面
}
var plane = new Plane(Point3d.Origin, normal.Value);
foreach (var sobject in ids)
{
if (sobject.IsOk())
{
var curve = sobject.ToEntity(tr) as Curve;
if (curve == null)
continue;
Curve pCurve = null;
try
{
pCurve = curve.GetOrthoProjectedCurve(plane);
}
catch
{ }
if (pCurve == null)
continue;
ls.Add(pCurve);
}
}
return ls;
}
/// <summary>
/// 将图形添加到数据库的当前空间中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="ent">图形对象</param>
/// <returns>图形的ObjectId</returns>
public static ObjectId AddEntityToMsPs(this Transaction tr, Database db, Entity ent)
{
ObjectId entId;
//在位编辑的时候自动加到块内了,而不是模型空间,因为会把临时的数据拷贝回去块表记录上面
//出现eLockViolation 是因 CommandFlags.Session | CommandFlags.Redraw 又没有锁文档
var btRec = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
//加图元到块表记录,设定返回值
entId = btRec.AppendEntity(ent);
//更新数据
tr.AddNewlyCreatedDBObject(ent, true);
btRec.DowngradeOpen();
btRec.Dispose();
return entId;
}
(完)