// Mouse events private void canvas_MouseDown(object sender, MouseEventArgs e) { _mouse_startX = e.X; _mouse_startY = e.Y; if (e.Button == MouseButtons.Left) { if (_edit_mode == Edit_mode.ADD_GEOM) //Add new geometry of chosen type in selected point { HGeometry new_geometry = null; if (_addMode == GeometryType.CIRCLE) { new_geometry = new HCircle(); setEditiMode(Edit_mode.EDIT_GEOMETRY); } else if (_addMode == GeometryType.POINT) { new_geometry = new HPoint(); setEditiMode(Edit_mode.EDIT_GEOMETRY); } else if (_addMode == GeometryType.LINE) { new_geometry = new HLineString(); setEditiMode(Edit_mode.ADD_POINT); } else if (_addMode == GeometryType.POLYGON) { new_geometry = new HPolygon(); setEditiMode(Edit_mode.ADD_POINT); } var p = vp.unproject(new Point(e.X, e.Y)); new_geometry.addPoint(p); symbol.geometry.Add(new_geometry); updateExplorerList(); geom_explorer.SelectedIndex = symbol.geometry.Count() - 1; selectGeometry(symbol.geometry.Count() - 1); canvas.Refresh(); } else if (_edit_mode == Edit_mode.ADD_POINT) // Append point to selected geometry { symbol.geometry[selected_geometry_idx].points.Add(vp.unproject(new Point(e.X, e.Y))); canvas.Refresh(); } else { if (selected_geometry_idx != -1) { var s_geom = symbol.geometry[selected_geometry_idx]; for (int i = 0; i < s_geom.points.Count(); i++) { var p = vp.project(s_geom.points[i]); if (Math.Abs(p.X - e.X) < 6 && Math.Abs(p.Y - e.Y) < 6) { if (e.Button == MouseButtons.Left) { selected_point_idx = i; p = symbol.geometry[selected_geometry_idx].points[selected_point_idx]; point_x_val.Enabled = true; point_y_val.Enabled = true; point_x_val.Value = p.X; point_y_val.Value = p.Y; canvas.Refresh(); } } } } } } }
// Converts symbol instructions to geometry public static void parseGeometry(ref Symbol sym) { string[] commands = Regex.Replace(sym.instruction, @"\t|\n|\r", "").Split(';'); // sym.instruction.Replace(Environment.NewLine, String.Empty).Split(';'); Point curpos = new Point(); Point newpos = new Point(); Pen curpen = new Pen(Color.Transparent, 1); List <Point> polygonbuffer = new List <Point>(); bool polygonmode = false; int transparency = 0; string color_name = ""; int penWidth = 1; var line = new HLineString(); foreach (string command in commands) { //Console.WriteLine(command); if (command.Length >= 2) { string[] points; switch (command.Substring(0, 2)) //http://www.informatics-consulting.de/software/plot2emf/p2e_hpgl.htm and http://www.isoplotec.co.jp/HPGL/eHPGL.htm { case "SP": //Select pen color_name = getColorName(command.Substring(2), sym.colref); break; case "SW": //Select width - TODO: not part of the standard, is it really line width penWidth = Convert.ToInt32(command[command.Length - 1].ToString()); break; case "ST": transparency = Convert.ToInt32(command.Substring(2)); break; case "PU": //Move with pen up points = command.Substring(2).Split(','); for (int i = 0; i < points.Length / 2; i++) { curpos.X = Convert.ToInt32(command.Substring(2).Split(',')[2 * i]); curpos.Y = Convert.ToInt32(command.Substring(2).Split(',')[2 * i + 1]); if (polygonmode) { polygonbuffer.Add(curpos); } else { if (line.points.Count() != 0) { sym.geometry.Add(new HLineString(line)); line.points.Clear(); } line.points.Add(curpos); line.color = color_name; line.penWidth = penWidth; line.transparency = transparency; } } break; case "PD": //Move with pen down if (command.Length > 2) { points = command.Substring(2).Split(','); for (int i = 0; i < points.Length / 2; i++) { newpos.X = Convert.ToInt32(command.Substring(2).Split(',')[2 * i]); newpos.Y = Convert.ToInt32(command.Substring(2).Split(',')[2 * i + 1]); curpos = newpos; if (polygonmode) { polygonbuffer.Add(curpos); } else { line.points.Add(curpos); } } } else //Weird, but some symbols have just PD without coordinates so I assume it to be a point { HPoint dot = new HPoint(); dot.points.Add(curpos); dot.color = color_name; dot.transparency = transparency; dot.penWidth = penWidth; sym.geometry.Add(dot); line.points.Clear(); } break; case "CI": //CI r [, qd] Draw Circle int radius = Convert.ToInt32(command.Substring(2)); radius = (int)(radius); bool filled = false; if (polygonmode) { filled = true; } HCircle ci = new HCircle(); ci.points.Add(curpos); ci.color = color_name; ci.transparency = transparency; ci.penWidth = penWidth; ci.radius = radius; ci.filled = filled; sym.geometry.Add(ci); polygonbuffer.Clear(); line.points.Clear(); break; case "PM": //PM [n] Polygon mode (HPGL/2) if (command.Substring(2) == "0") { Console.WriteLine(command.Substring(2)); polygonmode = !polygonmode; if (polygonmode) { polygonbuffer.Add(curpos); if (line.points.Count() != 0) { line.points.Clear(); } } } else { polygonmode = false; } break; case "FP": //FP Filled Polygon (HPGL/2) if (polygonbuffer.Count > 0) { HPolygon poly = new HPolygon(); poly.points = new List <Point>(polygonbuffer); poly.color = color_name; poly.transparency = transparency; poly.penWidth = penWidth; sym.geometry.Add(poly); polygonbuffer.Clear(); } break; default: Console.WriteLine("HP-GL command {0} unknown.", command); break; } } } if (line.points.Count() != 0) { sym.geometry.Add(new HLineString(line)); line.points.Clear(); } }