heyou
路人甲
路人甲
  • 注册日期2004-04-07
  • 发帖数59
  • QQ
  • 铜币324枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:2468回复:8

vc#+MO2.2中创建图层保存绘制点,线,面等代码问题求教!!!(附代码)

楼主#
更多 发布于:2004-08-22 17:52
<P>附代码如下:</P>
<P>using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;</P>
<P>namespace CreateLayer
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private AxMapObjects2.AxMap axMap1;
  private System.Windows.Forms.Button button1;</P>
<P>  string sName;
  MapObjects2.GeoDataset gds;
  MapObjects2.TableDesc Desc=new MapObjects2.TableDescClass();
  MapObjects2.DataConnection dc=new MapObjects2.DataConnectionClass();
  MapObjects2.MapLayer Lyr=new MapObjects2.MapLayerClass();
  MapObjects2.Symbol moSymbol=new MapObjects2.SymbolClass();
  MapObjects2.Point[]  Pt=new MapObjects2.Point[31];</P>

<P>  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;</P>
<P>  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();</P>
<P>   //
   // TOD 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }</P>
<P>  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }</P>
<P>  #region Windows Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.axMap1 = new AxMapObjects2.AxMap();
   this.button1 = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.axMap1)).BeginInit();
   this.SuspendLayout();
   //
   // axMap1
   //
   this.axMap1.Location = new System.Drawing.Point(16, 8);
   this.axMap1.Name = "axMap1";
   this.axMap1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap1.OcxState")));
   this.axMap1.Size = new System.Drawing.Size(448, 312);
   this.axMap1.TabIndex = 0;
   this.axMap1.MouseDownEvent += new AxMapObjects2._DMapEvents_MouseDownEventHandler(this.axMap1_MouseDownEvent);
   this.axMap1.AfterTrackingLayerDraw += new AxMapObjects2._DMapEvents_AfterTrackingLayerDrawEventHandler(this.axMap1_AfterTrackingLayerDraw);
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(200, 328);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(96, 24);
   this.button1.TabIndex = 1;
   this.button1.Text = "新建图层";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(520, 366);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.button1,
                    this.axMap1});
   this.Name = "Form1";
   this.Text = "Form1";
   ((System.ComponentModel.ISupportInitialize)(this.axMap1)).EndInit();
   this.ResumeLayout(false);</P>
<P>  }
  #endregion</P>
<P>  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }</P>
<P>  int k=1;
  private void button1_Click(object sender, System.EventArgs e)
  {
   dc.Database <a href='mailt=@"c:\' target="_blank" >=@"c:\</A>";
   if(!dc.Connect ())
   {
    MessageBox.Show ("connect failer!");
    return;
   }
   sName="crtLayer"+k+".shp";
   k++;
  
   //设置字段名;
   Desc.FieldCount =1;
   Desc.set_FieldName(0,"Name");
   Desc.set_FieldType(0,MapObjects2.FieldTypeConstants.moString);
   Desc.set_FieldLength(0,16);
  
   gds=dc.AddGeoDataset (sName,MapObjects2.ShapeTypeConstants .moShapeTypePoint ,Desc,null,null);
   if(gds==null)
   {
    MessageBox.Show ("gds geodataset is null");
    return;
   }
   Lyr.GeoDataset =gds;
  
   if(Lyr.Records .Updatable )
   {
    MessageBox.Show ("layer is updatable!");
    
    for(int i=1;i<p_count;i++)
    {
     try
     {
      Lyr.Records.AddNew();
      Lyr.Records.Fields.Item("Shape").Value = Pt;
      Lyr.Records.Fields.Item("Name").Value = "Name " + i;
      Lyr.Records.Update();
      
     }
     catch(Exception ee)
     {
      MessageBox.Show (ee.Message .ToString ()+ @"\n "+ee.Source .ToString ()+ @"\n "+ee.TargetSite .ToString ());
     }
    }</P>
<P>    Lyr.Records .StopEditing ();
   }
   axMap1.Layers .Add (Lyr);
   axMap1.Refresh ();
   axMap1.CtlRefresh ();</P>
<P>  }</P>
<P>  int p_count;
  private void axMap1_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e)
  {
   MapObjects2.Point pt;
   pt=axMap1.ToMapPoint (e.x,e.y);
   Pt[p_count]=pt;
   p_count=p_count+1;</P>
<P>   axMap1.CtlRefresh ();
   axMap1.TrackingLayer .Refresh (true,axMap1.Extent );  
  
  
  }</P>
<P>  private void axMap1_AfterTrackingLayerDraw(object sender, AxMapObjects2._DMapEvents_AfterTrackingLayerDrawEvent e)
  {
   MapObjects2.Symbol sym=new MapObjects2 .SymbolClass ();
   sym.SymbolType =MapObjects2.SymbolTypeConstants .moPointSymbol ;
   sym.Color =(uint)MapObjects2.ColorConstants .moRed ;
   sym.Size =5;
   for(int i=0;i<31;i++)
   {
    if(Pt!=null)
     axMap1.DrawShape (Pt,sym);
   }
  }
 }
}
</P>
<P>在代码</P>
<P>Lyr.Records.AddNew();
      Lyr.Records.Fields.Item("Shape").Value = Pt;
      Lyr.Records.Fields.Item("Name").Value = "Name " + i;
      Lyr.Records.Update();
。。。。。。。。。</P>
<P>附近出现问题:</P>
<P>(1)There are no more records in the RecordSet.</P>
<P>(2)AddNew failer!</P>

<P>请高手帮忙,看看问题在那里!在线等待中》》》》》》》》》》》》》》》</P>
喜欢0 评分0
ZHYP
路人甲
路人甲
  • 注册日期2003-12-11
  • 发帖数27
  • QQ
  • 铜币205枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2004-09-29 10:23
<P>定义:recs As MapObjects2.Recordset</P><P>然后
Set recs = Lyr.Records</P><P>后面的操作都通过recs来完成,比如recs.AddNew</P>
举报 回复(0) 喜欢(0)     评分
wjckaxi
路人甲
路人甲
  • 注册日期2004-04-13
  • 发帖数144
  • QQ
  • 铜币149枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2004-09-21 15:01
<P>有没有vb的例子啊,我想要一份看看。c#还是有点不懂哦</P>
菜虫 欢迎光临Blog http://blog.sina.com.cn/wjckaxi
举报 回复(0) 喜欢(0)     评分
heyou
路人甲
路人甲
  • 注册日期2004-04-07
  • 发帖数59
  • QQ
  • 铜币324枚
  • 威望0点
  • 贡献值0点
  • 银元0个
3楼#
发布于:2004-08-30 10:21
<P>不知道啊!是不是mo与c#的兼容性有关?主要是解决问题啊!</P><P>不然不能进行编辑,有何用啊!</P>
举报 回复(0) 喜欢(0)     评分
berush
路人甲
路人甲
  • 注册日期2003-12-01
  • 发帖数158
  • QQ
  • 铜币622枚
  • 威望0点
  • 贡献值0点
  • 银元0个
4楼#
发布于:2004-08-26 19:37
<P>addnew()进行添加时,出现编辑失败!</P><P>这个问题我现在也碰到了,非常奇怪~~在已经存在的系统可以添加线,但是另外的一个系统却不能。非常郁闷,这是不是mo与c#的兼容性有关?????</P>
举报 回复(0) 喜欢(0)     评分
heyou
路人甲
路人甲
  • 注册日期2004-04-07
  • 发帖数59
  • QQ
  • 铜币324枚
  • 威望0点
  • 贡献值0点
  • 银元0个
5楼#
发布于:2004-08-26 10:06
<P>legend控件的图形没有搞定 ?应如何?有思路马!</P><P>还有 ,addnew()进行添加时,出现编辑失败!(Edit fail!) 图层的Updatable为false!这是为什么啊!</P>
举报 回复(0) 喜欢(0)     评分
berush
路人甲
路人甲
  • 注册日期2003-12-01
  • 发帖数158
  • QQ
  • 铜币622枚
  • 威望0点
  • 贡献值0点
  • 银元0个
6楼#
发布于:2004-08-24 16:02
<P>你可以先试试添加单个点,看能不能成功,还有也不要自己建立shape层,用已经存在的shape文件,然后看能不能添加成功~~</P><P>我是在已经存在的shape层上添加线的,代码就是在mousedown事件中</P><P>使用axMap1.trackline产生新的线,然后再通过recordset.addnew()进行添加,没什么问题。</P><P><b>还有你的legend控件能不能用,如果你有自己做的legend控件能不能发给我一份,谢谢了</b></P>
举报 回复(0) 喜欢(0)     评分
heyou
路人甲
路人甲
  • 注册日期2004-04-07
  • 发帖数59
  • QQ
  • 铜币324枚
  • 威望0点
  • 贡献值0点
  • 银元0个
7楼#
发布于:2004-08-24 11:54
<P><B><FONT color=#000066>berush兄:</FONT></B></P>
<P><FONT color=#000066><B>上面我的程序那里有问题!可否解解!!!</B></FONT></P>
<P><FONT color=#000066><B>能否把</B></FONT><FONT color=#000000>添加线的程序的程序给我一份!谢谢!把代码贴在上面可否???</FONT></P>
[此贴子已经被作者于2004-8-24 12:00:11编辑过]
举报 回复(0) 喜欢(0)     评分
berush
路人甲
路人甲
  • 注册日期2003-12-01
  • 发帖数158
  • QQ
  • 铜币622枚
  • 威望0点
  • 贡献值0点
  • 银元0个
8楼#
发布于:2004-08-23 09:53
<P>用addnew方法添加数据应该是没问题的~我有做过添加线的程序~~</P><P>数据编辑出现edit failer的会在recordset.edit(),recordset.delete()等情况下出现吧.</P>
举报 回复(0) 喜欢(0)     评分
游客

返回顶部