2013-09-23 33 views
0

我正在使用AutoCAD COM庫來移動塊以避免重疊。我現在正在做的方式是將選擇設置爲一圈,試圖找到一個開放空間,如果沒有找到,它將擴展該圈。經過特定的迭代次數後,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); 
     } 
    } 
} 

我不確定如果我忘記做任何事情。任何幫助,將不勝感激。

另外,我已經嘗試過使用COM的2002和2013版本,不確定是否有區別。

UPDATE:在處理我發佈的版本問題時,我得到的應用程序在2002年工作。它在AutoCAD 2002上運行時不會變慢。它是完全相同的代碼,唯一的區別是我使用的庫和版本號(AutoCAD.Application.15 vs AutoCAD.Application.19)。所以,就是這樣。

+0

沒有任何一種方式來看待這樣的代碼,並作出任何猜測到爲什麼像AutoCAD這樣龐大複雜的應用程序會如何減速?刪除所有interop調用並驗證它是否一致運行。我相信會的。這就是它所能達到的目標。只有Autodesk的支持才能讓你更進一步,儘管我嚴重懷疑他們不會做任何事情,只是將其視爲「按設計」而不予考慮。 –

+0

你正在運行什麼版本的autocad?如果你不介意我問,你的任務的目的是什麼? –

+0

你可以把你正在使用的代碼迭代到你的文章中的SS嗎? –

回答

0

一個提示,而不是使用SendCommand,用這個變焦範圍(從AutoCAD .NET開發人員的幫助):

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

由於我使用的COM接口,我不認爲我有權訪問該確切的縮放命令。我嘗試使用_acadApp.ZoomExtents()方法,但它只是拋出一個沒有任何信息的異常。 – Natzely

相關問題