无法嵌入互操作类型“ESRI.ArcGIS.Carto.FeatureLayerClass”。请改用适用的接口
解决:
1、展开当前项目(Project)的“引用”;
2、找到引用的“ESRI.ArcGIS.Carto”,然后鼠标右键--属性。
3、将“嵌入互操作类型”改为“False”
1、打开
Program.cs把ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);这句放到Application.SetCompatibleTextRenderingDefault(false);和Application.Run(new Form1());之间应该就好了
2、在系统的入口添加下面的一行代码:
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
3、如果还不行When we migrate our code from ArcGis 9.3 to ArcGis 10 then this type of error occurs. 1.First we add Reference ESRI.ArcGis.ADF.Local
2.Add Reference ESRI.ArcGis.Version
3.Add Line
“ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); ”
prior to call any arcgis object.
4.In all ArcGis Reference property "Specific Version" set to false.
5.Now Go to Project menu of visual studio and there are one option "Add ArcGis License Checking" and Add this file to your project.
你将在VS2010里创建的项目,属性里,选择.netFramework3.5,就可以运行了,不会报错,跟版本有关。
解决问题的要点是其中一个命名空间要取别名代替。取别名的方法如下,记得还得修改引用中程序集dll的别名
对于这个有两个方法:
第一种:通过将 axmapcontrol 自带的 ShowMapTips 属性设置为 true 来实现。
第二种:通过 .NET 自带的控件 ToolTip 来实现!
第一种代码:
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
axMapControl1.ShowMapTips = true;
IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
pFeatureLayer.DisplayField = "Name";
pFeatureLayer.ShowTips = true;
}
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
pFeatureLayer.DisplayField = "Name";
pFeatureLayer.ShowTips = true;
string pTip;
pTip = pFeatureLayer.get_TipText(e.mapX, e.mapY, axMapControl1.ActiveView.FullExtent.Width / 10000);
if (pTip != null)
{
toolTip1.SetToolTip(axMapControl1, "名称:" + pTip);
}
else //当 ToolTip 空间显示的内容为 null 的时候,就不会显示了!相当于隐藏了!
{
toolTip1.SetToolTip(axMapControl1, "");
}
}
以上两种方法都可以实现显示标注,但是第二种效果更好一点~!
A:你用的是Visual Studio 2010和C# 4吧?如果是,那就对了。这个问题和Engine本身无关。
这是C# 4对COMInterop的一个改进,把创建CoClass时的类名的Class后缀去掉即可。不允许用CoClass本身,而必须用相应的接口来创建对象。
比如,
1. IPoint p = new PointClass()
改为:
1. IPoint p = new Point()
你的操作系统是64位的Windows吧?可是ArcGIS现在只有32位的,所以必须用X86平台生成项目。
打开项目属性,在“生成”选项卡中找到目标平台,把Any CPU改为x86,重新生成即可。
这个问题还是补充地具体一些吧:
1、在VS工具箱内右键,添加选项卡,取名ArcGIS Windows Form
2、在新选项卡上右键,选择项...
3、点浏览,找到ESRI.ArcGIS.AxControls.dll(缺省安装在"C:\Program Files\ArcGIS\DotNet\ESRI.ArcGIS.AxControls.dll"),打开。
4、勾选中新出现的那几个ArcGIS的控件,点确定。
首先,添加axSceneControl控件和linece控件,控件中添加ArcScene数据。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Analyst3D;
using ESRI.ArcGIS.Geometry;
namespace wheel1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MouseWheel += new System.Windows.Forms.MouseEventHandler
(this.axSceneControl1_Wheel);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void axSceneControl1_Wheel(object sender,
System.Windows.Forms.MouseEventArgs e)
{
try
{
System.Drawing.Point pSceLoc = axSceneControl1.PointToScreen
(this.axSceneControl1.Location);
System.Drawing.Point Pt = this.PointToScreen(e.Location);
if (Pt.X < pSceLoc.X | Pt.X > pSceLoc.X + axSceneControl1.Width | Pt.Y <
pSceLoc.Y | Pt.Y > pSceLoc.Y + axSceneControl1.Height) return;
double scale = 0.2;
if (e.Delta < 0) scale = -0.2;
ICamera pCamera = axSceneControl1.Camera;
IPoint pPtObs = pCamera.Observer;
IPoint pPtTar = pCamera.Target;
pPtObs.X += (pPtObs.X - pPtTar.X) * scale;
pPtObs.Y += (pPtObs.Y - pPtTar.Y) * scale;
pPtObs.Z += (pPtObs.Z - pPtTar.Z) * scale;
pCamera.Observer = pPtObs;
axSceneControl1.SceneGraph.RefreshViewers();
}
catch (Exception ex)
{
}
}
}
}
ILayer layer = axSceneControl1.SceneGraph.Scene.get_Layer(5);
IFeatureLayer featurelayer = (IFeatureLayer)layer;
IFeatureClass featureclass = featurelayer.FeatureClass;
IFeature feature = featureclass.GetFeature(0);
IPolyline polyline = (IPolyline )feature.Shape;
double d = polyline.Length;
IPoint point1 = new PointClass();
IPoint point2 = new PointClass();
for (int i = 2; i <= (int)d;i++ )
{
polyline.QueryPoint(esriSegmentExtension.esriNoExtension, i, false, point1);
polyline.QueryPoint(esriSegmentExtension.esriExtendAtFrom , i-150, false, point2);
point2 .Z =13;
point2.X= point2.X +-50;
ICamera camera = axSceneControl1.SceneViewer.Camera;
IPoint point3=new PointClass ();
point3.X = point1.X;
point3.Y = point1.Y;
point3.Z =13;
camera.Target = point3;
camera.Observer = point2;
IScene pscene = axSceneControl1.SceneGraph.Scene;
IMarker3DSymbol pmark3dsymbol = new Marker3DSymbolClass();
pmark3dsymbol.CreateFromFile("E:\\3dmax\\汽车.3DS");
IMarkerSymbol marksy = (IMarkerSymbol)pmark3dsymbol;
marksy.Size = 20;
marksy.Angle = 90;
IElement pelement = new MarkerElementClass();
IMarkerElement pmarkelement = (IMarkerElement)pelement;
pmarkelement.Symbol = (IMarkerSymbol)marksy;
pelement.Geometry = point1;
IGraphicsLayer player = axSceneControl1.SceneGraph.Scene.BasicGraphicsLayer;
IGraphicsContainer3D pgraphiccontainer3d = (IGraphicsContainer3D)player;
pgraphiccontainer3d.DeleteAllElements();
pgraphiccontainer3d.AddElement((IElement)pmarkelement);
axSceneControl1.SceneGraph.RefreshViewers();
}
void DrawTextToMap(string text, ESRI.ArcGIS.Geometry.Point point2)
{
//创建字体对象
ITextSymbol textSymbol = new TextSymbol();
//创建系统字体
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 32, System.Drawing.FontStyle.Regular);
//字体转换
// textSymbol.Font = (stdole.IFontDisp)ESRI.ArcGIS.Utility.COMSupport.OLE.GetIFontDispFromFont(drawFont);
textSymbol.Font = (stdole.IFontDisp)ESRI.ArcGIS.ADF.COMSupport.OLE.GetIFontDispFromFont(drawFont);
textSymbol.Color = GetRGBColor(0, 0, 0);
//创建字体路径
ITextPath textPath = new BezierTextPath(); //to spline the text
//Grab hold of the ISimpleTextSymbol interface through the ITextSymbol interface
ISimpleTextSymbol simpleTextSymbol = (ISimpleTextSymbol)textSymbol;
//Set the text path of the simple text symbol
simpleTextSymbol.TextPath = textPath;
IPoint m_point=new ESRI.ArcGIS.Geometry.Point();
// double xx = point2.X;
m_point.X = point2.X;
m_point.Y = point2.Y;
//输出字体
object oTextSymbol = textSymbol;
object opointSymbol = m_point;
mapControl.DrawText(m_point, text, ref oTextSymbol);
}
private IRgbColor GetRGBColor(int red, int green, int blue)
{
//Create rgb color and grab hold of the IRGBColor interface
IRgbColor rGB = new RgbColor();
//Set rgb color properties
rGB.Red = red;
rGB.Green = green;
rGB.Blue = blue;
rGB.UseWindowsDithering = true;
return rGB;
}
首先,确定你的图层是否设置了MinimumScale属性,如果设置了请把该属性去掉;
然后,利用ControlsZoomToSelectedCommandClass接口,代码如下:
ICommand pCommand = new ControlsZoomToSelectedCommandClass();
pCommand.OnCreate(axMapControl1.Object);
pCommand.OnClick();
即可实现缩放到选定要素
#region 一个通过属性表中属性查找图形要素的小功能
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection SelRows = this.dataGridView1.SelectedRows;
DataGridViewRow row;
RibbonForm1 form = (RibbonForm1)Application.OpenForms[0];
IMap m = form.getMapControl().Map;
m.ClearSelection();
for (int i = 0; i < SelRows.Count; i++)
{
row = SelRows[i];
int ID = Convert.ToInt32(row.Cells["OBJECTID"].Value.ToString());
IFeatureLayer flyr = (IFeatureLayer)layer;
IFeatureClass featurecls = flyr.FeatureClass;
IFeature feature = featurecls.GetFeature(ID);
m.SelectFeature(layer, feature);//获取属性表中选中行对应的图形要素
}
//form.getMapControl().Refresh();
//放大到一定的比例尺 实现选中要素的显示
ICommand pCommand = new ControlsZoomToSelectedCommandClass();
//pCommand.OnCreate(axMapControl1.Object);
pCommand.OnCreate(form.getMapControl().Object);
pCommand.OnClick();
}
#endregion
在ArcGIS的开发中,我们经常需要将当前地图打印(或是转出)到图片文件中。将Map或Layout中的图象转出有两种方法,一种为通过 IActiveView的OutPut函数,另外一种是通过IExport接口来实现。第一种方法导出速度较快,实现也比较方便,但该方法对于图片的行或列数超过10000左右时,导出经常会失败(具体原因未知),第二种方法导出速度较慢,但效果较好,且可以在导出过程中通过ITrackCancel来中止导出操作。
通过IActiveView的方式导出是通过创建Graphics对象来实现,具体示例代码如下:
///
/// 将Map上指定范围(该范围为规则区域)内的内容输出到Image,注意,当图片的行数或列数超过10000左右时,出现原因示知的失败
///
/// 需转出的MAP
/// 输出的图片大小
/// 指定的输出范围(为Envelope类型)
/// 输出的Image 具体需要保存为什么格式,可通过Image对象来实现
public static Image SaveCurrentToImage(IMap pMap, Size outRect, IEnvelope pEnvelope)
{
//赋值
tagRECT rect = new tagRECT();
rect.left = rect.top = 0;
rect.right = outRect.Width;
rect.bottom = outRect.Height;
try
{
//转换成activeView,若为ILayout,则将Layout转换为IActiveView
IActiveView pActiveView = (IActiveView)pMap;
// 创建图像,为24位色
Image image = new Bitmap(outRect.Width, outRect.Height); //, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
// 填充背景色(白色)
g.FillRectangle(Brushes.White, 0, 0, outRect.Width, outRect.Height);
int dpi = (int)(outRect.Width / pEnvelope.Width);
pActiveView.Output(g.GetHdc().ToInt32(), dpi, ref rect, pEnvelope, null);
g.ReleaseHdc();
return image;
}
catch (Exception excp)
{
MessageBox.Show(excp.Message + "将当前地图转出出错,原因未知", "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
通过IExport接口实现的导出,也需要通过IActiveView的OutPut来实现,但其转出句柄为IExport的StartExporting函数返回的DC,具体示例代码如下:
//输出当前地图至指定的文件
public void ExportMapExtent(IActiveView pView, Size outRect,string outPath)
{
try
{
//参数检查
if pView == null )
{
throw new Exception("输入参数错误,无法生成图片文件!");
}
//根据给定的文件扩展名,来决定生成不同类型的对象
ESRI.ArcGIS.Output.IExport export = null;
if (outPath.EndsWith(".jpg"))
{
export = new ESRI.ArcGIS.Output.ExportJPEGClass();
}
else if (outPath.EndsWith(".tiff"))
{
export = new ESRI.ArcGIS.Output.ExportTIFFClass();
}
else if (outPath.EndsWith(".bmp"))
{
export = new ESRI.ArcGIS.Output.ExportBMPClass();
}
else if (outPath.EndsWith(".emf"))
{
export = new ESRI.ArcGIS.Output.ExportEMFClass();
}
else if (outPath.EndsWith(".png"))
{
export = new ESRI.ArcGIS.Output.ExportPNGClass();
}
else if (outPath.EndsWith(".gif"))
{
export = new ESRI.ArcGIS.Output.ExportGIFClass();
}
export.ExportFileName = outPath;
IEnvelope pEnvelope = pView.Extent;
//导出参数
export.Resolution = 300;
tagRECT exportRect = new tagRECT();
exportRect.left = exportRect.top = 0;
exportRect.right = outRect.Width;
exportRect.bottom = (int)(exportRect.right * pEnvelope.Height / pEnvelope.Width);
ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();
//输出范围
envelope.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom);
export.PixelBounds = envelope;
//可用于取消操作
ITrackCancel pCancel = new CancelTrackerClass();
export.TrackCancel = pCancel;
pCancel.Reset();
//点击ESC键时,中止转出
pCancel.CancelOnKeyPress = true;
pCancel.CancelOnClick = false;
pCancel.ProcessMessages = true;
//获取handle
System.Int32 hDC = export.StartExporting();
//开始转出
pView.Output(hDC, (System.Int16)export.Resolution, ref exportRect, pEnvelope, pCancel);
bool bContinue = pCancel.Continue();
//捕获是否继续
if (bContinue)
{
export.FinishExporting();
export.Cleanup();
}
else
{
export.Cleanup();
}
bContinue = pCancel.Continue();
}
catch (Exception excep)
{
//错误信息提示
}
}
using System;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.NetworkAnalysis;
namespace GisEditor
{
///
/// 最短路径分析
///
public class ClsPathFinder
{
private IGeometricNetwork m_ipGeometricNetwork;
private IMap m_ipMap;
private IPointCollection m_ipPoints;
private IPointToEID m_ipPointToEID;
private double m_dblPathCost =0;
private IEnumNetEID m_ipEnumNetEID_Junctions;
private IEnumNetEID m_ipEnumNetEID_Edges;
private IPolyline m_ipPolyline;
#region Public Function
//返回和设置当前地图
public IMap SetOrGetMap
{
set{ m_ipMap = value;}
get{return m_ipMap;}
}
//打开几何数据集的网络工作空间
public void OpenFeatureDatasetNetwork(IFeatureDataset FeatureDataset)
{
CloseWorkspace();
if (!InitializeNetworkAndMap(FeatureDataset))
Console.WriteLine( "打开network出错");
}
//输入点的集合
public IPointCollection StopPoints
{
set{m_ipPoints= value;}
get{return m_ipPoints;}
}
//路径成本
public double PathCost
{
get {return m_dblPathCost;}
}
//返回路径的几何体
public IPolyline PathPolyLine()
{
IEIDInfo ipEIDInfo;
IGeometry ipGeometry;
if(m_ipPolyline!=null)return m_ipPolyline;
m_ipPolyline = new PolylineClass();
IGeometryCollection ipNewGeometryColl = m_ipPolyline as IGeometryCollection;
ISpatialReference ipSpatialReference = m_ipMap.SpatialReference;
IEIDHelper ipEIDHelper = new EIDHelperClass();
ipEIDHelper.GeometricNetwork = m_ipGeometricNetwork;
ipEIDHelper.OutputSpatialReference = ipSpatialReference;
ipEIDHelper.ReturnGeometries = true;
IEnumEIDInfo ipEnumEIDInfo = ipEIDHelper.CreateEnumEIDInfo(m_ipEnumNetEID_Edges);
int count = ipEnumEIDInfo.Count;
ipEnumEIDInfo.Reset();
for(int i =0;i dblHeight)
dblSearchTol = dblWidth / 100;
else
dblSearchTol = dblHeight / 100;
m_ipPointToEID.SnapTolerance = dblSearchTol;
return true ;
}
//关闭工作空间
private void CloseWorkspace()
{
m_ipGeometricNetwork = null;
m_ipPoints = null;
m_ipPointToEID = null;
m_ipEnumNetEID_Junctions = null;
m_ipEnumNetEID_Edges = null;
m_ipPolyline = null;
}
#endregion
}
}
备注:
在调用该类时的次序:
ClsPathFinder m_ipPathFinder;
if(m_ipPathFinder==null)//打开几何网络工作空间
{
m_ipPathFinder = new ClsPathFinder();
ipMap = this.m_ActiveView.FocusMap;
ipLayer = ipMap.get_Layer(0);
ipFeatureLayer = ipLayer as IFeatureLayer;
ipFDB = ipFeatureLayer.FeatureClass.FeatureDataset;
m_ipPathFinder.SetOrGetMap = ipMap;
m_ipPathFinder.OpenFeatureDatasetNetwork(ipFDB);
}
private void ViewMap_OnMouseDown(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e)//获取地图上鼠标输入的点
{
IPoint ipNew ;
if( m_ipPoints==null)
{
m_ipPoints = new MultipointClass();
m_ipPathFinder.StopPoints = m_ipPoints;
}
ipNew = ViewMap.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x,e.y);
object o = Type.Missing;
m_ipPoints.AddPoint(ipNew,ref o,ref o);
}
m_ipPathFinder.SolvePath("Weight");//先解析路径
IPolyline ipPolyResult = m_ipPathFinder.PathPolyLine();//最后返回最短路径
错误16:ArcEngine打开shapefile时报错 HRESULT:0x80040258
private IFeatureClass OpenShp(string pTableName, string pShpPath)
{
try
{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace pWorkspace =pWorkspaceFactory.OpenFromFile(pShpPath,0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pTableName);
return pFeatureClass;
}
catch (Exception ex)
{
return null;
}
}
走到红字的那行就报错了,后来才知道不是代码的问题,是带进去的参数不对了
pShpPath 只是路径名字,比如C:\\AAA
不是文件的全路径名字
我带进去了C:\\AAA\\BBB.shp所以就出错了
原来arcengine开发shapefile时,是把一个文件夹当作一个工作空间打开了,文件夹中的每一个shape文件又对应一个featureclass