ArcGIS Engine代码段笔记【一】

1、打开点云数据

        private void button4_Click(object sender, EventArgs e)
        {
            ESRI.ArcGIS.GeoDatabaseExtensions.ILasDataset pNewLas = new ESRI.ArcGIS.GeoDatabaseExtensions.LasDatasetClass();
            pNewLas.Init(@"D:\tempfile\lasss.lasd");
            ILasDatasetLayer pLasLayer = new LasDatasetLayerClass();
            pLasLayer.Dataset = pNewLas;
            axMapControl1.AddLayer(pLasLayer);
        }
     ILasDataset pNewLas = new LasDatasetClass();
     pNewLas.Init(@"D:\tempfile\lasss.lasd");
     ILasDatasetLayer pLasLayer = new LasDatasetLayerClass();
     pLasLayer.Dataset = pNewLas;
     axMapControl1.AddLayer(pLasLayer);

2、导出图层为shapefile文件

 public void OperateConvertToShape(string sFileName, IFeatureClass pFC)
        {
            try
            {

                string str = sFileName;
                string str2 = str.Substring(str.LastIndexOf(@"\") + 1);
                string str3 = str.Substring(0, (str.Length - str2.Length) - 1);

                IPropertySet set = new PropertySetClass();
                set.SetProperty("DATABASE", str3);

                IWorkspaceName pWorkSpaceName = new WorkspaceNameClass();
                pWorkSpaceName.ConnectionProperties = set;
                pWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileWorkspaceFactory.1";

                IFeatureClassName pFeatureClassName = new FeatureClassNameClass();
                IDatasetName pDatasetName = pFeatureClassName as IDatasetName;
                pDatasetName.Name = str2;
                pDatasetName.WorkspaceName = pWorkSpaceName;

                IDataset dataset = pFC as IDataset;
                IWorkspaceName sWorkSpaceName = new WorkspaceNameClass();
                sWorkSpaceName.ConnectionProperties = dataset.Workspace.ConnectionProperties;

                if (dataset.Workspace.WorkspaceFactory.get_WorkspaceDescription(true) == "File Geodatabases")
                {
                    sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory.1";
                }
                else if (dataset.Workspace.WorkspaceFactory.get_WorkspaceDescription(true) == "Access Geodatabases")
                {
                    sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory.1";
                }
                else if (dataset.Workspace.WorkspaceFactory.get_WorkspaceDescription(true) == "SDE Geodatabases")
                {
                    sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.SdeWorkspaceFactory.1";
                }
                else if (dataset.Workspace.WorkspaceFactory.get_WorkspaceDescription(true) == "In Memory Workspaces")
                {
                    sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.InMemoryWorkspaceFactory.1";
                }
                else if (dataset.Workspace.WorkspaceFactory.get_WorkspaceDescription(true) == "Shapefiles")
                {
                    sWorkSpaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileWorkspaceFactory.1";
                }

                IFeatureClassName sFeatureClassName = new FeatureClassNameClass();
                IDatasetName sDatasetName = sFeatureClassName as IDatasetName;
                sDatasetName.Name = dataset.Name;
                sDatasetName.WorkspaceName = sWorkSpaceName;

                IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
                featureDataConverter.ConvertFeatureClass(sFeatureClassName, null, null, pFeatureClassName, null, null, "", 0x3e8, 0);

                MessageBox.Show("shape文件转出完毕!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

3.开启捕捉状态

private IHookHelper m_hookHelper;
        IHookHelper2 m_hookHelper2;
        private IPoint m_snapPt;
        private ISnappingEnvironment m_SnapEnv;
        private IPointSnapper m_Snapper;
        private ISnappingFeedback m_SnapFeedback;
        private INewLineFeedback m_feedback;
        private ISnappingResult m_snapRst;  
        public newTool()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = ""; //localizable text   
            base.m_caption = "";  //localizable text   
            base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl";  //localizable text  
            base.m_toolTip = "";  //localizable text  
            base.m_name = "";   //unique id, non-localizable (e.g. "MyCategory_MyTool")  
            try
            {
                //  
                // TODO: change resource name if necessary  
                //  
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
                base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            } 
        }
        
        public override void OnCreate(object hook)
        {
            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                {
                    m_hookHelper = null;
                }
            }
            catch
            {
                m_hookHelper = null;
            }


            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;


            // TODO:  Add other initialization code  


            m_hookHelper2 = m_hookHelper as IHookHelper2;
        }
        
        public override void OnClick()
        {
            IExtensionManager extensionManager = m_hookHelper2.ExtensionManager;
            UID guid = new UIDClass();
            guid.Value = "{E07B4C52-C894-4558-B8D4-D4050018D1DA}";
            IExtension extension = extensionManager.FindExtension(guid);
            m_SnapEnv = extension as ISnappingEnvironment;
            m_Snapper = m_SnapEnv.PointSnapper;
            m_SnapFeedback = new SnappingFeedbackClass();
            m_SnapFeedback.Initialize(m_hookHelper.Hook, m_SnapEnv, true);
            m_SnapEnv.SnappingType = (esriSnappingType)((int)esriSnappingType.esriSnappingTypeEdge + (int)esriSnappingType.esriSnappingTypeVertex + 
                (int)esriSnappingType.esriSnappingTypePoint);
            m_SnapEnv.Enabled = true;
        }
        
        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add newTool.OnMouseDown implementation
            IPoint ppt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            if (m_snapPt != null)
            {
                ppt = m_snapPt;
            }


            if (m_feedback == null)
            {
                m_feedback = new NewLineFeedbackClass();
                m_feedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
                m_feedback.Start(ppt);
            }
            else
            {
                m_feedback.AddPoint(ppt);
            }  
        }
        
        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add newTool.OnMouseMove implementation
            IPoint ppt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            m_snapPt = ppt;
            m_snapRst = m_Snapper.Snap(m_snapPt);
            m_SnapFeedback.Update(m_snapRst, 0);
            if (m_snapRst != null)
            {
                m_snapPt = m_snapRst.Location;
            }


            if (m_feedback != null)
            {
                m_feedback.MoveTo(m_snapPt);
            }  
        }
        
        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add newTool.OnMouseUp implementation
        }

        public override void Refresh(int hDC)
        {
            if (m_feedback != null)
                m_feedback.Refresh(hDC);
        }

        public override void OnDblClick()
        {
            m_feedback.Stop();
        }

4、创建注记要素类

private static void CreateFeatureClassAnno(string featurename, List<PublicType.esriField> Fields, IWorkspace workspace)
        {
            try
            {
                using (ESRI.ArcGIS.ADF.ComReleaser comReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    IFeatureWorkspaceAnno pFeatureWorkspace = workspace as IFeatureWorkspaceAnno;

                    IFeatureClassDescription FCDescription = new AnnotationFeatureClassDescriptionClass();
                    comReleaser.ManageLifetime(FCDescription);

                    IObjectClassDescription OCDescription = FCDescription as IObjectClassDescription;
                    comReleaser.ManageLifetime(OCDescription);

                    IFields pFields = new FieldsClass();
                    comReleaser.ManageLifetime(pFields);
                    pFields = OCDescription.RequiredFields;

                    int shapeFieldIndex = pFields.FindField(FCDescription.ShapeFieldName);
                    IField pField = pFields.Field[shapeFieldIndex];
                    comReleaser.ManageLifetime(pField);
                    IGeometryDef pGeometryDef = pField.GeometryDef;
                    comReleaser.ManageLifetime(pGeometryDef);
                    IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
                    comReleaser.ManageLifetime(pGeometryDefEdit);
                    pGeometryDefEdit.GridCount_2 = 1;
                    pGeometryDefEdit.GridSize_2[0] = 1000;

                    //IUnknownCoordinateSystem pUnknownCoordinateSystem = new UnknownCoordinateSystemClass();
                    //comReleaser.ManageLifetime(pUnknownCoordinateSystem);
                    //pUnknownCoordinateSystem.SetDomain(PublicVars.pEsriExtent.minX - 5, PublicVars.pEsriExtent.maxX + 5, PublicVars.pEsriExtent.minY - 5, PublicVars.pEsriExtent.maxY + 5);
                    //pGeometryDefEdit.SpatialReference_2 = pUnknownCoordinateSystem as ISpatialReference;

                    for (int i = 0; i < Fields.Count; ++i)
                    {
                        IField xField = new FieldClass();
                        comReleaser.ManageLifetime(xField);
                        IFieldEdit xFieldEdit = xField as IFieldEdit;
                        comReleaser.ManageLifetime(xFieldEdit);
                        xFieldEdit.Name_2 = Fields[i].name;
                        xFieldEdit.AliasName_2 = Fields[i].aliasname;
                        xFieldEdit.Type_2 = Fields[i].fieldtype;
                        if (Fields[i].fieldtype == esriFieldType.esriFieldTypeDouble ||
                            Fields[i].fieldtype == esriFieldType.esriFieldTypeSingle)
                        {
                            xFieldEdit.Precision_2 = Fields[i].Length1;
                            xFieldEdit.Scale_2 = Fields[i].Length2;
                        }
                        else
                        {
                            xFieldEdit.Length_2 = Fields[i].Length1;
                        }
                    }

                    IFieldChecker pFieldChecker = new FieldCheckerClass();
                    comReleaser.ManageLifetime(pFieldChecker);
                    IEnumFieldError pEnumFieldError = null;
                    comReleaser.ManageLifetime(pEnumFieldError);
                    IFields validatedFields = null;
                    comReleaser.ManageLifetime(validatedFields);
                    pFieldChecker.ValidateWorkspace = pFeatureWorkspace as IWorkspace;
                    pFieldChecker.Validate(pFields, out pEnumFieldError, out validatedFields);


                    IGraphicsLayerScale pGLyrScale = new GraphicsLayerScaleClass();
                    pGLyrScale.ReferenceScale = 500;
                    pGLyrScale.Units = esriUnits.esriMeters;

                    ITextSymbol pTxtSymbol = new TextSymbolClass();
                    MakeTextSymbol(ref pTxtSymbol, "宋体", 10);

                    ISymbolCollection pSymbolColl = new SymbolCollectionClass();
                    pSymbolColl.set_Symbol(0, (ISymbol)pTxtSymbol);

                    IFeatureClass pFeatureClass = pFeatureWorkspace.CreateAnnotationClass(featurename, validatedFields,
                        OCDescription.InstanceCLSID, OCDescription.ClassExtensionCLSID, "SHAPE","",null,null,null,
                        pGLyrScale, pSymbolColl, true);
                    comReleaser.ManageLifetime(pFeatureClass);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
5、创建FileGeoDatabase
        private static IWorkspace CreateWorkspaceFromFileGDB(string FilePath)
         {
            IWorkspace ws = null;
            using (ESRI.ArcGIS.ADF.ComReleaser comReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();
                comReleaser.ManageLifetime(wsf);
                IWorkspaceName wsn = wsf.Create(FilePath,
                    DateTime.Today.ToString("yyyyMMdd")+ DateTime.Now.ToString("HHmmss"), null, 0);
                comReleaser.ManageLifetime(wsn);
                IName pName = wsn as IName;
                comReleaser.ManageLifetime(pName);
                ws = pName.Open() as IWorkspace;
            }
            return ws;
        }


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值