using GrxCAD.ApplicationServices;
using GrxCAD.DatabaseServices;
using GrxCAD.EditorInput;
using GrxCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HCTools
{
class FeatureOverlap
{
//比例尺
public static int blc;
//重叠地物数量
private int number = 0;
public void check()
{
LayerControl lc = new LayerControl();
if (lc.haslayername("地物重叠") == false)
{
lc.creatlayer("地物重叠", new colorgb(255, 0, 255));
lc.movelayertofront("地物重叠");
}
else
{
lc.movelayertofront("地物重叠");
}
//选择块
selecblock();
search(blocklist,entitylist);
}
///
/// 定义一个块类,主要包括位置和名字
///
public struct Block
{
public double x;
public double y;
public double z;
public string name;
public Block(double x, double y, double z, string name)
{
this.x = x;
this.y = y;
this.z = z;
this.name = name;
}
}
//块列表
private List blocklist = new List();
private List entitylist = new List();
private void selecblock()
{
Document document = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database database = HostApplicationServices.WorkingDatabase;
DocumentLock documentlock = null;
Transaction traction = null;
try
{
traction = database.TransactionManager.StartTransaction();
documentlock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
BlockTable blocktable = traction.GetObject(database.BlockTableId,
OpenMode.ForRead) as BlockTable;
Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
TypedValue[] typedvalue = new TypedValue[1];
typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Insert"), 0);
SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
PromptSelectionResult psr = editor.GetSelection(selectionfilter); ///手动选择块参照
//PromptSelectionResult psr = editor.SelectAll();
if (psr.Status == PromptStatus.OK)
{
SelectionSet selectionset = psr.Value;
ObjectId[] obj = new ObjectId[selectionset.Count];
obj = selectionset.GetObjectIds();
for (int i = 0; i < obj.Length; i++)
{
ObjectId objid = obj[i];
Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForWrite);
if (entity is BlockReference)
{
BlockReference block = entity as BlockReference;
ObjectId id = block.BlockTableRecord;
BlockTableRecord blocktablerecord = traction.GetObject(id,
OpenMode.ForRead) as BlockTableRecord;
string blockname = blocktablerecord.Name.ToLower();
Block blc = new Block();
///块参照的位置
blc.x = block.Position.X;
blc.y = block.Position.Y;
blc.z = block.Position.Z;
blc.name = blockname;
///块参照的名称
switch (blockname)
{
case "gc028":
case "gc090":
case "gc110":
case "gc111":
case "gc119":
case "gc120":
case "gc121":
case "gc122":
case "gc124":
case "gc125":
case "gc126":
case "gc143":
case "gc144":
case "gc145":
case "gc160":
case "gc163":
case "gc164":
case "gc165":
case "gc200":
case "gc205":
case "gc206":
case "gc207":
case "gc208":
case "gc210":
case "gc211":
case "gc213":
case "gc246":
case "gc251":
case "gc253":
case "gc254":
case "gc319":
case "gc320":
case "gc322":
case "gc323":
case "gc325":
case "gc326":
entitylist.Add(entity);
blocklist.Add(blc);
break;
case "gc170":
if (block.Layer == "9000")
{
entitylist.Add(entity);
blocklist.Add(blc);
}
break;
default:
break;
}
}
}
editor.WriteMessage("\n" + "选中块数量:" + blocklist.Count.ToString());
}
traction.Commit();
}
catch (GrxCAD.Runtime.Exception)
{
traction.Abort();
return;
}
finally
{
traction.Dispose();
documentlock.Dispose();
}
}
///
/// 寻找与上面已选实体相交的那部分
///
///
public void search(List list, List list1)
{
Database database = HostApplicationServices.WorkingDatabase;
Transaction traction = null;
DocumentLock documentlock = null;
try
{
Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
for (int i = 0; i < list.Count; i++)
{
double x = Math.Round((double)list[i].x, 3, MidpointRounding.AwayFromZero);
double y = Math.Round((double)list[i].y, 3, MidpointRounding.AwayFromZero);
Entity entity1 = list1[i];
string name = list[i].name;
rebound reb = get_boundary(name);
double height = reb.height;
double width = reb.width;
int type = reb.type;
///按照比例尺决定大小
if (blc == 1000)
{
height = height / 2;
width = width / 2;
}
else if (blc == 500)
{
height = height / 4;
width = width / 4;
}
else if (blc == 2000)
{
height = height * 1;
width = width * 1;
}
///是为了获得四个角,好框选整个图形
Point3d p1 = new Point3d(x - width, y, 0);
Point3d p2 = new Point3d(x + width, y, 0);
Point3d p3 = new Point3d(x + width, y + height, 0);
Point3d p4 = new Point3d(x - width, y + height, 0);
if (type == 2)
{
p1 = new Point3d(x - width, y - height, 0);
p2 = new Point3d(x + width, y - height, 0);
p3 = new Point3d(x + width, y + height, 0);
p4 = new Point3d(x - width, y + height, 0);
}
else if (type == 3)
{
p1 = new Point3d(x - width, y - height, 0);
p2 = new Point3d(x + width, y - height, 0);
p3 = new Point3d(x + width, y, 0);
p4 = new Point3d(x - width, y, 0);
}
using (Transaction acTrans = database.TransactionManager.StartTransaction())
{
ViewTableRecord currview = editor.GetCurrentView();
currview.CenterPoint = new Point2d(x, y);
currview.ViewDirection = new Vector3d(0, 0, 1);
currview.Width = 40;
currview.Height = 40;
editor.SetCurrentView(currview);
acTrans.Commit();
}
PromptSelectionResult pselectresult = editor.SelectCrossingWindow(p1, p3);///框选范围内相交的实体
///如果有,按照类型对相交实体进行判断
traction = database.TransactionManager.StartTransaction();
documentlock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
BlockTable blocktable = traction.GetObject(database.BlockTableId,
OpenMode.ForRead) as BlockTable;
BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
if (pselectresult.Status == PromptStatus.OK)
{
SelectionSet selectionset = pselectresult.Value;
ObjectId[] obj = new ObjectId[selectionset.Count];
obj = selectionset.GetObjectIds();
int count = 0;
for (int j = 0; j < obj.Length; j++)
{
ObjectId objid = obj[j];
Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForWrite);
///块参照
if (entity is BlockReference)
{
BlockReference cblock = entity as BlockReference;
ObjectId cid = cblock.BlockTableRecord;
BlockTableRecord crecord = traction.GetObject(cid,
OpenMode.ForRead) as BlockTableRecord;
string blockname = crecord.Name.ToLower();
if (blockname != "gc086")
{
count++;
}
}
///文字
else if (entity is DBText)
{
string layer = entity.Layer;
if (layer != "0")
{
if (entity1 is BlockReference)
{
BlockReference cblock = entity1 as BlockReference;
DBText txt = entity as DBText;
if (cblock.Position.Z.ToString("N1") != txt.TextString)
count++;
}
}
}
///线
else if (entity is Polyline || entity is Polyline2d)
{
string layer = entity.Layer;
if (layer == "4400" || layer == "6100" || layer == "4300" || layer == "2000")
{
if (name != "gc200")
{
count++;
}
}
}
}
if (count > 1)
{
double height1 = 2 * height;
Point3d orip = new Point3d(x, y, 0);
Point3d newp = new Point3d(x, y + height1, 0);
if (name == "gc120" || name == "gc119" || name == "gc126" || name == "gc123" || name == "gc325" || name == "gc205"
|| name == "gc206" || name == "gc207" || name == "gc125" || name == "gc165" || name == "gc326" || name == "gc170"
|| name == "gc160" || name == "gc246" || name == "gc208" || name == "gc121" || name == "gc163" || name == "gc164"
|| name == "gc090" || name == "gc124" || name == "gc379")
{
if (entity1.IsWriteEnabled == true)
{
Move(entity1, orip, newp);
}
}
double px1 = p1.X;
double py1 = p1.Y;
double px2 = p2.X;
double py2 = p2.Y;
double px3 = p3.X;
double py3 = p3.Y;
double px4 = p4.X;
double py4 = p4.Y;
p1 = new Point3d(px1, py1 + height1, 0);
p2 = new Point3d(px2, py2 + height1, 0);
p3 = new Point3d(px3, py3 + height1, 0);
p4 = new Point3d(px4, py4 + height1, 0);
Point3dCollection coll = new Point3dCollection();
coll.Add(p1);
coll.Add(p2);
coll.Add(p3);
coll.Add(p4);
Polyline2d line2d = new Polyline2d(Poly2dType.SimplePoly, coll, 0, true, 0, 0, null);
line2d.SetDatabaseDefaults();
line2d.Layer = "地物重叠";
blocktablerecord.AppendEntity(line2d);
traction.AddNewlyCreatedDBObject(line2d, true);
number++;
}
}
traction.Commit();
}
editor.WriteMessage("\n");
editor.WriteMessage("检测到重叠地物:" + number);
editor.WriteMessage("\n");
number = 0;
}
catch (GrxCAD.Runtime.Exception)
{
traction.Abort();
return;
}
finally
{
if (documentlock != null)
{
documentlock.Dispose();
}
if (traction != null)
{
traction.Dispose();
}
}
}
///
/// type=1表示图形为上半部分,type=2表示图形为上下两部分,type=3表示下半部分(中心点跟图形的关系)
///
private struct rebound
{
public double height;
public double width;
public int type;
public rebound(double height, double width, int type)
{
this.height = height;
this.width = width;
this.type = type;
}
}
///
/// 对每个地物图标的大小进行绘制
///
///
///
private rebound get_boundary(string name)
{
double height = 0;
double width = 0;
int type = 0;
switch (name)
{
case "gc028":
height = 5;
width = 4;
type = 2;
break;
case "gc090":
height = 2.5;
width = 2.5;
type = 2;
break;
//case "gc110":
// height = 4;
// width = 4;
// type = 2;
// break;
//case "gc111":
// height = 4;
// width = 4;
// type = 2;
// break;
case "gc119":
height = 2.6;
width = 2.6;
type = 1;
break;
case "gc120":
height = 5;
width = 1;
type = 1;
break;
case "gc121":
height = 1.1;
width = 1.1;
type = 2;
break;
case "gc122":
height = 2.1;
width = 1.1;
type = 2;
break;
case "gc123":
height = 4.2;
width = 2.1;
type = 2;
break;
case "gc124":
height = 3;
width = 1.5;
type = 1;
break;
case "gc125":
height = 3.8;
width = 1.2;
type = 2;
break;
case "gc126":
height = 2.5;
width = 3;
type = 2;
break;
case "gc143":
height = 6;
width = 2;
type = 1;
break;
case "gc144":
height = 6;
width = 2;
type = 1;
break;
case "gc145":
height = 6;
width = 2;
type = 1;
break;
case "gc160":
height = 4.7;
width = 4.7;
type = 2;
break;
case "gc163":
height = 0.6;
width = 2.5;
type = 2;
break;
case "gc164":
height = 0.7;
width = 0.7;
type = 2;
break;
case "gc165":
height = 5;
width = 1;
type = 2;
break;
case "gc170":
height = 1.1;
width = 1.1;
type = 2;
break;
case "gc200":
height = 1.5;
width = 1.5;
type = 2;
break;
case "gc205":
height = 5;
width = 2;
type = 1;
break;
case "gc206":
height = 5;
width = 2;
type = 1;
break;
case "gc207":
height = 4;
width = 1;
type = 2;
break;
case "gc208":
height = 1.6;
width = 2.4;
type = 2;
break;
case "gc210":
height = 6;
width = 2.2;
type = 1;
break;
case "gc211":
height = 3;
width = 2.8;
type = 3;
break;
case "gc212":
height = 5;
width = 2.6;
type = 1;
break;
case "gc246":
height = 6;
width = 4;
type = 1;
break;
case "gc251":
height = 1;
width = 3.5;
type = 2;
break;
case "gc253":
height = 0.7;
width = 0.7;
type = 2;
break;
case "gc254":
height = 3;
width = 2.5;
type = 2;
break;
case "gc319":
height = 6;
width = 2;
type = 1;
break;
case "gc320":
height = 6;
width = 2;
type = 1;
break;
case "gc322":
height = 6;
width = 2;
type = 1;
break;
case "gc323":
height = 4;
width = 1.1;
type = 1;
break;
case "gc325":
height = 5;
width = 0.7;
type = 1;
break;
case "gc326":
height = 1.6;
width = 1.6;
type = 2;
break;
default:
height = 2.5;
width = 2;
type = 2;
break;
}
rebound rec = new rebound(height, width, type);
return rec;
}
///
/// 指定基点与目标点移动实体
///
/// 实体对象
/// 基点
/// 目标点
public static void Move(Entity ent, Point3d basePt, Point3d targetPt)
{
Vector3d vec = targetPt - basePt;
Matrix3d mt = Matrix3d.Displacement(vec);
ent.TransformBy(mt);
}
}
}