简体   繁体   English

经过特定数量的迭代后,AutoCAD COM Application的速度降低

[英]AutoCAD COM Application slows down after a specific amount of iterations

I'm working with the AutoCAD COM library to move around blocks so they don't overlap. 我正在使用AutoCAD COM库在块之间移动,以使它们不会重叠。 The way i'm doing it right now is move a selection set around in a circle trying to find an open space, if none was found it expands the circle. 我现在的操作方式是在一个圆形中移动选择集,以尝试找到一个开放空间,如果没有找到,它将扩大该圆形。

The problem is that after the 387-388 step of this loop it slows down considerably. 问题在于,在此循环的387-388步骤之后,它会大大降低速度。 I added a stopwatch to see where the slowdown was appearing, but the exact location changes if I remove things. 我添加了一个秒表来查看减速出现的位置,但是如果我移除了东西,确切的位置就会改变。 I actually removed everything I thought could be slowing it down and that didn't help either. 实际上,我删除了所有我认为可能会降低速度的内容,但也没有帮助。 So, at this point I really have no clue why the slow down is consistently happening. 因此,在这一点上,我真的不知道为什么减速持续发生。

Here is the code I believe is causing the slowdown, let me know if you need more info: 这是我认为会导致速度下降的代码,如果您需要更多信息,请告诉我:

// 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);
    }
}

Then I use a foreach loop to move through the entities in the list and call my function that finds the empty space. 然后,我使用一个foreach循环foreach列表中的实体,并调用我的函数来查找空白空间。

if(listOfEntities.Count > 0)
{
    _thisDrawing.SendCommand("zoom extent ");

    foreach(AcadEntity entity in listOfEntities)
    {
        FindEmptySpace(entity);
    }
}

This is how I move the block around to find the empty space. 这就是我四处移动块以查找空白空间的方式。

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);
         }
    }
}

I'm not sure If I'm forgetting to do anything. 我不确定是否忘记做任何事情。 Any help would be appreciated. 任何帮助,将不胜感激。

Also, I've tried using the 2002 and 2013 versions of the COM, not sure if there is a difference. 另外,我尝试使用2002和2013版本的COM,不确定是否有区别。

UPDATE : While working on the version problem I posted, I got the app to work on 2002. It never slows down while running on AutoCAD 2002. It's the exact same code, the only differences are the library I use and the version number (AutoCAD.Application.15 vs AutoCAD.Application.19). 更新 :在解决我发布的版本问题时,我使该应用程序可以在2002年上运行。在AutoCAD 2002上运行时,它永远不会减速。它是完全相同的代码,唯一的区别是我使用的库和版本号(AutoCAD) .Application.15与AutoCAD.Application.19)。 So, there's that. 就是这样。

One tip, rather than use SendCommand, use this for zooming extents (from AutoCAD .NET developer help): 一个技巧(而不是使用SendCommand)可用于缩放范围(来自AutoCAD .NET开发人员帮助):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM