usingRhino;usingRhino.Commands;usingRhino.Display;usingRhino.Geometry;usingRhino.Input.Custom;usingRhino.DocObjects;usingSystem.Drawing;namespace examples_cs
{publicclassMeshDrawingCommand:Command{publicoverridestringEnglishName{get{return"csDrawMesh";}}protectedoverrideResultRunCommand(RhinoDoc doc,RunMode mode){var gs =newGetObject();
gs.SetCommandPrompt("select sphere");
gs.GeometryFilter=ObjectType.Surface;
gs.DisablePreSelect();
gs.SubObjectSelect=false;
gs.Get();if(gs.CommandResult()!=Result.Success)return gs.CommandResult();Sphere sphere;
gs.Object(0).Surface().TryGetSphere(out sphere);if(sphere.IsValid){var mesh =Mesh.CreateFromSphere(sphere,10,10);if(mesh ==null)returnResult.Failure;var conduit =newDrawBlueMeshConduit(mesh){Enabled=true};
doc.Views.Redraw();var in_str ="";Rhino.Input.RhinoGet.GetString("press <Enter> to continue",true,ref in_str);
conduit.Enabled=false;
doc.Views.Redraw();returnResult.Success;}elsereturnResult.Failure;}}classDrawBlueMeshConduit:DisplayConduit{readonlyMesh m_mesh;readonlyColor m_color;readonlyDisplayMaterial m_material;readonlyBoundingBox m_bbox;publicDrawBlueMeshConduit(Mesh mesh){// set up as much data as possible so we do the minimum amount of work possible inside// the actual display code
m_mesh = mesh;
m_color =System.Drawing.Color.Blue;
m_material =newDisplayMaterial();
m_material.Diffuse= m_color;if(m_mesh !=null&& m_mesh.IsValid)
m_bbox = m_mesh.GetBoundingBox(true);}// this is called every frame inside the drawing code so try to do as little as possible// in order to not degrade display speed. Don't create new objects if you don't have to as this// will incur an overhead on the heap and garbage collection.protectedoverridevoidCalculateBoundingBox(CalculateBoundingBoxEventArgs e){base.CalculateBoundingBox(e);// Since we are dynamically drawing geometry, we needed to override// CalculateBoundingBox. Otherwise, there is a good chance that our// dynamically drawing geometry would get clipped.// Union the mesh's bbox with the scene's bounding box
e.IncludeBoundingBox(m_bbox);}protectedoverridevoidPreDrawObjects(DrawEventArgs e){base.PreDrawObjects(e);var vp = e.Display.Viewport;if(vp.DisplayMode.EnglishName.ToLower()=="wireframe")
e.Display.DrawMeshWires(m_mesh, m_color);else
e.Display.DrawMeshShaded(m_mesh, m_material);}}}
ImportsRhinoImportsRhino.CommandsImportsRhino.DisplayImportsRhino.GeometryImportsRhino.Input.CustomImportsRhino.DocObjectsImportsSystem.DrawingNamespace examples_vb
PublicClassMeshDrawingCommandInheritsCommandPublicOverridesReadOnlyPropertyEnglishName()AsStringGetReturn"vbDrawMesh"EndGetEndPropertyProtectedOverridesFunctionRunCommand(doc AsRhinoDoc, mode AsRunMode)AsResultDim gs =NewGetObject()
gs.SetCommandPrompt("select sphere")
gs.GeometryFilter=ObjectType.Surface
gs.DisablePreSelect()
gs.SubObjectSelect=False
gs.[Get]()If gs.CommandResult()<>Result.SuccessThenReturn gs.CommandResult()EndIfDim sphere AsSphere
gs.[Object](0).Surface().TryGetSphere(sphere)If sphere.IsValidThenDim mesh__1 =Mesh.CreateFromSphere(sphere,10,10)If mesh__1 IsNothingThenReturnResult.FailureEndIfDim conduit =NewDrawBlueMeshConduit(mesh__1)
conduit.Enabled=True
doc.Views.Redraw()Dim inStr AsString=""Rhino.Input.RhinoGet.GetString("press <Enter> to continue",True, inStr)
conduit.Enabled=False
doc.Views.Redraw()ReturnResult.SuccessElseReturnResult.FailureEndIfEndFunctionEndClassClassDrawBlueMeshConduitInheritsDisplayConduitPrivate _mesh AsMesh=NothingPrivate _color AsColorPrivate _material AsDisplayMaterial=NothingPrivate _bbox AsBoundingBoxPublicSubNew(mesh AsMesh)' set up as much data as possible so we do the minimum amount of work possible inside' the actual display code
_mesh = mesh
_color =System.Drawing.Color.Blue
_material =NewDisplayMaterial()
_material.Diffuse= _color
If _mesh IsNotNothingAndAlso _mesh.IsValidThen
_bbox = _mesh.GetBoundingBox(True)EndIfEndSub' this is called every frame inside the drawing code so try to do as little as possible' in order to not degrade display speed. Don't create new objects if you don't have to as this' will incur an overhead on the heap and garbage collection.ProtectedOverridesSubCalculateBoundingBox(e AsCalculateBoundingBoxEventArgs)MyBase.CalculateBoundingBox(e)' Since we are dynamically drawing geometry, we needed to override' CalculateBoundingBox. Otherwise, there is a good chance that our' dynamically drawing geometry would get clipped.' Union the mesh's bbox with the scene's bounding box
e.IncludeBoundingBox(_bbox)EndSubProtectedOverridesSubPreDrawObjects(e AsDrawEventArgs)MyBase.PreDrawObjects(e)Dim vp = e.Display.ViewportIf vp.DisplayMode.EnglishName.ToLower()="wireframe"Then
e.Display.DrawMeshWires(_mesh, _color)Else
e.Display.DrawMeshShaded(_mesh, _material)EndIfEndSubEndClassEndNamespace