Solution to error reported by PromptForFamilyInstancePlacement method in Revit secondary development

reference

  1. Here are some solutions using hooks for cancellation, but they failed to run in my function. I will record them here and study the reasons for the failure later.

Revit secondary development knowledge sharing (12) Send the Esc key to Revit and
how to monitor keyboard events in Revit

  1. I checked the contents of the SDK, but it also failed to run. I put it here to see if there is any inspiration in the future.
 // Use custom Revit drag and drop behavior
                  LoadedFamilyDropHandler myhanlder = new LoadedFamilyDropHandler();
                  UIApplication.DoDragDrop(selectedItem.Tag, myhanlder);

/// <summary>
   /// Custom handler for placement of loaded family types
   /// </summary>
   public class LoadedFamilyDropHandler : IDropHandler
   {
    
    
       public void Execute(UIDocument document, object data)
       {
    
    
           ElementId familySymbolId = (ElementId)data;

           FamilySymbol symbol = document.Document.GetElement(familySymbolId) as FamilySymbol;

           if (symbol != null)
           {
    
    
               document.PromptForFamilyInstancePlacement(symbol);
           }
       }
   }

solution

https://thebuildingcoder.typepad.com/blog/2017/05/prompt-cancel-throws-exception-in-revit-2018.html
https://forums.autodesk.com/t5/revit-api-forum/revit-2018-api-undocumented-changes-have-you-found-any/m-p/7074819

Jimmy analyzed the reasons for the modification and the current solution in the article. Using try-catch capture can complete the operations before 2017. The development engineer of Autodesk felt that the previous user double-clicking esc to cancel would cause some misoperations, so this error was added to OperationCanceledExceptionuse Avoid this matter. For other discussions, please refer to the two reference websites above.

Now that we know why the error is reported, we can solve this problem by directly adding a try-catch. This problem lasts from 5:30 pm to 9:00 pm. I have always wanted to use a hook to operate on this, but I don’t know why. Hook The user's keyboard input can be displayed normally on the pin, but the double-click esc operation cannot be performed. The binding documentchangedwill also be canceled directly, which cannot be handled logically. All in all, it is a mess and gibberish, and here are two simple sentences to solve it.

            try
            {
    
    
                commandData.Application.ActiveUIDocument.PromptForFamilyInstancePlacement(symbol,new PromptForFamilyInstancePlacementOptions(){
    
    FaceBasedPlacementType = FaceBasedPlacementType.Default});
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
            {
    
    
                Console.WriteLine(ex);
            }
            

This way it can be placed normally and the user can complete it by double-clicking esc to cancel.

Guess you like

Origin blog.csdn.net/qq_41059339/article/details/130650312