使用方法如下:
SearchCondition searchCondition = new SearchCondition();
searchCondition.LayerName = m_DeviceMaterialLayerName;
searchCondition.EntityType = PrimitiveType.Circle;
searchCondition.ExtendedDataRegAppName = m_XDataRegAppName;
EntitySearch entitySearch = new EntitySearch();
entitySearch.SelectionFilter = searchCondition.GetSelectionFilter();
List<ComAlg.Circle> allConnectPoints = entitySearch.GetSearchResultAsCircle();
可以搜索多段线、圆、块参照、扩展数据,可以指定图层、线型、块名等
以下是实体搜索类/**
* @author xuhaiyan* @copyright opensource
* @date 2013-02-27
* @version 1.0
* @description
* @dependence .net 2.0
*/
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using ComAlg = Common.Algorithm;
using Autodesk.AutoCAD.EditorInput;
using AcadAppser = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
namespace Common.ACAD08
{
public enum PrimitiveType
{
Polyline = 1,
Line = 2,
BlockReference = 4,
Circle = 8
}
public class SearchCondition
{
public PrimitiveType m_EntityType = 0;
public string m_LayerName = "";
public string m_LineTypeName = "";
public string m_BlockName = "";
public string m_ExtendedDataRegAppName = "";
public SelectionFilter m_SelectionFilter = null;
public PrimitiveType EntityType
{
get { return m_EntityType; }
set { m_EntityType = value; }
}
public string LayerName
{
get { return m_LayerName; }
set { m_LayerName = value; }
}
public string LineTypeName
{
get { return m_LineTypeName; }
set { m_LineTypeName = value; }
}
public string BlockName
{
get { return m_BlockName; }
set { m_BlockName = value; }
}
public string ExtendedDataRegAppName
{
get { return m_ExtendedDataRegAppName; }
set { m_ExtendedDataRegAppName = value; }
}
public SearchCondition()
{
}
public SearchCondition(PrimitiveType entityType)
{
m_EntityType = entityType;
}
public SearchCondition(PrimitiveType entityType, string layerName)
{
m_EntityType = entityType;
m_LayerName = layerName;
}
public SearchCondition(string layerName, string lineTypeName)
{
m_LayerName = layerName;
m_LineTypeName = lineTypeName;
}
public SearchCondition(PrimitiveType entityType, string layerName, string lineTypeName)
{
m_EntityType = entityType;
m_LayerName = layerName;
m_LineTypeName = lineTypeName;
}
public SearchCondition(PrimitiveType entityType, string layerName, string lineTypeName, string extendedDataRegAppName)
{
m_EntityType = entityType;
m_LayerName = layerName;
m_LineTypeName = lineTypeName;
m_ExtendedDataRegAppName = extendedDataRegAppName;
}
public SelectionFilter GetSelectionFilter()
{
List<TypedValue> values = new List<TypedValue>();
if ("" != m_LayerName)
{
values.Add(new TypedValue((int)DxfCode.LayerName, m_LayerName));
}
if ("" != m_LineTypeName)
{
values.Add(new TypedValue((int)DxfCode.LinetypeName, m_LineTypeName));
}
if ("" != m_BlockName)
{
values.Add(new TypedValue((int)DxfCode.BlockName, m_BlockName));
}
if ("" != m_ExtendedDataRegAppName)
{
values.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, m_ExtendedDataRegAppName));
}
if (0 != m_EntityType)
{
StringBuilder sb = new StringBuilder();
foreach (PrimitiveType pt in Enum.GetValues(typeof(PrimitiveType)))
{
if (pt == (m_EntityType & pt))
{
if (pt == PrimitiveType.Line)
{
sb.Append("LINE");
sb.Append(",");
}
else if (pt == PrimitiveType.Polyline)
{
sb.Append("*POLYLINE");
sb.Append(",");
}
else if (pt == PrimitiveType.BlockReference)
{
sb.Append("INSERT");
sb.Append(",");
}
else if (pt == PrimitiveType.Circle)
{
sb.Append("CIRCLE");
sb.Append(",");
}
}
}
if (sb.Length > 0)
{
sb.Remove(sb.Length - 1, 1);
values.Add(new TypedValue((int)DxfCode.Start, sb.ToString()));
}
}
if (values.Count > 0)
{
m_SelectionFilter = new SelectionFilter(values.ToArray());
}
return m_SelectionFilter;
}
}
public class EntitySearch
{
private SelectionFilter m_SelectionFilter;
private AcadAppser.Document m_CurrentDocument;
public SelectionFilter SelectionFilter
{
get { return m_SelectionFilter; }
set { m_SelectionFilter = value; }
}
public EntitySearch()
{
m_CurrentDocument = AcadAppser.Application.DocumentManager.MdiActiveDocument;
}
public List<ObjectId> GetSearchResult()
{
List<ObjectId> re = null;
using (AcadAppser.DocumentLock m_doclock = m_CurrentDocument.LockDocument()) //在非模态下打开模型空间前要解锁
{
PromptSelectionResult res = m_CurrentDocument.Editor.SelectAll(m_SelectionFilter);
if (res.Value != null && res.Value.Count > 0 && res.Status == PromptStatus.OK)
{
ObjectId[] ids = res.Value.GetObjectIds();
re = new List<ObjectId>(ids);
}
}
return re;
}
public List<ObjectId> GetSearchResult(Point3dCollection windowPolygon)
{
List<ObjectId> re = null;
using (AcadAppser.DocumentLock m_doclock = m_CurrentDocument.LockDocument()) //在非模态下打开模型空间前要解锁
{
PromptSelectionResult res = m_CurrentDocument.Editor.SelectWindowPolygon(windowPolygon,m_SelectionFilter);
if (res.Value != null && res.Value.Count > 0 && res.Status == PromptStatus.OK)
{
ObjectId[] ids = res.Value.GetObjectIds();
re = new List<ObjectId>(ids);
}
}
return re;
}
public List<ComAlg.PolyLine2D> GetSearchResultAsPLine()
{
List<ComAlg.PolyLine2D> re = null;
List<ObjectId> ids = GetSearchResult();
if (ids != null && ids.Count > 0)
{
m_CurrentDocument.Editor.SetImpliedSelection(ids.ToArray());
re = new List<ComAlg.PolyLine2D>();
using (AcadAppser.DocumentLock m_doclock = m_CurrentDocument.LockDocument()) //在非模态下打开模型空间前要解锁
{
using (Transaction trans = m_CurrentDocument.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId id in ids)
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
//ent.Highlight();//此句可使实体高亮,但无夹点
Type t = ent.GetType();
string type = t.Name.ToLower();
switch (type)
{
case "line":
Line line = ent as Line;
re.Add(ConvertToPLine(line));
break;
case "polyline":
Polyline pl = ent as Polyline;
re.Add(ConvertToPLine(pl));
break;
default:
break;
}
}
}
}
}
return re;
}
public List<ComAlg.BlockReference> GetSearchResultAsBlockRefrence()
{
List<ComAlg.BlockReference> re = null;
List<ObjectId> ids = GetSearchResult();
if (ids != null && ids.Count > 0)
{
m_CurrentDocument.Editor.SetImpliedSelection(ids.ToArray());//设置选中状态
re = new List<ComAlg.BlockReference>();
using (AcadAppser.DocumentLock m_doclock = m_CurrentDocument.LockDocument()) //在非模态下打开模型空间前要解锁
{
using (Transaction trans = m_CurrentDocument.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId id in ids)
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
//ent.Highlight();//此句可使实体高亮,但无夹点
Type t = ent.GetType();
string type = t.Name.ToLower();
switch (type)
{
case "blockreference":
BlockReference blockRef = ent as BlockReference;
re.Add(ConvertToBlockReference(blockRef));
break;
default:
break;
}
}
}
}
}
return re;
}
public List<ComAlg.Circle> GetSearchResultAsCircle()
{
List<ComAlg.Circle> re = null;
List<ObjectId> ids = GetSearchResult();
if (ids != null && ids.Count > 0)
{
m_CurrentDocument.Editor.SetImpliedSelection(ids.ToArray());//设置选中状态
re = new List<ComAlg.Circle>();
using (AcadAppser.DocumentLock m_doclock = m_CurrentDocument.LockDocument()) //在非模态下打开模型空间前要解锁
{
using (Transaction trans = m_CurrentDocument.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId id in ids)
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
//ent.Highlight();//此句可使实体高亮,但无夹点
Type t = ent.GetType();
string type = t.Name.ToLower();
switch (type)
{
case "circle":
Circle circle = ent as Circle;
re.Add(ConvertToCircle(circle));
break;
default:
break;
}
}
}
}
}
return re;
}
private Common.Algorithm.Circle ConvertToCircle(Circle circle)
{
Common.Algorithm.Circle re = new Common.Algorithm.Circle();
re.CenterPt2d = ConvertToPoint2D(circle.Center);
re.Radius = circle.Radius;
if (circle.XData != null)
{
Dictionary<string, ComAlg.XData> xDatas = ConvertXDataToXDataStruct(circle.XData);
string regAppName = getRegAppNameCondition();
if ("" != regAppName && xDatas.ContainsKey(regAppName))
{
re.XData = xDatas[regAppName].ExtendedDataAsciiString;
}
}
return re;
}
public ComAlg.BlockReference ConvertToBlockReference(BlockReference blockRef)
{
Common.Algorithm.BlockReference re = new Common.Algorithm.BlockReference();
re.BlockName = blockRef.Name;
re.MinPoint = ConvertToPoint2D(blockRef.GeometricExtents.MinPoint);
re.MaxPoint = ConvertToPoint2D(blockRef.GeometricExtents.MaxPoint);
re.Position = new Common.Algorithm.Point2D(blockRef.Position.X, blockRef.Position.Y);
if (blockRef.XData != null)
{
Dictionary<string, ComAlg.XData> xDatas = ConvertXDataToXDataStruct(blockRef.XData);
string regAppName = getRegAppNameCondition();
if ("" != regAppName && xDatas.ContainsKey(regAppName))
{
re.XData = xDatas[regAppName].ExtendedDataAsciiString;
}
}
return re;
}
public ComAlg.PolyLine2D ConvertToPLine(Line line)
{
CADBase cadBase = new CADBase();
List<ComAlg.Point2D> pts = new List<ComAlg.Point2D>();
pts.Add(new ComAlg.Point2D(line.StartPoint.X, line.StartPoint.Y));
pts.Add(new ComAlg.Point2D(line.EndPoint.X, line.EndPoint.Y));
ComAlg.PolyLine2D pline = new Common.Algorithm.PolyLine2D(pts);
pline.ObjectId = line.ObjectId.ToString();
pline.LineTypeName = line.Linetype;
pline.LineWidth = cadBase.ConvertToLineWeightValue( line.LineWeight);
pline.LineScale = line.LinetypeScale;
return pline;
}
public ComAlg.PolyLine2D ConvertToPLine(Polyline pl)
{
CADBase cadBase = new CADBase();
List<ComAlg.Point2D> pts = new List<ComAlg.Point2D>();
int count = pl.NumberOfVertices;
Point2d pt;
for (int i = 0; i < count; i++)
{
pt = pl.GetPoint2dAt(i);
pts.Add(new ComAlg.Point2D(pt.X, pt.Y));
}
ComAlg.PolyLine2D pline = new Common.Algorithm.PolyLine2D(pts);
pline.ObjectId = pl.ObjectId.ToString();
pline.LineTypeName = pl.Linetype;
pline.LineWidth = cadBase.ConvertToLineWeightValue(pl.LineWeight);
pline.ConstantWidth = pl.ConstantWidth;
pline.LineScale = pl.LinetypeScale;
return pline;
}
private Dictionary<string,ComAlg.XData> ConvertXDataToXDataStruct(ResultBuffer resultBuffer)
{
Dictionary<string, ComAlg.XData> re = new Dictionary<string, ComAlg.XData>();
TypedValue[] tvs = resultBuffer.AsArray();
int count = tvs.Length;
ComAlg.XData caXdata = new Common.Algorithm.XData();
for (int i = 0; i < count; i++)
{
if (tvs[i].TypeCode == (short)DxfCode.ExtendedDataRegAppName)
{
if (caXdata.ExtendedDataRegAppName != null)
{
re.Add(caXdata.ExtendedDataRegAppName, caXdata);
}
caXdata = new Common.Algorithm.XData();
caXdata.ExtendedDataRegAppName = tvs[i].Value.ToString();
}
else if (tvs[i].TypeCode == (short)DxfCode.ExtendedDataAsciiString)
{
caXdata.ExtendedDataAsciiString = tvs[i].Value.ToString();
}
}
if (caXdata.ExtendedDataRegAppName != null)
{
re.Add(caXdata.ExtendedDataRegAppName, caXdata);
}
return re;
}
private string getRegAppNameCondition()
{
string re = "";
int count = m_SelectionFilter.GetFilter().Length;
TypedValue[] tvs = m_SelectionFilter.GetFilter();
for (int i = 0; i < count; i++)
{
if (tvs[i].TypeCode == (short)DxfCode.ExtendedDataRegAppName)
{
re = tvs[i].Value.ToString();
break;
}
}
return re;
}
private ComAlg.Point2D ConvertToPoint2D(Point3d pt3d)
{
return new ComAlg.Point2D(pt3d.X, pt3d.Y);
}
}
}