Return to Original Viewport at End of Command

Let me preface this by saying that I am an absolute novice with C#.

I’m trying to make a command that, no matter what the starting viewport, will run a “meshoutline” command from the top view, and upon completing the outline, return the user to the active view that they started with, leaving all viewports intact (i.e. creating a named view of the original view and switching to that view after the ‘outline’ didn’t work because it took the place of the top view, rather than changing the active viewport.)

The reason I’m trying to do this in C# rather than a simple macro or rhinoscript is that this command will be one of a growing library of custom tools in my office for which we need a degree of version control that we haven’t figured out a system for with macros or rhinoscripts (will be making a post about that question soon)

Any ideas?

Thanks

  using System;
using System.Collections.Generic;
using System.Linq;
using Rhino;
using Rhino.Commands;
using Rhino.Display;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;

namespace V3PROJECT
{

   

    public class OutlineToCplane : Command
    {
        public OutlineToCplane()
        {
            // Rhino only creates one instance of each command class defined in a
            // plug-in, so it is safe to store a refence in a static property.
            Instance = this;
        }

        ///<summary>The only instance of this command.</summary>
        public static OutlineToCplane Instance
        {
            get; private set;
        }

        ///<returns>The command name as it appears on the Rhino command line.</returns>
        public override string EnglishName
        {
            get { return "OutlineToCplane"; }
        }

      

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)

        {

            //Meshoutline

            

            void AcceptString(bool v)
            { AcceptString(true); }

       

            GetObject go = new GetObject();

         
            go.SetCommandPrompt("Type 'SelValue' or Select surfaces, polysurfaces, or meshes");
               go.GeometryFilter = ObjectType.Surface | ObjectType.PolysrfFilter | ObjectType.Mesh;
               go.GroupSelect = true;
               go.SubObjectSelect = false;
               go.GetMultiple(1, 0);

            
          

            if (go.CommandResult() != Result.Success)
                return go.CommandResult();

            List<Mesh> InMeshes = new List<Mesh>(go.ObjectCount);
            List<RhinoObject> InMeshObjects = new List<RhinoObject>(go.ObjectCount);
            List<RhinoObject> InObjects = new List<RhinoObject>(go.ObjectCount);

            for (int i = 0; i < go.ObjectCount; i++)
            {
                ObjRef objRef = go.Object(i);
                Mesh mesh = objRef.Mesh();
                if (null == mesh)
                    InObjects.Add(objRef.Object());
                else
                {
                    InMeshes.Add(mesh);
                    InMeshObjects.Add(objRef.Object());
                }
            }

            ObjRef[] meshRefs = null;
            if (InObjects.Count > 0)
            {
                meshRefs = RhinoObject.GetRenderMeshes(InObjects, true, false);
                if (null != meshRefs)
                {
                    for (int i = 0; i < meshRefs.Length; i++)
                    {
                        Mesh mesh = meshRefs[i].Mesh();
                        if (null != mesh)
                            InMeshes.Add(mesh);
                    }
                }
            }


            RhinoView view = null;

            view = doc.Views.Find("Top", false); // en
            if (null == view)
          

            if (null == view)
            {
                RhinoApp.WriteLine("Unable to find Top viewport.");
                return Result.Nothing;
            }

            doc.Views.ActiveView = view;

            RhinoViewport vp = doc.Views.ActiveView.ActiveViewport;

            for (int i = 0; i < InMeshes.Count; i++)
            {
                Polyline[] plines = InMeshes[i].GetOutlines(vp);
                if (null != plines)
                {
                    for (int j = 0; j < plines.Length; j++)
                    {
                        Rhino.Geometry.PolylineCurve plineCrv = new PolylineCurve(plines[j]);
                        plineCrv.RemoveShortSegments(RhinoMath.SqrtEpsilon);
                        if (plineCrv.IsValid)
                        {
                            Guid objId = doc.Objects.AddCurve(plineCrv);
                            RhinoObject obj = doc.Objects.Find(objId);
                            if (null != obj)
                                obj.Select(true);
                        }
                    }
                }
            }

            for (int i = 0; i < InObjects.Count; i++)
                InObjects[i].Select(false);
            for (int i = 0; i < InMeshObjects.Count; i++)
                InMeshObjects[i].Select(false);




            doc.Views.Redraw();

            return Result.Success;
        }

         

    }
}

Hi @bushdylanj,

How about this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var view = doc.Views.ActiveView;
  if (null == view)
    return Result.Failure;

  var viewport_id = view.ActiveViewportID;

  // TODO: do whatever here

  view = doc.Views.Find(viewport_id);
  if (null != view)
    doc.Views.ActiveView = view;

  doc.Views.Redraw(); 


  return Result.Success;
}

– Dale