经过一定量的迭代后,AutoCAD COM应用程序会变慢

时间:2013-09-23 19:04:29

标签: c# com autocad

我正在使用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 vs AutoCAD.Application.19)。那就是那个。

1 个答案:

答案 0 :(得分:0)

一个提示,而不是使用SendCommand,使用它来缩放范围(来自AutoCAD .NET开发人员帮助):

// Zoom to the extents of the current space
Zoom(new Point3d(), new Point3d(), new Point3d(), 1.01075);