コード例 #1
0
        public void ImportModelOsg(string osgFile)
        {
            //try
            {
                FileInfo         fInfo       = new FileInfo(osgFile);
                string           strFilePath = fInfo.DirectoryName;
                IObjectManager   rm          = this.axRenderControl1.ObjectManager;
                IGeometryFactory geoFactory  = new GeometryFactory();
                IResourceFactory symbolFac   = new ResourceFactory();

                //mc
                string       modelName   = fInfo.Name.Split('.')[0];
                string       osgFilePath = strFilePath + "\\" + modelName + ".osg";
                IPropertySet Images      = null;
                IModel       model       = null;
                IMatrix      matrix      = null;
                symbolFac.CreateModelAndImageFromFile(osgFilePath, out Images, out model, out matrix);
                if (model == null || model.GroupCount == 0)
                {
                    return;
                }
                rm.AddModel(modelName, model);

                //tc
                int nCount = Images.Count;
                if (Images != null && nCount > 0)
                {
                    string[] keys = Images.GetAllKeys();
                    foreach (string imgName in keys)
                    {
                        IImage img = Images.GetProperty(imgName) as IImage;

                        if (img == null)
                        {
                            continue;
                        }
                        if (string.IsNullOrEmpty(imgName))
                        {
                            continue;
                        }

                        rm.AddImage(imgName, img);
                    }
                }

                //modelpoint
                IModelPoint modelPoint = null;
                modelPoint = (IModelPoint)geoFactory.CreateGeometry(
                    gviGeometryType.gviGeometryModelPoint,
                    gviVertexAttribute.gviVertexAttributeZ);
                modelPoint.FromMatrix(matrix);
                modelPoint.ModelName  = modelName;
                modelPoint.SpatialCRS = (new CRSFactory()).CreateFromWKT(this.axRenderControl1.GetCurrentCrsWKT()) as ISpatialCRS;


                if (this.toolStripComboBoxOsgMode.SelectedIndex == 0)
                {
                    rmp = this.axRenderControl1.ObjectManager.CreateRenderModelPoint(modelPoint, null, rootId);
                    this.axRenderControl1.Camera.FlyToObject(rmp.Guid, gviActionCode.gviActionFlyTo);
                }
                else
                {
                    modelPoint.X = 0.0;
                    modelPoint.Y = 0.0;
                    modelPoint.Z = 0.0;

                    rmp = this.axRenderControl1.ObjectManager.CreateRenderModelPoint(modelPoint, null, rootId);
                    rmp.MouseSelectMask = gviViewportMask.gviViewNone;  //设置物体不可拾取,以免总是拾取到自身影响交互
                    IEulerAngle angle = new EulerAngle();
                    angle.Set(0, -50, 0);
                    this.axRenderControl1.Camera.LookAt2(modelPoint, 200, angle);

                    this.axRenderControl1.InteractMode = gviInteractMode.gviInteractEdit;
                    this.axRenderControl1.ObjectEditor.StartEditRenderGeometry(rmp, gviGeoEditType.gviGeoEditCreator);
                }
            }
            //catch (System.Exception ex)
            //{
            //    if (ex.GetType().Name.Equals("UnauthorizedAccessException"))
            //        MessageBox.Show("需要标准runtime授权");
            //    else
            //        MessageBox.Show(ex.Message);
            //}
        }