Issue with moving surface edits - duplicating/overwriting operations

Issue with moving surface edits - duplicating/overwriting operations

AdammReilly
Collaborator Collaborator
317 Views
3 Replies
Message 1 of 4

Issue with moving surface edits - duplicating/overwriting operations

AdammReilly
Collaborator
Collaborator

I've been working on a tool that would move the surface edits up or down the list after some other code it being run. The tool does some translations so that internally I'm storing some tracking info for each operation so that I can move the operations based on some user interactions in a dialog box. My current problem is that when I move an item and rebuild the surface, it appears to make the changes fine but if I then move the item again and rebuild, it makes a copy of the item that was moved and overwrites the one at the index it was supposed to move to.

I've used several different functions to do this, thinking maybe one was causing the issue over another.

The first used the SurfaceOperation.Swap, then the next used SurfaceOperation.SwapAt. Finally, I'm using a loop that looks at the original index and the user selected index of the item and moves it using the MoveToTop/MoveToBottom and steps it up/down with the MoveUp/MoveDown commands until it's in the right place. This option takes a little longer to process but all three options end up with the same results: Rebuilt 1 works, Rebuild 2 results in a duplicated SurfaceOperation.

 

Here's the code for updating the SurfaceOperations in the surface:

public void ApplyChanges()
        {
            if (_hasEditsToUpdate)
            {
                if (_surfaceId != ObjectId.Null)
                {
                    for (int i = 0; i < Edits.Count; i++)
                    {
                        bool matches = true;
                        try
                        { matches = Edits[i].MatchesCivilOperation(); }
                        catch (System.Exception ex)
                        { }
                        if (matches == false)
                        {
                            try
                            {
                                Edits[i].Update(_surfaceId);
                            }
                            catch (System.Exception ex)
                            { }
                        }
                        Edits[i].MoveToIndex();
                        Edits[i].ResetIndex();
                    }
                }
            }
            // rebuild the surface to include the new edits
            RebuildSurface();
            //// reset the edits list with the new list of edits, using new base data for each
            Edits = GetEdits();
            // reset the tracking flag
            HasEditsToUpdate = false;
        }

 

Here's the code that moves the edits up or down in the surface:

public void MoveToIndex()
        {
            if (_surfaceId != ObjectId.Null)
            {
                CivilDocument doc = CivilApplication.ActiveDocument;
                using (Transaction tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
                {
                    Autodesk.Civil.DatabaseServices.TinSurface surf = tr.GetObject(_surfaceId, OpenMode.ForRead, false, true) as Autodesk.Civil.DatabaseServices.TinSurface;
                    int editCount = surf.Operations.Count;
                    int mid = editCount / 2;
                    if (_currentIndex < mid)
                    {
                        // move the operation to 0
                        _operation.MoveToTop();
                        if (_currentIndex > 0)
                        {
                            // move the operation down until at the index
                            for (int i = 0; i < _currentIndex; i++)
                            {
                                _operation.MoveDown();
                            }
                        }
                    }
                    else if (_currentIndex >= mid)
                    {
                        // move the operation to the end
                        _operation.MoveToBottom();
                        if (_currentIndex < editCount)
                        {
                            // move the operation up until at the index
                            for (int i = editCount; i > _currentIndex; i--)
                            {
                                _operation.MoveUp();
                            }
                        }
                    }
                }
            }
        }
public void ResetIndex()
{
     _originalIndex = _currentIndex;
}

 

Here's what the output looks like:

SurfOpIssue.gif

 

 

I've been wracking the grey matter on this one for several days and I can't seem to nail down the issue, so I'm hoping someone else has seen something like it or has an idea that I can test out.

Adam Reilly

Civil 3D 2026
Creator A16 AI+ A3HVFG
Windows 11 Pro
AMD Ryzen AI 9 365 w/ Radeon 880M
LinkedIn


Group Leader badge

Austin CADD User Group

LinkedIn | Discord


Please select the Accept as Solution button if my post solves your issue or answers your question.
0 Likes
318 Views
3 Replies
Replies (3)
Message 2 of 4

hosneyalaa
Advisor
Advisor
0 Likes
Message 3 of 4

Jeff_M
Consultant
Consultant

@AdammReilly There is an uncommitted Transaction which may, or may not, be part of the problem. Without the rest of the code I can't really test anything. If you could post the project, or send it privately, I'd be happy to take a look at it. jeffm AT quuxsoft.com

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 4 of 4

AdammReilly
Collaborator
Collaborator

Thanks for pointing that out @Jeff_M but that didn't seem to help.

I think my problem comes down to the indexing. I took out most of the ApplyChanges code and just looped through each surface operation to reset their index values. I'm still tracking down exactly why it's wigging out but I think it has something to do with the shifted items causing the underlying surface operations list to differ too much from the list I'm working through. 

Adam Reilly

Civil 3D 2026
Creator A16 AI+ A3HVFG
Windows 11 Pro
AMD Ryzen AI 9 365 w/ Radeon 880M
LinkedIn


Group Leader badge

Austin CADD User Group

LinkedIn | Discord


Please select the Accept as Solution button if my post solves your issue or answers your question.
0 Likes