效果如下:


cs
private static void OnTick(object sender, EventArgs e)
{
if (_currentStep >= _maxSteps)
{
Stop();
return;
}
// 检查是否暂停
if (_isPaused)
return;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
var ed = doc.Editor;
using (doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 第一步暂停逻辑:如果第一步还没完成且需要第一步暂停
if (_pauseAtFirstStep && !_firstStepCompleted)
{
// 只执行一步
StepAllAnts(tr, ms);
_currentStep++;
_firstStepCompleted = true;
// 立即暂停
_isPaused = true;
tr.Commit();
ed.UpdateScreen();
ed.WriteMessage("\n第一步完成,已暂停。按空格继续。");
return; // 直接返回,不再执行后面的代码
}
else
{
// 正常执行多步
for (int i = 0; i < _stepsPerTick; i++)
{
if (_currentStep >= _maxSteps)
break;
StepAllAnts(tr, ms);
_currentStep++;
}
tr.Commit();
}
}
ed.UpdateScreen();
}
}