⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mappershape.cpp

📁 vc 和mapobjects地理信息系统组件开发 很有启发意义和参考价值
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	CMoDataConnection conn;
	if (!conn.CreateDispatch(TEXT("MapObjects2.DataConnection")))
		return FALSE;
	
	conn.SetDatabase(GetFileDirectory(sPathFile));
	if (!conn.Connect())
		return FALSE;
	
	CMoTableDesc tableDesc;
	if (!tableDesc.CreateDispatch(TEXT("MapObjects2.TableDesc")))
		return FALSE;
	
	tableDesc.SetFieldCount(6);
	tableDesc.SetFieldName(0, TEXT("Name"));
	tableDesc.SetFieldType(0, moString);
	tableDesc.SetFieldLength(0, 16);
	
	tableDesc.SetFieldName(1, TEXT("Area"));
	tableDesc.SetFieldType(1, moDouble);
	tableDesc.SetFieldPrecision(1, 15);
	tableDesc.SetFieldScale(1, 3);	// decimal places
	
	tableDesc.SetFieldName(2, TEXT("Perimeter"));
	tableDesc.SetFieldType(2, moDouble);
	tableDesc.SetFieldPrecision(2, 15);
	tableDesc.SetFieldScale(2, 3);	// decimal places

	tableDesc.SetFieldName(3, TEXT("text"));
	tableDesc.SetFieldType(3, moString);
	tableDesc.SetFieldLength(3, 32);

	tableDesc.SetFieldName(4, TEXT("height"));
	tableDesc.SetFieldType(4, moDouble);
	tableDesc.SetFieldPrecision(4, 15);
	tableDesc.SetFieldScale(4, 3);	// decimal places

	//引起问题:也需要设置精度
	tableDesc.SetFieldName(5, TEXT("color"));
	tableDesc.SetFieldType(5, moLong);
	tableDesc.SetFieldLength(5, 8);
	tableDesc.SetFieldPrecision(5, 8);
	tableDesc.SetFieldScale(5, 0);	// decimal places

	VARIANT va;
	VariantInit(&va);
	va.vt = VT_BOOL;
	va.boolVal = false;

	CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(sPathFile), moPolygon, tableDesc, va ,va));
	ASSERT(LPDISPATCH(geoDataset));
	CMoMapLayer layer;
	if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
		return FALSE;
	layer.SetGeoDataset(geoDataset);
	
	CMoRecordset recs(layer.GetRecords());
	CMoFields fields(recs.GetFields());
	CString featureName;

	for (int i = 0; i < m_oaTexts.GetSize(); i++)
	{
		recs.AddNew();
		SetValue(fields, TEXT("Shape"), LPDISPATCH(*m_oaTexts[i]->line));
		featureName.Format("%s%d", "line", i);
		SetValue(fields, TEXT("Name"), featureName);
		SetValue(fields, TEXT("Area"), 0.0);
		SetValue(fields, TEXT("Perimeter"), m_oaTexts[i]->line->GetLength());

		SetValue(fields, TEXT("text"),m_oaTexts[i]->text);
		SetValue(fields, TEXT("height"),m_oaTexts[i]->height);
		SetValue(fields, TEXT("color"),(long)m_oaTexts[i]->color);

		recs.Update();
	}

	mo_ReleaseTexts();	

	return TRUE;
}

void CMapperShape::Draw(CMap1 &map)
{
	//选中对象使用蓝色显示,问题:如果本身是蓝色则不容易判别是否选中
	//备选方案:使用外部的rect,虚线,黑色

	int i;
	CMoSymbol sym;
	sym.CreateDispatch(TEXT("MapObjects2.Symbol"));

	// Draw points
	sym.SetSymbolType(moPointSymbol);
	sym.SetOutline(m_tPointInfo.bOutline);
	sym.SetOutlineColor(m_tPointInfo.outlineColor);
	sym.SetSize(m_tPointInfo.size);
	sym.SetStyle(m_tPointInfo.style);
	for (i = 0; i < m_oaPoints.GetSize(); i++)
	{
		sym.SetColor(m_tPointInfo.color);
		if((m_tSel.type==1)&&(m_tSel.index==i))
			sym.SetColor(RGB(0,0,255));
		map.DrawShape(*m_oaPoints[i], sym);
	}

	// Draw lines
	sym.SetSymbolType(moLineSymbol);
	sym.SetSize(m_tLineInfo.size);
	sym.SetStyle(m_tLineInfo.style);
	for (i = 0; i < m_oaLines.GetSize(); i++)
	{
		sym.SetColor(m_tLineInfo.color);
		if((m_tSel.type==2)&&(m_tSel.index==i))
			sym.SetColor(RGB(0,0,255));
		map.DrawShape(*m_oaLines[i], sym);
	}

	//Draw rectangle
	sym.SetSymbolType(moFillSymbol);
	sym.SetStyle(m_tRectInfo.style);
	sym.SetOutline(m_tRectInfo.bOutline);
	sym.SetOutlineColor(m_tRectInfo.outlineColor);
	sym.SetSize(m_tRectInfo.size);
	for (i = 0; i < m_oaRects.GetSize(); i++)
	{
		sym.SetColor(m_tRectInfo.color);
		if((m_tSel.type==3)&&(m_tSel.index==i))
			sym.SetColor(RGB(0,0,255));
		map.DrawShape(*m_oaRects[i], sym);
	}

	//Draw Circle
	sym.SetSymbolType(moFillSymbol);
	sym.SetStyle(m_tCircleInfo.style);
	sym.SetOutline(m_tCircleInfo.bOutline);
	sym.SetOutlineColor(m_tCircleInfo.outlineColor);
	sym.SetSize(m_tCircleInfo.size);
	for (i = 0; i < m_oaCircles.GetSize(); i++)
	{
		sym.SetColor(m_tCircleInfo.color);
		if((m_tSel.type==4)&&(m_tSel.index==i))
			sym.SetColor(RGB(0,0,255));
		map.DrawShape(*m_oaCircles[i], sym);
	}

	//Draw Ellipse
	sym.SetSymbolType(moFillSymbol);
	sym.SetStyle(m_tEllipseInfo.style);
	sym.SetOutline(m_tEllipseInfo.bOutline);
	sym.SetOutlineColor(m_tEllipseInfo.outlineColor);
	sym.SetSize(m_tEllipseInfo.size);
	for (i = 0; i < m_oaEllipses.GetSize(); i++)
	{
		sym.SetColor(m_tEllipseInfo.color);
		if((m_tSel.type==5)&&(m_tSel.index==i))
			sym.SetColor(RGB(0,0,255));
		map.DrawShape(*m_oaEllipses[i], sym);
	}

	// Draw polygons
	sym.SetSymbolType(moFillSymbol);
	sym.SetStyle(m_tPolyInfo.style);
	sym.SetOutline(m_tPolyInfo.bOutline);
	sym.SetOutlineColor(m_tPolyInfo.outlineColor);
	sym.SetSize(m_tPolyInfo.size);
	for (i = 0; i < m_oaPolys.GetSize(); i++)
	{
		sym.SetColor(m_tPolyInfo.color);
		if((m_tSel.type==6)&&(m_tSel.index==i))
			sym.SetColor(RGB(0,0,255));
		map.DrawShape(*m_oaPolys[i], sym);
	}

	//Draw Text
	CMoTextSymbol tSym;
	tSym.CreateDispatch(TEXT("MapObjects2.TextSymbol"));
	for(i=0;i<m_oaTexts.GetSize();i++)
	{
		tSym.SetHeight(m_oaTexts[i]->height);
		tSym.SetColor(m_oaTexts[i]->color);
		if((m_tSel.type==7)&&(m_tSel.index==i))
			tSym.SetColor(RGB(0,0,255));
		map.DrawText(m_oaTexts[i]->text,*m_oaTexts[i]->line,tSym);
	}

}

void CMapperShape::mo_AddPoint(CMap1 &map,long X,long Y)
{
	// Capture a new point
	CMoPoint* point = new CMoPoint(map.ToMapPoint((float)X, (float)Y));
	if (LPDISPATCH(point))
		m_oaPoints.Add(point);
	CMoRectangle r = CMoRectangle();
	r.CreateDispatch(TEXT("MapObjects2.Rectangle"));
	r.SetBottom(point->GetY());
	r.SetLeft(point->GetX());
	r.SetTop(point->GetY()+1.0);
	r.SetRight(point->GetX()+1.0);
	r.ScaleRectangle(50.0);
	
	map.RefreshRect(r);
	r.ReleaseDispatch();
}

void CMapperShape::mo_AddLine(CMap1 &map)
{
	// Track a new line
	CMoLine* line = new CMoLine(map.TrackLine());
	if (LPDISPATCH(line))
		m_oaLines.Add(line);

	CMoRectangle r = CMoRectangle(line->GetExtent());
	map.RefreshRect(r.m_lpDispatch);
}

void CMapperShape::mo_AddRect(CMap1 &map)
{
	CMoRectangle* rect = new CMoRectangle(map.TrackRectangle());
	if (LPDISPATCH(rect))
		m_oaRects.Add(rect);
	map.RefreshRect(rect->m_lpDispatch);
}

void CMapperShape::mo_AddCircle(CMap1 &map)
{
	CMoEllipse* ellipse = new CMoEllipse(map.TrackCircle());
	if (LPDISPATCH(ellipse))
		m_oaCircles.Add(ellipse);

	CMoRectangle r = CMoRectangle();
	r.CreateDispatch(TEXT("MapObjects2.Rectangle"));
	r.SetBottom(ellipse->GetBottom());
	r.SetLeft(ellipse->GetLeft());
	r.SetTop(ellipse->GetTop());
	r.SetRight(ellipse->GetRight());
	map.RefreshRect(r.m_lpDispatch);
}

void CMapperShape::mo_AddEllipse(CMap1 &map)
{
	CMoEllipse* ellipse = new CMoEllipse();
	ellipse->CreateDispatch(TEXT("MapObjects2.Ellipse"));

	CMoRectangle rect(map.TrackRectangle());
	ellipse->SetBottom(rect.GetBottom());
	ellipse->SetLeft(rect.GetLeft());
	ellipse->SetRight(rect.GetRight());
	ellipse->SetTop(rect.GetTop());

	if (LPDISPATCH(ellipse))
		m_oaEllipses.Add(ellipse);

	map.RefreshRect(rect.m_lpDispatch);
}

void CMapperShape::mo_AddPoly(CMap1 &map)
{
	CMoPolygon* poly = new CMoPolygon(map.TrackPolygon());
	if (LPDISPATCH(poly))
		m_oaPolys.Add(poly);

	CMoRectangle rect = CMoRectangle(poly->GetExtent());
	map.RefreshRect(rect.m_lpDispatch);
}

void CMapperShape::mo_AddText(CMap1 &map)
{
	//只使用前2个点
	CMoLine line;
	line=map.TrackLine();

	CMoPoints pts,pts1;
	pts.CreateDispatch(TEXT("MapObjects2.Points"));
	pts1.CreateDispatch(TEXT("MapObjects2.Points"));
	pts=line.GetParts().Item(COleVariant((short)0));
	pts1.Add(pts.Item(COleVariant((short)0)));
	pts1.Add(pts.Item(COleVariant((short)1)));

	SMapperTextInfo *text;
	text=new SMapperTextInfo;
	text->line=new CMoLine();
	text->line->CreateDispatch(TEXT("MapObjects2.Line"));
	text->line->GetParts().Add(pts1);
	if (LPDISPATCH(text->line))
	{
		CShapeTextDlg std;
		CMoRectangle r=map.GetExtent();
		std.m_dHeight=r.GetHeight()/50.0;
		std.m_oColor.SetColor(RGB(255,0,0));
		if(std.DoModal()!=IDOK)
			return;
		text->color=std.m_oColor.GetColor();
		text->height=std.m_dHeight;
		strcpy(text->text,std.m_sText);
		m_oaTexts.Add(text);
	}
	map.Refresh();
}

BOOL CMapperShape::HitTest(CMap1 &map,long X, long Y)
{
	double tol=map.ToMapDistance(5);	//5个像素
	CMoPoint pt=CMoPoint(map.ToMapPoint((float)X, (float)Y));
	if(LPDISPATCH(pt)==0)
		return FALSE;

	short iType=-1; //1-6
	int nSel=-1;
	int i;
	for(i=0;i<m_oaPoints.GetSize();i++)
	{
		if(m_oaPoints[i]->DistanceTo(pt)<tol)
		{
			iType=1;
			nSel=i;
			break;
		}
	}

	if((iType==-1)||(nSel==-1))
	{
		for(i=0;i<m_oaLines.GetSize();i++)
		{
			if(m_oaLines[i]->DistanceTo(pt)<tol)
			{
				iType=2;
				nSel=i;
				break;
			}
		}
	}

	if((iType==-1)||(nSel==-1))
	{
		for(i=0;i<m_oaRects.GetSize();i++)
		{
			if(m_oaRects[i]->IsPointIn(pt))
			{
				iType=3;
				nSel=i;
				break;
			}
		}
	}

	if((iType==-1)||(nSel==-1))
	{
		for(i=0;i<m_oaCircles.GetSize();i++)
		{
			if(m_oaCircles[i]->IsPointIn(pt))
			{
				iType=4;
				nSel=i;
				break;
			}
		}
	}

	if((iType==-1)||(nSel==-1))
	{
		for(i=0;i<m_oaEllipses.GetSize();i++)
		{
			if(m_oaEllipses[i]->IsPointIn(pt))
			{
				iType=5;
				nSel=i;
				break;
			}
		}
	}

	if((iType==-1)||(nSel==-1))
	{
		for(i=0;i<m_oaPolys.GetSize();i++)
		{
			if(m_oaPolys[i]->IsPointIn(pt))
			{
				iType=6;
				nSel=i;
				break;
			}
		}
	}

	if((iType==-1)||(nSel==-1))
	{
/*
		CMoLine line;
		CMoPolygon p;
		line.CreateDispatch(TEXT("MapObjects2.Line"));
		p.CreateDispatch(TEXT("MapObjects2.Polygon"));
		VARIANT va;
		VariantInit(&va);
		va.vt = VT_NULL;
		for(i=0;i<m_oaTexts.GetSize();i++)
		{
			line=*m_oaTexts[i]->line;
			p=line.Buffer(m_oaTexts[i]->height/2.0,va);
			if(p.IsPointIn(pt))
			{
				iType=7;
				nSel=i;
				break;
			}
		}
		line.ReleaseDispatch();
		p.ReleaseDispatch();
*/
		CMoRectangle r;
		r.CreateDispatch(TEXT("MapObjects2.Rectangle"));
		for(i=0;i<m_oaTexts.GetSize();i++)
		{
			r=m_oaTexts[i]->line->GetExtent();
			if(r.GetHeight()<(m_oaTexts[i]->height/2.0))
				r.SetTop(r.GetBottom()+m_oaTexts[i]->height/2.0);
			if(r.IsPointIn(pt))
			{
				iType=7;
				nSel=i;
				break;
			}
		}
		r.ReleaseDispatch();
	}

	m_tSel.type=iType;
	m_tSel.index=nSel;

	return TRUE;
}

void CMapperShape::DeleteShape()
{
	if((m_tSel.type==-1)||(m_tSel.index==-1))
		return;

	switch(m_tSel.type)
	{
	case 1:
		m_oaPoints[m_tSel.index]->ReleaseDispatch();
		delete m_oaPoints[m_tSel.index];
		m_oaPoints.RemoveAt(m_tSel.index);
		break;
	case 2:
		m_oaLines[m_tSel.index]->ReleaseDispatch();
		delete m_oaLines[m_tSel.index];
		m_oaLines.RemoveAt(m_tSel.index);
		break;
	case 3:
		m_oaRects[m_tSel.index]->ReleaseDispatch();
		delete m_oaRects[m_tSel.index];
		m_oaRects.RemoveAt(m_tSel.index);
		break;
	case 4:
		m_oaCircles[m_tSel.index]->ReleaseDispatch();
		delete m_oaCircles[m_tSel.index];
		m_oaCircles.RemoveAt(m_tSel.index);
		break;
	case 5:
		m_oaEllipses[m_tSel.index]->ReleaseDispatch();
		delete m_oaEllipses[m_tSel.index];
		m_oaEllipses.RemoveAt(m_tSel.index);
		break;
	case 6:
		m_oaPolys[m_tSel.index]->ReleaseDispatch();
		delete m_oaPolys[m_tSel.index];
		m_oaPolys.RemoveAt(m_tSel.index);
		break;
	case 7:
		m_oaTexts[m_tSel.index]->line->ReleaseDispatch();
		delete m_oaTexts[m_tSel.index]->line;
		delete m_oaTexts[m_tSel.index];
		m_oaTexts.RemoveAt(m_tSel.index);
		break;
	}
	m_tSel.type=-1;
	m_tSel.index=-1;
}

void CMapperShape::Edit()
{
	if((m_tSel.type==-1)||(m_tSel.index==-1))
		return;

	switch(m_tSel.type)
	{
	case 1:
		{
			CPointEditDlg ped;
			ped.m_dX=m_oaPoints[m_tSel.index]->GetX();
			ped.m_dY=m_oaPoints[m_tSel.index]->GetY();
			if(ped.DoModal()!=IDOK)
				return;
			m_oaPoints[m_tSel.index]->SetX(ped.m_dX);
			m_oaPoints[m_tSel.index]->SetY(ped.m_dY);
		}
		break;
	case 2:
		{
			CLineEditDlg led;
			short i;
			long j;
			SMapperPoint mp;
			led.m_taPoint.RemoveAll();
			for(i=0;i<m_oaLines[m_tSel.index]->GetParts().GetCount();i++)
			{
				CMoPoints pts=m_oaLines[m_tSel.index]->GetParts().Item(COleVariant(i));
				for(j=0;j<pts.GetCount();j++)
				{
					mp.x=pts.Item(COleVariant(j)).GetX();
					mp.y=pts.Item(COleVariant(j)).GetY();
					led.m_taPoint.Add(mp);
				}
			}

			led.m_sInfo.Format("长度:%lf",m_oaLines[m_tSel.index]->GetLength());
			if(led.DoModal()!=IDOK)
				return;

			//删除原来对象,重新建立
			m_oaLines[m_tSel.index]->ReleaseDispatch();
			delete m_oaLines[m_tSel.index];
			m_oaLines.RemoveAt(m_tSel.index);

			//新建立对象有两个位置可以放置:原始位置,需要插入;末尾,需要设置选择索引
			//原位置
			CMoLine* line = new CMoLine();
			line->CreateDispatch(TEXT("MapObjects2.Line"));
			CMoPoint pt;
			CMoPoints pts;
			pt.CreateDispatch(TEXT("MapObjects2.Point"));
			pts.CreateDispatch(TEXT("MapObjects2.Points"));
			for(j=0;j<led.m_taPoint.GetSize();j++)
			{
				pt.SetX(led.m_taPoint[j].x);
				pt.SetY(led.m_taPoint[j].y);
				pts.Add(pt);
			}

			line->GetParts().Add(pts);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -