DlgCreateTINfromFeature.cpp
上传用户:juying163
上传日期:2014-09-24
资源大小:5867k
文件大小:8k
源码类别:

GIS编程

开发平台:

Visual C++

  1. // DlgCreateTINfromFeature.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "3D0214.h"
  5. #include "DlgCreateTINfromFeature.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDlgCreateTINfromFeature dialog
  13. CDlgCreateTINfromFeature::CDlgCreateTINfromFeature(CWnd* pParent /*=NULL*/)
  14. : CDialog(CDlgCreateTINfromFeature::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CDlgCreateTINfromFeature)
  17. m_edit_TINInputs = _T("");
  18. m_edit_TINOutput = _T("");
  19. //}}AFX_DATA_INIT
  20. }
  21. void CDlgCreateTINfromFeature::DoDataExchange(CDataExchange* pDX)
  22. {
  23. CDialog::DoDataExchange(pDX);
  24. //{{AFX_DATA_MAP(CDlgCreateTINfromFeature)
  25. DDX_Control(pDX, IDC_EDIT_TIN_OUTPUT, m_Control_Output_Path);
  26. DDX_Control(pDX, IDC_COMBO_TIN_HEIGHT_FIELD, m_combo_TINHeightField);
  27. DDX_Control(pDX, IDC_COMBO_TIN_TRIANGU, m_combo_TINTriangulateAs);
  28. DDX_Text(pDX, IDC_EDIT_TIN_INPUTS, m_edit_TINInputs);
  29. DDX_Text(pDX, IDC_EDIT_TIN_OUTPUT, m_edit_TINOutput);
  30. //}}AFX_DATA_MAP
  31. }
  32. BEGIN_MESSAGE_MAP(CDlgCreateTINfromFeature, CDialog)
  33. //{{AFX_MSG_MAP(CDlgCreateTINfromFeature)
  34. ON_CBN_SELCHANGE(IDC_COMBO_TIN_HEIGHT_FIELD, OnSelchangeComboTinHeightField)
  35. ON_CBN_SELCHANGE(IDC_COMBO_TIN_TRIANGU, OnSelchangeComboTinTriangu)
  36. ON_BN_CLICKED(IDC_BUTTON_TIN_INPUTS, OnButtonTinInputs)
  37. ON_BN_CLICKED(IDC_BUTTON_SAVEFILEPATH, OnButtonSavefilepath)
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CDlgCreateTINfromFeature message handlers
  42. BOOL CDlgCreateTINfromFeature::OnInitDialog() 
  43. {
  44. CDialog::OnInitDialog();
  45. // TODO: Add extra initialization here
  46. m_combo_TINTriangulateAs.SetCurSel(0);
  47. m_selHIndex=0;
  48. m_selTIndex=0;
  49. return TRUE;  // return TRUE unless you set the focus to a control
  50.               // EXCEPTION: OCX Property Pages should return FALSE
  51. }
  52. void CDlgCreateTINfromFeature::OnSelchangeComboTinHeightField() 
  53. {
  54. // TODO: Add your control notification handler code here
  55.  if(m_combo_TINHeightField. GetCurSel()!=CB_ERR)
  56.        m_selHIndex=m_combo_TINHeightField.GetCurSel();
  57. }
  58. void CDlgCreateTINfromFeature::OnSelchangeComboTinTriangu() 
  59. {
  60. // TODO: Add your control notification handler code here
  61. if(m_combo_TINTriangulateAs.GetCurSel()!=CB_ERR)
  62.         m_selTIndex=m_combo_TINTriangulateAs.GetCurSel();
  63. }
  64. void CDlgCreateTINfromFeature::OnButtonTinInputs() 
  65. {
  66. // TODO: Add your control notification handler code here
  67. //打开对话框
  68. ::OleInitialize(NULL);//组件操作初始化
  69. IGxDialogPtr        ipGxDialog(CLSID_GxDialog);
  70. IGxObjectFilterPtr  ipGxObjectFilter( CLSID_GxFilterFeatureClasses);
  71. HRESULT             hr=ipGxDialog->putref_ObjectFilter( ipGxObjectFilter);
  72. if(FAILED(hr)) return;
  73. hr=ipGxDialog->put_Title(CComBSTR("选择特征类"));
  74. if(FAILED(hr)) return;
  75. IEnumGxObjectPtr ipEnumGxObject;
  76. VARIANT_BOOL bResult;
  77. hr = ipGxDialog->DoModalOpen(0,&ipEnumGxObject,&bResult);
  78. if(FAILED(hr)) return;
  79. if(bResult==VARIANT_FALSE)return;
  80. hr= ipEnumGxObject->Reset();
  81. if(FAILED(hr)) return;
  82. IGxObjectPtr ipGxObject;
  83. while(!ipEnumGxObject->Next(&ipGxObject))
  84. {
  85. IGxDatasetPtr ipGxDataset(ipGxObject);
  86. if(ipGxDataset!=0)
  87. {
  88. IDatasetPtr ipDataset;
  89. hr=ipGxDataset->get_Dataset(&ipDataset);
  90. if(FAILED(hr)) return;
  91. IFeatureClassPtr ipFeatureClass(ipDataset);
  92. IFeatureLayerPtr ipFeatureLayer(CLSID_FeatureLayer);
  93. hr=ipFeatureLayer->putref_FeatureClass(ipFeatureClass);
  94. if(FAILED(hr)) return;
  95. m_ipfeat=ipFeatureClass;
  96. CComBSTR bstrName;//获得输入点层的全名
  97. ipGxObject->get_FullName(&bstrName);
  98. CString str( bstrName);
  99. m_edit_TINInputs=str;
  100. UpdateData(FALSE);
  101. //将层ipFeaureClass的所有字段添加到combo box控件列表中
  102. m_combo_TINHeightField.ResetContent(); //先清空
  103. long lFieldCount;
  104. IFieldsPtr pFClassPointFields;
  105. ipFeatureClass->get_Fields(&pFClassPointFields);
  106. pFClassPointFields->get_FieldCount(&lFieldCount);
  107. for(int i=0;i<lFieldCount;i++)
  108. {
  109. CComBSTR pname;
  110. IFieldPtr pField;
  111. hr = pFClassPointFields->get_Field(i,&pField);
  112. if ( FAILED(hr)) return;
  113. hr=pField->get_Name(&pname);
  114. if( FAILED(hr)) return;
  115. CString ppname(pname);
  116. m_combo_TINHeightField.AddString(ppname);
  117. }
  118. m_combo_TINHeightField.SetCurSel(0);
  119. //初始化显示comboBox
  120. }
  121. }
  122. }
  123. void CDlgCreateTINfromFeature::OnOK() 
  124. {
  125. // TODO: Add extra validation here
  126. UpdateData(TRUE);
  127. if(m_ipfeat!=NULL)
  128. {
  129.        CreateTINfromFeature(m_ipfeat);
  130. }
  131. else
  132. {
  133. AfxMessageBox("没有输入层!");
  134. return;
  135. }
  136. CDialog::OnOK();
  137. }
  138. void CDlgCreateTINfromFeature::CreateTINfromFeature(IFeatureClass *ipfeat)
  139. {
  140. //获得feature class的空间引用来构建TIN
  141. IGeoDatasetPtr pGeoDataset(ipfeat); 
  142. IEnvelopePtr pExtent;
  143. HRESULT hr=pGeoDataset->get_Extent(&pExtent);
  144. // TIN将使用通过pExten,设置的空间引用
  145. ISpatialReferencePtr ipSpatR;  
  146. hr= pGeoDataset->get_SpatialReference(&ipSpatR);
  147. hr=pExtent->putref_SpatialReference(ipSpatR);//创建TIN对象并且得到ITinEdit接口
  148. ITinEditPtr pTinEdit(CLSID_Tin);
  149. //初始化TIN
  150. hr=pTinEdit->InitNew(pExtent);
  151. //注意:到此为止虽然已经实际生成了TIN,但是此时的TIN是一个没有高程。
  152. //这样它就不是真正的高程模型,因此,通过下面步骤将feature中的高程信
  153. //刚刚生成的TIN,并且保存至硬盘。*/
  154. //获得高程字段
  155. IFieldsPtr pfields;
  156. IFieldPtr pHeightField;
  157. hr=ipfeat->get_Fields(&pfields);
  158. hr=pfields->get_Field(m_selHIndex,&pHeightField);
  159. if (FAILED(hr)) return;
  160. //调用相应的函数将shape文件(含高程信息)增加到TIN中
  161. if(m_selTIndex==0)
  162.  hr= pTinEdit->AddFromFeatureClass(ipfeat,NULL,pHeightField,NULL,esriTinMassPoint);
  163. if(m_selTIndex==1)    
  164. hr=pTinEdit->AddFromFeatureClass(ipfeat,NULL, pHeightField,NULL,esriTinHardLine);
  165. if(m_selTIndex==2)    
  166. hr=pTinEdit->AddFromFeatureClass(ipfeat,NULL,pHeightField,NULL, esriTinSoftLine);
  167. if(FAILED(hr))
  168. {
  169. AfxMessageBox("创建TIN失败");
  170. return;
  171. }
  172. //保存TIN
  173. ITinPtr ipTin(pTinEdit);
  174. if ( ipTin!=NULL)
  175. {
  176.         
  177.             HRESULT hr;    
  178. CMainFrame *pFrame=(CMainFrame*)AfxGetMainWnd();  //huoqu  
  179. CMy3D0214View  *pView=(CMy3D0214View*) pFrame->GetActiveView(); 
  180. if(!pView->GetSceneFromView(&m_ipScene))
  181. {
  182. AfxMessageBox("NO Scene");
  183. return  ;
  184. }
  185.  
  186.  
  187. //构造TIN图层
  188. ITinLayerPtr pTLayer( CLSID_TinLayer);
  189. hr=pTLayer->putref_Dataset(ipTin);
  190. BSTR fName;
  191.                 ipfeat->get_AliasName(&fName);//以输入点文件名为输出tin 的默认名
  192. hr=pTLayer->put_Name(CComBSTR(fName)); 
  193. ILayerPtr m_ipLayer=pTLayer;
  194. //增加TIN图层
  195.        hr=m_ipScene->AddLayer(m_ipLayer,VARIANT_TRUE);
  196. hr=pTinEdit->SaveAs(CComBSTR(m_edit_TINOutput));
  197. if(FAILED(hr))
  198. {
  199. AfxMessageBox("保存TIN失败!");
  200. return;
  201. }
  202. AfxMessageBox("成功!");
  203. }
  204. }
  205. void CDlgCreateTINfromFeature::OnButtonSavefilepath() 
  206. {
  207. // TODO: Add your control notification handler code here
  208. // TODO: Add your control notification handler code here
  209.     CString strFilter="File Folder(*.File Folder)|*.File Folder||";//设置打开文件的过滤条件
  210. CString strTitle="选择保存路径";
  211.         DWORD MAXFILE = 2412; // allocate enough memory space
  212.      CString strPath,strInit,strTemp;
  213.         CFileDialog pDlg(FALSE,0,0,OFN_ALLOWMULTISELECT| OFN_EXPLORER | OFN_LONGNAMES|
  214.                         OFN_FILEMUSTEXIST |OFN_HIDEREADONLY,strFilter, NULL);
  215.         //设置对话框               
  216.         pDlg.m_ofn.nMaxFile = MAXFILE; // set the buffer size        
  217.         pDlg.m_ofn.lpstrTitle=strTitle;
  218.         pDlg.m_ofn.lpstrInitialDir=strInit;
  219. char* buf = new char[MAXFILE];
  220. pDlg.m_ofn.lpstrFile = buf;
  221. pDlg.m_ofn.lpstrFile[0] = NULL;
  222. if(pDlg.DoModal()== IDOK)
  223. {
  224. //POSITION pos = pDlg.GetStartPosition();
  225. strPath=pDlg.GetPathName();
  226. //strPath=pDlg.GetFolderPath();
  227. m_Control_Output_Path.SetWindowText(strPath);
  228. AfxMessageBox(strPath,MB_YESNO);
  229. //AfxMessageBox("Do you really want to exit?",MB_YESNO);
  230. }
  231. }