圆弧标注程序 源码(JIG)
本帖最后由 j15tty 于 2015-3-28 16:48 编辑using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Collections;
namespace YFBZ
{
public class Class1
{
const int WM_RBUTTONDOWN = 516;
int flag = 0;
private void Application_PreTranslateMessage(object sender, PreTranslateMessageEventArgs e)
{
if (e.Message.message == WM_RBUTTONDOWN)
{
flag = 1;
}
}
public void yfbzj()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
handle01:
PromptEntityOptions options = new PromptEntityOptions(" \n请选择圆弧");
options.SetRejectMessage("\n所选择实体不是圆弧,请重新选择");
options.AddAllowedClass(typeof(Arc), true);
PromptEntityResult per = ed.GetEntity(options);
ObjectId arced;
if (per.Status == PromptStatus.Cancel)
{
return;
}
if (per.Status == PromptStatus.OK)
{
arced = per.ObjectId;
}
else
{
arced = per.ObjectId;
return;
}
using (DocumentLock acLock = doc.LockDocument())
{
using (Transaction tran = db.TransactionManager.StartOpenCloseTransaction())
{
Arc arc = tran.GetObject(arced, OpenMode.ForRead) as Arc;
Point3d pt1 = arc.StartPoint;
Point3d pt2 = arc.EndPoint;
Point3d cen = arc.Center;
Line line1 = new Line(cen, pt1);
Line line2 = new Line(cen, pt2);
line1.ColorIndex = 5;
line2.ColorIndex = 5;
double R = arc.Radius;
double Length = arc.Length;
double l = arc.GetDistanceAtParameter(Length / 2);
Point3d pt = arc.GetPointAtDist(Length / 2);
double cenRo = Math.Abs(arc.StartAngle - arc.EndAngle);
double BottomRo = (Math.PI - cenRo) / 2;
double xianLength = Math.Sqrt((pt1.X - pt2.X) * (pt1.X - pt2.X) + (pt1.Y - pt2.Y) * (pt1.Y - pt2.Y));
double T = xianLength / 2 / Math.Sin(BottomRo);
Point3d pt3 = xTextStyleTools.midPoint(cen, pt);
db.AddToModelSpace(line1);
db.AddToModelSpace(line2);
for (; ; )
{
Point3d point1 = pt3;
DBText text1 = new DBText();
text1.TextString="R = "+R.ToString("0.000");
ObjectId text1Id = db.AddToModelSpace(text1);
DBText text2 = new DBText();
//text1.Height = 3;
text2.TextString="K = "+ Length.ToString("0.000");
//text2.Height = 3;
ObjectId text2Id = db.AddToModelSpace(text2);
DBText text3 = new DBText();
text3.TextString = "α = "+ xTextStyleTools.DFM(cenRo);
ObjectId text3Id = db.AddToModelSpace(text3);
DBText text4 = new DBText();
text4.TextString = "T = " + T.ToString("0.000");
ObjectId text4Id = db.AddToModelSpace(text4);
YFBZ.EntYFJip pointBz = new YFBZ.EntYFJip(text1Id, text2Id, text3Id,text4Id, point1);
PromptResult resJip = ed.Drag(pointBz);
if (resJip.Status == PromptStatus.Cancel)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
text1 = (DBText)tr.GetObject(pointBz.m_text1, OpenMode.ForWrite);
text1.Erase();
text2 = (DBText)tr.GetObject(pointBz.m_text2, OpenMode.ForWrite);
text2.Erase();
text3 = (DBText)tr.GetObject(pointBz.m_text3, OpenMode.ForWrite);
text3.Erase();
text4 = (DBText)tr.GetObject(pointBz.m_text4, OpenMode.ForWrite);
text4.Erase();
tr.Commit();
}
break;
}
Autodesk.AutoCAD.ApplicationServices.Application.PreTranslateMessage += new PreTranslateMessageEventHandler(Application_PreTranslateMessage);
if (flag == 1)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
text1 = (DBText)tr.GetObject(pointBz.m_text1, OpenMode.ForWrite);
text1.Erase();
text2 = (DBText)tr.GetObject(pointBz.m_text2, OpenMode.ForWrite);
text2.Erase();
text3 = (DBText)tr.GetObject(pointBz.m_text3, OpenMode.ForWrite);
text3.Erase();
text4 = (DBText)tr.GetObject(pointBz.m_text4, OpenMode.ForWrite);
text4.Erase();
tr.Commit();
}
break;
}
if (resJip.Status == PromptStatus.OK)
{
db.AddToModelSpace(pointBz.GetEntity());
flag = 0;
gotohandle01;
}
}
flag = 0;
tran.Commit();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
namespace YFBZ
{
public class EntYFJip : EntityJig
{
private Point2d[] m_pts = new Point2d;
private Point3d m_peakPt;
private double length1, length2, length,length3,length4;
private Point3d firstPoint;
private Point3d m_textPt1;
private Point3d m_textPt2;
private Point3d m_textPt3;
private Point3d m_textPt4;
private Matrix3d m_mt = Matrix3d.Identity;
public ObjectId m_text1 = ObjectId.Null;
public ObjectId m_text2 = ObjectId.Null;
public ObjectId m_text3 = ObjectId.Null;
public ObjectId m_text4 = ObjectId.Null;
public EntYFJip(ObjectId text1, ObjectId text2, ObjectId text3, ObjectId text4, Point3d Point1)
: base(new Polyline())
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
m_mt = ed.CurrentUserCoordinateSystem;
//初始化引线
((Polyline)Entity).AddVertexAt(0, Point2d.Origin, 0.0, 0.0, 0.0);
((Polyline)Entity).AddVertexAt(1, Point2d.Origin, 0.0, 0.4, 0.4);
((Polyline)Entity).AddVertexAt(2, Point2d.Origin, 0.0, 0.4, 0.4);
//参数传递
firstPoint = Point1;
m_text1 = text1;
m_text2 = text2;
m_text3 = text3;
m_text4 = text4;
}
protected override bool Update()
{
//throw new NotImplementedException();
Database db = HostApplicationServices.WorkingDatabase;
((Polyline)Entity).SetPointAt(0, m_pts);
((Polyline)Entity).SetPointAt(1, m_pts);
((Polyline)Entity).SetPointAt(2, m_pts);
((Polyline)Entity).Normal = Vector3d.ZAxis;
((Polyline)Entity).Elevation = 0.0;
//((Polyline)Entity).ConstantWidth = 0.4;
//((Polyline)Entity).TransformBy(m_mt);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBText dtext1 = (DBText)tr.GetObject(m_text1, OpenMode.ForWrite);
dtext1.Position = m_textPt1;
dtext1.Height = 3;
dtext1.WidthFactor = 0.8;
dtext1.ColorIndex = 80;
//dtext1.TextString = "X=" + m_pts.Y.ToString("0.000");
length1 = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(m_text1, dtext1.TextString, 2.4).X;
DBText dtext2 = (DBText)tr.GetObject(m_text2, OpenMode.ForWrite);
dtext2.Position = m_textPt2;
dtext2.Height = 3;
dtext2.WidthFactor = 0.8;
dtext2.ColorIndex = 80;
//dtext2.TextString = "Y=" + m_pts.X.ToString("0.000");
length2 = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(m_text2, dtext2.TextString, 2.4).X;
DBText dtext3 = (DBText)tr.GetObject(m_text3, OpenMode.ForWrite);
dtext3.Position = m_textPt3;
dtext3.Height = 3;
dtext3.ColorIndex = 80;
dtext3.WidthFactor = 0.8;
length3 = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(m_text3, dtext3.TextString, 2.4).X;
DBText dtext4 = (DBText)tr.GetObject(m_text4, OpenMode.ForWrite);
dtext4.Position = m_textPt4;
dtext4.Height = 3;
dtext4.ColorIndex = 80;
dtext4.WidthFactor = 0.8;
length4 = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(m_text4, dtext4.TextString, 2.4).X;
if (length1 >= length2)
{
length = length1;
}
else
{
length = length2;
}
if (length < length3)
{
length = length3;
}
if (length < length4)
{
length = length4;
}
tr.Commit();
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
//throw new NotImplementedException();
// 定义一个点拖动交互类.
JigPromptPointOptions optJigPoint = new JigPromptPointOptions("\n请指定下一点");
// 设置拖拽的光标类型.
optJigPoint.Cursor = CursorType.RubberBand;
// 设置拖动光标基点.
optJigPoint.BasePoint = firstPoint.TransformBy(m_mt);
optJigPoint.UseBasePoint = true;
// 用AcquirePoint函数得到用户输入的点.
PromptPointResult resJigPoint = prompts.AcquirePoint(optJigPoint);
Point3d curPt = resJigPoint.Value;
if (resJigPoint.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
if (m_peakPt != curPt)
{
//保存鼠标拖曳点
m_peakPt = curPt;
//得到多线坐标
if ((curPt.X - firstPoint.X >= 0 && curPt.Y - firstPoint.Y >= 0) || (curPt.X - firstPoint.X >= 0 && curPt.Y - firstPoint.Y <= 0))
{
m_pts = new Point2d(firstPoint.X, firstPoint.Y);
m_pts = new Point2d(m_peakPt.X, m_peakPt.Y);
m_pts = new Point2d(m_peakPt.X + length, m_peakPt.Y);
m_textPt1 = new Point3d(m_peakPt.X + 0.5, m_peakPt.Y + 9, m_peakPt.Z);
m_textPt2 = new Point3d(m_peakPt.X + 0.5, m_peakPt.Y + 5, m_peakPt.Z);
m_textPt3 = new Point3d(m_peakPt.X + 0.5, m_peakPt.Y + 1, m_peakPt.Z);
m_textPt4 = new Point3d(m_peakPt.X + 0.5, m_peakPt.Y + 13, m_peakPt.Z);
}
else
{
m_pts = new Point2d(firstPoint.X, firstPoint.Y);
m_pts = new Point2d(m_peakPt.X, m_peakPt.Y);
m_pts = new Point2d(m_peakPt.X - length, m_peakPt.Y);
m_textPt1 = new Point3d(m_peakPt.X - length+0.5, m_peakPt.Y + 9, m_peakPt.Z);
m_textPt2 = new Point3d(m_peakPt.X - length+0.5, m_peakPt.Y + 5, m_peakPt.Z);
m_textPt3 = new Point3d(m_peakPt.X - length+0.5, m_peakPt.Y + 1, m_peakPt.Z);
m_textPt4 = new Point3d(m_peakPt.X -length + 0.5, m_peakPt.Y + 13, m_peakPt.Z);
}
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
public Entity GetEntity()
{
return Entity;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace YFBZ
{
public static class Class2
{
public static ObjectId AddToModelSpace(this Database db, Entity ent)
{
ObjectId entId;//用于返回添加到模型空间中的实体ObjectId
//定义一个指向当前数据库的事务处理,以添加直线
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//以读方式打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//以写方式打开模型空间块表记录.
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
entId = btr.AppendEntity(ent);//将图形对象的信息添加到块表记录中
trans.AddNewlyCreatedDBObject(ent, true);//把对象添加到事务处理中
trans.Commit();//提交事务处理
}
return entId; //返回实体的ObjectId
}
/// <summary>
/// 获取与定点指定角度与距离的点
/// </summary>
/// <param name="pline">多段线对象</param>
public static Point3d polarPoint(this Point3d point, double angle, double dist)
{
return new Point3d(point.X + dist * Math.Cos(angle), point.Y + dist * Math.Sin(angle), point.Z);
}
/// <summary>
/// 弧度转度分秒
/// </summary>
/// <param name="Alpha">弧度数组</param>
public static string DFM(double Alpha)
{
string dfm;
dfm = "0.00";
double t = 180.0 * Alpha / 3.1415926;
int d = (int)t;
int m = (int)((t - d) * 60);
int s = (int)((t - d - m / 60.0) * 3600);
dfm = d.ToString() + "o" + m.ToString() + "\'" + s.ToString() + "\"";
return dfm;
}
/// <summary>
/// 获取两点直线中点
/// </summary>
/// <param name="pline">多段线对象</param>
public static Point3d midPoint(Point3d point1, Point3d point2)
{
return new Point3d((point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2, (point1.Z + point2.Z) / 2);
}
}
} 自己顶一下 学习,谢谢! 编译错误,xTextStyleTools不存在,没有定义吗?能否把整个工程放上来供大家学习呢?谢谢! mycad 发表于 2015-4-28 16:35 static/image/common/back.gif
编译错误,xTextStyleTools不存在,没有定义吗?能否把整个工程放上来供大家学习呢?谢谢!
我把度分秒函数给出来了的,你随便放到那个类里面不就可以了。xTextStyleTools.DFM,如果你新建一个类,类名假如为TOOLS ,并把DFM函数放进去,那么他的引用就是TOOLS.DFM;所以这个不影响你的。 这个代码真的很不错啊,参考后可以用于标注程序,实用性非常强,赞一个,顶起来! 学习了 很好,谢谢!学习一下!
页:
[1]