在SceneControl中交互绘制点和线,通过GraphicLayer3D显示

原创文章,转载请注明出处!

这两天实现了一个在SceneControl中交互绘制点和线,通过GraphicLayer3D显示的类,分享一下!

public IRay ICamera.GetIdentifyVector(int x,int y)得到了射线后,可以根据我想要的点的z值(或x值或y值)推算出另外两个坐标值,从而确定该点。

自己定义了一个IBaseHeight的接口,用于设定鼠标点击产生点的基准高程!按照这种思路完全可以开发更加复杂的图形绘制!

View Code
  1  [Guid("971a527b-d128-4248-aa84-918c8bd08499")]
2 [ClassInterface(ClassInterfaceType.None)]
3 [ProgId("DrawProfile.DrawLine3Tool")]
4 public sealed class DrawLine3Tool : BaseTool,IBaseHeight
5 {
6 #region COM Registration Function(s)
7 [ComRegisterFunction()]
8 [ComVisible(false)]
9 static void RegisterFunction(Type registerType)
10 {
11 // Required for ArcGIS Component Category Registrar support
12 ArcGISCategoryRegistration(registerType);
13
14 //
15 // TODO: Add any COM registration code here
16 //
17 }
18
19 [ComUnregisterFunction()]
20 [ComVisible(false)]
21 static void UnregisterFunction(Type registerType)
22 {
23 // Required for ArcGIS Component Category Registrar support
24 ArcGISCategoryUnregistration(registerType);
25
26 //
27 // TODO: Add any COM unregistration code here
28 //
29 }
30
31 #region ArcGIS Component Category Registrar generated code
32 /// <summary>
33 /// Required method for ArcGIS Component Category registration -
34 /// Do not modify the contents of this method with the code editor.
35 /// </summary>
36 private static void ArcGISCategoryRegistration(Type registerType)
37 {
38 string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
39 SxCommands.Register(regKey);
40 ControlsCommands.Register(regKey);
41 }
42 /// <summary>
43 /// Required method for ArcGIS Component Category unregistration -
44 /// Do not modify the contents of this method with the code editor.
45 /// </summary>
46 private static void ArcGISCategoryUnregistration(Type registerType)
47 {
48 string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
49 SxCommands.Unregister(regKey);
50 ControlsCommands.Unregister(regKey);
51 }
52
53 #endregion
54 #endregion
55
56 private ISceneHookHelper m_sceneHookHelper = null;
57 private static IGraphicsContainer3D m_graphicsContainer3D = null;
58 double m_height = 0;
59
60 public double Height
61 {
62 get { return m_height; }
63 set { m_height = value; }
64 }
65 public DrawLine3Tool()
66 {
67 //
68 // TODO: Define values for the public properties
69 //
70 base.m_category = "绘制3D线"; //localizable text
71 base.m_caption = "DrawLine3Tool"; //localizable text
72 base.m_message = "This should work in ArcScene/SceneControl"; //localizable text
73 base.m_toolTip = "DrawLine3Tool"; //localizable text
74 base.m_name = "DrawLine3Tool"; //unique id, non-localizable (e.g. "MyCategory_MyTool")
75 try
76 {
77 //
78 // TODO: change resource name if necessary
79 //
80 string bitmapResourceName = GetType().Name + ".bmp";
81 base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
82 base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
83
84 }
85 catch (Exception ex)
86 {
87 System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
88 }
89 }
90
91 #region Overriden Class Methods
92
93 /// <summary>
94 /// Occurs when this tool is created
95 /// </summary>
96 /// <param name="hook">Instance of the application</param>
97 public override void OnCreate(object hook)
98 {
99 try
100 {
101 m_sceneHookHelper = new SceneHookHelperClass();
102 m_sceneHookHelper.Hook = hook;
103 if (m_sceneHookHelper.ActiveViewer == null)
104 {
105 m_sceneHookHelper = null;
106 }
107 }
108 catch
109 {
110 m_sceneHookHelper = null;
111 }
112
113 if (m_sceneHookHelper == null)
114 base.m_enabled = false;
115 else
116 base.m_enabled = true;
117
118 // TODO: Add other initialization code
119 }
120
121 /// <summary>
122 /// Occurs when this tool is clicked
123 /// </summary>
124 public override void OnClick()
125 {
126 if (m_graphicsContainer3D == null)
127 {
128 m_graphicsContainer3D = new GraphicsLayer3DClass();
129 ILayer layer = m_graphicsContainer3D as ILayer;
130 layer.Name = "绘制";
131 m_sceneHookHelper.Scene.AddLayer(layer, true);
132 }
133 // TODO: Add DrawLine3Tool.OnClick implementation
134 }
135 IPointCollection m_ptCol = null;
136 IElement pLineElement = null;
137 int i = 0;
138 public override void OnMouseDown(int Button, int Shift, int X, int Y)
139 {
140 if (Button==1)
141 {
142 double ValueZ = m_height;//假设所单击的点的坐标的Z值为0
143 ICamera pCamara = m_sceneHookHelper.Camera;
144 IRay pRay = pCamara.GetIdentifyRay(X, Y);
145 IVector3D pVecotor3D = pRay.Vector;
146 pVecotor3D.Normalize();
147 double a = (ValueZ - pRay.Origin.Z) / pVecotor3D.ZComponent;
148 IPoint Pnt = new PointClass();
149 Pnt.Z = ValueZ;
150 Pnt.X = pRay.Origin.X + pVecotor3D.XComponent * a;
151 Pnt.Y = pRay.Origin.Y + pVecotor3D.YComponent * a;
152
153 (Pnt as IZAware).ZAware = true;
154
155 IElement pElement = new MarkerElementClass();
156 IRgbColor pColor = new RgbColorClass();
157 pColor.Red = 255;
158 //ISimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbolClass();
159 //pMarkerSymbol.Size = 30;
160 //pMarkerSymbol.Color = pColor;
161 //(pElement as IMarkerElement).Symbol = pMarkerSymbol;
162 //pElement.Geometry = Pnt;
163 //pGC3D.AddElement(pElement);
164 //this.axSceneControl1.Scene.SceneGraph.RefreshViewers();
165
166 IMarkerSymbol pMarkerSymbol = new SimpleMarker3DSymbolClass();
167 ((ISimpleMarker3DSymbol)pMarkerSymbol).Style = esriSimple3DMarkerStyle.esriS3DMSSphere;
168 ((ISimpleMarker3DSymbol)pMarkerSymbol).ResolutionQuality = 1.0;
169
170 pMarkerSymbol.Size = 100;
171 pMarkerSymbol.Color = pColor;
172 //IElement pElement = new MarkerElementClass();
173 ((IMarkerElement)pElement).Symbol = pMarkerSymbol;
174
175 pElement.Geometry = Pnt;
176 m_graphicsContainer3D.AddElement(pElement);
177 object Missing1 = Type.Missing;
178 i++;
179 if (i < 2)
180 {
181 m_ptCol = new PolylineClass();
182 pLineElement = new LineElementClass();
183 m_ptCol.AddPoint(Pnt, ref Missing1, ref Missing1);
184 }
185 else
186 {
187 m_ptCol.AddPoint(Pnt, ref Missing1, ref Missing1);
188 IPolyline pPolyline = m_ptCol as IPolyline;
189 IGeometry geometry = (IGeometry)pPolyline;
190 pLineElement.Geometry = geometry;
191 m_graphicsContainer3D.AddElement(pLineElement);
192 }
193
194 }
195 else if (Button==2)
196 {
197 IPolyline pPolyline = m_ptCol as IPolyline;
198 //让Z值生效
199 IZAware Zaware = pPolyline as IZAware;
200 Zaware.ZAware = true;
201 IGeometry geometry = (IGeometry)pPolyline;
202
203 //更新到Graphics窗口
204 pLineElement.Geometry = geometry;
205 m_graphicsContainer3D.AddElement(pLineElement);
206 IGraphicsSelection pSelection = m_graphicsContainer3D as IGraphicsSelection;
207 pSelection.UnselectAllElements();
208 pSelection.SelectElement(pLineElement);
209 m_ptCol = null;
210 i = 0;
211 }
212 m_sceneHookHelper.ActiveViewer.SceneGraph.RefreshViewers();
213
214 }
215
216 public override void OnMouseMove(int Button, int Shift, int X, int Y)
217 {
218 // TODO: Add DrawLine3Tool.OnMouseMove implementation
219 }
220
221 public override void OnMouseUp(int Button, int Shift, int X, int Y)
222 {
223 // TODO: Add DrawLine3Tool.OnMouseUp implementation
224 }
225 #endregion
226 }
227 interface IBaseHeight
228 {
229 double Height { get; set; }
230 }

参考文章:http://blog.csdn.net/sunqunsunqun/article/details/6946652,感谢sunqunsunqun

posted @ 2011-12-29 11:17  太一吾鱼水  阅读(488)  评论(2编辑  收藏  举报