I'm working with the AutoCAD COM library to move around blocks so they don't overlap. 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. 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.
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.
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). So, there's that.
One tip, rather than use SendCommand, use this for zooming extents (from AutoCAD .NET developer help):
// Zoom to the extents of the current space
Zoom(new Point3d(), new Point3d(), new Point3d(), 1.01075);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.