BuildingCoder.CmdPressKeys.Execute C# (CSharp) Method

Execute() public method

Here is a part or our code to start a Revit command. The aim of the code is to set a wall type current in the Revit property window. We only start up the wall command with the API and let the user do the drawing of the wall. This solution can also be used to launch other Revit commands.
public Execute ( ExternalCommandData commandData, string &message, ElementSet elements ) : System.Result
commandData ExternalCommandData
message string
elements ElementSet
return System.Result
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
              UIDocument uidoc = uiapp.ActiveUIDocument;
              Application app = uiapp.Application;
              Document doc = uidoc.Document;

              // Name of target wall type that we want to use:

              string wallTypeName = "Generic - 203";

              WallType wallType = GetFirstWallTypeNamed(
            doc, wallTypeName );

              Wall wall = GetFirstWallUsingType(
            doc, wallType );

              // Select the wall in the UI

              //uidoc.Selection.Elements.Add( wall ); // 2014

              List<ElementId> ids = new List<ElementId>( 1 );
              ids.Add( wall.Id );
              uidoc.Selection.SetElementIds( ids ); // 2015

              //if( 0 == uidoc.Selection.Elements.Size ) // 2014

              if( 0 == uidoc.Selection.GetElementIds().Count ) // 2015
              {
            // No wall with the correct wall type found

            FilteredElementCollector collector
              = new FilteredElementCollector( doc );

            Level ll = collector
              .OfClass( typeof( Level ) )
              .FirstElement() as Level;

            // place a new wall with the
            // correct wall type in the project

            //Line geomLine = app.Create.NewLineBound( XYZ.Zero, new XYZ( 2, 0, 0 ) ); // 2013
            Line geomLine = Line.CreateBound( XYZ.Zero, new XYZ( 2, 0, 0 ) ); // 2014

            Transaction t = new Transaction(
              doc, "Create dummy wall" );

            t.Start();

            //Wall nw = doc.Create.NewWall( geomLine, // 2012
            //  wallType, ll, 1, 0, false, false );

            Wall nw = Wall.Create( doc, geomLine, // 2013
              wallType.Id, ll.Id, 1, 0, false, false );

            t.Commit();

            // Select the new wall in the project

            //uidoc.Selection.Elements.Add( nw ); // 2014

            ids.Clear();
            ids.Add( nw.Id );
            uidoc.Selection.SetElementIds( ids ); // 2015

            // Start command create similar. In the
            // property menu, our wall type is set current

            Press.Keys( "CS" );

            // Select the new wall in the project,
            // so we can delete it

            //uidoc.Selection.Elements.Add( nw ); // 2014

            ids.Clear();
            ids.Add( nw.Id );
            uidoc.Selection.SetElementIds( ids ); // 2015

            // Erase the selected wall (remark:
            // doc.delete(nw) may not be used,
            // this command will undo)

            Press.Keys( "DE" );

            // Start up wall command

            Press.Keys( "WA" );
              }
              else
              {
            // The correct wall is already selected:

            Press.Keys( "CS" ); // Start "create similar"
              }
              return Result.Succeeded;
        }