采用C#+CAD进行二次开发,首先需要获取数据,此处是获取选择数据的方法
db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
TypedValue[] values = new TypedValue[]
{
// new TypedValue((int)DxfCode.Start,"INSERT"),
//new TypedValue((int)DxfCode.LayerName,"ssd")
};
SelectionFilter filter = new SelectionFilter(values);
PromptSelectionResult psr = ed.GetSelection(filter);
if (psr.Status == PromptStatus.OK)
{
ids = psr.Value.GetObjectIds();
object drawing = comboBox1.SelectedValue;
drawingCode = Convert.ToString(drawing);
startButton.Enabled = false;
using (Transaction trasn = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
Entity br = (Entity)ids[i].GetObject(OpenMode.ForRead);
if (br is BlockReference)
{
BlockTable bt = (BlockTable)trasn.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockReference br1 = (BlockReference)ids[i].GetObject(OpenMode.ForRead);
string blkname = br1.Name;
//电源电路环线及零层端子配线图
if (blkname.Trim().Equals("JD73"))
{
TextData d = new TextData();
d.text = "JD73";
d.X = br1.Position.X;
d.Y = br1.Position.Y;
texts.Add(d);
}
//组合排列表
if (blkname.Trim().Equals("plb4"))
{
ObjectId blockRecordId = bt[blkname];
BlockTableRecord blockRecord = (BlockTableRecord)blockRecordId.GetObject(OpenMode.ForRead);
foreach (ObjectId entID in blockRecord)
{
Entity entity = (Entity)trasn.GetObject(entID, OpenMode.ForRead);
if (entity is DBText)
{
DBText textEntity = entity as DBText;
if (textEntity.Position.X > 0)
{
TextData d = new TextData();
d.text = textEntity.TextString;
d.X = textEntity.Position.X + br1.Position.X;
d.Y = textEntity.Position.Y + br1.Position.Y;
texts.Add(d);
}
}
else if (entity is MText)
{
MText textEntity = entity as MText;
if (textEntity.Location.X > 0)
{
TextData d = new TextData();
d.text = textEntity.Contents;
d.X = textEntity.Location.X + br1.Position.X;
d.Y = textEntity.Location.Y + br1.Position.Y;
texts.Add(d);
}
}
else if (entity is Polyline)
{
Polyline l = entity as Polyline;
LineData line = new LineData();
char[] separators = { '.' };
String startX = l.StartPoint.X.ToString().Split(separators)[0];
String startY = l.StartPoint.Y.ToString().Split(separators)[0];
String endX = l.EndPoint.X.ToString().Split(separators)[0];
String endY = l.EndPoint.Y.ToString().Split(separators)[0];
line.polyLine = l;
if (startY.Equals(endY) && !startX.Equals(endX))
{
//横线
if (l.StartPoint.X > l.EndPoint.X)
{
line.startX = l.EndPoint.X + br1.Position.X;
line.startY = l.EndPoint.Y + br1.Position.Y;
line.endX = l.StartPoint.X + br1.Position.X;
line.endY = l.StartPoint.Y + br1.Position.Y;
}
else
{
line.startX = l.StartPoint.X + br1.Position.X;
line.startY = l.StartPoint.Y + br1.Position.Y;
line.endX = l.EndPoint.X + br1.Position.X;
line.endY = l.EndPoint.Y + br1.Position.Y;
}
line.angle = 0;
lines.Add(line);
}
else if (startX.Equals(endX) && !startY.Equals(endY))
{
//竖线
if (l.StartPoint.Y > l.EndPoint.Y)
{
line.startX = l.StartPoint.X + br1.Position.X;
line.startY = l.StartPoint.Y + br1.Position.Y;
line.endX = l.EndPoint.X + br1.Position.X;
line.endY = l.EndPoint.Y + br1.Position.Y;
}
else
{
line.startX = l.EndPoint.X + br1.Position.X;
line.startY = l.EndPoint.Y + br1.Position.Y;
line.endX = l.StartPoint.X + br1.Position.X;
line.endY = l.StartPoint.Y + br1.Position.Y;
}
line.angle = 1;
lines.Add(line);
}
}
else if (entity is Line)
{
Line l = entity as Line;
LineData line = new LineData();
line.line = l;
char[] separators = { '.' };
String startX = l.StartPoint.X.ToString().Split(separators)[0];
String startY = l.StartPoint.Y.ToString().Split(separators)[0];
String endX = l.EndPoint.X.ToString().Split(separators)[0];
String endY = l.EndPoint.Y.ToString().Split(separators)[0];
if (startY.Equals(endY) && !startX.Equals(endX))
{
//横线
if (l.StartPoint.X > l.EndPoint.X)
{
line.startX = l.EndPoint.X + br1.Position.X;
line.startY = l.EndPoint.Y + br1.Position.Y;
line.endX = l.StartPoint.X + br1.Position.X;
line.endY = l.StartPoint.Y + br1.Position.Y;
}
else
{
line.startX = l.StartPoint.X + br1.Position.X;
line.startY = l.StartPoint.Y + br1.Position.Y;
line.endX = l.EndPoint.X + br1.Position.X;
line.endY = l.EndPoint.Y + br1.Position.Y;
}
line.angle = 0;
lines.Add(line);
}
else if (!startY.Equals(endY) && startX.Equals(endX))
{
if (l.StartPoint.X > 4349)
{
line.line = l;
}
//竖线
if (l.StartPoint.Y > l.EndPoint.Y)
{
line.startX = l.StartPoint.X + br1.Position.X;
line.startY = l.StartPoint.Y + br1.Position.Y;
line.endX = l.EndPoint.X + br1.Position.X;
line.endY = l.EndPoint.Y + br1.Position.Y;
}
else
{
line.startX = l.EndPoint.X + br1.Position.X;
line.startY = l.EndPoint.Y + br1.Position.Y;
line.endX = l.StartPoint.X + br1.Position.X;
line.endY = l.StartPoint.Y + br1.Position.Y;
}
line.angle = 1;
lines.Add(line);
}
}
}
}
//综合柜零层配线图
if (blkname.Trim().Equals("F"))
{
TextData d = new TextData();
d.text = "※";
d.X = br1.Position.X;
d.Y = br1.Position.Y;
texts.Add(d);
}
if (blkname.Trim().Equals("CONTI_1"))
{
TextData d = new TextData();
d.text = "△";
d.X = br1.Position.X;
d.Y = br1.Position.Y;
texts.Add(d);
}
}
else if (br is DBText)
{
DBText textEntity = br as DBText;
//if (textEntity.Position.X > 0)
//{
TextData d = new TextData();
d.text = textEntity.TextString;
d.X = textEntity.Position.X+0.3;
d.Y = textEntity.Position.Y;
texts.Add(d);
//}
}
else if (br is MText)
{
MText textEntity = br as MText;
//if (textEntity.Location.X > 0)
//{
TextData d = new TextData();
d.text = textEntity.Contents;
d.X = textEntity.Location.X;
d.Y = textEntity.Location.Y;
texts.Add(d);
//}
}
else if (br is Polyline)
{
Polyline l = br as Polyline;
int count = l.NumberOfVertices;
for (int j = 0; j < count - 1; j++)
{
Point3d pt1 = l.GetPoint3dAt(j);
Point3d pt2 = l.GetPoint3dAt(j + 1);
char[] separators1 = { '.' };
String startX = pt1.X.ToString().Split(separators1)[0];
String startY = pt1.Y.ToString().Split(separators1)[0];
String endX = pt2.X.ToString().Split(separators1)[0];
String endY = pt2.Y.ToString().Split(separators1)[0];
LineData line = new LineData();
if (startY.Equals(endY) && !startX.Equals(endX))
{
//横线
if (l.StartPoint.X > l.EndPoint.X)
{
line.startX = l.EndPoint.X;
line.startY = l.EndPoint.Y;
line.endX = l.StartPoint.X;
line.endY = l.StartPoint.Y;
}
else
{
line.startX = l.StartPoint.X;
line.startY = l.StartPoint.Y;
line.endX = l.EndPoint.X;
line.endY = l.EndPoint.Y;
}
line.angle = 0;
lines.Add(line);
}
else if (startX.Equals(endX) && !startY.Equals(endY))
{
//竖线
if (l.StartPoint.Y > l.EndPoint.Y)
{
line.startX = l.StartPoint.X;
line.startY = l.StartPoint.Y;
line.endX = l.EndPoint.X;
line.endY = l.EndPoint.Y;
}
else
{
line.startX = l.EndPoint.X;
line.startY = l.EndPoint.Y;
line.endX = l.StartPoint.X;
line.endY = l.StartPoint.Y;
}
line.angle = 1;
lines.Add(line);
}
}
}
else if (br is Line)
{
Line l = br as Line;
LineData line = new LineData();
line.line = l;
char[] separators = { '.' };
String startX = l.StartPoint.X.ToString().Split(separators)[0];
String startY = l.StartPoint.Y.ToString().Split(separators)[0];
String endX = l.EndPoint.X.ToString().Split(separators)[0];
String endY = l.EndPoint.Y.ToString().Split(separators)[0];
if (startY.Equals(endY) && !startX.Equals(endX))
{
//横线
if (l.StartPoint.X > l.EndPoint.X)
{
line.startX = l.EndPoint.X;
line.startY = l.EndPoint.Y;
line.endX = l.StartPoint.X;
line.endY = l.StartPoint.Y;
}
else
{
line.startX = l.StartPoint.X;
line.startY = l.StartPoint.Y;
line.endX = l.EndPoint.X;
line.endY = l.EndPoint.Y;
}
line.angle = 0;
lines.Add(line);
}
else if (!startY.Equals(endY) && startX.Equals(endX))
{
if (l.StartPoint.X > 4349)
{
line.line = l;
}
//竖线
if (l.StartPoint.Y > l.EndPoint.Y)
{
line.startX = l.StartPoint.X;
line.startY = l.StartPoint.Y;
line.endX = l.EndPoint.X;
line.endY = l.EndPoint.Y;
}
else
{
line.startX = l.EndPoint.X;
line.startY = l.EndPoint.Y;
line.endX = l.StartPoint.X;
line.endY = l.StartPoint.Y;
}
line.angle = 1;
lines.Add(line);
}
}else if(br is Circle)
{
Circle r1 = br as Circle;
TextData d = new TextData();
d.text = "○";
d.X = r1.StartPoint.X;
d.Y = r1.StartPoint.Y;
texts.Add(d);
}
}
}
Thread t = new Thread(detection);
t.Start();
}