C# Simulation Programming - Start Simulation Engine from saved StateFile


1. Prepare SPL simulation script
  1. In this C# project, below simulation script will be used to generate sim state file named "boxsim.xml".

      
    StartSimulationEngine  "SimState/basicsim.xml"
    
    
    AddNewEntity    wall1  	/Position:-0.5  0.25  -2.5
    AddBoxShape  	/Dimensions:3.0  0.5  0.5  	/Mass:1.0	/Texture:"WoodFloor.jpg"
    
    AddNewEntity    wall2  	/Position:-0.5  0.25  2.5
    AddBoxShape  	/Dimensions:3.0  0.5  0.5  	/Mass:1.0	/Texture:"WoodFloor.jpg"
    
    AddNewEntity    wall3  	/Position:-2.5  0.25  0
    AddBoxShape  	/Dimensions:0.5  0.5  4.5  	/Mass:1.0	/Texture:"WoodFloor.jpg"
    
    AddNewEntity    wall4  	/Position:2.5  0.25  0
    AddBoxShape  	/Dimensions:0.5  0.5  4.5  	/Mass:1.0	/Texture:"WoodFloor.jpg"
    
    
    
    AddDifferentialDriveEntity    myrobot
    
    AddBumperEntity    bumper1	/ParentEntity:myrobot
    	/FrontPosition:0    0.1    -0.3
    	/RearPosition:0    0.1    0.3
    
    AddLaserRangeFinderEntity    lrf1	/Position:0  0.4  0	/ParentEntity:myrobot
    
    FlushScript  
    
    wait 10000
    SaveSceneAs    "SimState/boxsim.xml"  
    
      


  2. Save script.

  3. Press "F5" key or click "Run" icon.

  4. This script will generate sim state file named "boxsim.xml" under the "SimState" directory.



2. Download and open C# project file
  1. Download source code.

    SimulationFromState1.zip


  2. Unzip downloaded file under the MSRDS installation folder.

  3. Open "DSS Command Prompt" from the MSRDS windows menu and execute "DssProjectMigration.exe" for the unzipped folder.

      
    dssprojectmigration "here_your_unzipped_folder_name"	
    
      



3. C# source code for SimulationFromState1.proj
  1. SimulationFromState1.cs

      
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using Microsoft.Ccr.Core;
    using Microsoft.Dss.Core;
    using Microsoft.Dss.Core.Attributes;
    using Microsoft.Dss.ServiceModel.Dssp;
    using Microsoft.Dss.ServiceModel.DsspServiceBase;
    using W3C.Soap;
    using System.IO;
    
    using dssp = Microsoft.Dss.ServiceModel.Dssp;
    using engineproxy = Microsoft.Robotics.Simulation.Engine.Proxy;
    using submgr = Microsoft.Dss.Services.SubscriptionManager;
    using simengine = Microsoft.Robotics.Simulation.Engine;
    using physics = Microsoft.Robotics.Simulation.Physics;
    
    using Microsoft.Robotics.Simulation;
    using Microsoft.Robotics.Simulation.Engine;
    using Microsoft.Robotics.Simulation.Physics;
    using Microsoft.Robotics.PhysicalModel;
    
    
    namespace SimulationFromState1
    {
    	[Contract(Contract.Identifier)]
    	[DisplayName("SimulationFromState1")]
    	[Description("SimulationFromState1 service (no description provided)")]
    	class SimulationFromState1Service : DsspServiceBase
    	{        
    		[ServiceState]
    		SimulationFromState1State _state = new SimulationFromState1State();
    		
    		[ServicePort("/SimulationFromState1", AllowMultipleInstances = true)]
    		SimulationFromState1Operations _mainPort = new SimulationFromState1Operations();
    		
    		public SimulationFromState1Service(DsspServiceCreationPort creationPort)
    			: base(creationPort)
    		{
    		}
    
            protected override void Start()
            {
                base.Start();
    
                //Start simulation engine from the state file
                string filename = "SimState/boxsim.xml";
    
                dssp.PartnerType pt = new dssp.PartnerType();
                System.Xml.XmlQualifiedName qName = 
                	new System.Xml.XmlQualifiedName("StateService", "http://schemas.microsoft.com/xw/2004/10/dssp.html");
                pt.Name = qName;
                pt.Service = filename;
                pt.Contract = "http://schemas.microsoft.com/robotics/2006/04/simulationengine.html";
    
                engineproxy.Contract.CreateService(ConstructorPort, new PartnerType[] { pt });
            }
            
    	}    
    }
    
      


  2. SimulationFromState1Types.cs

      
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using Microsoft.Ccr.Core;
    using Microsoft.Dss.Core.Attributes;
    using Microsoft.Dss.ServiceModel.Dssp;
    using Microsoft.Dss.ServiceModel.DsspServiceBase;
    using W3C.Soap;
    
    namespace SimulationFromState1
    {
    	public sealed class Contract
    	{
    		[DataMember]
    		public const string Identifier = "http://schemas.tempuri.org/2009/12/simulationfromstate1.html";
    	}
    	
    	[DataContract]
    	public class SimulationFromState1State
    	{
    	}
    	
    	[ServicePort]
    	public class SimulationFromState1Operations : PortSet
    	{
    	}
    	
    	public class Get : Get>
    	{
    		public Get()
    		{
    		}
    		
    		public Get(GetRequestType body)
    			: base(body)
    		{
    		}
    		
    		public Get(GetRequestType body, PortSet responsePort)
    			: base(body, responsePort)
    		{
    		}
    	}
    }
    
      



4. Execute C# project
  1. Press "F5" key to run sample code.