面朝大海,春暖华开

focus on scientific computue, 3dgis, spatial database
专注于科学计算、GIS空间分析

 

VisualGraph文档

 

visualGraph文档

VisualGraph是一个基于.net GDI的矢量绘图引擎控件,使用visual studio 2005 c#2.0开发,并利用.net/com互操作,注册为activex控件,可以在winformasp.net中应用。它采用面向对象的设计和开发方法,具有良好的可维护性和可扩展性,适合于做嵌入式和移动的CadGis的应用。

VisualGraph的功能:

1 绘制基本图形:直线、多边形,椭圆

2 绘制文本。

3 图形组合:可以导出自定义图元(块),在以后的可以插入图元。

4 基于xml的持久化:图元(块)和工程都采用XML格式存储。

5 完全基于.net,移植性好。

VisualGraphasp.net的功能:

1 自动加载网站app_data/工程目录下的XML工程文档。

2 自动加载网站app_data/图元目录下的XML图元文档。

3 默认加载web.config中指定的工程。配置节如下:

 <appSettings>

    <addkey="DefaultGraph"value="vg1" />

 </appSettings>

 <connectionStrings/>

4 在url请求中,可以指定默认加载的图形工程,此参数覆盖web.config中的配置。url参数形式如下:

http://***.aspx? DefaultGraph=vg

VisualGraphasp.net中的应用方法:

visualGraph是用.net写的用户控件,并通过.net/com互操作,注册为activex控件。所以在asp.net中第一次应用时,需要在客户端端下载安装。同时需要客户端安装.net framework2.0

1 在服务器端中,读取“app_data/ 图元”目录下的XML图元文件名称和内容,并注册到页面html中。

获取图元文件名称:

    public string[] GetTuyuanFiles()

    {

        string serverPath = Server.MapPath(".");

        string tuyuanPath = serverPath + @""App_Data"图元";

        string[] files = Directory.GetFiles(tuyuanPath, "*.xml");

        return files;

}

注册图元文件名称:

    public void registerXmlTuyuanName(string[] files)

    {

        ClientScriptManager cs = Page.ClientScript;

        string tuyuanName = "tuyuanName";

        string[] fileName = new string[files.Length];

        for (int i = 0; i < files.Length; i++)

        {

            fileName[i] = Path.GetFileNameWithoutExtension(files[i]);

        }

        string arrayValue = string.Join(",", fileName);

        cs.RegisterHiddenField(tuyuanName, arrayValue);

    }

注册XML文件内容:

    public void registerXmlText(string[] files)

    {

        ClientScriptManager cs = Page.ClientScript;

        string[] fileText = new string[files.Length];

        for (int i = 0; i < files.Length; i++)

        {

            StreamReader sr = new StreamReader(files[i]);

            cs.RegisterHiddenField(Path.GetFileNameWithoutExtension(files[i]), sr.ReadToEnd());

            string.Format("'{0}'", sr.ReadToEnd());

            sr.Close();

            sr.Dispose();

        }

    }

2 在服务器端,读取“app_data/ 工程”目录下的XML工程文件名称和内容,并注册到页面html中。

获取工程文件名称:

    private string[] GetProjectFiles()

    {

        string serverPath = Server.MapPath(".");

        string projectPath = serverPath + @""App_Data"工程";

        string[] files = Directory.GetFiles(projectPath, "*.xml");

        return files;

}

注册工程文件名称:

    public void registerXmlProjectName(string[] files)

    {

        ClientScriptManager cs = Page.ClientScript;

        string projectName = "projectName";

        string[] fileName = new string[files.Length];

        for (int i = 0; i < files.Length; i++)

        {

            fileName[i] = Path.GetFileNameWithoutExtension(files[i]);

        }

        string arrayValue = string.Join(",", fileName);

        cs.RegisterHiddenField(projectName, arrayValue);

}

注册XML文件内容:

public void registerXmlText(string[] files)

    {

        ClientScriptManager cs = Page.ClientScript;

        string[] fileText = new string[files.Length];

        for (int i = 0; i < files.Length; i++)

        {

            StreamReader sr = new StreamReader(files[i]);

            cs.RegisterHiddenField(Path.GetFileNameWithoutExtension(files[i]), sr.ReadToEnd());

            string.Format("'{0}'", sr.ReadToEnd());

            sr.Close();

            sr.Dispose();

        }

    }

3 在服务器端,获取默认工程名称,并进行注册

首先检测url请求参数DefaultGraph,如果没有url参数,则检测web.config配置DefaultGraph

    public void getDefaultGraph()

    {

        string defaultGraph = Request["DefaultGraph"];

        if (string.IsNullOrEmpty(defaultGraph))

        {

            defaultGraph = ConfigurationManager.AppSettings["DefaultGraph"];

        }

        ClientScriptManager cs = Page.ClientScript;

        cs.RegisterHiddenField("DefaultGraph", defaultGraph);

}

4 在客户端,用js获取图元名称。

var tuyuanName = document.getElementById("tuyuanName").value;

5 在客户端,用js调用控件方法,把图元名称添加到自定义工具栏中

document.getElementById("_vg_").AddToolStripItem(tuyuanName);

6 在客户端,用js调用控件方法,把图元内容(xml文件的内容)保存到控件的图元字典中。

    var tuyuanNames = tuyuanName.split(",");

    for(var i =0;i<tuyuanNames.length;i++)

    {

        var theName = tuyuanNames[i];

        var theText = document.getElementById(theName).value;

        document.getElementById("_vg_").AddTuyuanText(theName,theText);

}

7在客户端,用js获取工程名称。

var projectName = document.getElementById("projectName").value;

8 在客户端,用js调用控件方法,把工程名称添加到工程下拉列表中

document.getElementById("_vg_").AddToolStripMenuItem(projectName);

9 在客户端,用js调用控件方法,把图元内容(xml文件的内容)保存到控件的工程字典中。

    var projectNames = projectName.split(",");

    for(var i =0;i<projectNames.length;i++)

    {

        var theName = projectNames[i];

        var theText = document.getElementById(theName).value;

        document.getElementById("_vg_").AddProjectText(theName,theText);

}

10 在客户端,获取默认工程名称,用js调用控件方法,打开默认工程

     var defaultGraph = document.getElementById("DefaultGraph").value;

     if(defaultGraph && defaultGraph!="")

     {

            document.getElementById("_vg_").OpenGraph(defaultGraph);

     }

在页面加载后的事件中,调用上面的js方法。

现在页面就会自动加载图元和工程文件,并打开默认的图形。

VisualGraph绘图方法

visualGraph中定义了图形文档(画布):

获取控件画布:

DrawDoc drawDoc = visualGraph1.Document;

画布的图形集合:

List<BaseDrawObject> DrawObjectList = visualGraph1.Document.DrawObjectList

在画布中,添加图形:

visualGraph1.Document.DrawObjectList.Add(BaseDrawObject item);

VG中定义的图形有:

DrawLine:线段

DrawPolyline:多线段

DrawEllipse:椭圆

DrawPolygon:多边形

DrawRectangle:矩形

DrawText:文字

DrawComponent:自定义图元(块)

图形模型图

ClassDiagram.png

常见操作代码

XML文件中打开工程:

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "(*.xml)|*.xml|(*.*)|*.*";

            if (ofd.ShowDialog() == DialogResult.OK)

            {

                string fn = ofd.FileName;

                FileStream fs = new FileStream(fn, FileMode.Open);

                XmlSerializer xs = new XmlSerializer(typeof(DrawDoc));

                visualGraph1.Document.DrawObjectList.Clear();

                visualGraph1.Document = (DrawDoc)xs.Deserialize(fs);

                fs.Close();

                fs.Dispose();

                visualGraph1.Invalidate();

            }

保存工程到xml文件

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "(*.xml)|*.xml|(*.*)|*.*";

            if (sfd.ShowDialog() == DialogResult.OK)

            {

                string fn = sfd.FileName;

                StreamWriter sw = new StreamWriter(fn);

                XmlSerializer xs = new XmlSerializer(typeof(DrawDoc));

                xs.Serialize(sw, visualGraph1.Document);

                sw.Close();

                sw.Dispose();

            }

新建工程:

            DrawDoc doc = new DrawDoc();

            visualGraph1.Document = doc;

            visualGraph1.Invalidate();

            visualGraph1.ResizeCanvase();

导出图形:

        private void tsbExport_Click(object sender, EventArgs e)

        {

            if (visualGraph1.Document.DrawObjectList.Count == 0)

            {

                MessageBox.Show("当前绘图工作区中没有图形");

                return;

            }

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "*.jpg|*.jpg";

            if (sfd.ShowDialog() == DialogResult.OK)

            {

                string filename = sfd.FileName;

                SaveControlImage(visualGraph1, filename);

            }

        }

        void SaveControlImage(Control c, string file)

        {

            int w = c.Size.Width;

            int h = c.Size.Height;

            using (Bitmap bmp = new Bitmap(w, h))

            {

                c.DrawToBitmap(bmp, new Rectangle(0, 0, w, h));

                bmp.Save(file, System.Drawing.Imaging.ImageFormat.Jpeg);

            }

        }

清空图形:

        private void tsbClear_Click(object sender, EventArgs e)

        {

            visualGraph1.Document.DrawObjectList.Clear();

            visualGraph1.Invalidate();

        }

删除选择的图形:

        private void tsbDelete_Click(object sender, EventArgs e)

        {

            visualGraph1.Document.Delete();

            visualGraph1.Invalidate();

        }

常用绘图工具代码集:

绘制线段工具

        private void tsbLine_Click(object sender, EventArgs e)

        {

            PolyObjectTool tool = (PolyObjectTool)BaseDrawTool.FindTool(DrawToolEnum.PolyObject);

            tool.PolyType = PolyType.PolyLine;

            BaseDrawTool.CurrentDrawTool = DrawToolEnum.PolyObject;

            toolStripButton_Click((sender as ToolStripItem).Owner, sender as ToolStripButton);

        }

绘制多边形工具

        private void tsbPolygon_Click(object sender, EventArgs e)

        {

            PolyObjectTool tool = (PolyObjectTool)BaseDrawTool.FindTool(DrawToolEnum.PolyObject);

            tool.PolyType = PolyType.Polygon;

            BaseDrawTool.CurrentDrawTool = DrawToolEnum.PolyObject;

            toolStripButton_Click((sender as ToolStripItem).Owner, sender as ToolStripButton);

        }

绘制椭圆工具

        private void tsbEllipse_Click(object sender, EventArgs e)

        {

            QuardShapeTool tool = (QuardShapeTool)BaseDrawTool.FindTool(DrawToolEnum.QuardShape);

            tool.QuardShapType = QuardShapeType.Ellipse;

            BaseDrawTool.CurrentDrawTool = DrawToolEnum.QuardShape;

            toolStripButton_Click((sender as ToolStripItem).Owner, sender as ToolStripButton);

        }

绘制矩形工具

        private void tsbRectangle_Click(object sender, EventArgs e)

        {

            QuardShapeTool tool = (QuardShapeTool)BaseDrawTool.FindTool(DrawToolEnum.QuardShape);

            tool.QuardShapType = QuardShapeType.Rectangle;

            BaseDrawTool.CurrentDrawTool = DrawToolEnum.QuardShape;

            toolStripButton_Click((sender as ToolStripItem).Owner, sender as ToolStripButton);

        }

绘制文本工具:

                string s = Interaction.InputBox("文本", "", null, -1, -1);

                if (string.IsNullOrEmpty(s))

                    return;

                DrawText dt = new DrawText();

                dt.Text = s;

                dt.StartPoint = e.Location;

                dt.Selectable = true;

                Size size = TextRenderer.MeasureText(dt.Text, this.Font);

                dt.Width = Convert.ToInt32(size.Width * 1.2);

                dt.Height = Convert.ToInt32(size.Height * 1.1);

                visualGraph1.Document.DrawObjectList.Add(dt);

                visualGraph1.Invalidate();

设置鼠标为选择工具

        private void tsbSelect_Click(object sender, EventArgs e)

        {

            SelectTool tool = (SelectTool)BaseDrawTool.FindTool(DrawToolEnum.Selection);

            BaseDrawTool.CurrentDrawTool = DrawToolEnum.Selection;

            toolStripButton_Click((sender as ToolStripItem).Owner, sender as ToolStripButton);

        }

posted on 2008-05-12 21:21  风过 无痕  阅读(3713)  评论(0编辑  收藏  举报

导航

向日葵支付宝收钱码