SelectCrossingPlygone() not detecting entites that are from an array exploded through c#.

SelectCrossingPlygone() not detecting entites that are from an array exploded through c#.

shricharana_bharadwaj
Enthusiast Enthusiast
329 Views
4 Replies
Message 1 of 5

SelectCrossingPlygone() not detecting entites that are from an array exploded through c#.

shricharana_bharadwaj
Enthusiast
Enthusiast

Hi all, 

 

I have an array within which I have multiple blocks. 
I need to explode the array and delete blocks within a given area. Currently this area is being calculated around a selected point. 

 

I'm using  PromptSelectionResult psr = Active.Editor.SelectCrossingPolygon(collection); to get the array at first and then using the same again to get the blocks.

 

But it is detecting the array without an issue and I'm then exploding the array. After exploding the array with "exploded = AssocArray.Explode(rackArray.Id);"  I'm running SelectCrossPolygon() again with the same point collection, but it is not returning any blocks. 


I tried using two different transactions and same transaction but there is no change in this behavior. 

 

When I manually explode the block before I run the command the with the second part only, then it is selecting the blocks properly as expected, but not when doing it through code.

//Code for detection and expolosion of array block
using (Transaction tr1 = Active.Database.TransactionManager.StartUndoGroupTransaction())
{
BlockReference rackArray = GetOverlappingRackArray(tr1);
if (rackArray == null)
{
return;
}
Matrix3d rackMatrixInverted = rackArray.BlockTransform.Inverse();
ObjectIdCollection exploded = new ObjectIdCollection();
if (AssocArray.IsAssociativeArray(rackArray.Id))
exploded = AssocArray.Explode(rackArray.Id);

//foreach (ObjectId id in exploded)
//{
// Entity ent = (Entity)tr1.GetObject(id, OpenMode.ForWrite);
// //modelSpace.AppendEntity(ent);
// //tr.AddNewlyCreatedDBObject(ent, true);
//}
tr1.Commit();
}
// code for detecting the new exploded blocks
using (Transaction tr = Active.Database.TransactionManager.StartUndoGroupTransaction())
{
ObjectIdCollection overlapped = FindOverlappingBlocks();

foreach (ObjectId id in overlapped)
{
Active.Editor.WriteMessage(id.ToString());
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
ent.Erase();
}
tr.Commit();
}

 

// Code for detection of entities.
PromptSelectionResult psr = Active.Editor.SelectCrossingPolygon(collection);
if (psr.Status != PromptStatus.OK)
{
Active.WriteError("selection failed");
return null;
}

SelectionSet ss = psr.Value;
ObjectIdCollection intersectingObjects = new ObjectIdCollection();
foreach (SelectedObject so in ss)
{
intersectingObjects.Add(so.ObjectId);
}

return intersectingObjects;

Edit: Note that all of this is wrapped in another transaction, so the transactions above are nested within.

Could anyone please let me know what I'm missing here? 

Thanks for the help in advance.

 

0 Likes
Accepted solutions (1)
330 Views
4 Replies
Replies (4)
Message 2 of 5

essam-salah
Advisor
Advisor

@shricharana_bharadwaj wrote:

Hi all, 

...

Edit: Note that all of this is wrapped in another transaction, ...


hi, i think this is the issue, because nested (inner) transactions will not commit until the outer one committed.

Message 3 of 5

ActivistInvestor
Mentor
Mentor

What is StartUndoGroupTransaction()?

 

If these are nested transactions they will not be committed until the outermost transaction is committed. So you may need to start/end two separate outermost transactions. 

Message 4 of 5

shricharana_bharadwaj
Enthusiast
Enthusiast

Hi, thank you for the replies. 

This is the undo group transaction. I got this somewhere in this forum or a related forum, I don't recall right now.

public class UndoGroupTransaction : Transaction
{
    protected internal UndoGroupTransaction(Transaction tr)
        : base(tr.UnmanagedObject, tr.AutoDelete)
    {
        Interop.DetachUnmanagedObject(tr);
        GC.SuppressFinalize(tr);
        Utils.SetUndoMark(true);
    }

    protected override void Dispose(bool A_1)
    {
        base.Dispose(A_1);
        if (A_1)
            Utils.SetUndoMark(false);
    }
}

 
My transactions are as below.

using (Transaction tr1)
{
    using (Transaction tr2)
    {
        //get and explode array block
        tr2.Commit();
    }

    using (Transaction tr3)
    {
        //get the new entites and erase overlapping ones
        tr3.Commit();
    }

    tr1.Commit();
}

So, if I'm understanding correctly until tr1 is committed tr2 and 3 is not committed.

I know that the final commit() will finalize all the earlier transaction commits (since if I abort tr1 , then no changes are made to the drawing) but I thought when working inside tr1, tr2 would commit and changes would be available for the next transactions (albeit temporarily), but only if tr1 commits then the whole change would take place.
An example is I use a nested transaction to create a new block definition and it is immediately available after the nested commit (tr2 commit).  

So for my case I will need to have two outermost transactions for this to work. 

This also raises another question, does Editor.Select() methods operate outside of these transactions? since it seems to only read the final state of the drawing (after the outermost commit) and not the intermediate ones. 

Thanks again for your replies.

0 Likes
Message 5 of 5

shricharana_bharadwaj
Enthusiast
Enthusiast
Accepted solution

Ok, I just switched the StartUndoGroupTransaction() method to StartTransaction() method in the nested transactions (while the outermost transaction still remains an start undo transaction) and its working as intended. 

It seems the undo transaction was the issue for some reason. I can only guess the nested start and end undo marks were conflicting with something. 

So the new structure looks like this.

using (UndoGroupTransaction)
{
    using (StartTransaction)
    {
        //get and explode the array blocks
        commit;
    }

    using (StartTransaction)
    {
        //get the new entities from the explode and erase
        commit;
    }
    commit;
}
0 Likes