C#联合Visionpro编程学习记录(将指定颜色的十字线图形添加到CogRecordDisplay上)


1
/// <summary> 2 /// 将指定颜色的十字线图形添加到CogRecordDisplay上 3 /// </summary> 4 /// <param name="icogimage"></param> 5 /// <returns></returns> 6 public static string AddCrossCurveRecord2CogRecordDisplay(ICogImage Image, /*ref*/ CogRecordDisplay cogrecorddisplay, Cognex.VisionPro.CogColorConstants CrosshairColor) 7 { 8 string strmsg = ""; 9 CogCreateLineTool[] cogCreateTool = new CogCreateLineTool[2] {new CogCreateLineTool(),new CogCreateLineTool() }; 10 CogGraphicCollection cgc = new CogGraphicCollection(); 11 ICogImage icogimage = Image; 12 ClearCogRecordDisplay(cogrecorddisplay); 13 try 14 { 15 //cogrecorddisplay.Image = icogimage; 16 if (cogrecorddisplay.Image==null) 17 { 18 if (icogimage!=null) 19 { 20 cogrecorddisplay.Image = icogimage; 21 } 22 else 23 { 24 cogrecorddisplay.Image = new CogImage8Grey(icogimage.Width, icogimage.Height); 25 } 26 } 27 if (cogrecorddisplay.Image != null) 28 { 29 for (int i = 0; i < 2; i++) 30 { 31 cogCreateTool[i].InputImage = cogrecorddisplay.Image; 32 cogCreateTool[i].OutputColor = CrosshairColor; 33 cogCreateTool[i].Line.X = cogrecorddisplay.Image.Width / 2; 34 cogCreateTool[i].Line.Y = cogrecorddisplay.Image.Height / 2; 35 36 switch (i) 37 { 38 case 0: 39 cogCreateTool[i].Line.Rotation = 0; 40 break; 41 case 1: 42 cogCreateTool[i].Line.Rotation = 90 / (180 / Math.PI); 43 break; 44 default: 45 break; 46 } 47 cogCreateTool[i].Run(); 48 GraphicsAddIn(Get_ToolResultGC(cogCreateTool[i]), ref cgc); 49 } 50 ShowImageAndGc2CogRecordDisplay(ref cogrecorddisplay, cgc); 51 } 52 else 53 { 54 strmsg += "cogrecorddisplay中没有图像!"; 55 } 56 } 57 catch (Exception ex) 58 { 59 strmsg += ex.Message; 60 } 61 return strmsg; 62 }

 

  1  /// <summary>
  2         /// 清空CogRecordDisplay中的图形和图像
  3         /// </summary>
  4         /// <param name="crd"></param>
  5         /// <returns></returns>
  6         public static string ClearCogRecordDisplay( CogRecordDisplay crd)
  7         {
  8             string strmsg = "";
  9             try
 10             {
 11                 ClearCogRecordDisplayWithoutImage(crd);
 12                 crd.Image = null;
 13             }
 14             catch (Exception ex)
 15             {
 16                 strmsg = ex.Message;
 17             }
 18             return strmsg;
 19         } 
 20         /// <summary>
 21         /// 清空CogRecordDisplay中的图形但不清空图像
 22         /// </summary>
 23         /// <param name="crd"></param>
 24         /// <returns></returns>
 25         public static string ClearCogRecordDisplayWithoutImage(CogRecordDisplay crd)
 26         {
 27             string strmsg = "";
 28             try
 29             {
 30                 crd.StaticGraphics.Clear();
 31                 crd.InteractiveGraphics.Clear();
 32                 //crd.Image = null;
 33             }
 34             catch (Exception ex)
 35             {
 36                 strmsg = ex.Message;
 37             }
 38             return strmsg;
 39         }
 40 
 41 
 42         /// <summary>
 43         /// 获取工具的CogGraphicCollection(几何图形图集)
 44         /// </summary>
 45         /// <param name="Tool"></param>
 46         /// <returns></returns>
 47         public static CogGraphicCollection Get_ToolResultGC(ICogTool Tool)
 48         {
 49             CogGraphicCollection graphics = new CogGraphicCollection();
 50             if (Tool == null)
 51             {
 52                 return graphics;
 53             }
 54             return Get_ToolResultGC_Ext(Tool);
 55         }
 56 
 57  /// <summary>
 58         /// 获取工具的CogGraphicCollection(几何图形图集)
 59         /// </summary>
 60         /// <param name="Tool"></param>
 61         /// <returns></returns>
 62         public static CogGraphicCollection Get_ToolResultGC_Ext(ICogTool Tool)
 63         {
 64             CogGraphicCollection graphics = new CogGraphicCollection();
 65             if (Tool != null)
 66             {
 67                 Tool.GetType();
 68                 ICogRecord record = Tool.CreateLastRunRecord();
 69                 ICogRecords records = record.SubRecords;
 70                 try
 71                 {
 72 
 73                     if (records == null)
 74                     {
 75                         return graphics;
 76                     }
 77                     int count = record.SubRecords.Count;
 78                     List<string> list = new List<string>();
 79                     for (int i = 0; i < record.SubRecords.Count; i++)
 80                     {
 81                         foreach (object obj2 in record.SubRecords[i].SubRecords)
 82                         {
 83                             ICogRecord record3 = (ICogRecord)obj2;
 84 
 85                             Type type = ((ICogRecord)obj2).ContentType;
 86                             list.Add(type.Name);
 87                             if (type.Name == "CogGraphicCollection")
 88                             {
 89                                 CogGraphicCollection graphics2 = (CogGraphicCollection)record3.Content;
 90                                 for (int j = 0; j < graphics2.Count; j++)
 91                                 {
 92                                     graphics.Add(graphics2[j]);
 93                                 }
 94                             }
 95                             else if (type.Name == "ICogGraphicInteractive")
 96                             {
 97                                 ICogGraphicInteractive interactive = (ICogGraphicInteractive)record3.Content;
 98                                 graphics.Add(interactive);
 99                             }
100                         }
101                     }
102                     int num3 = records.Count;
103                 }
104                 catch (Exception ex)
105                 {
106                 }
107             }
108             return graphics;
109         }
110 
111 /// <summary>
112         /// 将一个CogGraphicCollection(几何图形集合)添加到另一个CogGraphicCollection(几何图形集合)
113         /// </summary>
114         /// <param name="GC_Source"></param>
115         /// <param name="GC_Target"></param>
116         public static void GraphicsAddIn(CogGraphicCollection GC_Source, ref CogGraphicCollection GC_Target)
117         {
118             if ((GC_Source != null) && (GC_Target != null))
119             {
120                 for (int i = 0; i < GC_Source.Count; i++)
121                 {
122                     GC_Target.Add(GC_Source[i]);
123                 }
124             }
125         }
126 
127  /// <summary>
128         /// 将几何图形添加到指定CogRecordDisplay上
129         /// </summary>
130         /// <param name="cogRD"></param>
131         /// <param name="gc"></param>
132         public static void ShowImageAndGc2CogRecordDisplay(ref CogRecordDisplay cogRD, CogGraphicCollection gc)
133         {
134             cogRD.Fit(false);
135             if ((gc != null) && (gc.Count > 0))
136             {
137                 try
138                 {
139                     for (int i = 0; i < gc.Count; i++)
140                     {
141                         cogRD.StaticGraphics.Add(gc[i], "");
142                     }
143                 }
144                 catch (Exception ex)
145                 {
146                 }
147             }
148         }

 

posted @ 2024-10-09 19:12  稳稳地稳稳地  阅读(352)  评论(0)    收藏  举报