【问题标题】:AutoCAD COM Application slows down after a specific amount of iterationsAutoCAD COM 应用程序在特定数量的迭代后速度变慢
【发布时间】:2013-10-01 02:36:51
【问题描述】:

我正在使用 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)。所以,就是这样。

【问题讨论】:

  • 没有任何方法可以查看这样的代码并猜测为什么像 AutoCAD 这样庞大而复杂的应用程序会变慢。删除所有互操作调用并验证它是否始终如一地运行。我相信它会的。这就是你能做的事情的结束。只有 Autodesk 支持才能让您走得更远,尽管我严重怀疑他们不会做任何事情,只会将其视为“设计”。
  • 你运行的是什么版本的autocad?如果你不介意我问,你的任务的目的是什么?
  • 你能把你用来遍历 SS 的代码放在你的帖子中吗?
  • @TraeMoore 现在我在 2013 年工作,但它也需要在 2002 年工作,所以我不能使用 .Net api,除非我也想编写 ObjectARX。稍后我会发布更多代码。
  • 你总是可以使用 lisp...只是说..

标签: c# com autocad


【解决方案1】:

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

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

【讨论】:

  • 当我使用 COM 接口时,我认为我无法访问那个确切的缩放命令。我尝试改用 _acadApp.ZoomExtents() 方法,但它只是在没有任何信息的情况下抛出异常。
猜你喜欢
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多