214 lines
6.8 KiB
C#
214 lines
6.8 KiB
C#
using netDxf.Blocks;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace MetroGauges.Controls.BlockShaps
|
|
{
|
|
/// <summary>
|
|
/// 操作方法
|
|
/// </summary>
|
|
public enum OprType
|
|
{
|
|
DrawDxfBlocks,
|
|
ExportSourceBlock,//通过源bock插入
|
|
CopyBlock,
|
|
MoveBlock,
|
|
MoveByX,
|
|
MoveByY,
|
|
ScaleBlock
|
|
|
|
}
|
|
public class BlockOpr
|
|
{
|
|
public double ScaleRale { get; set; }
|
|
public netDxf.DxfDocument dxf { get; set; }
|
|
public BlockData BlockData { get; set; }
|
|
public netDxf.Tables.Layer layer { get; set; }
|
|
|
|
public System.Windows.Point currentPoint { get; set; }
|
|
public System.Windows.Point dragPoint { get; set; }
|
|
|
|
public double rate { get; set; }
|
|
|
|
private BaseShap GetBaseShap(FrameworkElement shapeitem)
|
|
{
|
|
BaseShap baseShap = null;
|
|
if (shapeitem is Line) //线段
|
|
{
|
|
baseShap = new BlockLine(shapeitem) { m_ScaleRale = this.ScaleRale };
|
|
}
|
|
else if (shapeitem is Polyline) //多义线
|
|
{
|
|
baseShap = new BlockPolyline(shapeitem) { m_ScaleRale = this.ScaleRale };
|
|
}
|
|
else if (shapeitem is Path) //
|
|
{
|
|
Path path = shapeitem as Path;
|
|
if (path.Data is EllipseGeometry) //圆
|
|
{
|
|
baseShap = new BlockEllipse(shapeitem) { m_ScaleRale = this.ScaleRale };
|
|
}
|
|
else if (path.Data is PathGeometry) //圆弧
|
|
{
|
|
baseShap = new BlockArc(shapeitem) { m_ScaleRale = this.ScaleRale };
|
|
}
|
|
}
|
|
else if (shapeitem is TextBlock)
|
|
{
|
|
baseShap = new BlockText(shapeitem) { m_ScaleRale = this.ScaleRale };
|
|
}
|
|
else if (shapeitem is Rectangle)
|
|
{
|
|
baseShap = new BlockRectangle(shapeitem) { m_ScaleRale = this.ScaleRale };
|
|
}
|
|
|
|
return baseShap;
|
|
}
|
|
/// <summary>
|
|
/// 执行方法
|
|
/// </summary>
|
|
/// <param name="block"></param>
|
|
/// <param name="oprType"></param>
|
|
public void ExctecOpr(List<FrameworkElement> block, OprType oprType)
|
|
{
|
|
if (oprType == OprType.ExportSourceBlock)
|
|
{
|
|
ExportBlock();
|
|
return;
|
|
}
|
|
|
|
foreach (var shapeitem in block)
|
|
{
|
|
BaseShap baseShap = GetBaseShap(shapeitem);
|
|
switch (oprType)
|
|
{
|
|
case OprType.CopyBlock:
|
|
baseShap.CopyBlock();
|
|
break;
|
|
case OprType.DrawDxfBlocks: //拆分导出
|
|
baseShap.DrawDxfBlocks(this.dxf, this.layer);
|
|
break;
|
|
case OprType.MoveBlock:
|
|
baseShap.MoveBlock(this.currentPoint, this.dragPoint);
|
|
break;
|
|
case OprType.MoveByX:
|
|
baseShap.MoveByX(this.currentPoint.X);
|
|
break;
|
|
case OprType.MoveByY:
|
|
baseShap.MoveByY(this.currentPoint.Y);
|
|
break;
|
|
case OprType.ScaleBlock:
|
|
baseShap.ScaleBlock(this.rate);
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 插入块
|
|
/// </summary>
|
|
private void ExportBlock()
|
|
{
|
|
if (this.BlockData != null)
|
|
{
|
|
|
|
|
|
netDxf.DxfDocument dxf2 = netDxf.DxfDocument.Load(this.BlockData.FilePath);
|
|
double moveX = 0, moveY = 0;
|
|
|
|
string blockName = BlockData.BlockName.Split('_')[0];
|
|
Block block = dxf2.Blocks[blockName];
|
|
|
|
|
|
Block forBlock = BlockData.SourceBlock;
|
|
if (BlockData.IsDirection)
|
|
{
|
|
block = BlockData.ReverseBlock;
|
|
forBlock = BlockData.ReverseBlock;
|
|
}
|
|
|
|
|
|
//通过两个点计算块的偏移量
|
|
foreach (netDxf.Entities.EntityObject item in forBlock.Entities)
|
|
{
|
|
if (item.Type.ToString() == "Line")
|
|
{
|
|
Line line = item.DataContext as Line; //最终块图的某个点
|
|
|
|
|
|
netDxf.Entities.Line dataLine = item as netDxf.Entities.Line; //对应原始块的某个点
|
|
if (BlockData.IsDirection)
|
|
{
|
|
moveX = -line.X1 * ScaleRale - dataLine.StartPoint.X;
|
|
}
|
|
else
|
|
{
|
|
moveX = line.X1 * ScaleRale - dataLine.StartPoint.X;
|
|
}
|
|
|
|
moveY = line.Y1 * ScaleRale + dataLine.StartPoint.Y;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
netDxf.Entities.Insert insert = new netDxf.Entities.Insert(block, new netDxf.Vector3(moveX, -moveY, 0));
|
|
//netDxf.Entities.Insert insert2 = new netDxf.Entities.Insert(block, new netDxf.Vector3(0, 0, 0));
|
|
insert.Layer = new netDxf.Tables.Layer("block");
|
|
insert.Layer.Color.Index = 1;
|
|
dxf.AddEntity(insert);
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取块的所有坐标点
|
|
/// </summary>
|
|
/// <param name="block"></param>
|
|
/// <returns></returns>
|
|
public List<Point> GetBlockPoints(List<FrameworkElement> block,out Point minPont,out Point centerPoint)
|
|
{
|
|
List<Point> points = new List<Point>();
|
|
minPont = new Point() { X= double.MaxValue, Y=double.MinValue };
|
|
centerPoint = new Point() { X=double.MaxValue, Y= double.MaxValue };
|
|
|
|
foreach (var shapeitem in block)
|
|
{
|
|
BaseShap baseShap = GetBaseShap(shapeitem);
|
|
baseShap.m_ScaleRale = this.ScaleRale;
|
|
List<Point> ps = baseShap.GetPoints(); //获取图形外围框左下角坐标
|
|
|
|
foreach (var item in ps)
|
|
{
|
|
if (item.X < minPont.X)
|
|
{
|
|
minPont.X = item.X;
|
|
}
|
|
if (item.Y > minPont.Y)
|
|
{
|
|
minPont.Y = item.Y;
|
|
}
|
|
}
|
|
points.AddRange(ps);
|
|
}
|
|
|
|
|
|
return points;
|
|
}
|
|
|
|
|
|
}
|
|
}
|