Answered by:
How to remove entity from simulation

Question
-
Hello,
I'm inserting box entity into a simulation, and I'm interested after a certain
amount of time to remove it from simulation.
Ive inserted a box using this line of code
SimulationEngine.GlobalInstancePort.Insert(box1)
and I'm trying to remove that box using this line of code ...but without success:
SimulationEngine.GlobalInstancePort.Delete(box1)
Would appreciate any response for this question,
Simon
Wednesday, December 12, 2012 10:23 PM
Answers
-
As I can see from decompiled Insert and Delete methods - they are all asynchronous:
public void Insert(VisualEntity entity) { this.Post(new InsertSimulationEntity(entity)); }
Delete has probably nothing to delete cause Insert had not enough time to finish its business. So the first way is to wait a little (guess how long) between calls to Insert and Delete:
yield return Arbiter.Receive(false, TimeoutPort(50), _ => { });
The alternative is to synchronize on return value of Proxy.SimulationEnginePort. You may already have these lines:
using EngPxy = Microsoft.Robotics.Simulation.Engine.Proxy; ... [Partner("SimEngine", Contract = Microsoft.Robotics.Simulation.Engine.Proxy.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.CreateAlways)] EngPxy.SimulationEnginePort _simEngine = new EngPxy.SimulationEnginePort();
Then use it:
yield return Arbiter.Choice(_simEngine.InsertSimulationEntity(DssTypeHelper.TransformToProxy(box1) as EngPxy.VisualEntity)); yield return Arbiter.Choice(_simEngine.DeleteSimulationEntity(DssTypeHelper.TransformToProxy(box1) as EngPxy.VisualEntity));
Works on my machine.
- Proposed as answer by Gershon ParentModerator Friday, December 14, 2012 11:19 PM
- Marked as answer by SimonMay4 Saturday, December 22, 2012 12:19 PM
Thursday, December 13, 2012 8:02 AM -
There is a problem with second approach if entity has some children. The correct way is:
var insRequest = new InsertSimulationEntity(box1); SimulationEngine.GlobalInstancePort.Post(insRequest);
And then to wait on insRequest.ResponsePort.- Marked as answer by Harshavardhana KikkeriModerator Tuesday, December 18, 2012 9:41 PM
Monday, December 17, 2012 3:51 PM
All replies
-
As I can see from decompiled Insert and Delete methods - they are all asynchronous:
public void Insert(VisualEntity entity) { this.Post(new InsertSimulationEntity(entity)); }
Delete has probably nothing to delete cause Insert had not enough time to finish its business. So the first way is to wait a little (guess how long) between calls to Insert and Delete:
yield return Arbiter.Receive(false, TimeoutPort(50), _ => { });
The alternative is to synchronize on return value of Proxy.SimulationEnginePort. You may already have these lines:
using EngPxy = Microsoft.Robotics.Simulation.Engine.Proxy; ... [Partner("SimEngine", Contract = Microsoft.Robotics.Simulation.Engine.Proxy.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.CreateAlways)] EngPxy.SimulationEnginePort _simEngine = new EngPxy.SimulationEnginePort();
Then use it:
yield return Arbiter.Choice(_simEngine.InsertSimulationEntity(DssTypeHelper.TransformToProxy(box1) as EngPxy.VisualEntity)); yield return Arbiter.Choice(_simEngine.DeleteSimulationEntity(DssTypeHelper.TransformToProxy(box1) as EngPxy.VisualEntity));
Works on my machine.
- Proposed as answer by Gershon ParentModerator Friday, December 14, 2012 11:19 PM
- Marked as answer by SimonMay4 Saturday, December 22, 2012 12:19 PM
Thursday, December 13, 2012 8:02 AM -
There is a problem with second approach if entity has some children. The correct way is:
var insRequest = new InsertSimulationEntity(box1); SimulationEngine.GlobalInstancePort.Post(insRequest);
And then to wait on insRequest.ResponsePort.- Marked as answer by Harshavardhana KikkeriModerator Tuesday, December 18, 2012 9:41 PM
Monday, December 17, 2012 3:51 PM -
Thanks a lot for ur help,
I did worked for me.
Simon
Saturday, December 22, 2012 12:19 PM