- UID
- 806649
- 积分
- 78
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2020-12-24
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using TBPublicMethod;
using Autodesk.AutoCAD.Runtime;
using DotNetARX;
using SuperNetARX;
using Autodesk.AutoCAD.ApplicationServices;
using System;
namespace TestCommand
{
public class OverrulePoly: DrawableOverrule
{
public override bool WorldDraw(Drawable drawable, WorldDraw wd)
{
Autodesk.AutoCAD.DatabaseServices.Polyline pl = drawable as Autodesk.AutoCAD.DatabaseServices.Polyline;
double dLen = pl.Length;
double dScale = 0.5;
if (dLen < 10000) dScale = 0.05;
else if (dLen < 30000) dScale = 0.28;
else if (dLen < 50000) dScale = 0.4;
else if (dLen < 100000) dScale = 0.45;
Point3d ptRes = pl.GetPointAtDist(dLen * dScale);
//string strName = "疏散路径距离:" /*pl.GetTextData("Name") */ + (dLen/1000).ToString("N2") + "m";
short stOri = wd.SubEntityTraits.Color;
ResultBuffer rb = pl.GetXDataForApplication("TYPolyXData");
if (rb == null)
{
return base.WorldDraw(drawable, wd);
}
//文字内容
string strTitle = ":";
if (rb.AsArray() != null && rb.AsArray().Count() > 1)
strTitle = rb.AsArray()[1].Value.ToString() + ":"; //"疏散路径距离:";
string strName = strTitle + (dLen / 1000).ToString("N2") + "m";
//文字大小
double dTxtSize = 5;
if (rb.AsArray() != null && rb.AsArray().Count() > 2)
dTxtSize = (double)rb.AsArray()[2].Value; //"疏散路径距离:";
//文字颜色
short st = stOri;
if (rb.AsArray() != null && rb.AsArray().Count() > 3)
st = (short)rb.AsArray()[3].Value;
wd.SubEntityTraits.Color = st;
Vector3d vec = ComputeDir(pl, ptRes);
Matrix3d matrix = Matrix3d.Rotation(Math.PI / 2, Vector3d.ZAxis, ptRes);
ptRes += vec.TransformBy(matrix).GetUnitVector() * dTxtSize * 40;
//ObjectId txtId = CreateTextStyle("微软雅黑12", "微软雅黑");
//TextStyle textStyle = txtId.GetObject(OpenMode.ForRead) as TextStyle;
//TextStyle textStyle = new TextStyle();
//textStyle.FromTextStyleTableRecord("AAD-HT");
TextStyle textStyle = new TextStyle()
{
TextSize = dTxtSize * 80,
StyleName = "TYTextStyle",
FileName = "微软雅黑",
BigFontFileName = ""
};
wd.Geometry.Text(ptRes, Vector3d.ZAxis, vec, strName, true, textStyle);
//wd.Geometry.Text(ptRes, Vector3d.ZAxis, vec, 1000, 0.9 ,0, strName);
//本身
//wd.Geometry.Draw(pl);
//wd.Geometry.Polyline(pl,0,0);
//return true;
//圆弧
//wd.Geometry.CircularArc(startPoint, dLen, line.Normal, delta, Math.PI / 2, ArcType.ArcSimple);
//wd.Geometry.CircularArc(startPoint,pt3,endPoint, ArcType.ArcSimple);
//绘制直线本身
return base.WorldDraw(drawable, wd);
}
public override bool IsApplicable(RXObject overruledSubject)
{
Autodesk.AutoCAD.DatabaseServices.Polyline polyline = overruledSubject as Autodesk.AutoCAD.DatabaseServices.Polyline;
if (polyline != null && polyline.Layer == "0A-A-TY-PL")
return true;
else
return false;
}
private Vector3d ComputeDir(Autodesk.AutoCAD.DatabaseServices.Polyline pl, Point3d pt)
{
Vector3d vecDir = new Vector3d(1, 0, 0);
Point3dCollection gripPts = pl.GetEntityGripPoint();
gripPts.ClearCenterPt(1);
for(int i = 0; i < gripPts.Count-1; i++)
{
LineSegment3d ln = new LineSegment3d(gripPts[i],gripPts[i+1]);
PointOnCurve3d ptOnCrv3d = ln.GetClosestPointTo(pt);
double dDis = ptOnCrv3d.Point.DistanceTo(pt);
if(dDis < 1e-4)
{
vecDir = ln.Direction;
return vecDir;
}
}
return vecDir;
}
private ObjectId CreateTextStyle(string strName, string fontFileName, string bigFontFileName = "")
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
TextStyleTable tst = db.TextStyleTableId.GetObject(OpenMode.ForRead) as TextStyleTable;
if (!tst.Has(strName))
{
TextStyleTableRecord tstr = new TextStyleTableRecord();
tstr.Name = strName;
tstr.FileName = fontFileName;
tstr.BigFontFileName = bigFontFileName;
tst.UpgradeOpen();
tst.Add(tstr);
db.TransactionManager.AddNewlyCreatedDBObject(tstr, true);
tst.DowngradeOpen();
}
trans.Commit();
return tst[strName];
}
}
private Entity CreateTCHText()
{
Database db = HostApplicationServices.WorkingDatabase;
Entity entRes = null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
ListenNewCreateObj listen = new ListenNewCreateObj();
listen.StartToGetNewCreateEnt(db);
ResultBuffer rb = new ResultBuffer();
rb.Add(new TypedValue((int)ResBufCode.String, "TText"));
rb.Add(new TypedValue((int)ResBufCode.Point3d, Point3d.Origin));
rb.Add(new TypedValue((int)ResBufCode.String, ""));
TestClass.resBuffer = rb;
Application.DocumentManager.MdiActiveDocument.SendCommand("TYSendAcedCmd\n");
ObjectIdCollection ids = new ObjectIdCollection();
listen.GetAllNewIds(ids,db);
if(ids.Count == 1)
{
entRes = ids[0].GetObject(OpenMode.ForWrite) as Entity;
}
trans.Commit();
}
return entRes;
}
}
}
添加规则重定义
//规则重定义对象
private static OverrulePoly theOverride;
[CommandMethod("TYOverrulePoly")]
public static void TYOverrulePoly()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//Entity ent;
try
{
if (theOverride == null)
{
theOverride = new OverrulePoly();
//添加
Overrule.AddOverrule(RXObject.GetClass(typeof(Polyline)), theOverride, false);
//theOverride.SetXDataFilter("TYPolyline");
theOverride.SetCustomFilter();
}
Overrule.Overruling = true;//开启
Application.DocumentManager.MdiActiveDocument.Editor.Regen();
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message + " " + ex.StackTrace + "\n");
}
trans.Commit();
}
}
|
|