|
|
|
|
using HalconDotNet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
|
|
|
|
|
|
namespace Rs.MotionPlat
|
|
|
|
|
{
|
|
|
|
|
public partial class Form2 : Form
|
|
|
|
|
{
|
|
|
|
|
private List<HDrawingObject> list = new List<HDrawingObject>();
|
|
|
|
|
private Stack<HObject> graphic_stack = new Stack<HObject>();
|
|
|
|
|
private HDrawingObject selected_drawing_object = new HDrawingObject(250, 250, 100);
|
|
|
|
|
private HImage image = null;
|
|
|
|
|
private object stack_lock = new object();
|
|
|
|
|
HWindow window = null;
|
|
|
|
|
public Form2()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
window = hWindow_Final1.hWindowControl.HalconWindow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ROI内的区域加入Stack中
|
|
|
|
|
public void AddToStack(HObject obj)
|
|
|
|
|
{
|
|
|
|
|
lock (stack_lock)
|
|
|
|
|
{
|
|
|
|
|
graphic_stack.Push(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
public void DisplayResults()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
hWindow_Final1.hWindowControl.BeginInvoke((MethodInvoker)(() =>
|
|
|
|
|
{
|
|
|
|
|
DisplayGraphicStack();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
private void DisplayGraphicStack()
|
|
|
|
|
{
|
|
|
|
|
lock (stack_lock)
|
|
|
|
|
{
|
|
|
|
|
HSystem.SetSystem("flush_graphic", "false");
|
|
|
|
|
window.ClearWindow();
|
|
|
|
|
while (graphic_stack.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
//显示ROI内效果,并移除
|
|
|
|
|
window.DispObj(graphic_stack.Pop());
|
|
|
|
|
}
|
|
|
|
|
HSystem.SetSystem("flush_graphic", "true");
|
|
|
|
|
}
|
|
|
|
|
window.DispCross(-10.0, -10.0, 3.0, 0.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//HDrawingObject rec1 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1,
|
|
|
|
|
// 100, 100, 210, 210);
|
|
|
|
|
//this.AttachDrawingObj(rec1);
|
|
|
|
|
|
|
|
|
|
HDrawingObject rec1 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, 250, 250, 50, 0, 1);
|
|
|
|
|
this.AttachDrawingObj(rec1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//与委托参数匹配
|
|
|
|
|
private void OnSelectDrawingObject(HDrawingObject drawid, HWindow window, string type)
|
|
|
|
|
{
|
|
|
|
|
selected_drawing_object = drawid;
|
|
|
|
|
|
|
|
|
|
ProcessMethod(drawid, window, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessMethod(HDrawingObject drawid, HWindow window, string type)
|
|
|
|
|
{
|
|
|
|
|
foreach (HDrawingObject item in list)
|
|
|
|
|
{
|
|
|
|
|
//注意事项:改变ROI颜色的时候,一定要通过HDrawingObject对象的ID属性和list中的成员ID进行匹配,
|
|
|
|
|
//不能直接通过对象==对象的形式进行判断
|
|
|
|
|
if (item.ID == drawid.ID)
|
|
|
|
|
{
|
|
|
|
|
item.SetDrawingObjectParams("color", "blue");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item.SetDrawingObjectParams("color", "green");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HRegion region = new HRegion(drawid.GetDrawingObjectIconic());
|
|
|
|
|
window.SetColor("red");
|
|
|
|
|
window.DispObj(region);
|
|
|
|
|
AddToStack(region);
|
|
|
|
|
|
|
|
|
|
//AddToStack(region);
|
|
|
|
|
//加入stack
|
|
|
|
|
window.SetColor("red");
|
|
|
|
|
//this.AddToStack(image.ReduceDomain(region).Threshold(new HTuple(128), new HTuple(255)));
|
|
|
|
|
//展示并清空stack
|
|
|
|
|
this.DisplayResults();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Form2_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//图片
|
|
|
|
|
image = new HImage("fabrik");
|
|
|
|
|
image.GetImageSize(out HTuple width, out HTuple height);
|
|
|
|
|
window.SetPart(0, 0, height.I - 1, width.I - 1);
|
|
|
|
|
window.AttachBackgroundToWindow(image);
|
|
|
|
|
//
|
|
|
|
|
//this.AttachDrawingObj(selected_drawing_object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AttachDrawingObj(HDrawingObject obj)
|
|
|
|
|
{
|
|
|
|
|
//list添加
|
|
|
|
|
list.Add(obj);
|
|
|
|
|
//
|
|
|
|
|
obj.OnAttach(ProcessMethod);
|
|
|
|
|
obj.OnSelect(OnSelectDrawingObject);
|
|
|
|
|
obj.OnDrag(ProcessMethod);
|
|
|
|
|
obj.OnResize(ProcessMethod);
|
|
|
|
|
//
|
|
|
|
|
window.AttachDrawingObjectToWindow(obj);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button2_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
HDrawingObject rec2 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2,
|
|
|
|
|
100, 100, 0, 100, 50);
|
|
|
|
|
this.AttachDrawingObj(rec2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button3_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
HDrawingObject circle = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE,
|
|
|
|
|
200, 200, 70);
|
|
|
|
|
this.AttachDrawingObj(circle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button4_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lock (stack_lock)
|
|
|
|
|
{
|
|
|
|
|
foreach (HDrawingObject dobj in list)
|
|
|
|
|
{
|
|
|
|
|
dobj.ClearDrawingObject();
|
|
|
|
|
dobj.Dispose();
|
|
|
|
|
}
|
|
|
|
|
list.Clear();
|
|
|
|
|
//graphic_stack.Clear();
|
|
|
|
|
}
|
|
|
|
|
DisplayGraphicStack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button5_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
HObject region = selected_drawing_object.GetDrawingObjectIconic();
|
|
|
|
|
AddToStack(region);
|
|
|
|
|
|
|
|
|
|
lock (stack_lock)
|
|
|
|
|
{
|
|
|
|
|
foreach (HDrawingObject dobj in list)
|
|
|
|
|
{
|
|
|
|
|
dobj.ClearDrawingObject();
|
|
|
|
|
dobj.Dispose();
|
|
|
|
|
}
|
|
|
|
|
list.Clear();
|
|
|
|
|
//graphic_stack.Clear();
|
|
|
|
|
}
|
|
|
|
|
DisplayGraphicStack();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|