Thank you for replying:
I have tried @JimJia's solution and the code ran without exceptions.
As I post, I wrap the 'highlight' operation into a sub-transaction. I did the following steps:
- Start transaction
- Pick an element (for example, a duct, I didn't tried many other elements because my work is focused on MEP)
- Start subtransaction
- Override the graphic settings of the picked Duct element
- Commit subtransaction
- Now we are back to the open 'main' transaction. I start another pick operation to prompt the user to pick something (for example, a point on the former picked element)
- Do other job, like modify the documents .
Codes ran well, no exception. But, in step 6, when picking, Revit does NOT display the correct graphic settings.
Here are my codes:
using (SubTransaction subt = new SubTransaction(doc))
{
// GetActiveUIView() is my own extension method
View v = doc.GetElement(Workspace.UiDoc.GetActiveUIView().ViewId) as View;
OverrideGraphicSettings oldOgs = new OverrideGraphicSettings(v.GetElementOverrides(mainRefer.Id));
OverrideGraphicSettings newOgs = new OverrideGraphicSettings(oldOgs);
// 临时修改求出的参照主管的显示颜色
// Temporarily change line color
subt.Start();
newOgs.SetProjectionLineColor(new Color(200, 0, 0));
v.SetElementOverrides(mainRefer.Id, newOgs);
// Workspace is my own Utility static class for easy approach of common API objects, such as doc, uidoc...
Workspace.Doc.Regenerate();
Workspace.UiDoc.RefreshActiveView();
subt.Commit();
// 在参照主管上拾取一个用来求出行进方向的参照点
mainPickRef = Workspace.UiDoc.Selection.PickObject(
ObjectType.PointOnElement,
filter,
"单击拾取一个参照点以确定偏移的行进方向");
// 复位参照图元的显示设置
subt.Start();
v.SetElementOverrides(mainRefer.Id, oldOgs);
subt.Commit();
}
Here is the screen shot of Revit UI:

In the View settings, all ducts should be displayed with blue line.
Notice there is a dark gray line duct. I picked that Duct ahead, and invoke another Pick operation. At this moment, it should be displayed with RED line.
I was thinking that perhaps it's because I had not commit the main Transaction.
If I override the graphic settings in a Transaction, it works. But in my former mentioned steps, it does NOT work as I demanded.
To be honest, I don't want this CHANGING COLOR operation to be listed in the UNDO list 