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

GIS编程

开发平台:

Visual C++

  1. // DlgClassBreakRenderer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "3D0214.h"
  5. #include "DlgClassBreakRenderer.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDlgClassBreakRenderer dialog
  13. CDlgClassBreakRenderer::CDlgClassBreakRenderer(CWnd* pParent /*=NULL*/)
  14. : CDialog(CDlgClassBreakRenderer::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CDlgClassBreakRenderer)
  17. // NOTE: the ClassWizard will add member initialization here
  18. //}}AFX_DATA_INIT
  19. }
  20. void CDlgClassBreakRenderer::DoDataExchange(CDataExchange* pDX)
  21. {
  22. CDialog::DoDataExchange(pDX);
  23. //{{AFX_DATA_MAP(CDlgClassBreakRenderer)
  24. DDX_Control(pDX, IDC_COMBO_CLASSES_NUM, m_combo_classesNum);
  25. DDX_Control(pDX, IDC_BUTTON_END_COLOR, m_endcolor);
  26. DDX_Control(pDX, IDC_BUTTON_START_COLOR, m_startcolor);
  27. DDX_Control(pDX, IDC_LIST_COLOR_RANGE, m_list_colorrange);
  28. //DDX_Control(pDX, IDC_BUTTON1, m_b);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CDlgClassBreakRenderer, CDialog)
  32. //{{AFX_MSG_MAP(CDlgClassBreakRenderer)
  33. ON_BN_CLICKED(IDC_BUTTON_COLOR_RAMP, OnButtonColorRamp)
  34. ON_CBN_SELCHANGE(IDC_COMBO_CLASSES_NUM, OnSelchangeComboClassesNum)
  35. ON_BN_CLICKED(IDC_BUTTON_START_COLOR, OnButtonStartColor)
  36. ON_BN_CLICKED(IDC_BUTTON_END_COLOR, OnButtonEndColor)
  37. ON_LBN_SELCHANGE(IDC_LIST_COLOR_RANGE, OnSelchangeListColorRange)
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CDlgClassBreakRenderer message handlers
  42. void CDlgClassBreakRenderer::OnButtonColorRamp() 
  43. {
  44. // TODO: Add your control notification handler code here
  45. m_list_colorrange.ResetContent();
  46. HRESULT hr;
  47.  
  48. CreateRampColor(m_ClassNum);
  49.  
  50. m_EnumColors->Reset();
  51. OLE_COLOR rgb;
  52. for( long i=0;i<m_ClassNum;i++)
  53. {
  54.     
  55. IColorPtr pColor; 
  56. IUnknownPtr pUnk;
  57. pUnk=pColor;
  58.     hr=m_ipArray->get_Element((long)i,&pUnk);
  59.         pColor=pUnk;
  60.         pColor->get_RGB(&rgb);
  61.         m_list_colorrange.AddString(_T(""),rgb);
  62. }
  63.  
  64.  m_list_colorrange.SetCurSel(0);
  65.  m_SelIndex=0;
  66. }
  67. BOOL CDlgClassBreakRenderer::OnInitDialog() 
  68. {
  69. CDialog::OnInitDialog();
  70. // TODO: Add extra initialization here
  71. m_combo_classesNum.SetCurSel(4);
  72.     m_ClassNum=5;
  73. OnButtonColorRamp() ;
  74. return TRUE;  // return TRUE unless you set the focus to a control
  75.               // EXCEPTION: OCX Property Pages should return FALSE
  76. }
  77. void CDlgClassBreakRenderer::OnSelchangeComboClassesNum() 
  78. {
  79. // TODO: Add your control notification handler code here
  80. m_ClassNum=m_combo_classesNum.GetCurSel()+1;
  81.     OnButtonColorRamp();
  82. Invalidate();
  83. }
  84. void CDlgClassBreakRenderer::CreateRampColor(double nSize)
  85. {
  86. m_ipArray.CreateInstance(CLSID_Array);
  87. HRESULT hr;
  88.     //建一个AlgorithmicColorRamp对象,
  89.     //获得它的IAlgorithmicColorRamp接口
  90.     IAlgorithmicColorRampPtr pAlgoRamp(CLSID_AlgorithmicColorRamp);
  91. hr=pAlgoRamp->put_Size((long)nSize);
  92. //设置属性
  93. IRgbColorPtr pColor(CLSID_RgbColor);
  94.   pColor->put_RGB(OLE_COLOR(m_startcolor.GetColor()));
  95. pAlgoRamp->put_FromColor(pColor);
  96. pColor->put_RGB(OLE_COLOR(m_endcolor.GetColor()));
  97.     pAlgoRamp->put_ToColor( pColor);
  98.     pAlgoRamp->put_Algorithm(esriHSVAlgorithm);
  99.     VARIANT_BOOL vb=VARIANT_TRUE;
  100.     pAlgoRamp->CreateRamp(&vb);
  101.     if(vb) 
  102. pAlgoRamp->get_Colors(&m_EnumColors);
  103.       m_EnumColors->Reset();
  104.     for( long i=0;i<m_ClassNum;i++)
  105. {
  106.     
  107. IColorPtr pColor;    
  108. m_EnumColors->Next(&pColor); //取出颜色  
  109. m_ipArray->Add(pColor);  //颜色添加到IArray    
  110. }
  111. }
  112. BOOL CDlgClassBreakRenderer::GetLayerFromCurrentScene(ILayer **ipLayer)
  113. {
  114. HRESULT hr;
  115.     CMainFrame *pFrame=(CMainFrame*)AfxGetMainWnd();
  116.     CMy3D0214View  *pView=(CMy3D0214View*) pFrame->GetActiveView(); 
  117. if(!pView->GetSceneFromView(&m_ipScene))
  118. {
  119. AfxMessageBox("NO Scene");
  120. return FALSE;
  121. }
  122. hr=m_ipScene->get_Layer(2,ipLayer);
  123. if(FAILED(hr)) return FALSE;
  124. return TRUE ;
  125. }
  126. void CDlgClassBreakRenderer::RasterClassBreak()
  127. {HRESULT hr;    
  128.     if(m_ipLayer == NULL) return;
  129. IRasterLayerPtr ipRasLyr(m_ipLayer);
  130.     IRasterClassifyColorRampRendererPtr pClassRen(CLSID_RasterClassifyColorRampRenderer);
  131. IRasterRendererPtr pRasRen(pClassRen);
  132. IRasterPtr pRaster;
  133. ipRasLyr->get_Raster(&pRaster);
  134. hr=pRasRen->putref_Raster(pRaster);
  135. hr=pClassRen->put_ClassCount(m_ClassNum);
  136. hr=pRasRen->Update();
  137. IFillSymbolPtr pFSymbol(CLSID_SimpleFillSymbol);
  138. //CreateRampColor(m_ClassNum);
  139. m_EnumColors->Reset();//Resets the enumerator to the beginning.
  140. for ( long i=0;i<m_ClassNum;i++)
  141. {
  142.     
  143. IColorPtr pColor;
  144.         IUnknownPtr pUnk;
  145.     m_ipArray->get_Element(i,&pUnk);
  146.         pColor=pUnk;
  147. pFSymbol->put_Color(pColor);    
  148. ISymbolPtr pSymbol(pFSymbol); 
  149. hr=pClassRen->put_Symbol(i,pSymbol);
  150. // pClassRen->put_Label(i, CComBSTR("Class"+CString (i)));
  151. }
  152. IRgbColorPtr pColor( CLSID_RgbColor);
  153. //更新渲染效果,并应用到图层
  154. hr=pRasRen->Update();
  155. hr=ipRasLyr->putref_Renderer( pRasRen);
  156. ISceneGraphPtr ipSceneG;
  157. hr=m_ipScene->get_SceneGraph(&ipSceneG);
  158. ISceneViewerPtr ppViewer;
  159. hr=ipSceneG->get_ActiveViewer(&ppViewer);
  160.     //为了刷新
  161. hr=m_ipScene->DeleteLayer(ipRasLyr);
  162. hr=m_ipScene->AddLayer(ipRasLyr,VARIANT_TRUE);
  163. }
  164. void CDlgClassBreakRenderer::TinClassBreak()//Tin的分层设色过程函数
  165. {
  166.   
  167.     HRESULT hr;
  168.     if(m_ipLayer==NULL) return;
  169.     ITinLayerPtr ipTLyr(m_ipLayer);
  170. ITinPtr ipT;
  171. hr=ipTLyr->get_Dataset(&ipT);
  172.   //获得Zmax , Zmin ;
  173. double Zmax,Zmin;
  174. IEnvelopePtr ipEnv;
  175. hr=ipT->get_Extent(&ipEnv);
  176. hr=ipEnv->get_ZMax(&Zmax);
  177. hr=ipEnv->get_ZMin(&Zmin);
  178. double d;
  179. double dint;
  180. //创建Tin渲染对象
  181. ITinColorRampRendererPtr pTinRender(CLSID_TinElevationRenderer);
  182. d=Zmax-Zmin;
  183. dint=d/m_ClassNum;
  184. hr=pTinRender->put_BreakCount(m_ClassNum);
  185. hr=pTinRender->put_MinimumBreak(Zmin);
  186. hr=pTinRender->put_Break(0,Zmin+dint);
  187. //hr=pTinRender->put_MinimumBreak(Zmin); 
  188.   for(int i=1;i<m_ClassNum;i++)
  189. {
  190. double pValue;    
  191. pTinRender->get_Break(i-1,&pValue);    
  192. pTinRender->put_Break(i,pValue+dint);
  193. }
  194. //为每个渲染赋颜色
  195. ISimpleFillSymbolPtr pSym(CLSID_SimpleFillSymbol);
  196. IColorPtr pColor;
  197. IClassBreaksUIPropertiesPtr pC(pTinRender);
  198. hr=m_EnumColors->Reset();
  199. double min,value;
  200. pTinRender->get_MinimumBreak(&min);
  201. hr=pC->put_LowBreak(0,min); 
  202. //为每段渲染添加相应的颜色
  203.     IUnknownPtr pUnk;
  204. for(i=0;i<m_ClassNum;i++)
  205. {
  206.     
  207. /////////////////////////////////////////////////////////////
  208.         m_ipArray->get_Element(i,&pUnk);
  209.             pColor=pUnk;
  210.             /////////////////////////////////////////////////////////////
  211. pSym->put_Color(pColor);   
  212. pTinRender->put_Symbol(i,(ISymbolPtr)pSym);    
  213. pTinRender->get_Break(i-1,&value); 
  214. CString strs;
  215. strs.Format("%f",(double)(Zmax-dint*i));
  216.      hr=pTinRender->put_Label(i, CComBSTR(strs));
  217. pC->put_LowBreak(i,value);
  218. }
  219.    
  220. ITinRendererPtr pTRend(pTinRender);
  221. //删除以前的渲染效果
  222. ITinRendererPtr ipRender; 
  223. long count;
  224. ipTLyr->get_RendererCount(&count); 
  225. for(i=0;i<count;i++)
  226. {    
  227. hr=ipTLyr->GetRenderer(i,&ipRender);    
  228. CComBSTR name;    
  229. hr=ipRender->get_Name(&name);    
  230. if(name=="Elevation") hr=ipTLyr->DeleteRenderer(ipRender);
  231. if(name=="Faces") hr=ipTLyr->DeleteRenderer(ipRender);
  232. }
  233. //添加新的渲染,并设为可视
  234. hr=pTRend->putref_Tin(ipT);
  235. hr=ipTLyr->AddRenderer( pTRend);
  236. hr=ipTLyr->GetRenderer( count-1,&ipRender);
  237. hr=ipRender->put_Visible(VARIANT_TRUE);
  238.  
  239. //为了刷新
  240. m_ipScene->DeleteLayer(ipTLyr);
  241. m_ipScene->AddLayer((ILayerPtr)ipTLyr,VARIANT_TRUE); 
  242. }
  243. void CDlgClassBreakRenderer::OnButtonStartColor() 
  244. {
  245. // TODO: Add your control notification handler code here
  246.  OnButtonColorRamp();
  247.  Invalidate();
  248. }
  249. void CDlgClassBreakRenderer::OnButtonEndColor() 
  250. {
  251. // TODO: Add your control notification handler code here
  252.  OnButtonColorRamp();
  253.  Invalidate();
  254. }
  255. void CDlgClassBreakRenderer::OnOK() 
  256. {
  257. // TODO: Add extra validation here
  258. if (m_ipLayer==NULL)
  259. GetLayerFromCurrentScene(&m_ipLayer);
  260. ITinLayerPtr pTLayer(m_ipLayer);
  261. if (pTLayer!=NULL) 
  262. {
  263. TinClassBreak() ;
  264. }
  265. else
  266. {
  267. RasterClassBreak();
  268.         delete pTLayer;
  269. //删除对象,释放内存,没有成功实例化的对象,不能自动释放,会引起内存溢出
  270. }
  271. CDialog::OnOK();
  272. }
  273. void CDlgClassBreakRenderer::OnSelchangeListColorRange() 
  274. {
  275. // TODO: Add your control notification handler code here
  276. m_SelIndex=m_list_colorrange.GetCaretIndex();
  277.    Invalidate();
  278. }