我正在使用 AutoCAD COM 库来移动块,这样它们就不会重叠。我现在正在做的方式是在一个圆圈中移动一个选择集,试图找到一个开放空间,如果没有找到它会扩大圆圈。
问题是,在这个循环的 387-388 步之后,它的速度会大大降低。我添加了一个秒表来查看减速出现的位置,但如果我移除东西,确切的位置会改变。我实际上删除了我认为可能会减慢它的所有内容,但这也无济于事。所以,在这一点上,我真的不知道为什么减速一直在发生。
这是我认为导致速度下降的代码,如果您需要更多信息,请告诉我:
// Selection set
Int16[] filterCode = new Int16[] { 8 };
object[] filterValue = new object[] { LAYERNAME };
selectionSet.Select(AcSelect.acSelectionSetAll, Type.Missing, Type.Missing, filterCode, filterValue)
// This is how I grab the blocks
listOfEntities = new List<AcadEntity>();
foreach(AcadEntity entity in selectionSet)
{
if(entity.ObjectName.Equals("AcDbBlockReference"))
{
AcadBlockReference block = entity as AcadBlockReference;
if(block.Name.Equals(BLOCKNAME))
listOfEntities.Add(entity);
}
else if(entity.ObjectName.Equals("AcDbText"))
{
listOfEntities.add(entity);
}
}
然后我使用foreach
循环遍历列表中的实体并调用查找空白空间的函数。
if(listOfEntities.Count > 0)
{
_thisDrawing.SendCommand("zoom extent ");
foreach(AcadEntity entity in listOfEntities)
{
FindEmptySpace(entity);
}
}
这就是我移动块以找到空白空间的方式。
radius = (blockHeight > blockWidth ? blockHeight: blockWidth) / 10;
for(double distance = radius; ; distance += radius)
{
angle = PIx2 / distance;
for (double currentAngle = angle; currentAngle < PIx2; currentAngle += angle)
{
try
{
AcadSelectionSet spaceSelection;
// This ends up being 387-388 every time the slow down starts.
_totalSteps++;
// The new location of the block
newCenterLocation[0] = ((Math.Cos(currentAngle) * distance) + centerLocatoin[0]);
newCenterLocation[1] = ((Math.Sin(currentAngle) * distance) + centerLocation[1]);
// The new bounding box points
newMaxExt[0] = newCenterLocation[0] + (blockWidth / 2);
newMaxExt[1] = newCenterLocation[1] + (blockHeight / 2);
newMinExt[0] = newMaxExt[0] - blockWidth;
newMinExt[1] = newMinExt[1] - blockHeight;
// Make sure the "SpaceSet" isn't already created.
// I'm not sure if there is an easier way to do this.
try
{
_thisDrawing.SelectionSets.Item("SpaceSet").Delete();
}
catch
{}
spaceSelection = _thisDrawing.SelectionSets.Add("SpaceSet");
block.Move(centerLocation, newCenterLocation);
spaceSelection.Select(AcSelect.acSelectionSetCrossing, newMinExt, newMaxExt, Type.Missing, Type.Missing);
// This is '== 1' because I'm moving the block as well as the selectionset
if(spaceSelection.Count == 1)
{
spaceSelection.Clear();
// Found empty space
return;
}
spaceSelection.Clear();
// Empty space wasn't found at this location, move block back.
// I need to to this because it seems that the Move function moves
// the blocks based on the difference between the two locations.
block.Move(newCenterLoaction, centerLocation);
}
}
}
我不确定我是否忘记做任何事情。任何帮助,将不胜感激。
另外,我试过使用 2002 和 2013 版本的 COM,不确定是否有区别。
更新:在处理我发布的版本问题时,我让应用程序在 2002 上运行。在 AutoCAD 2002 上运行时它永远不会变慢。代码完全相同,唯一的区别是我使用的库和版本号(AutoCAD .Application.15 与 AutoCAD.Application.19)。所以,就是这样。