private bool ImportOsg(string modelName, string filePath) { try { IImage property = null; IMatrix mat = null; IPropertySet images = null; IModel smodel = null; IModel fmodel = null; bool bRes = this.OpenOsgModel(filePath, out fmodel, out smodel, out images, out mat); if (!bRes) { return(false); } DF3DFeatureClass dffc = CommonUtils.Instance().CurEditLayer; if (dffc != null) { IFeatureClass fc = dffc.GetFeatureClass(); if (fc != null) { IResourceManager resManager = fc.FeatureDataSet as IResourceManager; if ((images != null) && (images.Count > 0)) { foreach (string str in images.GetAllKeys()) { if (!resManager.ImageExist(str)) { property = images.GetProperty(str) as IImage; bRes = resManager.AddImage(str, property); if (!bRes) { return(false); } } } } bRes = resManager.AddModel(modelName, fmodel, smodel); if (!bRes) { return(false); } } } return(true); } catch (Exception ex) { return(false); } }
private bool OpenOsgModel(string osgPath, out IModel fmodel, out IModel smodel, out IPropertySet images, out IMatrix mat) { fmodel = null; smodel = null; images = null; mat = null; IResourceFactory resFactory = new ResourceFactoryClass(); if ((resFactory == null) || !File.Exists(osgPath)) { return(false); } Dictionary <string, string> dictionary = null; IDrawGroup group = null; IDrawPrimitive primitive = null; IPropertySet set = null; string str = ""; IImage property = null; try { resFactory.CreateModelAndImageFromFileEx(osgPath, out images, out smodel, out fmodel, out mat); if ((images != null) && (images.Count > 0)) { set = new PropertySetClass(); dictionary = new Dictionary <string, string>(); foreach (string str2 in images.GetAllKeys()) { property = images.GetProperty(str2) as IImage; IImage temp = null; string filePath = string.Format(string.Format(@"{0}\..\temp\{1}.png", Application.StartupPath, Guid.NewGuid().ToString()), new object[0]); if (property.WriteFile(filePath)) { temp = resFactory.CreateImageFromFile(filePath); } str = BitConverter.ToString(ObjectIdGenerator.Generate()).Replace("-", string.Empty).ToLowerInvariant(); dictionary.Add(str2, str); set.SetProperty(str, temp); if (File.Exists(filePath)) { File.Delete(filePath); } } images = set; } if ((fmodel != null) && (fmodel.GroupCount > 0)) { for (int i = 0; i < fmodel.GroupCount; i++) { group = fmodel.GetGroup(i); if (group != null) { if (!string.IsNullOrEmpty(group.CompleteMapTextureName) && dictionary.ContainsKey(group.CompleteMapTextureName)) { group.CompleteMapTextureName = dictionary[group.CompleteMapTextureName]; } if (!string.IsNullOrEmpty(group.LightMapTextureName) && dictionary.ContainsKey(group.LightMapTextureName)) { group.LightMapTextureName = dictionary[group.LightMapTextureName]; } if (group.PrimitiveCount > 0) { for (int j = 0; j < group.PrimitiveCount; j++) { primitive = group.GetPrimitive(j); if (((primitive != null) && (primitive.Material != null)) && (!string.IsNullOrEmpty(primitive.Material.TextureName) && dictionary.ContainsKey(primitive.Material.TextureName))) { primitive.Material.TextureName = dictionary[primitive.Material.TextureName]; } } } } } } if ((smodel != null) && (smodel.GroupCount > 0)) { for (int k = 0; k < smodel.GroupCount; k++) { group = smodel.GetGroup(k); if (group != null) { if (!string.IsNullOrEmpty(group.CompleteMapTextureName) && dictionary.ContainsKey(group.CompleteMapTextureName)) { group.CompleteMapTextureName = dictionary[group.CompleteMapTextureName]; } if (!string.IsNullOrEmpty(group.LightMapTextureName) && dictionary.ContainsKey(group.LightMapTextureName)) { group.LightMapTextureName = dictionary[group.LightMapTextureName]; } if (group.PrimitiveCount > 0) { for (int m = 0; m < group.PrimitiveCount; m++) { primitive = group.GetPrimitive(m); if (((primitive != null) && (primitive.Material != null)) && (!string.IsNullOrEmpty(primitive.Material.TextureName) && dictionary.ContainsKey(primitive.Material.TextureName))) { primitive.Material.TextureName = dictionary[primitive.Material.TextureName]; } } } } } } return(true); } catch (Exception exception) { return(false); } }
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); //} }