|
发表于 2021-2-24 11:32:00
|
显示全部楼层
#region 公共函数
//创建实体,把实体加入CAD数据库
public ObjectId m_CreateEntity(Entity m_entity)
{
ObjectId m_objid = new ObjectId();
if (m_entity != null)
{
using (Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction m_tr = m_db.TransactionManager.StartTransaction())
{
BlockTableRecord m_btr = (BlockTableRecord)m_tr.GetObject(m_db.CurrentSpaceId, OpenMode.ForWrite, false);
m_objid = m_btr.AppendEntity(m_entity);
m_tr.AddNewlyCreatedDBObject(m_entity, true);
m_tr.Commit();
}
}
}
return m_objid;
}
//删除实体
public void m_EraseEntity(ObjectId m_EntityId)
{
using (DocumentLock m_doclock = Application.DocumentManager.MdiActiveDocument.LockDocument())
{
if (!m_EntityId.IsNull && !m_EntityId.IsErased)
{
Entity m_Ent = m_OpenEntity(m_EntityId) as Entity;
LayerTableRecord m_ltr = (LayerTableRecord)m_OpenEntity(m_Ent.LayerId);
if (!m_ltr.IsLocked)//实体不在锁定图层上
{
using (Transaction m_tr = m_db.TransactionManager.StartTransaction())
{
try
{
Entity m_Entity = (Entity)m_tr.GetObject(m_EntityId, OpenMode.ForWrite, false);
if (m_Entity != null) m_Entity.Erase();
}
catch { }
m_tr.Commit();
}
}
}
}
}
public void m_EraseEntity(Entity m_Entity)
{
using (DocumentLock m_doclock = Application.DocumentManager.MdiActiveDocument.LockDocument())
{
if (m_Entity != null) m_Entity.Erase();
else m_Entity.Dispose();
}
}
//打开实体
public Object m_OpenEntity(ObjectId m_ObjId)
{
Object m_Obj = new Object();
if (!m_ObjId.IsNull)
{
using (Transaction m_tr = m_db.TransactionManager.StartTransaction())
{
m_Obj = (Object)m_tr.GetObject(m_ObjId, OpenMode.ForRead);
m_tr.Commit();
}
}
return m_Obj;
}
//生成矩形
public Autodesk.AutoCAD.DatabaseServices.Polyline m_CreateRectangle(Point3d m_Pt1, Point3d m_Pt2, bool m_bLUorLW)
{
if (!m_bLUorLW)//不是左上和右下点,而是左下点和右上点
{
Point3d m_Pt3 = m_Pt1, m_Pt4 = m_Pt2;
m_Pt1 = new Point3d(m_Pt3.X, m_Pt4.Y, 0);
m_Pt2 = new Point3d(m_Pt4.X, m_Pt3.Y, 0);
}
Autodesk.AutoCAD.DatabaseServices.Polyline m_lwpline = new Autodesk.AutoCAD.DatabaseServices.Polyline();
m_lwpline.AddVertexAt(0, m_Pt1.Convert2d(new Plane()), 0, 0, 0);
m_lwpline.AddVertexAt(1, new Point2d(m_Pt2.X, m_Pt1.Y), 0, 0, 0);
m_lwpline.AddVertexAt(2, m_Pt2.Convert2d(new Plane()), 0, 0, 0);
m_lwpline.AddVertexAt(3, new Point2d(m_Pt1.X, m_Pt2.Y), 0, 0, 0);
m_lwpline.Closed = true;
return m_lwpline;
}
//提示用户输入一点
public bool m_GetPoint(string m_Msg, ref Point3d m_Pt, Point3d m_BasePoint, bool m_AllowNone, bool m_UseBasePoint)
{
if (!m_Pt.Equals(new Point3d())) m_Msg += "";
PromptPointOptions m_ppo = new PromptPointOptions("\n" + m_Msg);
m_ppo.AllowNone = m_AllowNone;
m_ppo.BasePoint = m_BasePoint;
m_ppo.UseBasePoint = m_UseBasePoint;
PromptPointResult m_ppr = m_ed.GetPoint(m_ppo);
if (m_ppr.Status != PromptStatus.OK)
{
if (m_ppr.Status == PromptStatus.None) { m_Pt = m_BasePoint; return true; }
else return false;
}
m_Pt = m_ppr.Value;
return true;
}
//提示选择单个实体
public bool m_SelectEntity(string m_MSG, string m_RejectMSG, Type[] m_EntityType, ref PromptEntityResult m_PER)
{
PromptEntityOptions m_peo = new PromptEntityOptions((m_MSG.Substring(0, 2).Equals("\n") ? m_MSG : "\n" + m_MSG));
m_peo.SetRejectMessage(m_RejectMSG);
foreach (Type et in m_EntityType) m_peo.AddAllowedClass(et, false);
m_peo.AllowNone = true;
m_PER = m_ed.GetEntity(m_peo);
if (m_PER.Status != PromptStatus.OK) return false;
else return true;
}
//判断点pt在直线pt1->Pt2的哪一侧?返回值:1右侧;0三点共线;-1左侧
public int m_PtSide(Point2d pt1, Point2d pt2, Point2d pt)
{
Vector2d m_vect1 = pt1.GetVectorTo(pt2);
Vector2d m_vect2 = pt1.GetVectorTo(pt);
double m_value = m_vect2.X * m_vect1.Y - m_vect1.X * m_vect2.Y;
if (Math.Abs(m_value) 0) return 1;
else return -1;
}
//求空间两曲线c1与c2在c1上的交点(带标高值)
public Point3dCollection m_GetIntersectPoints(Curve c1, Curve c2)
{
Point3dCollection m_interpts = new Point3dCollection(), m_interpts1 = new Point3dCollection();
Point3dCollection m_VertexPoints = new Point3dCollection();
c1.IntersectWith(c2, Intersect.OnBothOperands, new Plane(), m_interpts, 0, 0);//求交点
if (c1 is Polyline3d && !(c2 is Polyline3d))
{
foreach (Point3d pt in m_interpts)
{
Polyline3d m_pl3d = new Polyline3d(Poly3dType.SimplePoly,
new Point3dCollection(new Point3d[] { pt, pt.Add(new Vector3d(0, 0, 10)) }), false);
Point3dCollection m_Pts = new Point3dCollection();
c1.IntersectWith(m_pl3d, Intersect.ExtendArgument, new Plane(), m_Pts, 0, 0);
foreach (Point3d pt1 in m_Pts) m_interpts1.Add(pt1);
}
m_interpts = m_interpts1;
}
//去掉不正确的交点(IntersectWith求出的交点有可能不正确!!!)
ArrayList m_DistPtList = new ArrayList();
foreach (Point3d pt in m_interpts)
{
try
{
double m_Dist = c1.GetDistAtPoint(pt);//点是否在曲线上?
m_DistPoint3d m_DP = new m_DistPoint3d();
m_DP.m_dist = m_Dist;
m_DP.m_pt = pt;
m_DistPtList.Add(m_DP);
}
catch { }
}
m_DistPtList.Sort(new m_DistPtsSort());//排序
m_interpts1 = new Point3dCollection();
foreach (m_DistPoint3d dp in m_DistPtList) m_interpts1.Add(dp.m_pt);
return m_interpts1;
}
private struct m_DistPoint3d
{
public double m_dist;
public Point3d m_pt;
}
private class m_DistPtsSort : IComparer
{
int IComparer.Compare(object a, object b)
{
m_DistPoint3d dp1 = (m_DistPoint3d)a;
m_DistPoint3d dp2 = (m_DistPoint3d)b;
if (dp1.m_dist > dp2.m_dist)
return 1;
if (dp1.m_dist < dp2.m_dist)
return -1;
else
return 0;
}
}
//二维多段线转换为轻量多段线
public Autodesk.AutoCAD.DatabaseServices.Polyline m_Polyline2dToLWPolyline(ObjectId m_ObjId)
{
Autodesk.AutoCAD.DatabaseServices.Polyline m_pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
Polyline2d m_pl2d = m_OpenEntity(m_ObjId) as Polyline2d;
if (m_pl2d != null)
{
switch (m_pl2d.PolyType)
{
case Poly2dType.FitCurvePoly:
m_pl.ConvertFrom(m_pl2d, false);
break;
case Poly2dType.SimplePoly:
case Poly2dType.CubicSplinePoly:
case Poly2dType.QuadSplinePoly:
int index = 0;
foreach (ObjectId vid in m_pl2d)
{
Vertex2d v2d = m_OpenEntity(vid) as Vertex2d;
if (v2d.VertexType != Vertex2dType.SplineControlVertex)
m_pl.AddVertexAt(index++, new Point2d(v2d.Position.X, v2d.Position.Y), 0, v2d.StartWidth, v2d.EndWidth);
}
m_pl.LayerId = m_pl2d.LayerId;//继承层属性
m_pl.Elevation = m_pl2d.Elevation;//继承高程属性
m_pl.LinetypeId = m_pl2d.LinetypeId;//继承线型属性
m_pl.ColorIndex = m_pl2d.ColorIndex;//继承颜色属性
break;
default:
break;
}
}
return m_pl;
}
#endregion
}
}
|
|